• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 base from '@ohos.base';
17import common from '@ohos.app.ability.common';
18import dataShare from '@ohos.data.dataShare';
19import formBindingData from '@ohos.app.form.formBindingData';
20import formInfo from '@ohos.app.form.formInfo';
21import formObserver from '@ohos.app.form.formObserver';
22import formProvider from '@ohos.app.form.formProvider';
23import Logger from '../../common/Logger';
24import { getPersistentConditionID, savePersistentConditionID } from '../../common/StatePersistence';
25
26const TAG = '[Sample_ModifyConditionIndex]';
27let dataShareHelper: dataShare.DataShareHelper;
28let conditionID = '110000';
29let context: common.UIAbilityContext;
30
31function updateCardDisplayContent(runningFormInfo: formInfo.RunningFormInfo): void {
32  Logger.info(`TAG updateCardDisplayContent bundle: ${runningFormInfo.bundleName}`);
33  let template: dataShare.Template = {
34    predicates: {
35      list: `select cityTemperature as cityTemperature, cityName as cityName from TBL00 where cityId = ${conditionID}`
36    },
37    scheduler: ''
38  };
39  dataShare.createDataShareHelper(context, 'datashareproxy://ohos.samples.formextability', { isProxy: true })
40    .then((data: dataShare.DataShareHelper) => {
41      dataShareHelper = data;
42      dataShareHelper.addTemplate('datashareproxy://ohos.samples.formextability/test', conditionID, template);
43    });
44
45  class ProxiesType {
46    key: string = '';
47    subscriberId: string = '';
48  };
49
50  let proxies = [{
51    key: 'datashareproxy://ohos.samples.formextability/test',
52    subscriberId: conditionID
53  } as ProxiesType];
54  let formData: string = '';
55  Logger.info(TAG, `formData: ${JSON.stringify(formData)}`);
56  let formBinding = formBindingData.createFormBindingData(formData);
57  formBinding['proxies'] = proxies;
58  formProvider.updateForm(runningFormInfo.formId, formBinding).then(() => {
59    Logger.info(TAG, `update Form OK formData is ${JSON.stringify(formBinding)}`);
60  }).catch((err: base.BusinessError<void>) => {
61    Logger.error(TAG, `update Form Failed ${JSON.stringify(err)}`);
62  });
63}
64
65function modifyCondition(): void {
66  let formInstanceFilterArkTS: formInfo.FormProviderFilter = {
67    bundleName: 'ohos.samples.formextability',
68    abilityName: 'EntryFormAbility',
69    formName: 'persistentWidget',
70    moduleName: 'persistentProxyForm'
71  };
72  let formInstanceFilterJS: formInfo.FormProviderFilter = {
73    bundleName: 'ohos.samples.formextability',
74    abilityName: 'EntryFormAbility',
75    formName: 'persistentWidgetJS',
76    moduleName: 'persistentProxyForm'
77  };
78  formObserver.getRunningFormInfosByFilter(formInstanceFilterArkTS).then((data: Array<formInfo.RunningFormInfo>) => {
79    Logger.info(TAG, `getRunningFormInfosByFilter data: ${JSON.stringify(data)}`);
80    AppStorage.SetOrCreate('runningFormInfo', JSON.stringify(data));
81    data.forEach(updateCardDisplayContent);
82  }).catch((err: base.BusinessError<void>) => {
83    Logger.error(TAG, `getRunningFormInfosByFilter err is ${JSON.stringify(err)}`);
84  });
85  formObserver.getRunningFormInfosByFilter(formInstanceFilterJS).then((data: Array<formInfo.RunningFormInfo>) => {
86    Logger.info(TAG, `getRunningFormInfosByFilter data: ${JSON.stringify(data)}`);
87    AppStorage.SetOrCreate('runningFormInfo', JSON.stringify(data));
88    data.forEach(updateCardDisplayContent);
89  }).catch((err: base.BusinessError<void>) => {
90    Logger.error(TAG, `getRunningFormInfosByFilter err is ${JSON.stringify(err)}`);
91  });
92}
93
94@Entry
95@Component
96struct IndexThi {
97  @State persistentConditionID: string = '';
98  @State persistentSwitchFlag: boolean = true;
99
100  aboutToAppear() {
101    context = getContext(this) as common.UIAbilityContext;
102    this.persistentConditionID = getPersistentConditionID(context);
103    if (typeof this.persistentConditionID !== 'string' || this.persistentConditionID === '') {
104      this.persistentConditionID = '110000'
105    }
106    this.persistentSwitchFlag = this.persistentConditionID.localeCompare('110000') === 0;
107    Logger.debug(TAG, `persistentSwitchFlag : ${JSON.stringify(this.persistentSwitchFlag)}`);
108  }
109
110  aboutToDisappear() {
111    savePersistentConditionID(context, this.persistentConditionID);
112  }
113
114  build() {
115    Row() {
116      Column({ space: 150 }) {
117        Text($r('app.string.subscription_conditions'))
118          .fontColor('#182431')
119          .fontSize(40)
120          .lineHeight(41)
121          .fontWeight('Bold')
122        Column() {
123          Text($r('app.string.modify_subscription_conditions'))
124            .fontSize(28)
125            .margin({ bottom: 20 })
126          Row() {
127            Flex({ justifyContent: FlexAlign.SpaceEvenly }) {
128              Column() {
129                Text($r('app.string.city_sy'))
130                  .margin({ bottom: 10 })
131                Radio({ value: 'sy', group: 'modifyConditionGroup' })
132                  .checked(this.persistentSwitchFlag)
133                  .height(24)
134                  .width(24)
135                  .radioStyle({
136                    checkedBackgroundColor: $r('sys.color.ohos_id_color_text_primary_activated'),
137                    uncheckedBorderColor: $r('sys.color.ohos_id_color_switch_outline_off'),
138                    indicatorColor: $r('sys.color.ohos_id_color_foreground_contrary')
139                  })
140                  .onChange((isChecked: boolean) => {
141                    Logger.info('firstRadio status is ' + isChecked);
142                    if (isChecked) {
143                      conditionID = '110000';
144                      this.persistentConditionID = conditionID;
145                      modifyCondition();
146                    }
147                  })
148              }
149
150              Column() {
151                Text($r('app.string.city_hz'))
152                  .margin({ bottom: 10 })
153                Radio({ value: 'hz', group: 'modifyConditionGroup' })
154                  .checked(!this.persistentSwitchFlag)
155                  .height(24)
156                  .width(24)
157                  .radioStyle({
158                    checkedBackgroundColor: $r('sys.color.ohos_id_color_text_primary_activated'),
159                    uncheckedBorderColor: $r('sys.color.ohos_id_color_switch_outline_off'),
160                    indicatorColor: $r('sys.color.ohos_id_color_foreground_contrary')
161                  })
162                  .onChange((isChecked: boolean) => {
163                    Logger.info('secondRadio status is ' + isChecked);
164                    if (isChecked) {
165                      conditionID = '310000';
166                      this.persistentConditionID = conditionID;
167                      modifyCondition();
168                    }
169                  })
170              }
171            }
172          }
173          .width('70%')
174        }
175        .width('100%')
176      }
177    }
178    .height('100%')
179  }
180}