1/* 2 * Copyright (c) 2023-2025 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} 19const LengthMetrics = requireNapi('arkui.node').LengthMetrics; 20const hilog = requireNapi('ohos.hilog'); 21const DEFAULT_MARGIN = 16; 22const ITEM_SPACE = 4; 23const MIN_SIZE = 24; 24const MID_SIZE = 40; 25const MAX_SIZE = 48; 26const MAX_FONT_SIZE = 2; 27export class SwipeRefresher extends ViewPU { 28 constructor(t, u, v, w = -1, x = undefined, y) { 29 super(t, v, w, y); 30 if (typeof x === 'function') { 31 this.paramsGenerator_ = x; 32 } 33 this.__content = new SynchedPropertyObjectOneWayPU(u.content, this, 'content'); 34 this.__isLoading = new SynchedPropertySimpleOneWayPU(u.isLoading, this, 'isLoading'); 35 this.maxAppFontScale = 1; 36 this.isFollowingSystemFontScale = false; 37 this.minFontSize = 1.75; 38 this.maxFontSize = 2; 39 this.setInitiallyProvidedValue(u); 40 this.finalizeConstruction(); 41 } 42 setInitiallyProvidedValue(s) { 43 if (s.content === undefined) { 44 this.__content.set(''); 45 } 46 if (s.isLoading === undefined) { 47 this.__isLoading.set(false); 48 } 49 if (s.maxAppFontScale !== undefined) { 50 this.maxAppFontScale = s.maxAppFontScale; 51 } 52 if (s.isFollowingSystemFontScale !== undefined) { 53 this.isFollowingSystemFontScale = s.isFollowingSystemFontScale; 54 } 55 if (s.minFontSize !== undefined) { 56 this.minFontSize = s.minFontSize; 57 } 58 if (s.maxFontSize !== undefined) { 59 this.maxFontSize = s.maxFontSize; 60 } 61 } 62 updateStateVars(r) { 63 this.__content.reset(r.content); 64 this.__isLoading.reset(r.isLoading); 65 } 66 purgeVariableDependenciesOnElmtId(q) { 67 this.__content.purgeDependencyOnElmtId(q); 68 this.__isLoading.purgeDependencyOnElmtId(q); 69 } 70 aboutToBeDeleted() { 71 this.__content.aboutToBeDeleted(); 72 this.__isLoading.aboutToBeDeleted(); 73 SubscriberManager.Get().delete(this.id__()); 74 this.aboutToBeDeletedInternal(); 75 } 76 get content() { 77 return this.__content.get(); 78 } 79 set content(p) { 80 this.__content.set(p); 81 } 82 get isLoading() { 83 return this.__isLoading.get(); 84 } 85 set isLoading(o) { 86 this.__isLoading.set(o); 87 } 88 aboutToAppear() { 89 try { 90 let n = this.getUIContext(); 91 this.isFollowingSystemFontScale = n.isFollowingSystemFontScale(); 92 this.maxAppFontScale = n.getMaxFontScale(); 93 } 94 catch (k) { 95 let l = k.code; 96 let m = k.message; 97 hilog.error(0x3900, 'SwipeRefresher', `Failed to init fontsizescale info, cause, code: ${l}, message: ${m}`); 98 } 99 } 100 updateFontScale() { 101 let i = this.getUIContext(); 102 let j = i.getHostContext()?.config?.fontSizeScale ?? 1; 103 if (!this.isFollowingSystemFontScale) { 104 return 1; 105 } 106 return Math.min(j, this.maxAppFontScale); 107 } 108 initialRender() { 109 this.observeComponentCreation2((g, h) => { 110 Flex.create({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }); 111 }, Flex); 112 this.observeComponentCreation2((c, d) => { 113 If.create(); 114 if (this.isLoading) { 115 this.ifElseBranchUpdateFunction(0, () => { 116 this.observeComponentCreation2((e, f) => { 117 LoadingProgress.create(); 118 LoadingProgress.height(Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.maxFontSize ? MAX_SIZE : 119 (Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.minFontSize ? MID_SIZE : MIN_SIZE)); 120 LoadingProgress.width(Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.maxFontSize ? MAX_SIZE : 121 (Math.min(this.updateFontScale(), MAX_FONT_SIZE) === this.minFontSize ? MID_SIZE : MIN_SIZE)); 122 LoadingProgress.margin({ 123 end: LengthMetrics.vp(ITEM_SPACE) 124 }); 125 }, LoadingProgress); 126 }); 127 } 128 else { 129 this.ifElseBranchUpdateFunction(1, () => { 130 }); 131 } 132 }, If); 133 If.pop(); 134 this.observeComponentCreation2((a, b) => { 135 Text.create(this.content); 136 Text.fontColor({ 'id': -1, 'type': 10001, params: ['sys.color.ohos_id_color_text_secondary'], 'bundleName': '__harDefaultBundleName__', 'moduleName': '__harDefaultModuleName__' }); 137 Text.fontSize({ 'id': -1, 'type': 10002, params: ['sys.float.ohos_id_text_size_body2'], 'bundleName': '__harDefaultBundleName__', 'moduleName': '__harDefaultModuleName__' }); 138 Text.minFontScale(1); 139 Text.maxFontScale(Math.min(this.updateFontScale(), 2)); 140 Text.padding({ 141 top: DEFAULT_MARGIN, 142 bottom: DEFAULT_MARGIN 143 }); 144 }, Text); 145 Text.pop(); 146 Flex.pop(); 147 } 148 rerender() { 149 this.updateDirtyElements(); 150 } 151} 152export default { SwipeRefresher };