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 formBindingData from '@ohos.app.form.formBindingData'; 19import formInfo from '@ohos.app.form.formInfo'; 20import formObserver from '@ohos.app.form.formObserver'; 21import formProvider from '@ohos.app.form.formProvider'; 22import Logger from '../../common/Logger'; 23import { getProcessConditionID, saveProcessConditionID } from '../../common/StatePersistence'; 24 25const TAG = '[Sample_ModifyConditionIndex]'; 26let conditionID = '110000'; 27let context: common.UIAbilityContext; 28 29function updateCardDisplayContent(runningFormInfo: formInfo.RunningFormInfo): void { 30 Logger.info(TAG, `updateCardDisplayContent bundle: ${runningFormInfo.bundleName}`); 31 let proxies:Array<formBindingData.ProxyData> = [ 32 { 33 key: 'cityName', 34 subscriberId: conditionID 35 }, 36 { 37 key: 'cityTemperature', 38 subscriberId: conditionID 39 } 40 ]; 41 let formBinding = formBindingData.createFormBindingData(); 42 formBinding['proxies'] = proxies; 43 formProvider.updateForm(runningFormInfo.formId, formBinding).then(() => { 44 Logger.info(TAG, `update Form OK formData is ${JSON.stringify(formBinding)}`); 45 }).catch((err: base.BusinessError<void>) => { 46 Logger.error(TAG, `update Form Failed ${JSON.stringify(err)}`); 47 }); 48} 49 50function modifyCondition(): void { 51 let formInstanceFilter: formInfo.FormProviderFilter = { 52 bundleName: 'ohos.samples.formextability', 53 moduleName: 'processProxyForm' 54 }; 55 formObserver.getRunningFormInfosByFilter(formInstanceFilter).then((data: Array<formInfo.RunningFormInfo>) => { 56 Logger.info(TAG, `getRunningFormInfosByFilter data: ${JSON.stringify(data)}`); 57 AppStorage.SetOrCreate('runningFormInfo', JSON.stringify(data)); 58 data.forEach(updateCardDisplayContent); 59 }).catch((err: base.BusinessError<void>) => { 60 Logger.error(TAG, `getRunningFormInfosByFilter err is ${JSON.stringify(err)}`); 61 }); 62} 63 64@Entry 65@Component 66struct IndexThi { 67 @State processConditionID: string = ''; 68 @State processSwitchFlag: boolean = true; 69 70 aboutToAppear() { 71 context = getContext(this) as common.UIAbilityContext; 72 this.processConditionID = getProcessConditionID(context); 73 if (typeof this.processConditionID !== 'string' || this.processConditionID === '') { 74 this.processConditionID = '110000' 75 } 76 this.processSwitchFlag = this.processConditionID.localeCompare('110000') === 0; 77 Logger.debug(TAG, `persistentSwitchFlag : ${JSON.stringify(this.processSwitchFlag)}`); 78 } 79 80 aboutToDisappear() { 81 saveProcessConditionID(context, this.processConditionID); 82 } 83 84 build() { 85 Row() { 86 Column({ space: 150 }) { 87 Text($r('app.string.subscription_conditions')) 88 .fontColor('#182431') 89 .fontSize(40) 90 .lineHeight(41) 91 .fontWeight('100%') 92 Column() { 93 Text($r('app.string.modify_subscription_conditions')) 94 .fontSize(28) 95 .margin({ bottom: 20 }) 96 Row() { 97 Flex({ justifyContent: FlexAlign.SpaceEvenly }) { 98 Column() { 99 Text($r('app.string.city_sy')) 100 .margin({ bottom: 10 }) 101 Radio({ value: 'sy', group: 'modifyConditionGroup' }) 102 .height(24) 103 .width(24) 104 .radioStyle({ 105 checkedBackgroundColor: $r('sys.color.ohos_id_color_text_primary_activated'), 106 indicatorColor: $r('sys.color.ohos_fa_foreground_contrary'), 107 uncheckedBorderColor: $r('sys.color.ohos_id_color_switch_outline_off') }) 108 .checked(this.processSwitchFlag) 109 .onChange((isChecked: boolean) => { 110 Logger.info('firstRadio status is ' + isChecked); 111 if (isChecked) { 112 conditionID = '110000'; 113 this.processConditionID = conditionID; 114 modifyCondition(); 115 } 116 }) 117 } 118 119 Column() { 120 Text($r('app.string.city_hz')) 121 .margin({ bottom: 10 }) 122 Radio({ value: 'hz', group: 'modifyConditionGroup' }) 123 .height(24) 124 .width(24) 125 .checked(!this.processSwitchFlag) 126 .radioStyle({ 127 checkedBackgroundColor: $r('sys.color.ohos_id_color_text_primary_activated'), 128 indicatorColor: $r('sys.color.ohos_fa_foreground_contrary'), 129 uncheckedBorderColor: $r('sys.color.ohos_id_color_switch_outline_off') }) 130 .onChange((isChecked: boolean) => { 131 Logger.info('secondRadio status is ' + isChecked); 132 if (isChecked) { 133 conditionID = '310000'; 134 this.processConditionID = conditionID; 135 modifyCondition(); 136 } 137 }) 138 } 139 } 140 } 141 .width('70%') 142 } 143 .width('100%') 144 } 145 } 146 .height('100%') 147 } 148}