1/* 2 * Copyright (c) 2022 Huawei Device 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 fileio from '@ohos.fileio' 17import { NoteHomeComp } from './NoteHome' 18import { NoteHomePortraitComp } from './NoteHomePortrait' 19import { LogUtil } from '@ohos/utils/src/main/ets/default/baseUtil/LogUtil' 20import RdbStoreUtil from '@ohos/utils/src/main/ets/default/baseUtil/RdbStoreUtil' 21import inputMethod from '@ohos.inputMethod'; 22import router from '@system.router'; 23import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 24import webview from '@ohos.web.webview'; 25import { BusinessError } from '@ohos.base'; 26import common from '@ohos.app.ability.common'; 27 28@Entry 29@Component 30export struct MyNoteHomeComp { 31 @StorageLink('topHeight') topHeight: number = 0; // 窗口规避区域高 32 @StorageLink('topWidth') topWidth: number = 0; // 窗口规避区域宽 33 @StorageLink('DBQueryFinished') dBQueryFinished: number = 0 34 @Provide('PortraitModel') portraitModel: boolean = true 35 @Provide('RefreshFlag') refreshFlag: number = 0 36 private controllerShow: WebviewController = new webview.WebviewController() 37 private context = getContext(this) as common.UIAbilityContext; 38 TAG = "MyNoteHomeComp_Tablet" 39 @StorageLink('breakPoint') @Watch('onBreakPointChange') breakPoints: string = 'lg' 40 41 onBreakPointChange() { 42 if (this.breakPoints == 'sm' || this.breakPoints == 'md') { 43 this.portraitModel = true 44 } else { 45 this.portraitModel = false 46 } 47 } 48 49 build() { 50 Column() { 51 // 窗口顶部规避区域 52 Row() 53 .width(px2vp(this.topWidth)) 54 .height(px2vp(this.topHeight)) 55 Row() { 56 if (this.dBQueryFinished == 1) { 57 if (this.breakPoints == 'sm') { 58 NoteHomePortraitComp() 59 } else { 60 NoteHomeComp({ controllerShow: this.controllerShow }) 61 } 62 63 } 64 } 65 .width('100%') 66 .height('100%') 67 } 68 } 69 70 aboutToAppear(): void { 71 LogUtil.info(this.TAG, "aboutToAppear") 72 this.breakPoints = AppStorage.Get('breakPoint')!; 73 if (this.breakPoints == 'sm' || this.breakPoints == 'md') { 74 this.portraitModel = true 75 } else { 76 this.portraitModel = false 77 } 78 if (this.context == undefined || this.context == null) { 79 LogUtil.warn(this.TAG, "context is error") 80 return 81 } 82 let permissionList: Array<string> = [ 83 "ohos.permission.DISTRIBUTED_DATASYNC" 84 ] 85 LogUtil.info(this.TAG, 'permissions need to require from user') 86 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 87 let AtManager = abilityAccessCtrl.createAtManager(); 88 //requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗 89 AtManager.requestPermissionsFromUser(context, ["ohos.permission.MANAGE_DISPOSED_APP_STATUS"]).then((data) => { 90 LogUtil.info(this.TAG, 'data permissions : ' + data.permissions) 91 LogUtil.info(this.TAG, 'data result: ' + data.authResults) 92 let sum = 0 93 for (let i = 0; i < data.authResults.length; i++) { 94 sum += data.authResults[i] 95 } 96 LogUtil.info(this.TAG, 'request permissions sum: ' + sum) 97 }).catch((err: BusinessError) => { 98 LogUtil.warn(this.TAG, 'failed to requestPermissionsFromUser : ' + err.code); 99 }) 100 let dbExist = false; 101 let dbPath = context.databaseDir + "/db/note.db" 102 try { 103 fileio.accessSync(dbPath) 104 LogUtil.info(this.TAG, "db has created") 105 RdbStoreUtil.initAppStorage(this.context) 106 dbExist = true 107 } catch (err) { 108 LogUtil.info(this.TAG, "db has not created, find to rdb folder") 109 } 110 if (!dbExist) { 111 dbPath = context.databaseDir + "/rdb/note.db" 112 try { 113 fileio.accessSync(dbPath) 114 LogUtil.info(this.TAG, "db has created") 115 RdbStoreUtil.initAppStorage(this.context) 116 } catch (err) { 117 LogUtil.info(this.TAG, "db has not created, start to create db") 118 RdbStoreUtil.createRdbStore(this.context) 119 } 120 } 121 } 122 123 aboutToDisappear(): void { 124 LogUtil.info(this.TAG, "aboutToDisappear") 125 } 126 127 onPageShow(): void { 128 LogUtil.info(this.TAG, "onPageShow") 129 // continue from tablet 130 let continueFromTablet = AppStorage.Get<boolean>('ContinueFromTablet') 131 LogUtil.info(this.TAG, "onPageShow, continueFromTablet : " + continueFromTablet) 132 let noteContentHomeExist = AppStorage.Get<boolean>('NoteContentHomeExist') 133 LogUtil.info(this.TAG, "onPageShow, noteContentHomeExist : " + noteContentHomeExist) 134 if (continueFromTablet && !noteContentHomeExist) { 135 router.push({ uri: 'pages/NoteContentHome' }) 136 AppStorage.SetOrCreate<boolean>('ContinueFromTablet', false) 137 } 138 this.refreshFlag = (this.refreshFlag == 0 ? 1 : 0) 139 } 140 141 onBackPress() { 142 LogUtil.info(this.TAG, "onBackPress") 143 // 退出键盘 144 inputMethod.getController().stopInputSession(); 145 setTimeout(() => { 146 LogUtil.info(this.TAG, "wait save cotext") 147 }, 50) 148 } 149}