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 formInfo from '@ohos.app.form.formInfo'; 20import formObserver from '@ohos.app.form.formObserver'; 21import prompt from '@ohos.promptAction'; 22import Logger from '../../common/Logger'; 23 24const TAG = '[Sample_PushIndex]'; 25let context: common.UIAbilityContext; 26let dataShareHelper: dataShare.DataShareHelper; 27let uri = ('datashareproxy://ohos.samples.formextability'); 28let conditionCity = ''; 29let conditionID = '110000'; 30let conditionData = '-30 ℃'; 31let formInstanceFilter: formInfo.FormProviderFilter = { 32 bundleName: 'ohos.samples.formextability', 33 moduleName: 'processProxyForm' 34}; 35 36function updateCardDisplayContent(runningFormInfo: formInfo.RunningFormInfo): void { 37 Logger.info(TAG, `updateCardDisplayContent bundle: ${runningFormInfo.bundleName}`); 38 dataShare.createDataShareHelper(context, uri, { isProxy: true }, (err, dataHelper) => { 39 Logger.info(TAG, `dataShareHelper err: ${err}`, `data: ${dataShareHelper}`); 40 dataShareHelper = dataHelper; 41 if (err !== undefined) { 42 Logger.error(TAG, `createDataShareHelper error code: ${err.code}`, `message: ${err.message} `); 43 prompt.showToast({ message: 'createDataShareHelper:' + err.message, duration: 5000 }); 44 } else { 45 let publishedItemData: Array<dataShare.PublishedItem> = [ 46 { key: 'cityName', data: JSON.stringify(conditionCity), subscriberId: conditionID }, 47 { key: 'cityTemperature', data: JSON.stringify(conditionData), subscriberId: conditionID } 48 ]; 49 dataShareHelper.publish(publishedItemData, 'ohos.samples.formextability').then((data: Object) => { 50 Logger.info(TAG, `publish success, data is ${JSON.stringify(data)}`); 51 prompt.showToast( 52 { message: `publish success ${conditionCity} ${conditionData}`, duration: 5000 }); 53 }).catch((err: base.BusinessError<void>) => { 54 Logger.error(TAG, `publish error: ${JSON.stringify(err)}`); 55 prompt.showToast({ message: `publish err: ${JSON.stringify(err)}`, duration: 5000 }); 56 }); 57 } 58 }); 59} 60 61function publish(): void { 62 Logger.info(TAG, 'publish called'); 63 formObserver.getRunningFormInfosByFilter(formInstanceFilter).then((data: Array<formInfo.RunningFormInfo>) => { 64 Logger.info(TAG, `getRunningFormInfosByFilter data: ${JSON.stringify(data)}`); 65 AppStorage.SetOrCreate('runningFormInfo', JSON.stringify(data)); 66 data.forEach(updateCardDisplayContent); 67 }).catch((err: base.BusinessError<void>) => { 68 Logger.error(TAG, `getRunningFormInfosByFilter err is ${JSON.stringify(err)}`); 69 prompt.showToast( 70 { message: `publish err, getRunningFormInfosByFilter failed ${JSON.stringify(err)}`, duration: 5000 }); 71 }); 72} 73 74@Entry 75@Component 76struct IndexSec { 77 @StorageLink('runningFormInfo') runningFormInfo: string = ''; 78 @State focus: boolean = false; 79 80 aboutToAppear() { 81 context = getContext(this) as common.UIAbilityContext; 82 conditionCity = context.resourceManager.getStringSync($r('app.string.city_sy')); 83 } 84 85 build() { 86 Row() { 87 Column({ space: 200 }) { 88 Column({ space: 5 }) { 89 Text($r('app.string.modify_publish_data')) 90 .fontColor('#182431') 91 .fontSize(40) 92 .lineHeight(41) 93 .fontWeight('100%') 94 } 95 96 Column() { 97 Column() { 98 Row() { 99 Text($r('app.string.selection_city')) 100 .fontSize(30) 101 Row() { 102 Column() { 103 Text($r('app.string.city_sy')) 104 .fontSize(15) 105 .margin({ bottom: 10 }) 106 Radio({ value: 'sy', group: 'cityGroup' }) 107 .checked(true) 108 .height(24) 109 .width(24) 110 .radioStyle({ 111 checkedBackgroundColor: $r('sys.color.ohos_id_color_text_primary_activated'), 112 indicatorColor: $r('sys.color.ohos_fa_foreground_contrary'), 113 uncheckedBorderColor: $r('sys.color.ohos_id_color_switch_outline_off') }) 114 .onChange((isChecked: boolean) => { 115 Logger.info(TAG, `sy status is ${JSON.stringify(isChecked)}`); 116 if (isChecked) { 117 conditionID = '110000'; 118 conditionCity = context.resourceManager.getStringSync($r('app.string.city_sy')); 119 } 120 }) 121 } 122 123 Column() { 124 Text($r('app.string.city_hz')) 125 .fontSize(15) 126 .margin({ bottom: 10 }) 127 Radio({ value: 'hz', group: 'cityGroup' }) 128 .height(24) 129 .width(24) 130 .radioStyle({ 131 checkedBackgroundColor: $r('sys.color.ohos_id_color_text_primary_activated'), 132 indicatorColor: $r('sys.color.ohos_fa_foreground_contrary'), 133 uncheckedBorderColor: $r('sys.color.ohos_id_color_switch_outline_off') }) 134 .onChange((isChecked: boolean) => { 135 Logger.info(TAG, `hz status is ${JSON.stringify(isChecked)}`); 136 if (isChecked) { 137 conditionID = '310000'; 138 conditionCity = context.resourceManager.getStringSync($r('app.string.city_hz')); 139 } 140 }) 141 } 142 .margin({ left: 20 }) 143 } 144 .margin({ left: 10 }) 145 } 146 147 Row() { 148 Text($r('app.string.input_temperature')) 149 .fontSize(30) 150 .width(200) 151 .height(60) 152 TextInput({ text: '-30' }) 153 .fontSize(20) 154 .fontColor($r('sys.color.ohos_id_color_text_primary')) 155 .width(100) 156 .height(40) 157 .type(InputType.Normal) 158 .borderRadius(16) 159 .backgroundColor($r('sys.color.ohos_id_color_text_field_sub_bg')) 160 .onChange((text) => { 161 conditionData = text; 162 }) 163 Text('℃') 164 .fontSize(30) 165 .height(60) 166 } 167 } 168 .alignItems(HorizontalAlign.Start) 169 170 Button() { 171 Text($r('app.string.published_data')) 172 .fontColor($r('sys.color.ohos_id_color_foreground_contrary')) 173 .fontSize($r('sys.float.ohos_id_text_size_button1')) 174 .fontWeight(FontWeight.Bold) 175 } 176 .height(40) 177 .width(220) 178 .borderRadius($r('sys.float.ohos_id_corner_radius_button')) 179 .backgroundColor($r('sys.color.ohos_id_color_component_activated')) 180 .type(ButtonType.Capsule) 181 .margin({ top: 20 }) 182 .onClick(() => { 183 if (Number(conditionData) >= -40 && Number(conditionData) <= 60) { 184 Logger.info(TAG, `correct temperature is ${conditionData}`); 185 publish(); 186 } else { 187 Logger.info(TAG, `incorrect temperature is ${conditionData}`); 188 prompt.showToast({ message: 'Please enter the correct value from -40 to 60', duration: 5000 }); 189 } 190 }) 191 } 192 .alignItems(HorizontalAlign.Center) 193 .height('30%') 194 } 195 .width('100%') 196 } 197 .height('100%') 198 } 199}