1/* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import common from '@ohos.app.ability.common'; 17import fs from '@ohos.file.fs'; 18import router from '@ohos.router'; 19import promptAction from '@ohos.promptAction'; 20import fileFs from '../fileFs/fileFs'; 21import Logger from '../common/Logger'; 22 23const TAG = 'show'; 24@Entry 25@Component 26struct Show { 27 @State myContext: Context = getContext(this) as common.UIAbilityContext; 28 @State myFileName: string = router.getParams()['fileName']; 29 @StorageLink('myFileSize') myFileSize: number = 0; 30 @StorageLink('myFileContent') myFileContent: string = router.getParams()['fileContent']; 31 @State path: string = router.getParams()['path']; 32 @State opacityValue: number = 0.6; 33 @State fileFs: fileFs = new fileFs(); 34 scroller: Scroller = new Scroller(); 35 36 onPageShow() { 37 this.myFileContent = this.fileFs.getFileContent(this.path); 38 this.myFileSize = fs.statSync(this.path).size; 39 } 40 41 aboutToDisappear() { 42 43 } 44 45 onPageHide() { 46 47 } 48 49 build() { 50 Column() { 51 // 第一行 52 Row() { 53 // 后退箭头 54 Row() { 55 Image($r('app.media.ic_back')) 56 .focusable(true) 57 .focusOnTouch(true) 58 .width(25) 59 .height(25) 60 .id('back') 61 .align(Alignment.Start) 62 .onClick(() => { 63 router.back(); 64 }) 65 } 66 .margin({ right: '5%' }) 67 68 // 文件名及信息 69 Column() { 70 Row() { // 文件名 71 Text(this.myFileName) 72 .focusable(true) 73 .focusOnTouch(true) 74 .fontSize(20) 75 .fontFamily('HarmonyHeiTi-Bold') 76 .fontColor('#182431') 77 .textAlign(TextAlign.Start) 78 .fontWeight(700) 79 .lineHeight(28) 80 .maxLines(1) 81 .textOverflow({ overflow: TextOverflow.Ellipsis }) 82 } 83 .align(Alignment.Start) 84 .margin({ top: '0.4%', bottom: '0.3%' }) 85 .width('100%') 86 87 Row() { // 文件大小 88 Text('size: ' + JSON.stringify(this.myFileSize) + 'B') 89 .focusable(true) 90 .focusOnTouch(true) 91 .opacity(0.6) 92 .fontFamily('HarmonyHeiTi') 93 .fontSize(14) 94 .fontColor('#182431') 95 .textAlign(TextAlign.Start) 96 .lineHeight(19) 97 .fontWeight(400) 98 } 99 .margin({ top: '0.3%', bottom: '0.5%' }) 100 .align(Alignment.Start) 101 .width('100%') 102 } 103 .width('45%') 104 105 // 右边两个图标 106 Row() { 107 Image($r('app.media.ic_refresh')) 108 .focusable(true) 109 .focusOnTouch(true) 110 .width(25) 111 .height(25) 112 .margin({ right: '12%' }) 113 .id('refresh') 114 .onClick(() => { 115 let sta = fs.statSync(this.path); 116 this.myFileSize = sta.size; 117 this.myFileContent = this.fileFs.getFileContent(this.path); 118 AppStorage.SetOrCreate('myFileSize', this.myFileSize); 119 AppStorage.SetOrCreate('myFileContent', this.myFileContent); 120 121 promptAction.showToast({ message: $r('app.string.refresh') }); 122 }) 123 124 Image($r('app.media.ic_share')) 125 .focusable(true) 126 .focusOnTouch(true) 127 .width(25) 128 .height(25) 129 .id('share') 130 .margin({ right: '5%' }) 131 .onClick(() => { 132 let file = fs.openSync(this.path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 133 let context = getContext(this); 134 Logger.info(TAG, "context this "+ context); 135 this.fileFs.implicitStartAbility(context, this.myFileName, this.myFileSize, file.fd, this.path); 136 }) 137 } 138 .height('100%') 139 .width('50%') 140 .padding({ right: '6%' }) 141 .justifyContent(FlexAlign.End) 142 } 143 .height('7.4%') 144 .width('100%') 145 146 147 Scroll(this.scroller) { 148 // TextArea的行容器 149 Row() { 150 TextArea({ text: this.myFileContent }) 151 .id('textArea') 152 .fontSize(16) 153 .fontColor('#182431') 154 .opacity(this.opacityValue) 155 .fontWeight(400) 156 .align(Alignment.Center) 157 .textAlign(TextAlign.Start) 158 .backgroundColor('#f1f3f5') 159 .fontFamily('HarmonyHeiTi') 160 .focusable(false) 161 .focusOnTouch(true) 162 .defaultFocus(false) 163 .padding({ 164 top: 8, 165 right: 0, 166 left: 0, 167 bottom: 8 168 }) // 默认为top: 8 vp, right: 16 vp, bottom: 8 vp, left: 16 vp,需要更改 169 } 170 .padding({ top: 8 }) 171 } 172 .margin({ top: 8 }) 173 } 174 .backgroundColor('#f1f3f5') 175 .height('100%') 176 .width('100%') 177 .padding({ left: '6.7%', right: '6.7%' }) 178 } 179}