Skip to content

Commit 1767fe3

Browse files
authored
feat: add restartApp (#488)
1 parent 7a9f579 commit 1767fe3

File tree

8 files changed

+91
-0
lines changed

8 files changed

+91
-0
lines changed

android/src/main/java/cn/reactnative/modules/update/UpdateModuleImpl.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ public void run() {
176176
});
177177
}
178178

179+
public static void restartApp(final ReactApplicationContext mContext, Promise promise) {
180+
UiThreadUtil.runOnUiThread(new Runnable() {
181+
@Override
182+
public void run() {
183+
try {
184+
final Context application = mContext.getApplicationContext();
185+
ReactInstanceManager instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
186+
187+
instanceManager.recreateReactContextInBackground();
188+
promise.resolve(true);
189+
190+
} catch (Throwable err) {
191+
promise.reject("restartApp failed: "+err.getMessage());
192+
Log.e("pushy", "restartApp failed", err);
193+
194+
final Activity currentActivity = mContext.getCurrentActivity();
195+
if (currentActivity == null) {
196+
return;
197+
}
198+
currentActivity.runOnUiThread(new Runnable() {
199+
@Override
200+
public void run() {
201+
currentActivity.recreate();
202+
}
203+
});
204+
}
205+
}
206+
});
207+
}
179208

180209
public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options, Promise promise) {
181210
final String hash = options.getString("hash");

android/src/newarch/cn/reactnative/modules/update/UpdateModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public void reloadUpdate(ReadableMap options,Promise promise) {
9797
UpdateModuleImpl.reloadUpdate(updateContext, mContext, options,promise);
9898
}
9999

100+
@Override
101+
public void restartApp(Promise promise) {
102+
UpdateModuleImpl.restartApp(updateContext, mContext, promise);
103+
}
104+
100105
@Override
101106
public void setNeedUpdate(ReadableMap options,Promise promise) {
102107
UpdateModuleImpl.setNeedUpdate(updateContext, options,promise);

android/src/oldarch/cn/reactnative/modules/update/UpdateModule.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,29 @@ public void run() {
224224
});
225225
}
226226

227+
@ReactMethod
228+
public void restartApp(final Promise promise) {
229+
230+
UiThreadUtil.runOnUiThread(new Runnable() {
231+
@Override
232+
public void run() {
233+
try {
234+
final Context application = getReactApplicationContext().getApplicationContext();
235+
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
236+
if (instanceManager == null) {
237+
instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager();
238+
}
239+
instanceManager.recreateReactContextInBackground();
240+
promise.resolve(true);
241+
242+
} catch (Throwable err) {
243+
promise.reject(err);
244+
Log.e("pushy", "restartApp failed ", err);
245+
}
246+
}
247+
});
248+
}
249+
227250
@ReactMethod
228251
public void setNeedUpdate(ReadableMap options) {
229252
final String hash = options.getString("hash");

ios/RCTPushy/RCTPushy.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,26 @@ - (instancetype)init
338338
}
339339
}
340340

341+
RCT_EXPORT_METHOD(restartApp:(RCTPromiseResolveBlock)resolve
342+
rejecter:(RCTPromiseRejectBlock)reject)
343+
{
344+
@try {
345+
dispatch_async(dispatch_get_main_queue(), ^{
346+
[self.bridge reload];
347+
});
348+
#if __has_include("RCTReloadCommand.h")
349+
// reload 0.62+
350+
RCTReloadCommandSetBundleURL([[self class] bundleURL]);
351+
RCTTriggerReloadCommandListeners(@"pushy restartApp");
352+
#endif
353+
354+
resolve(@true);
355+
}
356+
@catch (NSException *exception) {
357+
reject(@"执行报错", exception.reason, nil);
358+
}
359+
}
360+
341361
RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve
342362
rejecter:(RCTPromiseRejectBlock)reject)
343363
{

src/NativePushy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface Spec extends TurboModule {
1515
getLocalHashInfo(hash: string): Promise<string>;
1616
setUuid(uuid: string): Promise<void>;
1717
reloadUpdate(options: { hash: string }): Promise<void>;
18+
restartApp(): Promise<void>;
1819
setNeedUpdate(options: { hash: string }): Promise<void>;
1920
markSuccess(): Promise<void>;
2021
downloadPatchFromPpk(options: {

src/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ export class Pushy {
544544
delete Pushy.progressHandlers[progressKey];
545545
}
546546
};
547+
restartApp = async () => {
548+
return PushyModule.restartApp();
549+
};
547550
}
548551

549552
// for international users

src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const defaultContext = {
1313
dismissError: noop,
1414
downloadUpdate: asyncNoop,
1515
downloadAndInstallApk: asyncNoop,
16+
restartApp: asyncNoop,
1617
getCurrentVersionInfo: () => Promise.resolve({}),
1718
parseTestQrCode: () => false,
1819
currentHash: '',
@@ -33,6 +34,7 @@ export const UpdateContext = createContext<{
3334
metaInfo?: string;
3435
}>;
3536
parseTestQrCode: (code: string) => boolean;
37+
restartApp: () => Promise<void>;
3638
currentHash: string;
3739
packageVersion: string;
3840
client?: Pushy | Cresc;

src/provider.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ export const UpdateProvider = ({
316316
[parseTestPayload],
317317
);
318318

319+
const restartApp = useCallback(
320+
async () => {
321+
return client.restartApp();
322+
},
323+
[client],
324+
);
325+
319326
useEffect(() => {
320327
const parseLinking = (url: string | null) => {
321328
if (!url) {
@@ -361,6 +368,7 @@ export const UpdateProvider = ({
361368
downloadAndInstallApk,
362369
getCurrentVersionInfo,
363370
parseTestQrCode,
371+
restartApp,
364372
}}>
365373
{children}
366374
</UpdateContext.Provider>

0 commit comments

Comments
 (0)