Skip to content

Commit

Permalink
[MALI-1408] - MiniApp preferences feature (#289)
Browse files Browse the repository at this point in the history
* [MALI-1408] - Initial commit

* Refactor
  • Loading branch information
rleojoseph authored Jan 18, 2024
1 parent 8fc3597 commit cc222c9
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 40 deletions.
51 changes: 36 additions & 15 deletions js-miniapp-bridge/src/common-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
NotificationInfo,
NotificationInfoType,
} from './types/notification/notification-info';
import { MiniAppPreferences } from './modules/miniapp-preferences';

/** @internal */
const mabMessageQueue: Callback[] = [];
Expand Down Expand Up @@ -88,11 +89,13 @@ export class MiniAppBridge {
isSecureStorageReady = false;
secureStorageLoadError: MiniAppError | null = null;
private notificationBridge: NotificationBridge;
preferences: MiniAppPreferences;

constructor(executor: PlatformExecutor) {
this.executor = executor;
this.platform = executor.getPlatform();
this.notificationBridge = new NotificationBridge(executor);
this.preferences = new MiniAppPreferences(executor);

if (window) {
window.addEventListener(
Expand Down Expand Up @@ -805,7 +808,7 @@ export class MiniAppBridge {
'isDarkMode',
null,
response => {
resolve(BooleanValue(response));
resolve(MiniAppBridgeUtils.BooleanValue(response));
},
error => reject(parseMiniAppError(error))
);
Expand Down Expand Up @@ -864,6 +867,22 @@ export class MiniAppBridge {
notificationDetailedInfo
);
}

set(key: string, value: string) {
return this.preferences.set(key, value);
}

get(key: string) {
return this.preferences.get(key);
}

remove(key: string) {
return this.preferences.remove(key);
}

clearMiniAppPreferences() {
return this.preferences.clearMiniAppPreferences();
}
}

/**
Expand Down Expand Up @@ -907,20 +926,6 @@ function trimBannerText(message: string = null, maxLength = 128) {
: message;
}

function BooleanValue(value) {
if (typeof value === 'boolean') {
return value;
} else if (typeof value === 'string') {
const lowerCaseValue = value.toLowerCase();
if (lowerCaseValue === 'true' || lowerCaseValue === '1') {
return true;
} else if (lowerCaseValue === 'false' || lowerCaseValue === '0') {
return false;
}
}
return false;
}

const parseIntOctal = octalCode => {
return Number.parseInt(octalCode, 8);
};
Expand Down Expand Up @@ -965,3 +970,19 @@ function isValidJson(str) {
}
return true;
}

export class MiniAppBridgeUtils {
static BooleanValue(value) {
if (typeof value === 'boolean') {
return value;
} else if (typeof value === 'string') {
const lowerCaseValue = value.toLowerCase();
if (lowerCaseValue === 'true' || lowerCaseValue === '1') {
return true;
} else if (lowerCaseValue === 'false' || lowerCaseValue === '0') {
return false;
}
}
return false;
}
}
85 changes: 85 additions & 0 deletions js-miniapp-bridge/src/modules/miniapp-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { MiniAppBridgeUtils, PlatformExecutor } from '../common-bridge';
import { parseMiniAppError } from '../types/error-types';

export class MiniAppPreferences {
executor: PlatformExecutor;
platform: string;

constructor(executor: PlatformExecutor) {
this.executor = executor;
this.platform = executor.getPlatform();
}

/**
* Sets the value of the specified default key.
* @param {key} string
* @param {value} string
* @see {set}
*/
set(key: string, value: string) {
return new Promise<boolean>((resolve, reject) => {
return this.executor.exec(
'setPreference',
{ preferenceKey: key, preferenceValue: value },
response => {
resolve(MiniAppBridgeUtils.BooleanValue(response));
},
error => reject(parseMiniAppError(error))
);
});
}

/**
* Returns the object associated with the specified key.
* @param {key} string
* @see {get}
*/
get(key: string) {
return new Promise<string>((resolve, reject) => {
return this.executor.exec(
'getPreference',
{ preferenceKey: key },
response => {
resolve(response);
},
error => reject(parseMiniAppError(error))
);
});
}

/**
* Removes the value of the specified default key.
* @param {key} string
* @see {remove}
*/
remove(key: string) {
return new Promise<boolean>((resolve, reject) => {
return this.executor.exec(
'getPreference',
{ preferenceKey: key },
response => {
resolve(MiniAppBridgeUtils.BooleanValue(response));
},
error => reject(parseMiniAppError(error))
);
});
}

/**
* Removes all keys that is stored
* @param {key} string
* @see {clearMiniAppPreferences}
*/
clearMiniAppPreferences() {
return new Promise<boolean>((resolve, reject) => {
return this.executor.exec(
'clearMiniAppPreferences',
null,
response => {
resolve(MiniAppBridgeUtils.BooleanValue(response));
},
error => reject(parseMiniAppError(error))
);
});
}
}
5 changes: 5 additions & 0 deletions js-miniapp-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

### 1.20.0 (2024-xx-xx)
- **Feature:** Added new interfaces `set(key: string, value: string)`, `get(key: string)`, `remove(key: string)`, `clearMiniAppPreferences()` which uses the native storage features like Shared Preferences/User defaults to store anything from MiniApp.
- **Feature:** Updated HostEnvironmentInfo to have `hostBuildType`, `deviceToken` and `pushToken`
- **Fix:** Few Contacts with special characters is failed to retrieve, its fixed now

### 1.19.0 (2023-11-02)
- **Feature:** Added new interface `getAllCookies()` to get `CookieInfo` which contains `name` and `value` of the cookie
- **Feature:** Added new interface `getCookies(cookieNameList:)` that requests for certain cookies to host application that will get `CookieInfo` which contains `name` and `value` of the cookie
Expand Down
Loading

0 comments on commit cc222c9

Please sign in to comment.