提交 6a772945 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

优化iOS插件示例

上级 3c5f0a7f
......@@ -14,8 +14,6 @@ export const getBatteryInfo : GetBatteryInfo = function (options) {
// 返回数据
const res : GetBatteryInfoSuccess = {
errCode: 0,
errSubject: "uni-getBatteryInfo",
errMsg: "getBatteryInfo:ok",
level: new Int(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
......
......@@ -2,11 +2,13 @@ import { NotificationCenter } from 'Foundation';
import { UIApplication } from "UIKit"
import { Selector } from "ObjectiveC"
import { OffMemoryWarning, OnMemoryWarning, OnMemoryWarningCallback, OnMemoryWarningCallbackResult } from "../interface.uts"
class MemoryWarningTool {
static listeners: UTSCallback[] = []
static listeners: OnMemoryWarningCallback[] = []
// 监听内存警告
static listenMemoryWarning(callback: UTSCallback) {
static listenMemoryWarning(callback: OnMemoryWarningCallback) {
// 只有首次才需要注册监听事件
if (this.listeners.length == 0) {
......@@ -23,12 +25,13 @@ class MemoryWarningTool {
@objc static receiveMemoryWarning() {
// 触发回调
this.listeners.forEach(listener => {
listener({})
const ret: OnMemoryWarningCallbackResult = {}
listener(ret)
})
}
// 移除监听事件
static removeListen(callback: UTSCallback | null) {
static removeListen(callback: OnMemoryWarningCallback | null) {
// 移除所有监听
if (callback == null) {
this.listeners = []
......@@ -46,11 +49,11 @@ class MemoryWarningTool {
}
// 开启监听内存警告
export function onMemoryWarning(callback: UTSCallback) {
export const onMemoryWarning : OnMemoryWarning = function (callback: OnMemoryWarningCallback) {
MemoryWarningTool.listenMemoryWarning(callback)
}
// 关闭监听内存警告
export function offMemoryWarning(callback: UTSCallback | null) {
export const offMemoryWarning : OffMemoryWarning = function (callback: OnMemoryWarningCallback | null) {
MemoryWarningTool.removeListen(callback)
}
\ No newline at end of file
export type OnMemoryWarningCallbackResult = {
level : number | null
}
export type OnMemoryWarningCallback = (res : OnMemoryWarningCallbackResult) => void
export type OnMemoryWarning = (callback : OnMemoryWarningCallback) => void
export type OffMemoryWarning = (callback : OnMemoryWarningCallback | null) => void
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册