• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import LogUtil from '../../../../../utils/src/main/ets/default/baseUtil/LogUtil';
17import { emitter } from '@kit.BasicServicesKit';
18
19@Component
20export default struct ProfileComponent {
21  @State isOn: boolean = false;
22  @Link @Watch('onIsEnableChange') isEnable: boolean;
23  @State desp?: string | ResourceStr = '';
24  @State profileText: string | ResourceStr = '';
25  @State textFontColor: ResourceColor = $r('sys.color.font_primary')
26  @State despFontColor: ResourceColor = $r('sys.color.font_secondary')
27  @State profileId: number = -1;
28  @State isShow: boolean = true;
29  private onChange?: (isOn: boolean) => void;
30  private fontSize?: Resource = $r('sys.float.Body_L');
31  private fontWeight?: FontWeight = FontWeight.Medium;
32
33  onIsEnableChange(): void {
34    this.isShow = this.isEnable
35    LogUtil.info('onIsEnableChange successful')
36  }
37
38  aboutToAppear(): void {
39    this.monitoringProfile();
40  }
41
42  private monitoringProfile(): void {
43    let innerEvent: emitter.InnerEvent = { eventId: this.profileId }
44    emitter.on(innerEvent, (eventData: emitter.EventData) => {
45      if (eventData.data) {
46        this.isEnable = eventData.data.Enable;
47        this.desp = eventData.data.Description;
48        this.isOn = eventData.data.State;
49        LogUtil.info('monitoringProfile() = ' + this.profileId + 'this.isOn = ' + this.isOn)
50      }
51    })
52  }
53
54  private getMinMenuHeight(): number {
55    return this.desp ? 64 : 48
56  }
57
58  build() {
59    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
60      if (this.isShow) {
61        Column() {
62          Text(this.profileText)
63            .fontColor($r('sys.color.font_primary'))
64            .fontSize(this.fontSize)
65            .fontWeight(this.fontWeight)
66            .fontFamily('HarmonyHeiTi')
67            .lineHeight(22)
68
69          if (this.desp) {
70            Text(this.desp)
71              .fontFamily('HarmonyHeiTi')
72              .fontSize($r('sys.float.Body_M'))
73              .fontColor($r('sys.color.font_secondary'))
74              .fontWeight(FontWeight.Regular)
75              .lineHeight(19)
76              .margin({
77                top: $r('sys.float.padding_level1')
78              })
79          }
80        }
81        .padding({
82          left: $r('sys.float.padding_level4'),
83          right: $r('sys.float.padding_level4')
84        })
85        .flexGrow(1)
86        .alignItems(HorizontalAlign.Start)
87      } else {
88        Column() {
89          Text(this.profileText)
90            .fontColor($r('app.color.color_E3E3E3_grey'))
91            .fontSize(this.fontSize)
92            .fontWeight(this.fontWeight)
93            .fontFamily('HarmonyHeiTi')
94            .lineHeight(22)
95
96          if (this.desp) {
97            Text(this.desp)
98              .fontFamily('HarmonyHeiTi')
99              .fontSize($r('sys.float.Body_M'))
100              .fontColor($r('app.color.color_E3E3E3_grey'))
101              .fontWeight(FontWeight.Regular)
102              .lineHeight(19)
103              .margin({
104                top: $r('sys.float.padding_level1')
105              })
106          }
107        }
108        .padding({
109          left: $r('sys.float.padding_level4'),
110          right: $r('sys.float.padding_level4')
111        })
112        .flexGrow(1)
113        .alignItems(HorizontalAlign.Start)
114      }
115      Toggle({
116        type: ToggleType.Switch,
117        isOn: this.isOn
118      })
119        .enabled(this.isEnable)
120        .id('entry_toggle')
121        .selectedColor('#007DFF')
122        .onChange(this.onChange as ((isOn: boolean) => void))
123        .hoverEffect(HoverEffect.None)
124        .responseRegion({ width: 48, height: 48 })
125        .margin({
126          right: $r('sys.float.padding_level4')
127        })
128        .flexShrink(0)
129        .defaultFocus(true)
130    }
131    .constraintSize({
132      minHeight: this.getMinMenuHeight()
133    })
134  }
135}