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