1/* 2 * Copyright (c) 2023-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 16if (!('finalizeConstruction' in ViewPU.prototype)) { 17 Reflect.set(ViewPU.prototype, 'finalizeConstruction', () => { }); 18} 19if (PUV2ViewBase.contextStack === undefined) { 20 Reflect.set(PUV2ViewBase, 'contextStack', []); 21} 22const LengthMetrics = requireNapi('arkui.node').LengthMetrics; 23const hilog = requireNapi('ohos.hilog'); 24const DEFAULT_MARGIN = 16; 25const ITEM_SPACE = 4; 26const MIN_SIZE = 24; 27const MID_SIZE = 40; 28const MAX_SIZE = 48; 29const MAX_FONT_SIZE = 2; 30export class SwipeRefresher extends ViewPU { 31 constructor(u11, v11, w11, x11 = -1, y11 = undefined, z11) { 32 super(u11, w11, x11, z11); 33 if (typeof y11 === 'function') { 34 this.paramsGenerator_ = y11; 35 } 36 this.__content = new SynchedPropertySimpleOneWayPU(v11.content, this, 'content'); 37 this.__isLoading = new SynchedPropertySimpleOneWayPU(v11.isLoading, this, 'isLoading'); 38 this.maxAppFontScale = 1; 39 this.isFollowingSystemFontScale = false; 40 this.minFontSize = 1.75; 41 this.maxFontSize = 2; 42 this.setInitiallyProvidedValue(v11); 43 this.finalizeConstruction(); 44 } 45 setInitiallyProvidedValue(t11) { 46 if (t11.content === undefined) { 47 this.__content.set(''); 48 } 49 if (t11.isLoading === undefined) { 50 this.__isLoading.set(false); 51 } 52 if (t11.maxAppFontScale !== undefined) { 53 this.maxAppFontScale = t11.maxAppFontScale; 54 } 55 if (t11.isFollowingSystemFontScale !== undefined) { 56 this.isFollowingSystemFontScale = t11.isFollowingSystemFontScale; 57 } 58 if (t11.minFontSize !== undefined) { 59 this.minFontSize = t11.minFontSize; 60 } 61 if (t11.maxFontSize !== undefined) { 62 this.maxFontSize = t11.maxFontSize; 63 } 64 } 65 updateStateVars(s11) { 66 this.__content.reset(s11.content); 67 this.__isLoading.reset(s11.isLoading); 68 } 69 purgeVariableDependenciesOnElmtId(r11) { 70 this.__content.purgeDependencyOnElmtId(r11); 71 this.__isLoading.purgeDependencyOnElmtId(r11); 72 } 73 aboutToBeDeleted() { 74 this.__content.aboutToBeDeleted(); 75 this.__isLoading.aboutToBeDeleted(); 76 SubscriberManager.Get().delete(this.id__()); 77 this.aboutToBeDeletedInternal(); 78 } 79 get content() { 80 return this.__content.get(); 81 } 82 set content(q11) { 83 this.__content.set(q11); 84 } 85 get isLoading() { 86 return this.__isLoading.get(); 87 } 88 set isLoading(p11) { 89 this.__isLoading.set(p11); 90 } 91 aboutToAppear() { 92 try { 93 let o11 = this.getUIContext(); 94 this.isFollowingSystemFontScale = o11.isFollowingSystemFontScale(); 95 this.maxAppFontScale = o11.getMaxFontScale(); 96 } 97 catch (l11) { 98 let m11 = l11.code; 99 let n11 = l11.message; 100 hilog.error(0x3900, 'SwipeRefresher', `Failed to init fontsizescale info, cause, code: ${m11}, message: ${n11}`); 101 } 102 } 103 updateFontScale() { 104 let j11 = this.getUIContext(); 105 let k11 = j11.getHostContext()?.config?.fontSizeScale ?? 1; 106 if (!this.isFollowingSystemFontScale) { 107 return 1; 108 } 109 return Math.min(k11, this.maxAppFontScale); 110 } 111 initialRender() { 112 PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.push(this); 113 this.observeComponentCreation((h11, i11) => { 114 ViewStackProcessor.StartGetAccessRecordingFor(h11); 115 Flex.create({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }); 116 if (!i11) { 117 Flex.pop(); 118 } 119 ViewStackProcessor.StopGetAccessRecording(); 120 }); 121 this.observeComponentCreation((a11, b11) => { 122 ViewStackProcessor.StartGetAccessRecordingFor(a11); 123 If.create(); 124 if (this.isLoading) { 125 this.ifElseBranchUpdateFunction(0, () => { 126 this.observeComponentCreation((f11, g11) => { 127 ViewStackProcessor.StartGetAccessRecordingFor(f11); 128 LoadingProgress.create(); 129 LoadingProgress.height(Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.maxFontSize ? MAX_SIZE : 130 (Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.minFontSize ? MID_SIZE : MIN_SIZE)); 131 LoadingProgress.width(Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.maxFontSize ? MAX_SIZE : 132 (Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.minFontSize ? MID_SIZE : MIN_SIZE)); 133 LoadingProgress.margin({ 134 end: LengthMetrics.vp(ITEM_SPACE) 135 }); 136 if (!g11) { 137 LoadingProgress.pop(); 138 } 139 ViewStackProcessor.StopGetAccessRecording(); 140 }); 141 }); 142 } 143 else { 144 this.ifElseBranchUpdateFunction(1, () => { 145 }); 146 } 147 if (!b11) { 148 If.pop(); 149 } 150 ViewStackProcessor.StopGetAccessRecording(); 151 }); 152 If.pop(); 153 this.observeComponentCreation((y10, z10) => { 154 ViewStackProcessor.StartGetAccessRecordingFor(y10); 155 Text.create(this.content); 156 Text.fontColor({ 'id': -1, 'type': 10001, params: ['sys.color.ohos_id_color_text_secondary'], 'bundleName': '__harDefaultBundleName__', 'moduleName': '__harDefaultModuleName__' }); 157 Text.fontSize({ 'id': -1, 'type': 10002, params: ['sys.float.ohos_id_text_size_body2'], 'bundleName': '__harDefaultBundleName__', 'moduleName': '__harDefaultModuleName__' }); 158 Text.minFontScale(1); 159 Text.maxFontScale(Math.min(this.updateFontScale(), 2)); 160 Text.padding({ 161 top: DEFAULT_MARGIN, 162 bottom: DEFAULT_MARGIN 163 }); 164 if (!z10) { 165 Text.pop(); 166 } 167 ViewStackProcessor.StopGetAccessRecording(); 168 }); 169 Text.pop(); 170 Flex.pop(); 171 PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.pop(); 172 } 173 rerender() { 174 PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.push(this); 175 this.updateDirtyElements(); 176 PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.pop(); 177 } 178} 179export default { SwipeRefresher };