index.ets 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (C) 2024 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. import router from '@ohos.router'
  16. import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
  17. import dataStorage from '@ohos.data.storage';
  18. import common from '@ohos.app.ability.common';
  19. @Entry
  20. @Component
  21. struct indexPage {
  22. @State currentMode: number = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
  23. private storage: dataStorage.Storage | undefined = undefined;
  24. async aboutToAppear() {
  25. try {
  26. const context = getContext(this) as common.UIAbilityContext;
  27. this.storage = await dataStorage.getStorage(context.cacheDir);
  28. if (this.storage) {
  29. const colorMode = await this.storage.get('currentColorMode', this.currentMode);
  30. this.currentMode = colorMode as number;
  31. this.updateUI();
  32. }
  33. } catch (err) {
  34. console.error('Failed to get color mode:', err);
  35. }
  36. }
  37. updateUI() {
  38. if (this.currentMode == ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {
  39. console.info('当前为浅色模式');
  40. } else {
  41. console.info('当前为深色模式');
  42. }
  43. }
  44. build() {
  45. Column() {
  46. Button($r('app.string.video_demo'))
  47. .margin({ bottom: 20 })
  48. .width(200)
  49. .onClick(() => {
  50. router.pushUrl({ url: 'pages/SampleVideoListPage' })
  51. })
  52. Button($r('app.string.audio_demo'))
  53. .width(200)
  54. .onClick(() => {
  55. router.pushUrl({ url: 'pages/SampleAudioListPage' })
  56. })
  57. }
  58. .width('100%')
  59. .height('100%')
  60. .backgroundColor(this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT ? Color.Gray : '#121212')
  61. .justifyContent(FlexAlign.Center)
  62. }
  63. }