• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Logger from '../model/Logger'
17import preferences from '@ohos.data.preferences'
18import ThemeDesktop from '../common/ThemeDesktop'
19
20const THEMES = [
21  [
22    { image: $r('app.media.dialer'), name: '电话' },
23    { image: $r('app.media.shopping'), name: '商城' },
24    { image: $r('app.media.notes'), name: '备忘录' },
25    { image: $r('app.media.settings'), name: '设置' },
26    { image: $r('app.media.camera'), name: '相机' },
27    { image: $r('app.media.gallery'), name: '相册' },
28    { image: $r('app.media.music'), name: '音乐' },
29    { image: $r('app.media.video'), name: '视频' },
30  ],
31  [
32    { image: $r('app.media.simplicityCall'), name: '电话' },
33    { image: $r('app.media.simplicityShop'), name: '商城' },
34    { image: $r('app.media.simplicityNotes'), name: '备忘录' },
35    { image: $r('app.media.simplicitySetting'), name: '设置' },
36    { image: $r('app.media.simplicityCamera'), name: '相机' },
37    { image: $r('app.media.simplicityPhotos'), name: '相册' },
38    { image: $r('app.media.simplicityMusic'), name: '音乐' },
39    { image: $r('app.media.simplicityVideo'), name: '视频' },
40  ],
41  [
42    { image: $r('app.media.pwcall'), name: '电话' },
43    { image: $r('app.media.pwshop'), name: '商城' },
44    { image: $r('app.media.pwnotes'), name: '备忘录' },
45    { image: $r('app.media.pwsetting'), name: '设置' },
46    { image: $r('app.media.pwcamera'), name: '相机' },
47    { image: $r('app.media.pwphotos'), name: '相册' },
48    { image: $r('app.media.pwmusic'), name: '音乐' },
49    { image: $r('app.media.pwvideo'), name: '视频' },
50  ]
51]
52const TAG: string = '[Index]'
53const PREFERENCES_NAME = 'theme.db'
54const THEME_NAMES: string[] = ['default', 'simplicity', 'pomeloWhtie']
55let preferenceTheme: preferences.Preferences = null
56
57@Entry
58@Component
59struct Index {
60  @State nowTheme: string = ''
61  @State themeDatas: Array<{
62    image: Resource,
63    name: string
64  }> = []
65
66  async aboutToAppear() {
67    //从内存中获取轻量级存储db文件
68    await this.getPreferencesFromStorage()
69    //从轻量级存储db文件中获取键名为theme的键值
70    this.nowTheme = await this.getPreference()
71    let index = THEME_NAMES.indexOf(this.nowTheme)
72    this.themeDatas = THEMES[index]
73  }
74
75  async getPreferencesFromStorage() {
76    let context = getContext(this) as any
77    preferenceTheme = await preferences.getPreferences(context, PREFERENCES_NAME)
78  }
79
80  async putPreference(data: string) {
81    Logger.info(TAG, `Put begin`)
82    if (preferenceTheme !== null) {
83      await preferenceTheme.put('theme', data)
84      await preferenceTheme.flush()
85    }
86  }
87
88  async getPreference() {
89    Logger.info(TAG, `Get begin`)
90    let theme: string = ''
91    if (preferenceTheme !== null) {
92      theme = <string> await preferenceTheme.get('theme', 'default')
93      return theme
94    }
95  }
96
97  changeTheme(themeNum: number) {
98    this.themeDatas = THEMES[themeNum]
99    this.putPreference(THEME_NAMES[themeNum])
100  }
101
102  build() {
103    Column() {
104      Row() {
105        Text($r('app.string.MainAbility_label'))
106          .fontSize(25)
107          .layoutWeight(5)
108          .padding({ left: 10 })
109          .fontColor(Color.White)
110          .fontWeight(FontWeight.Bold)
111        Image($r('app.media.change'))
112          .key('changeBtn')
113          .height(30)
114          .layoutWeight(1)
115          .objectFit(ImageFit.ScaleDown)
116          .bindMenu([
117            {
118              value: THEME_NAMES[0],
119              action: () => {
120                this.changeTheme(0)
121              }
122            },
123            {
124              value: THEME_NAMES[1],
125              action: () => {
126                this.changeTheme(1)
127              }
128            },
129            {
130              value: THEME_NAMES[2],
131              action: () => {
132                this.changeTheme(2)
133              }
134            }
135          ])
136      }
137      .width('100%')
138      .height(50)
139      .backgroundColor('#0D9FFB')
140
141      ThemeDesktop({ themeDatas: $themeDatas })
142    }
143    .width('100%')
144    .height('100%')
145  }
146}