调整架构 添加base 基础工具模块

This commit is contained in:
2024-04-18 18:44:19 +08:00
parent e3873432bc
commit ec474c43d9
29 changed files with 225 additions and 41 deletions

View File

@@ -1,8 +1,5 @@
export { TitleBar, TitleBarMenuType } from './src/main/ets/component/TitleBar'
export { ComponentConst } from './src/main/ets/component/ComponentConst'
export { WebViewEventKey } from './src/main/ets/event/EventKey'
export { Emitter } from './src/main/ets/event/Emitter'
export { WebViewEventKey } from '../base/src/main/ets/event/EventKey'
export { WebView, WebViewController, WebViewParam } from './src/main/ets/component/web/WebView'
export { CommonRes, CommonMediaName } from './src/main/ets/utils/CommonRes'
export { ScreenUtil } from './src/main/ets/utils/ScreenUtil'

View File

@@ -7,5 +7,6 @@
"license": "Apache-2.0",
"packageType": "InterfaceHar",
"dependencies": {
"@devwiki/base": "file:../base"
}
}

View File

@@ -1,110 +0,0 @@
export interface EmitterItem {
key: string
listener: Function
}
type XYEmitterAllType = string | object | boolean | number | Object | Number | BigInt;
export class Emitter {
private static instance: Emitter;
private static events: Map<string, EmitterItem[]> = new Map
private static viscosityData: Map<string, XYEmitterAllType[]> = new Map
public static getInstance(): Emitter {
if (!Emitter.instance) {
Emitter.instance = new Emitter();
}
return Emitter.instance;
}
/**
* 添加监听
* @param key
* @param listener
* @returns
*/
public on(key: string, listener: Function): EmitterItem {
let item: EmitterItem = {key: key, listener: listener};
if (typeof listener !== "function") {
throw new TypeError('the listener not a function')
}
let items: EmitterItem[] | undefined = Emitter.events.get(key);
if (!items) {
items = []
}
items.push(item)
Emitter.events.set(key, items)
if (Emitter.viscosityData.has(key)) {
let value = Emitter.viscosityData.get(key)
if (value == undefined) {
listener();
} else {
listener(...value);
}
}
return item
}
/**
* 取消监听
* @param listener
*/
public off(listener: EmitterItem): void {
let items = Emitter.events.get(listener.key)
if (!items) {
return;
}
items = items.filter((it)=> {
return it !== listener;
})
Emitter.events.set(listener.key, items);
}
/**
* 发布普通消息
* @param key
* @param args
*/
public emmit<T>(key: string, ...args: T[]) {
let items = Emitter.events.get(key)
if (!items || items.length == 0) {
return;
}
items.forEach((item: EmitterItem, index: number) => {
if (typeof item.listener === 'function') {
item.listener(...args)
}
});
}
/**
* 发布粘性消息
* @param key
* @param args
*/
public emmitViscosity<T>(key: string, ...args: T[]) {
this.emmit(key, ...args);
Emitter.viscosityData.set(key, args as XYEmitterAllType[]);
}
/**
* 移除粘性消息
* @param key
*/
public deleteViscosity<T>(key: string) {
if (Emitter.viscosityData.has(key)) {
Emitter.viscosityData.delete(key)
}
}
/**
* 删除所有监听
*/
public removeAllListener() {
Emitter.events = new Map;
}
}

View File

@@ -1,5 +0,0 @@
export class WebViewEventKey {
public static readonly TitleEvent: string = "web_view_event_title";
public static readonly DarkModeChangedEvent: string = "web_view_event_dark_mode_changed";
public static readonly UrlChangedEvent: string = "web_view_event_url_change_changed";
}

View File

@@ -1,106 +0,0 @@
import window from '@ohos.window';
import mediaquery from '@ohos.mediaquery';
export class ScreenUtil {
// TODO: 工具待抽离, key统一定义
static readonly isPortraitKey: string = "xy_screen_is_portrait";
private static instance: ScreenUtil;
public width: number = 0
public height: number = 0
public statusBarHeight: number = 0
public bottomSafeHeight: number = 0
public contentHeight: number = 0;
public layoutWidth: number = 0
public layoutHeight: number = 0
private portraitListener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: portrait)');
public static getInstance(): ScreenUtil {
if (!ScreenUtil.instance) {
ScreenUtil.instance = new ScreenUtil();
}
return ScreenUtil.instance;
}
initScreenSize(): void {
this.portraitListener.on('change', (result)=> {
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, result.matches)
})
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
let avoidArea = windowClass.getWindowAvoidArea(type);
this.bottomSafeHeight = px2vp(avoidArea.bottomRect.height); // 获取到导航条区域的高度
type = window.AvoidAreaType.TYPE_SYSTEM;
avoidArea = windowClass.getWindowAvoidArea(type);
this.statusBarHeight = px2vp(avoidArea.topRect.height);
this.width = px2vp(windowClass.getWindowProperties().windowRect.width);
this.height = px2vp(windowClass.getWindowProperties().windowRect.height);
this.layoutWidth = this.width;
this.layoutHeight = this.height - this.statusBarHeight - this.bottomSafeHeight;
})
.catch((error: Error) => {
})
}
setPreferredOrientation(orientation: window.Orientation): void {
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
windowClass.setPreferredOrientation(orientation);
})
.catch((error: Error) => {
})
}
setPreferredOrientationCallBack(orientation: window.Orientation, callback: ()=>void): void{
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
windowClass.setPreferredOrientation(orientation,callback);
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, orientation == window.Orientation.PORTRAIT || orientation == window.Orientation.PORTRAIT_INVERTED);
})
.catch((error: Error) => {
})
}
setWindowLayoutFullScreen(isLayoutFullScreen: boolean): void {
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen);
windowClass.setSpecificSystemBarEnabled('status', !isLayoutFullScreen);
})
.catch((error: Error) => {
})
}
setWindowSystemBarProperties(systemBarProperties: window.SystemBarProperties): void {
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
windowClass.setWindowSystemBarProperties(systemBarProperties, (err) => {
if (err.code) {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the system bar properties.');
});
})
.catch((error: Error) => {
})
}
setWindowSystemBarEnable(names:Array<'status' | 'navigation'>){
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
windowClass.setWindowSystemBarEnable(names, (err) => {
if (err.code) {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the system bar properties.');
});
})
.catch((error: Error) => {
})
}
}