react-native-ota-hot-update 2.3.3 → 2.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -118,22 +118,26 @@ var taskIdentifier: UIBackgroundTaskIdentifier = .invalid
118
118
  ...
119
119
  ```
120
120
  ```bash
121
- override func applicationWillResignActive(_ application: UIApplication) {
122
- // End any existing background task
123
- if taskIdentifier != .invalid {
124
- application.endBackgroundTask(taskIdentifier)
125
- taskIdentifier = .invalid
126
- }
127
-
128
- // Start a new background task
129
- taskIdentifier = application.beginBackgroundTask(withName: nil) { [weak self] in
130
- if let strongSelf = self {
131
- application.endBackgroundTask(strongSelf.taskIdentifier)
132
- strongSelf.taskIdentifier = .invalid
133
- }
134
- }
121
+ public override func applicationDidEnterBackground(_ application: UIApplication) {
122
+ if taskIdentifier != .invalid {
123
+ application.endBackgroundTask(taskIdentifier)
124
+ taskIdentifier = .invalid
125
+ }
126
+
127
+ taskIdentifier = application.beginBackgroundTask(withName: "OTAUpdate") { [weak self] in
128
+ if let strongSelf = self {
129
+ application.endBackgroundTask(strongSelf.taskIdentifier)
130
+ strongSelf.taskIdentifier = .invalid
135
131
  }
136
-
132
+ }
133
+ }
134
+
135
+ public override func applicationWillEnterForeground(_ application: UIApplication) {
136
+ if taskIdentifier != .invalid {
137
+ application.endBackgroundTask(taskIdentifier)
138
+ taskIdentifier = .invalid
139
+ }
140
+ }
137
141
  ```
138
142
 
139
143
 
@@ -142,9 +146,14 @@ Open `MainApplication.kt` and add these codes bellow:
142
146
  ```bash
143
147
  import com.otahotupdate.OtaHotUpdate
144
148
  ...
145
- override fun getJSBundleFile(): String? {
146
- return OtaHotUpdate.bundleJS(this@MainApplication)
147
- }
149
+ override val reactNativeHost: ReactNativeHost =
150
+ object : DefaultReactNativeHost(this) {
151
+ ...
152
+ override fun getJSBundleFile(): String? {
153
+ return OtaHotUpdate.bundleJS(this@MainApplication)
154
+ }
155
+ ...
156
+ }
148
157
 
149
158
  ```
150
159
 
@@ -157,6 +166,22 @@ MainApplication.java:
157
166
  }
158
167
  ```
159
168
 
169
+ ### Android in react native 0.82 or above:
170
+
171
+ ```bash
172
+ override val reactHost: ReactHost by lazy {
173
+ getDefaultReactHost(
174
+ context = applicationContext,
175
+ packageList =
176
+ PackageList(this).packages.apply {
177
+ // Packages that cannot be autolinked yet can be added manually here, for example:
178
+ // add(MyReactNativePackage())
179
+ },
180
+ jsBundleFilePath = OtaHotUpdate.bundleJS(applicationContext)
181
+ )
182
+ }
183
+ ```
184
+
160
185
  For java it maybe can be like: `OtaHotUpdate.Companion.getBundleJS(this)` depend on kotlin / jdk version on your project, you can use android studio to get the correct format coding.
161
186
 
162
187
  If want to remove bundle wrong handler, pass false param in getBundleJS like this:
@@ -3,7 +3,7 @@ package com.otahotupdate
3
3
  import android.content.Context
4
4
  import android.content.pm.PackageManager
5
5
  import android.os.Build
6
- import com.facebook.react.TurboReactPackage
6
+ import com.facebook.react.BaseReactPackage
7
7
  import com.facebook.react.bridge.NativeModule
8
8
  import com.facebook.react.bridge.ReactApplicationContext
9
9
  import com.facebook.react.module.model.ReactModuleInfo
@@ -15,7 +15,7 @@ import com.rnhotupdate.Common.VERSION
15
15
  import com.rnhotupdate.SharedPrefs
16
16
 
17
17
 
18
- class OtaHotUpdate : TurboReactPackage() {
18
+ class OtaHotUpdate : BaseReactPackage() {
19
19
  override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
20
20
  return if (name == OtaHotUpdateModule.NAME) {
21
21
  OtaHotUpdateModule(reactContext)
@@ -33,7 +33,6 @@ class OtaHotUpdate : TurboReactPackage() {
33
33
  OtaHotUpdateModule.NAME,
34
34
  false, // canOverrideExistingModule
35
35
  false, // needsEagerInit
36
- true, // hasConstants
37
36
  false, // isCxxModule
38
37
  isTurboModule // isTurboModule
39
38
  )
@@ -68,6 +67,9 @@ class OtaHotUpdate : TurboReactPackage() {
68
67
  val version = sharedPrefs.getString(VERSION)
69
68
  val currentVersionName = sharedPrefs.getString(CURRENT_VERSION_CODE)
70
69
  if (pathBundle == "" || (currentVersionName != context.getVersionCode())) {
70
+ if (pathBundle != "") {
71
+ sharedPrefs.putString(PATH, "")
72
+ }
71
73
  if (version != "") {
72
74
  // reset version number because bundle is wrong version, need download from new version
73
75
  sharedPrefs.putString(VERSION, "")
@@ -141,13 +141,18 @@ class OtaHotUpdateModule internal constructor(context: ReactApplicationContext)
141
141
 
142
142
  @ReactMethod
143
143
  override fun setExactBundlePath(path: String?, promise: Promise) {
144
- val sharedPrefs = SharedPrefs(reactApplicationContext)
145
- sharedPrefs.putString(PATH, path)
146
- sharedPrefs.putString(
147
- CURRENT_VERSION_CODE,
148
- reactApplicationContext.getVersionCode()
149
- )
150
- promise.resolve(true)
144
+ val file = File(path)
145
+ if (file.exists() && file.isFile) {
146
+ val sharedPrefs = SharedPrefs(reactApplicationContext)
147
+ sharedPrefs.putString(PATH, path)
148
+ sharedPrefs.putString(
149
+ CURRENT_VERSION_CODE,
150
+ reactApplicationContext.getVersionCode()
151
+ )
152
+ promise.resolve(true)
153
+ } else {
154
+ promise.resolve(false)
155
+ }
151
156
  }
152
157
 
153
158
  @ReactMethod
@@ -162,6 +162,7 @@ void OTAExceptionHandler(NSException *exception) {
162
162
  } else {
163
163
  // reset version number because bundle is wrong version, need download from new version
164
164
  [defaults removeObjectForKey:@"VERSION"];
165
+ [defaults removeObjectForKey:@"PATH"];
165
166
  [defaults synchronize];
166
167
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
167
168
  }
@@ -394,7 +395,7 @@ RCT_EXPORT_METHOD(getUpdateMetadata:(double)a
394
395
  RCT_EXPORT_METHOD(setExactBundlePath:(NSString *)path
395
396
  resolve:(RCTPromiseResolveBlock)resolve
396
397
  reject:(RCTPromiseRejectBlock)reject) {
397
- if (path) {
398
+ if ([OtaHotUpdate isFilePathValid:path]) {
398
399
  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
399
400
  [defaults setObject:path forKey:@"PATH"];
400
401
  [defaults setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forKey:@"VERSION_NAME"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ota-hot-update",
3
- "version": "2.3.3",
3
+ "version": "2.3.5",
4
4
  "description": "Hot update for react native",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",
@@ -3,17 +3,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const config_plugins_1 = require("@expo/config-plugins");
4
4
  const withAndroidAction = (config) => {
5
5
  return (0, config_plugins_1.withMainApplication)(config, (config) => {
6
- if (!config.modResults.contents.includes('override fun getJSBundleFile(): String = OtaHotUpdate.bundleJS(this@MainApplication)')) {
7
- config.modResults.contents = config.modResults.contents.replace(/override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED/g, `
8
- override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
6
+ let content = config.modResults.contents;
7
+ const isNewReactHost = content.includes('context = applicationContext');
8
+ if (!content.includes('import com.otahotupdate.OtaHotUpdate')) {
9
+ content = content.replace(/import android.app.Application/g, `
10
+ import android.app.Application
11
+ import com.otahotupdate.OtaHotUpdate`);
12
+ }
13
+ if (isNewReactHost) {
14
+ if (!content.includes('OtaHotUpdate.bundleJS')) {
15
+ content = content.replace(/context = applicationContext,/, `context = applicationContext,
16
+ jsBundleFilePath = OtaHotUpdate.bundleJS(applicationContext),
17
+ `);
18
+ }
19
+ config.modResults.contents = content;
20
+ return config;
21
+ }
22
+ if (!content.includes('OtaHotUpdate.bundleJS(this@MainApplication)')) {
23
+ content = content.replace(/DefaultReactNativeHost\s*\(this\)\s*\{/, `
24
+ DefaultReactNativeHost(this) {
9
25
 
10
26
  override fun getJSBundleFile(): String = OtaHotUpdate.bundleJS(this@MainApplication)`);
11
27
  }
12
- if (!config.modResults.contents.includes('import com.otahotupdate.OtaHotUpdate')) {
13
- config.modResults.contents = config.modResults.contents.replace(/import expo.modules.ReactNativeHostWrapper/g, `
14
- import expo.modules.ReactNativeHostWrapper
15
- import com.otahotupdate.OtaHotUpdate`);
16
- }
28
+ config.modResults.contents = content;
17
29
  return config;
18
30
  });
19
31
  };
@@ -6,24 +6,42 @@ import {
6
6
 
7
7
  const withAndroidAction: any = (config: any) => {
8
8
  return withMainApplication(config, (config) => {
9
- if (!config.modResults.contents.includes('override fun getJSBundleFile(): String = OtaHotUpdate.bundleJS(this@MainApplication)')) {
10
- config.modResults.contents = config.modResults.contents.replace(
11
- /override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED/g,
12
- `
13
- override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
9
+ let content = config.modResults.contents;
14
10
 
15
- override fun getJSBundleFile(): String = OtaHotUpdate.bundleJS(this@MainApplication)`
11
+ const isNewReactHost = content.includes('context = applicationContext');
12
+
13
+ if (!content.includes('import com.otahotupdate.OtaHotUpdate')) {
14
+ content = content.replace(
15
+ /import android.app.Application/g,
16
+ `
17
+ import android.app.Application
18
+ import com.otahotupdate.OtaHotUpdate`
16
19
  );
17
20
  }
21
+ if (isNewReactHost) {
22
+ if (!content.includes('OtaHotUpdate.bundleJS')) {
23
+ content = content.replace(
24
+ /context = applicationContext,/,
25
+ `context = applicationContext,
26
+ jsBundleFilePath = OtaHotUpdate.bundleJS(applicationContext),`
27
+ );
28
+ }
18
29
 
19
- if (!config.modResults.contents.includes('import com.otahotupdate.OtaHotUpdate')) {
20
- config.modResults.contents = config.modResults.contents.replace(
21
- /import expo.modules.ReactNativeHostWrapper/g,
30
+ config.modResults.contents = content;
31
+ return config;
32
+ }
33
+
34
+ if (!content.includes('OtaHotUpdate.bundleJS(this@MainApplication)')) {
35
+ content = content.replace(
36
+ /DefaultReactNativeHost\s*\(this\)\s*\{/,
22
37
  `
23
- import expo.modules.ReactNativeHostWrapper
24
- import com.otahotupdate.OtaHotUpdate`
38
+ DefaultReactNativeHost(this) {
39
+
40
+ override fun getJSBundleFile(): String = OtaHotUpdate.bundleJS(this@MainApplication)`
25
41
  );
26
42
  }
43
+
44
+ config.modResults.contents = content;
27
45
  return config;
28
46
  });
29
47
  };