|
| 1 | +import { RNC } from '@rnoh/react-native-openharmony/generated/ts' |
| 2 | +import { webview } from '@kit.ArkWeb' |
| 3 | +import { CACHE_MODE, ONE_HUNDRED, WebViewEventParams } from './Magic'; |
| 4 | +import Logger from './Logger'; |
| 5 | + |
| 6 | +export const TAG = "WebView" |
| 7 | + |
| 8 | + |
| 9 | +interface ProgressInterface { |
| 10 | + progress: number; |
| 11 | +} |
| 12 | + |
| 13 | +interface LoadingErrorInterface { |
| 14 | + code: number; |
| 15 | + description: string |
| 16 | +} |
| 17 | + |
| 18 | +interface ScrollInterface { |
| 19 | + x: number; |
| 20 | + y: number |
| 21 | +} |
| 22 | + |
| 23 | +interface CreateWebViewEventInterface { |
| 24 | + type: string; |
| 25 | + progress: number; |
| 26 | +} |
| 27 | + |
| 28 | +export class BaseOperate { |
| 29 | + static instance: BaseOperate | null = null |
| 30 | + private eventEmitter: RNC.RNCWebView.EventEmitter |
| 31 | + private controller: webview.WebviewController |
| 32 | + |
| 33 | + constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { |
| 34 | + this.eventEmitter = eventEmitter |
| 35 | + this.controller = controller |
| 36 | + } |
| 37 | + |
| 38 | + static getInstance(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController): BaseOperate { |
| 39 | + if (BaseOperate.instance === null) { |
| 40 | + BaseOperate.instance = new BaseOperate(eventEmitter, controller) |
| 41 | + } |
| 42 | + return BaseOperate.instance |
| 43 | + } |
| 44 | + |
| 45 | + emitProgressChange(params: ProgressInterface) { |
| 46 | + try { |
| 47 | + this.eventEmitter!.emit('loadingProgress', { |
| 48 | + url: this.controller.getUrl(), |
| 49 | + loading: params.progress != ONE_HUNDRED, |
| 50 | + title: this.controller.getTitle(), |
| 51 | + canGoBack: this.controller.accessBackward(), |
| 52 | + canGoForward: this.controller.accessForward(), |
| 53 | + lockIdentifier: 0, |
| 54 | + progress: params.progress / ONE_HUNDRED |
| 55 | + }) |
| 56 | + } catch (error) { |
| 57 | + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + emitLoadingStart(params: ProgressInterface) { |
| 62 | + try { |
| 63 | + this.eventEmitter!.emit('loadingStart', { |
| 64 | + url: this.controller.getUrl(), |
| 65 | + loading: params.progress != ONE_HUNDRED, |
| 66 | + title: this.controller.getTitle(), |
| 67 | + canGoBack: this.controller.accessBackward(), |
| 68 | + canGoForward: this.controller.accessForward(), |
| 69 | + lockIdentifier: 0, |
| 70 | + navigationType: "other", |
| 71 | + mainDocumentURL: "" |
| 72 | + }) |
| 73 | + } catch (error) { |
| 74 | + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + emitLoadingFinish(params: ProgressInterface) { |
| 79 | + try { |
| 80 | + this.eventEmitter!.emit('loadingFinish', { |
| 81 | + url: this.controller.getUrl(), |
| 82 | + loading: params.progress != ONE_HUNDRED, |
| 83 | + title: this.controller.getTitle(), |
| 84 | + canGoBack: this.controller.accessBackward(), |
| 85 | + canGoForward: this.controller.accessForward(), |
| 86 | + lockIdentifier: 0, |
| 87 | + navigationType: "other", |
| 88 | + mainDocumentURL: "" |
| 89 | + }) |
| 90 | + } catch (error) { |
| 91 | + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + emitLoadingError(params: LoadingErrorInterface) { |
| 96 | + try { |
| 97 | + this.eventEmitter!.emit('loadingError', { |
| 98 | + url: this.controller.getUrl(), |
| 99 | + loading: false, |
| 100 | + title: this.controller.getTitle(), |
| 101 | + canGoBack: this.controller.accessBackward(), |
| 102 | + canGoForward: this.controller.accessForward(), |
| 103 | + lockIdentifier: 0, |
| 104 | + domain: "", |
| 105 | + code: params.code, |
| 106 | + description: params.description |
| 107 | + }) |
| 108 | + } catch (error) { |
| 109 | + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + emitHttpError(params: LoadingErrorInterface) { |
| 114 | + try { |
| 115 | + this.eventEmitter!.emit('httpError', { |
| 116 | + url: this.controller.getUrl(), |
| 117 | + loading: false, |
| 118 | + title: this.controller.getTitle(), |
| 119 | + canGoBack: this.controller.accessBackward(), |
| 120 | + canGoForward: this.controller.accessForward(), |
| 121 | + lockIdentifier: 0, |
| 122 | + description: params.description, |
| 123 | + statusCode: params.code |
| 124 | + }) |
| 125 | + } catch (error) { |
| 126 | + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + emitScroll(params: ScrollInterface) { |
| 131 | + this.eventEmitter!.emit('scroll', { |
| 132 | + contentInset: { |
| 133 | + bottom: 0, |
| 134 | + left: 0, |
| 135 | + right: 0, |
| 136 | + top: 0 |
| 137 | + }, |
| 138 | + contentOffset: { y: params.y, x: params.x }, |
| 139 | + contentSize: { height: 0, width: 0 }, |
| 140 | + layoutMeasurement: { height: 0, width: 0 } |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + emitShouldStartLoadWithRequest(params: ProgressInterface) { |
| 145 | + try { |
| 146 | + this.eventEmitter!.emit('shouldStartLoadWithRequest', { |
| 147 | + url: this.controller.getUrl(), |
| 148 | + loading: params.progress != ONE_HUNDRED, |
| 149 | + title: this.controller.getTitle(), |
| 150 | + canGoBack: this.controller.accessBackward(), |
| 151 | + canGoForward: this.controller.accessForward(), |
| 152 | + lockIdentifier: 0, |
| 153 | + navigationType: "other", |
| 154 | + mainDocumentURL: "", |
| 155 | + isTopFrame: false |
| 156 | + }) |
| 157 | + } catch (error) { |
| 158 | + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + transCacheMode(cacheMode: CACHE_MODE): CacheMode { |
| 163 | + let mode = CacheMode.Default |
| 164 | + switch (cacheMode) { |
| 165 | + case CACHE_MODE.LOAD_DEFAULT: |
| 166 | + mode = CacheMode.Default |
| 167 | + break; |
| 168 | + case CACHE_MODE.LOAD_CACHE_ELSE_NETWORK: |
| 169 | + mode = CacheMode.None |
| 170 | + break; |
| 171 | + case CACHE_MODE.LOAD_NO_CACHE: |
| 172 | + mode = CacheMode.Online |
| 173 | + break; |
| 174 | + case CACHE_MODE.LOAD_CACHE_ONLY: |
| 175 | + mode = CacheMode.Only |
| 176 | + break; |
| 177 | + default: |
| 178 | + break; |
| 179 | + } |
| 180 | + return mode |
| 181 | + } |
| 182 | + |
| 183 | + createWebViewEvent(param: CreateWebViewEventInterface): WebViewEventParams { |
| 184 | + let result: WebViewEventParams = new WebViewEventParams(param.type); |
| 185 | + result.loading = param.progress != ONE_HUNDRED |
| 186 | + try { |
| 187 | + result.url = this.controller.getUrl(); |
| 188 | + result.title = this.controller.getTitle(); |
| 189 | + result.canGoBack = this.controller.accessBackward(); |
| 190 | + result.canGoForward = this.controller.accessForward(); |
| 191 | + } catch (error) { |
| 192 | + result.url = ""; |
| 193 | + result.title = ""; |
| 194 | + result.canGoBack = false; |
| 195 | + result.canGoForward = false; |
| 196 | + } |
| 197 | + return result; |
| 198 | + } |
| 199 | +} |
0 commit comments