调整架构 添加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

@@ -0,0 +1,65 @@
import { Context } from '@kit.AbilityKit';
import dataPreferences from '@ohos.data.preferences';
import { Log } from './Log';
export class BaseLocalStorageKey {
static readonly KEY_SERVER_CONFIG = "key_config";
}
export class BaseLocalStorage {
private static instance: BaseLocalStorage;
private static lock: boolean = false;
private static readonly XY_DP_Name = "LocalData";
static getInstance(): BaseLocalStorage {
if (!BaseLocalStorage.instance) {
if (!BaseLocalStorage.lock) {
BaseLocalStorage.lock = true;
BaseLocalStorage.instance = new BaseLocalStorage();
BaseLocalStorage.lock = false;
}
}
return BaseLocalStorage.instance;
}
private context: Context | null = null;
private preferences: dataPreferences.Preferences | null = null;
private constructor() {
}
public init(context: Context): void {
if (!this.context) {
this.context = context.getApplicationContext();
let options: dataPreferences.Options = { name: BaseLocalStorage.XY_DP_Name };
this.preferences = dataPreferences.getPreferencesSync(this.context, options);
} else {
Log.i("LocalStorage is already init.")
}
}
public putData(key: string, value: dataPreferences.ValueType) {
Log.i(`put sp data, key:${key}, value:${value}`)
this.preferences?.putSync(key, value);
this.preferences?.flush();
}
public clearData(key: string) {
this.preferences?.delete(key);
this.preferences?.flush();
}
public getData(key: string, defaultValue: dataPreferences.ValueType): dataPreferences.ValueType | undefined {
let value = this.preferences?.getSync(key, defaultValue);
Log.i(`get sp data, key:${key}, value:${value}`)
return value;
}
public getObject<T>(key: string): T {
let value = this.getData(key, "{}") as string;
if (value.toString().length > 0) {
return JSON.parse(value) as T;
}
return "{}" as T;
}
}

View File

@@ -0,0 +1,33 @@
import hilog from '@ohos.hilog';
let domain: number = 0xFF00;
let prefix: string = 'HM4Demo';
let format: string = `%{public}s, %{public}s`;
export class Log {
static d(...args: string[]) {
hilog.debug(domain, prefix, format, args);
}
static i(...args: string[]) {
hilog.info(domain, prefix, format, args);
}
static w(...args: string[]) {
hilog.warn(domain, prefix, format, args);
}
static e(...args: string[]) {
hilog.error(domain, prefix, format, args);
}
static f(...args: string[]) {
hilog.fatal(domain,prefix, format, args);
}
static isLoggable(level: number) {
hilog.isLoggable(domain, prefix, level);
}
}

View File

@@ -0,0 +1,115 @@
import window from '@ohos.window';
import mediaquery from '@ohos.mediaquery';
import { Log } from './Log';
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) => {
})
}
setWindowKeepScreenOn(isKeep: boolean) {
Log.i(`setWindowKeepScreenOn:${isKeep}`)
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
windowClass.setWindowKeepScreenOn(isKeep);
});
}
}