• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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 Constants from '../common/utils/constant';
17import common from '@ohos.app.ability.common';
18import { CustomContentDialog } from '@ohos.arkui.advanced.Dialog';
19import { GlobalDialogModel } from '../GlobalExtAbility/GlobalDialogModel';
20import { GlobalDialogViewState } from '../GlobalExtAbility/GlobalDialogViewState';
21
22let globalDialogModel: GlobalDialogModel = new GlobalDialogModel();
23
24@Entry({ useSharedStorage: true })
25@Component
26struct globalSwitch {
27  private context = this.getUIContext().getHostContext() as common.ServiceExtensionContext;
28  @LocalStorageLink('globalState') globalState: string = '';
29  @State viewState: GlobalDialogViewState = globalDialogModel.initViewState(this.globalState);
30
31  dialogController: CustomDialogController | null = new CustomDialogController({
32    builder: CustomContentDialog({
33      contentBuilder: () => {
34        this.buildContent();
35      },
36      contentAreaPadding: { left: Constants.PADDING_24, right: Constants.PADDING_24 },
37      buttons: [
38        {
39          value: $r('app.string.cancel'),
40          buttonStyle: ButtonStyleMode.TEXTUAL,
41          action: () => {
42            this.context.terminateSelf();
43          }
44        },
45        {
46          value: $r('app.string.open'),
47          buttonStyle: ButtonStyleMode.TEXTUAL,
48          action: () => {
49            globalDialogModel.setMuteState(this.context, this.globalState, false, true);
50          }
51        }
52      ],
53    }),
54    autoCancel: false,
55    cancel: () => {
56      this.context.terminateSelf();
57    }
58  });
59
60  @Builder
61  buildContent(): void {
62    Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
63      Column() {
64        Text(this.viewState.title)
65          .fontSize(Constants.TEXT_BIG_FONT_SIZE)
66          .fontColor($r('sys.color.font_primary'))
67          .fontWeight(FontWeight.Medium)
68          .lineHeight(Constants.TEXT_BIG_LINE_HEIGHT)
69          .width(Constants.FULL_WIDTH)
70          .padding({ top: Constants.PADDING_14, bottom: Constants.PADDING_14 })
71        Text(this.viewState.text)
72          .fontSize(Constants.TEXT_MIDDLE_FONT_SIZE)
73          .fontColor($r('sys.color.font_primary'))
74          .lineHeight(Constants.TEXT_LINE_HEIGHT)
75      }
76      .clip(true)
77    }
78  }
79
80  build() {}
81
82  aboutToAppear() {
83    this.dialogController?.open();
84  }
85
86  aboutToDisappear() {
87    this.dialogController = null;
88  }
89}