index.uts 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
import { SetStorage, SetStorageOptions, SetStorageSuccess } from "../interface.uts"
import { SetStorageSync } from "../interface.uts"
import { GetStorage, GetStorageOptions, GetStorageSuccess } from "../interface.uts"
import { GetStorageSync } from "../interface.uts"
import { GetStorageInfoSuccess, GetStorageInfo, GetStorageInfoOptions } from "../interface.uts"
import { GetStorageInfoSync } from "../interface.uts"

import { RemoveStorage, RemoveStorageOptions, RemoveStorageSuccess } from "../interface.uts"
import { RemoveStorageSync } from "../interface.uts"
import { ClearStorage, ClearStorageSuccess, ClearStorageOptions } from "../interface.uts"
import { ClearStorageSync } from "../interface.uts"

// @ts-expect-error
import DCStorage from "io.dcloud.common.util.db.DCStorage";
// @ts-expect-error
import UTSAndroid from 'io.dcloud.uts.UTSAndroid';
// @ts-expect-error
import List from 'java.util.List';
import {
  uni_getStorageAsync,
  uni_getStorageSync,
  uni_setStorageAsync,
  uni_setStorageSync
} from "../uniStorageTool.uts"


/**
 * 设置储存项
 */
export const setStorage: SetStorage = function (options: SetStorageOptions) {

  setTimeout(function () {

    let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());
    if (dcStorage == null) {
	  let ret = new UniError("uni-setStorage", -1, "storage not found.") 
      options.fail?.(ret)
      options.complete?.(ret)
      return
    }
    /**
     * 通过公共函数执行设置逻辑
     */
    uni_setStorageAsync(options, (itemKey: string, itemData: string) => {
      dcStorage.performSetItem(UTSAndroid.getUniActivity(), UTSAndroid.getAppId(), itemKey, itemData);
    }, (itemKey: string) => {
      dcStorage.performRemoveItem(UTSAndroid.getUniActivity(), UTSAndroid.getAppId(), itemKey);
    })


  }, 0)
}

/**
 * 同步设置储存项
 */
export const setStorageSync: SetStorageSync = function (key: string, data: any) {

  let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());

  if (dcStorage == null) {
    // 异常了
    return;
  }
  uni_setStorageSync(key, data, (itemKey: string, itemValue: string) => {
    let dataString = itemValue
    if (typeof data == "string") {
      // string 类型不进行序列化
      dataString = data as string
    }
    dcStorage.performSetItem(UTSAndroid.getUniActivity(), UTSAndroid.getAppId(), itemKey, dataString);
  }, (itemKey: string) => {
    dcStorage.performRemoveItem(UTSAndroid.getUniActivity(), UTSAndroid.getAppId(), itemKey);
  })


}


/**
 * 获取储存项
 */
function includeKey(key: string): boolean {

  let info = DCStorage.getDCStorage(UTSAndroid.getUniActivity()!).performGetAllKeys(UTSAndroid.getAppId())
  console.log(info)
  if (info.v != null && info.code == DCStorage.SUCCESS) {
    //&& info.v instanceof java.util.List
    // @ts-expect-error
    let keys = UTSArray.fromNative((info.v as ArrayList<String>));
    if (keys.indexOf(key) > -1) {
      return true
    }
  }
  return false;
}

export const getStorage: GetStorage = function getStorage(options: GetStorageOptions) {


  let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());

  if (dcStorage == null) {
	let ret = new UniError("uni-setStorage", -1, "storage not found.") 
    options.fail?.(ret)
    options.complete?.(ret)
    return
  }

  setTimeout(function () {

    uni_getStorageAsync(options, function (itemKey: string): string | null {

      let info = dcStorage.performGetItem(UTSAndroid.getAppId(), itemKey);
      if (info != null && info.code == DCStorage.SUCCESS && info.v != null) {
        // 获取成功
        return info.v as string
      }

      return null
    }, function (key: string): boolean {
      let list: String[] = []

      let info = dcStorage.performGetAllKeys(UTSAndroid.getAppId())
      if (info.code == DCStorage.SUCCESS && info.v != null) {

        /**
         * 临时语法支持,foreach 组织数组格式
         */
        let arrayKeys: String[] = []
        ;(info.v as List<string>).forEach((perKey: string) => {
          arrayKeys.push(perKey)
        });

        list = arrayKeys
      }

      if (list != null) {
        let item = list!.find((value): boolean => {
          if (typeof value == "string") {
            return (value as string) == key;
          }
          return false;
        })
        return (item != null);
      }
      return false;
    })


  }, 0)
}


/**
 * 同步获取储存项
 */
export const getStorageSync: GetStorageSync = function (key: string): any | null {


  return uni_getStorageSync(key, function (itemKey: string): string | null {
    let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());

    if (dcStorage == null) {
      return "";
    }

    let info = dcStorage.performGetItem(UTSAndroid.getAppId(), itemKey);
    if (info != null && info.code == DCStorage.SUCCESS && info.v != null) {
      // 获取成功
      return info.v as string
    }
    return ""
  });


}

