mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 18:12:09 +08:00

BREAKING CHANGES: `Application.orientation` is no longer a function. Migration: Remove `()` from the `Application.orientation()` call: ```diff import { Application } from "@nativescript/core"; -console.log(Application.orientation()); +console.log(Application.orientation); ``` `Application.systemAppearance` is no longer a function. Migration: Remove `()` from the `Application.systemAppearance()` call: ```diff import { Application } from "@nativescript/core"; -console.log(Application.systemAppearance()); +console.log(Application.systemAppearance); ```
153 lines
3.4 KiB
TypeScript
153 lines
3.4 KiB
TypeScript
import * as AndroidUtils from './android';
|
|
import { platformCheck } from './platform-check';
|
|
import { numberHasDecimals, numberIs64Bit } from './types';
|
|
|
|
export function dataDeserialize(nativeData?: any) {
|
|
if (nativeData === null || typeof nativeData !== 'object') {
|
|
return nativeData;
|
|
}
|
|
let store;
|
|
|
|
switch (nativeData.getClass().getName()) {
|
|
case 'java.lang.String': {
|
|
return String(nativeData);
|
|
}
|
|
|
|
case 'java.lang.Boolean': {
|
|
return String(nativeData) === 'true';
|
|
}
|
|
|
|
case 'java.lang.Float':
|
|
case 'java.lang.Integer':
|
|
case 'java.lang.Long':
|
|
case 'java.lang.Double':
|
|
case 'java.lang.Short': {
|
|
return Number(nativeData);
|
|
}
|
|
|
|
case 'org.json.JSONArray': {
|
|
store = [];
|
|
for (let j = 0; j < nativeData.length(); j++) {
|
|
store[j] = dataDeserialize(nativeData.get(j));
|
|
}
|
|
break;
|
|
}
|
|
case 'org.json.JSONObject': {
|
|
store = {};
|
|
const i = nativeData.keys();
|
|
let key;
|
|
while (i.hasNext()) {
|
|
key = i.next();
|
|
store[key] = dataDeserialize(nativeData.get(key));
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'androidx.collection.SimpleArrayMap': {
|
|
const count = nativeData.size();
|
|
for (let l = 0; l < count; l++) {
|
|
const key = nativeData.keyAt(l);
|
|
store[key] = dataDeserialize(nativeData.get(key));
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'androidx.collection.ArrayMap':
|
|
case 'android.os.Bundle':
|
|
case 'java.util.HashMap':
|
|
case 'java.util.Map': {
|
|
store = {};
|
|
const keys = nativeData.keySet().toArray();
|
|
for (let k = 0; k < keys.length; k++) {
|
|
const key = keys[k];
|
|
store[key] = dataDeserialize(nativeData.get(key));
|
|
}
|
|
break;
|
|
}
|
|
|
|
default:
|
|
if (typeof nativeData === 'object' && nativeData instanceof java.util.List) {
|
|
const array = [];
|
|
const size = nativeData.size();
|
|
for (let i = 0, n = size; i < n; i++) {
|
|
array[i] = dataDeserialize(nativeData.get(i));
|
|
}
|
|
store = array;
|
|
} else {
|
|
store = null;
|
|
}
|
|
break;
|
|
}
|
|
return store;
|
|
}
|
|
|
|
export function dataSerialize(data?: any, wrapPrimitives?: boolean) {
|
|
let store;
|
|
switch (typeof data) {
|
|
case 'string':
|
|
case 'boolean': {
|
|
if (wrapPrimitives) {
|
|
if (typeof data === 'string') {
|
|
return new java.lang.String(data);
|
|
}
|
|
return new java.lang.Boolean(data);
|
|
}
|
|
return data;
|
|
}
|
|
case 'number': {
|
|
const hasDecimals = numberHasDecimals(data);
|
|
if (numberIs64Bit(data)) {
|
|
if (hasDecimals) {
|
|
return java.lang.Double.valueOf(data);
|
|
} else {
|
|
return java.lang.Long.valueOf(data);
|
|
}
|
|
} else {
|
|
if (hasDecimals) {
|
|
return java.lang.Float.valueOf(data);
|
|
} else {
|
|
return java.lang.Integer.valueOf(data);
|
|
}
|
|
}
|
|
}
|
|
|
|
case 'object': {
|
|
if (!data) {
|
|
return null;
|
|
}
|
|
|
|
if (data instanceof Date) {
|
|
return new java.util.Date(data.getTime());
|
|
}
|
|
|
|
if (Array.isArray(data)) {
|
|
store = new java.util.ArrayList();
|
|
data.forEach((item) => store.add(dataSerialize(item, wrapPrimitives)));
|
|
return store;
|
|
}
|
|
|
|
if (data.native) {
|
|
return data.native;
|
|
}
|
|
|
|
store = new java.util.HashMap();
|
|
Object.keys(data).forEach((key) => store.put(key, dataSerialize(data[key], wrapPrimitives)));
|
|
return store;
|
|
}
|
|
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export import android = AndroidUtils;
|
|
|
|
/**
|
|
* @deprecated Use `Utils.android` instead.
|
|
*/
|
|
export import ad = AndroidUtils;
|
|
|
|
// these don't exist on Android.Stub them to empty functions.
|
|
export const iOSNativeHelper = platformCheck('Utils.iOSNativeHelper');
|
|
export const ios = platformCheck('Utils.ios');
|