Skip to content

Commit 9e6c7ea

Browse files
committed
setBlockUpdate
1 parent f461c8d commit 9e6c7ea

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

ios/RCTPushy/RCTPushy.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
static NSString *const paramCurrentVersion = @"currentVersion";
2828
static NSString *const paramIsFirstTime = @"isFirstTime";
2929
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
30+
static NSString *const keyBlockUpdate = @"blockUpdate"
3031
static NSString *const keyFirstLoadMarked = @"REACTNATIVECN_PUSHY_FIRSTLOADMARKED_KEY";
3132
static NSString *const keyRolledBackMarked = @"REACTNATIVECN_PUSHY_ROLLEDBACKMARKED_KEY";
3233
static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_PUSHY_ISPACKAGEUPDATEDMARKED_KEY";
@@ -155,6 +156,7 @@ - (NSDictionary *)constantsToExport
155156
ret[@"buildTime"] = [RCTPushy buildTime];
156157
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
157158
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
159+
ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate];
158160
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
159161
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
160162

@@ -188,6 +190,16 @@ - (instancetype)init
188190
return self;
189191
}
190192

193+
RCT_EXPORT_METHOD(setBlockUpdate:(NSDictionary *)options)
194+
{
195+
// NSMutableDictionary *blockUpdateInfo = [NSMutableDictionary new];
196+
// blockUpdateInfo[@"reason"] = options[@"reason"];
197+
// blockUpdateInfo[@"until"] = options[@"until"];
198+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
199+
[defaults setObject:options forKey:keyBlockUpdate];
200+
[defaults synchronize];
201+
}
202+
191203
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options
192204
resolver:(RCTPromiseResolveBlock)resolve
193205
rejecter:(RCTPromiseRejectBlock)reject)

lib/index.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const currentVersion = Pushy.currentVersion;
1818
export const isFirstTime = Pushy.isFirstTime;
1919
export const isRolledBack = Pushy.isRolledBack;
2020
export const buildTime = Pushy.buildTime;
21+
let blockUpdate = Pushy.blockUpdate;
2122

2223
if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
2324
throw new Error(
@@ -27,7 +28,7 @@ if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
2728

2829
/*
2930
Return json:
30-
Package was expired:
31+
Package expired:
3132
{
3233
expired: true,
3334
downloadUrl: 'http://appstore/downloadUrl',
@@ -56,6 +57,14 @@ function assertRelease() {
5657

5758
export async function checkUpdate(APPKEY, isRetry) {
5859
assertRelease();
60+
if (blockUpdate && blockUpdate.until > Date.now()) {
61+
console.warn(
62+
`Pushy update is blocked until ${new Date(
63+
blockUpdate.until,
64+
).toLocaleString()}. Reason: ${blockUpdate.reason}`,
65+
);
66+
return;
67+
}
5968
let resp;
6069
try {
6170
resp = await fetch(getCheckUrl(APPKEY), {
@@ -78,19 +87,35 @@ export async function checkUpdate(APPKEY, isRetry) {
7887
return checkUpdate(APPKEY, true);
7988
}
8089

90+
checkOperation(resp);
91+
8192
if (resp.status !== 200) {
8293
throw new Error((await resp.json()).message);
8394
}
8495

8596
return resp.json();
8697
}
8798

99+
function checkOperation(resp) {
100+
if (!Array.isArray(resp.op)) {
101+
return;
102+
}
103+
resp.op.forEach((action) => {
104+
if (action.type === 'block') {
105+
blockUpdate = {
106+
reason: action.reason,
107+
until: Date.now() + action.duration,
108+
};
109+
Pushy.setBlockUpdate(blockUpdate);
110+
}
111+
});
112+
}
113+
88114
export async function downloadUpdate(options) {
89115
assertRelease();
90116
if (!options.update) {
91117
return;
92118
}
93-
94119
if (options.diffUrl) {
95120
await Pushy.downloadPatchFromPpk({
96121
updateUrl: options.diffUrl,

0 commit comments

Comments
 (0)