mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-10-31 21:33:12 +08:00 
			
		
		
		
	feat: add restartApp (#488)
This commit is contained in:
		| @@ -176,6 +176,35 @@ public class UpdateModuleImpl { | |||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static void restartApp(final ReactApplicationContext mContext, Promise promise) { | ||||||
|  |           UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|  |               @Override | ||||||
|  |               public void run() { | ||||||
|  |                   try { | ||||||
|  |                       final Context application = mContext.getApplicationContext(); | ||||||
|  |                       ReactInstanceManager instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager(); | ||||||
|  |  | ||||||
|  |                       instanceManager.recreateReactContextInBackground(); | ||||||
|  |                       promise.resolve(true); | ||||||
|  |  | ||||||
|  |                   } catch (Throwable err) { | ||||||
|  |                       promise.reject("restartApp failed: "+err.getMessage()); | ||||||
|  |                       Log.e("pushy", "restartApp failed", err); | ||||||
|  |  | ||||||
|  |                       final Activity currentActivity = mContext.getCurrentActivity(); | ||||||
|  |                       if (currentActivity == null) { | ||||||
|  |                           return; | ||||||
|  |                       } | ||||||
|  |                       currentActivity.runOnUiThread(new Runnable() { | ||||||
|  |                           @Override | ||||||
|  |                           public void run() { | ||||||
|  |                               currentActivity.recreate(); | ||||||
|  |                           } | ||||||
|  |                       }); | ||||||
|  |                   } | ||||||
|  |               } | ||||||
|  |           }); | ||||||
|  |       } | ||||||
|  |  | ||||||
|     public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options, Promise promise) { |     public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options, Promise promise) { | ||||||
|         final String hash = options.getString("hash"); |         final String hash = options.getString("hash"); | ||||||
|   | |||||||
| @@ -97,6 +97,11 @@ public class UpdateModule extends NativePushySpec { | |||||||
|         UpdateModuleImpl.reloadUpdate(updateContext, mContext, options,promise); |         UpdateModuleImpl.reloadUpdate(updateContext, mContext, options,promise); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void restartApp(Promise promise) { | ||||||
|  |         UpdateModuleImpl.restartApp(updateContext, mContext, promise); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void setNeedUpdate(ReadableMap options,Promise promise) { |     public void setNeedUpdate(ReadableMap options,Promise promise) { | ||||||
|         UpdateModuleImpl.setNeedUpdate(updateContext, options,promise); |         UpdateModuleImpl.setNeedUpdate(updateContext, options,promise); | ||||||
|   | |||||||
| @@ -224,6 +224,29 @@ public class UpdateModule extends ReactContextBaseJavaModule { | |||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @ReactMethod | ||||||
|  |     public void restartApp(final Promise promise) { | ||||||
|  |  | ||||||
|  |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|  |             @Override | ||||||
|  |             public void run() { | ||||||
|  |                 try { | ||||||
|  |                     final Context application = getReactApplicationContext().getApplicationContext(); | ||||||
|  |                     ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager(); | ||||||
|  |                     if (instanceManager == null) { | ||||||
|  |                         instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager(); | ||||||
|  |                     } | ||||||
|  |                     instanceManager.recreateReactContextInBackground(); | ||||||
|  |                     promise.resolve(true); | ||||||
|  |  | ||||||
|  |                 } catch (Throwable err) { | ||||||
|  |                     promise.reject(err); | ||||||
|  |                     Log.e("pushy", "restartApp failed ", err); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @ReactMethod |     @ReactMethod | ||||||
|     public void setNeedUpdate(ReadableMap options) { |     public void setNeedUpdate(ReadableMap options) { | ||||||
|         final String hash = options.getString("hash"); |         final String hash = options.getString("hash"); | ||||||
|   | |||||||
| @@ -338,6 +338,26 @@ RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | RCT_EXPORT_METHOD(restartApp:(RCTPromiseResolveBlock)resolve | ||||||
|  |                   rejecter:(RCTPromiseRejectBlock)reject) | ||||||
|  | { | ||||||
|  |     @try { | ||||||
|  |         dispatch_async(dispatch_get_main_queue(), ^{ | ||||||
|  |             [self.bridge reload]; | ||||||
|  |         }); | ||||||
|  |         #if __has_include("RCTReloadCommand.h") | ||||||
|  |             // reload 0.62+ | ||||||
|  |             RCTReloadCommandSetBundleURL([[self class] bundleURL]); | ||||||
|  |             RCTTriggerReloadCommandListeners(@"pushy restartApp"); | ||||||
|  |         #endif | ||||||
|  |  | ||||||
|  |         resolve(@true); | ||||||
|  |     } | ||||||
|  |     @catch (NSException *exception) { | ||||||
|  |         reject(@"执行报错", exception.reason, nil); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve | RCT_EXPORT_METHOD(markSuccess:(RCTPromiseResolveBlock)resolve | ||||||
|                                     rejecter:(RCTPromiseRejectBlock)reject) |                                     rejecter:(RCTPromiseRejectBlock)reject) | ||||||
| { | { | ||||||
|   | |||||||
| @@ -15,6 +15,7 @@ export interface Spec extends TurboModule { | |||||||
|   getLocalHashInfo(hash: string): Promise<string>; |   getLocalHashInfo(hash: string): Promise<string>; | ||||||
|   setUuid(uuid: string): Promise<void>; |   setUuid(uuid: string): Promise<void>; | ||||||
|   reloadUpdate(options: { hash: string }): Promise<void>; |   reloadUpdate(options: { hash: string }): Promise<void>; | ||||||
|  |   restartApp(): Promise<void>; | ||||||
|   setNeedUpdate(options: { hash: string }): Promise<void>; |   setNeedUpdate(options: { hash: string }): Promise<void>; | ||||||
|   markSuccess(): Promise<void>; |   markSuccess(): Promise<void>; | ||||||
|   downloadPatchFromPpk(options: { |   downloadPatchFromPpk(options: { | ||||||
|   | |||||||
| @@ -544,6 +544,9 @@ export class Pushy { | |||||||
|       delete Pushy.progressHandlers[progressKey]; |       delete Pushy.progressHandlers[progressKey]; | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
|  |   restartApp = async () => { | ||||||
|  |     return PushyModule.restartApp(); | ||||||
|  |   }; | ||||||
| } | } | ||||||
|  |  | ||||||
| // for international users | // for international users | ||||||
|   | |||||||
| @@ -13,6 +13,7 @@ export const defaultContext = { | |||||||
|   dismissError: noop, |   dismissError: noop, | ||||||
|   downloadUpdate: asyncNoop, |   downloadUpdate: asyncNoop, | ||||||
|   downloadAndInstallApk: asyncNoop, |   downloadAndInstallApk: asyncNoop, | ||||||
|  |   restartApp: asyncNoop, | ||||||
|   getCurrentVersionInfo: () => Promise.resolve({}), |   getCurrentVersionInfo: () => Promise.resolve({}), | ||||||
|   parseTestQrCode: () => false, |   parseTestQrCode: () => false, | ||||||
|   currentHash: '', |   currentHash: '', | ||||||
| @@ -33,6 +34,7 @@ export const UpdateContext = createContext<{ | |||||||
|     metaInfo?: string; |     metaInfo?: string; | ||||||
|   }>; |   }>; | ||||||
|   parseTestQrCode: (code: string) => boolean; |   parseTestQrCode: (code: string) => boolean; | ||||||
|  |   restartApp: () => Promise<void>; | ||||||
|   currentHash: string; |   currentHash: string; | ||||||
|   packageVersion: string; |   packageVersion: string; | ||||||
|   client?: Pushy | Cresc; |   client?: Pushy | Cresc; | ||||||
|   | |||||||
| @@ -316,6 +316,13 @@ export const UpdateProvider = ({ | |||||||
|     [parseTestPayload], |     [parseTestPayload], | ||||||
|   ); |   ); | ||||||
|  |  | ||||||
|  |   const restartApp = useCallback( | ||||||
|  |     async () => { | ||||||
|  |       return client.restartApp(); | ||||||
|  |     }, | ||||||
|  |     [client], | ||||||
|  |   ); | ||||||
|  |  | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     const parseLinking = (url: string | null) => { |     const parseLinking = (url: string | null) => { | ||||||
|       if (!url) { |       if (!url) { | ||||||
| @@ -361,6 +368,7 @@ export const UpdateProvider = ({ | |||||||
|         downloadAndInstallApk, |         downloadAndInstallApk, | ||||||
|         getCurrentVersionInfo, |         getCurrentVersionInfo, | ||||||
|         parseTestQrCode, |         parseTestQrCode, | ||||||
|  |         restartApp, | ||||||
|       }}> |       }}> | ||||||
|       {children} |       {children} | ||||||
|     </UpdateContext.Provider> |     </UpdateContext.Provider> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 陈赳赳
					陈赳赳