diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets index 7783aff..5966051 100644 --- a/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeComponent.ets @@ -163,10 +163,11 @@ export struct ImageKnifeComponent { configNecessary(request: RequestOption) { request.load(this.imageKnifeOption.loadSrc) - .addListener((err, data) => { + .addListener({ callback: (err, data) => { LogUtil.log('ImageKnifeComponent request.load callback') this.runNextFunction(this.displayMainSource.bind(this, data)); return false; + } }) let realSize = { @@ -196,23 +197,23 @@ export struct ImageKnifeComponent { configDisplay(request: RequestOption) { if (this.imageKnifeOption.placeholderSrc) { - request.placeholder(this.imageKnifeOption.placeholderSrc, (data) => { + request.placeholder(this.imageKnifeOption.placeholderSrc, {asyncSuccess:(data) => { LogUtil.log('ImageKnifeComponent request.placeholder callback') this.runNextFunction(this.displayPlaceholder.bind(this, data)) - + } }) } if (this.imageKnifeOption.thumbSizeMultiplier) { - request.thumbnail(this.imageKnifeOption.thumbSizeMultiplier, (data) => { + request.thumbnail(this.imageKnifeOption.thumbSizeMultiplier, {asyncSuccess:(data) => { LogUtil.log('ImageKnifeComponent request.thumbnail callback') this.runNextFunction(this.displayThumbSizeMultiplier.bind(this, data)) - }, this.imageKnifeOption.thumbSizeDelay) + }}, this.imageKnifeOption.thumbSizeDelay) } if (this.imageKnifeOption.errorholderSrc) { - request.errorholder(this.imageKnifeOption.errorholderSrc, (data) => { + request.errorholder(this.imageKnifeOption.errorholderSrc, {asyncSuccess:(data) => { LogUtil.log('ImageKnifeComponent request.errorholder callback') this.runNextFunction(this.displayErrorholder.bind(this, data)) - }) + }}) } if (this.imageKnifeOption.transform) { @@ -230,19 +231,19 @@ export struct ImageKnifeComponent { if (this.imageKnifeOption.displayProgress) { - request.addProgressListener((percentValue: number) => { + request.addProgressListener({asyncSuccess:(percentValue: number) => { // 如果进度条百分比 未展示大小,展示其动画 LogUtil.log('ImageKnifeComponent request.addProgressListener callback') this.runNextFunction(this.displayProgress.bind(this, percentValue)) - }) + }}) } if (this.imageKnifeOption.retryholderSrc) { - request.retryholder(this.imageKnifeOption.retryholderSrc, (data) => { + request.retryholder(this.imageKnifeOption.retryholderSrc,{asyncSuccess: (data) => { LogUtil.log('ImageKnifeComponent request.retryholder callback') this.hasDisplayRetryholder = true this.runNextFunction(this.displayRetryholder.bind(this, data)) - }) + }}) } } diff --git a/imageknife/src/main/ets/components/imageknife/ImageKnifeGlobal.ets b/imageknife/src/main/ets/components/imageknife/ImageKnifeGlobal.ets new file mode 100644 index 0000000..5b20d3f --- /dev/null +++ b/imageknife/src/main/ets/components/imageknife/ImageKnifeGlobal.ets @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { ImageKnife } from './ImageKnife'; +import { LogUtil } from './utils/LogUtil'; +export enum GlobalEnum{ + // 主hap的context对象key + HAP_CONTEXT_KEY = 'HAP_CONTEXT_KEY', + // ImageKnife的对象key + IMAGE_KNIFE_KEY = 'IMAGE_KNIFE_KEY' +} + +// 构造单例对象 +export class ImageKnifeGlobal { + private constructor() {} + private static instance: ImageKnifeGlobal; + private _objects = new Map(); + + + public static getContext(): ImageKnifeGlobal { + if (!ImageKnifeGlobal.instance) { + ImageKnifeGlobal.instance = new ImageKnifeGlobal(); + } + return ImageKnifeGlobal.instance; + } + + getObject(value: string): Object | undefined { + return this._objects.get(value); + } + + setObject(key: string, objectClass: Object): void { + this._objects.set(key, objectClass); + } + + getImageKnife():ImageKnife | undefined{ + let imageKnifeObj:Object | undefined = this._objects.get(GlobalEnum.IMAGE_KNIFE_KEY); + if(imageKnifeObj == undefined){ + LogUtil.error('ImageKnifeGlobal imageKnifeObj is undefined, you need to initialize before using') + return undefined + }else{ + return (imageKnifeObj as ImageKnife) + } + } + + setImageKnife(imageKnife:ImageKnife):void{ + this._objects.set(GlobalEnum.IMAGE_KNIFE_KEY, imageKnife); + } + + getHapContext():Object | undefined{ + let hapContext:Object | undefined = this._objects.get(GlobalEnum.HAP_CONTEXT_KEY); + if(hapContext == undefined){ + LogUtil.error('ImageKnifeGlobal hapContext is undefined, you need to initialize before using') + return undefined + }else{ + return hapContext + } + } + + setHapContext(hapContext:Record):void{ + this._objects.set(GlobalEnum.HAP_CONTEXT_KEY, hapContext); + } + + +} \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/RequestOption.ets b/imageknife/src/main/ets/components/imageknife/RequestOption.ets index d130631..05c44d9 100644 --- a/imageknife/src/main/ets/components/imageknife/RequestOption.ets +++ b/imageknife/src/main/ets/components/imageknife/RequestOption.ets @@ -374,7 +374,7 @@ export class RequestOption { LogUtil.log("Main Image is Ready:" + this.loadMainReady); if (!this.loadMainReady && !(this.loadErrorReady || this.loadRetryReady) && !this.loadThumbnailReady) { // 主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图 - this.placeholderFunc(imageKnifeData) + this.placeholderFunc.asyncSuccess(imageKnifeData) } } @@ -388,7 +388,7 @@ export class RequestOption { thumbholderOnComplete(imageKnifeData: ImageKnifeData) { if (!this.loadMainReady && !(this.loadErrorReady || this.loadRetryReady)) { //主图未加载成功,并且未加载失败 显示占位图 主图加载成功或者加载失败后=>不展示占位图 - this.thumbHolderFunc(imageKnifeData) + this.thumbHolderFunc.asyncSuccess(imageKnifeData) } } @@ -402,7 +402,7 @@ export class RequestOption { // 如果有错误占位图 先解析并保存在RequestOption中 等到加载失败时候进行调用 this.errorholderData = imageKnifeData; if (this.loadErrorReady) { - this.errorholderFunc(imageKnifeData) + this.errorholderFunc.asyncSuccess(imageKnifeData) } } @@ -414,7 +414,7 @@ export class RequestOption { retryholderOnComplete(imageKnifeData: ImageKnifeData) { this.retryholderData = imageKnifeData; if (this.loadRetryReady) { - this.retryholderFunc(imageKnifeData) + this.retryholderFunc.asyncSuccess(imageKnifeData) } } @@ -426,7 +426,7 @@ export class RequestOption { this.loadMainReady = true; // 三级缓存数据加载成功 for (let requestListener of this.requestListeners) { - var ret = requestListener("", imageKnifeData); + var ret = requestListener.callback("", imageKnifeData); if (ret) { break; } @@ -455,13 +455,13 @@ export class RequestOption { // 重试图层优先于加载失败展示 this.loadRetryReady = true; if (this.retryholderData != null) { - this.retryholderFunc(this.retryholderData) + this.retryholderFunc.asyncSuccess(this.retryholderData) } } else { // 失败图层标记,如果已经有数据直接展示失败图层 this.loadErrorReady = true; if (this.errorholderData != null) { - this.errorholderFunc(this.errorholderData) + this.errorholderFunc.asyncSuccess(this.errorholderData) } } // 加载失败之后 diff --git a/imageknife/src/main/ets/components/imageknife/compress/CompressBuilder.ets b/imageknife/src/main/ets/components/imageknife/compress/CompressBuilder.ets index 7f4ea19..0631338 100644 --- a/imageknife/src/main/ets/components/imageknife/compress/CompressBuilder.ets +++ b/imageknife/src/main/ets/components/imageknife/compress/CompressBuilder.ets @@ -54,14 +54,14 @@ export class CompressBuilder { public setTargetDir(targetDir: string): CompressBuilder { this._mTargetDir = targetDir; if (this._mTargetDir) { - var timestamp = (new Date()).valueOf(); + let timestamp = (new Date()).valueOf(); this._outFilePath = this._mTargetDir + "/" + timestamp + (Math.random() * 100).toFixed(0) + ".jpg"; } return this; } public load(list: Array): CompressBuilder { - for (var i = 0; i < list.length; i++) { + for (let i = 0; i < list.length; i++) { let element = list[i]; if (typeof element === "string") { this.loadString(element); diff --git a/imageknife/src/main/ets/components/imageknife/compress/provider/CompressAdapter.ets b/imageknife/src/main/ets/components/imageknife/compress/provider/CompressAdapter.ets index ff7a7e9..7de64b6 100644 --- a/imageknife/src/main/ets/components/imageknife/compress/provider/CompressAdapter.ets +++ b/imageknife/src/main/ets/components/imageknife/compress/provider/CompressAdapter.ets @@ -17,8 +17,8 @@ import {CompressProvider} from "../provider/CompressProvider" import {CompressDataListener} from "../listener/CompressDataListener" export abstract class CompressAdapter implements CompressProvider { - mData: ArrayBuffer; - mPath: string; + mData: ArrayBuffer = new ArrayBuffer(0); + mPath: string = ''; close() { } @@ -37,7 +37,7 @@ export abstract class CompressAdapter implements CompressProvider { abstract getPixelMapFormat(): PixelMapFormat; - getFormat(s: string): PixelMapFormat{ + getFormat(s: string): PixelMapFormat | undefined{ if (!s) { return undefined; } diff --git a/imageknife/src/main/ets/components/imageknife/interface/AsyncCallback.ets b/imageknife/src/main/ets/components/imageknife/interface/AsyncCallback.ets index 0097fc0..be30df4 100644 --- a/imageknife/src/main/ets/components/imageknife/interface/AsyncCallback.ets +++ b/imageknife/src/main/ets/components/imageknife/interface/AsyncCallback.ets @@ -14,5 +14,5 @@ */ export interface AsyncCallback { - (err: string, data: T): boolean; + callback:(err: string, data: T)=>boolean; } diff --git a/imageknife/src/main/ets/components/imageknife/interface/AsyncSuccess.ets b/imageknife/src/main/ets/components/imageknife/interface/AsyncSuccess.ets index 816e3d4..3bb8d34 100644 --- a/imageknife/src/main/ets/components/imageknife/interface/AsyncSuccess.ets +++ b/imageknife/src/main/ets/components/imageknife/interface/AsyncSuccess.ets @@ -14,5 +14,5 @@ */ export interface AsyncSuccess { - (data: T); + asyncSuccess:(data: T)=>void; }