/**
 * 获取储存信息
 */
export const getStorageInfo: GetStorageInfo = function (options: GetStorageInfoOptions) {

  setTimeout(function () {


    let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());

    if (dcStorage == null) {
	  let ret = new UniError("uni-setStorage", -1, "storage not found.") 
      options.fail?.(ret)
      options.complete?.(ret)
    }

    let ret: GetStorageInfoSuccess = {
      keys: [],
      currentSize: 0,
      limitSize: 10240
    }

    let info = dcStorage.performGetAllKeys(UTSAndroid.getAppId());
    if (info.code == DCStorage.SUCCESS && info.v != null) {

      let arrayKeys: string[] = []

        /**
         * 临时语法支持,foreach 组织数组格式
         */
      ;(info.v as List<string>).forEach((perKey: string) => {
        arrayKeys.push(perKey)
      });

      /**
       * 储存的最大值:SQLLite数据库当前允许的最大值
       * 储存的已用值:SQLLite数据文件当前的大小
       */
      try {
        ret.keys = arrayKeys

        let limitNum = dcStorage.getDBMaxLength(UTSAndroid.getAppId()).toDouble()
        ret.limitSize = limitNum / 1024

        let currentNum = dcStorage.getDBCurrentLength(UTSAndroid.getAppId()).toDouble()
        ret.currentSize = currentNum / 1024
      } catch (e) {
        //TODO handle the exception
      }

      // let arrayRet = info.v as Array
      options.success?.(ret)
    }
  }, 0)

}


/**
 * 同步获取储存信息
 */
export const getStorageInfoSync: GetStorageInfoSync = function (): GetStorageInfoSuccess {

  // android SQL 单表最大限制为
  let ret: GetStorageInfoSuccess = {
    keys: [],
    currentSize: 0,
    limitSize: 10240,
  }

  let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());
  if (dcStorage == null) {
    return ret
  }


  let info = dcStorage.performGetAllKeys(UTSAndroid.getAppId());
  if (info.code == DCStorage.SUCCESS && info.v != null) {

    /**
     * 临时语法支持,foreach 组织数组格式
     */
    let arrayKeys: string[] = []
    ;(info.v as List<string>).forEach((perKey: string) => {
      arrayKeys.push(perKey)
    });

    ret.keys = arrayKeys
  }

  /**
   * 储存的最大值:SQLLite数据库当前允许的最大值
   * 储存的已用值:SQLLite数据文件当前的大小
   */
  try {
    let limitNum = dcStorage.getDBMaxLength(UTSAndroid.getAppId()).toDouble()
    ret.limitSize = limitNum / 1024

    let currentNum = dcStorage.getDBCurrentLength(UTSAndroid.getAppId()).toDouble()
    ret.currentSize = currentNum / 1024
  } catch (e) {
    //TODO handle the exception
  }


  return ret

}

/**
 * 移除指定储存项
 */
export const removeStorage: RemoveStorage = function (options: RemoveStorageOptions) {

  setTimeout(function () {
    let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());
    if (dcStorage == null) {
	  let ret = new UniError("uni-removeStorage", -1, "storage not found.") 
      options.fail?.(ret)
      options.complete?.(ret)
      return;
    }
    dcStorage.performRemoveItem(UTSAndroid.getUniActivity(), UTSAndroid.getAppId(), options.key);
    let ret: RemoveStorageSuccess = {}
    options.success?.(ret)
    options.complete?.(ret)

  }, 0)

}

/**
 * 同步移除指定储存项
 */
export const removeStorageSync: RemoveStorageSync = function (key: string) {

  let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());
  if (dcStorage == null) {
    return;
  }
  dcStorage.performRemoveItem(UTSAndroid.getUniActivity(), UTSAndroid.getAppId(), key);

}

/**
 * 清空储存选项
 */
export const clearStorage: ClearStorage = function (option: ClearStorageOptions | null) {

  setTimeout(function () {
    let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());
    if (dcStorage == null) {
	  let ret = new UniError("uni-clearStorage", -1, "error:storage not found.") 
      option?.fail?.(ret)
      option?.complete?.(ret)
      return;
    }
    dcStorage.performClear(UTSAndroid.getUniActivity(), UTSAndroid.getAppId());


    let ret: ClearStorageSuccess = {}
    option?.success?.(ret)
    option?.complete?.(ret)
  }, 0)

}

/**
 * 同步清空储存选项
 */
export const clearStorageSync: ClearStorageSync = function () {


  let dcStorage = DCStorage.getDCStorage(UTSAndroid.getUniActivity());
  if (dcStorage == null) {
    return;
  }
  dcStorage.performClear(UTSAndroid.getUniActivity(), UTSAndroid.getAppId());

}