From e13675522cdb7d200b6d464e38c34aa4b69accc4 Mon Sep 17 00:00:00 2001 From: tdzl2003 Date: Tue, 5 Apr 2016 17:00:03 +0800 Subject: [PATCH] bugfix --- .../modules/update/UpdateContext.java | 42 +++++++++++++------ .../modules/update/UpdateModule.java | 10 +++-- lib/index.js | 4 +- local-cli/src/app.js | 2 +- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java index f5e6f5a..23246b7 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java @@ -27,6 +27,16 @@ public class UpdateContext { } this.sp = context.getSharedPreferences("update", Context.MODE_PRIVATE); + + String packageVersion = getPackageVersion(); + if (!packageVersion.equals(this.sp.getString("packageVersion", null))) { + SharedPreferences.Editor editor = sp.edit(); + editor.clear(); + editor.putString("packageVersion", packageVersion); + editor.apply(); + + this.clearUp(); + } } public String getRootDir() { @@ -98,7 +108,7 @@ public class UpdateContext { editor.putString("lastVersion", hashName); } editor.putBoolean("firstTime", true); - editor.putBoolean("shouldRollback", false); + editor.putBoolean("firstTimeOk", false); editor.putBoolean("rolledBack", false); editor.apply(); } @@ -115,10 +125,17 @@ public class UpdateContext { return sp.getBoolean("rolledBack", false); } - public void clearFirstTimeMark() { + public void markSuccess() { + SharedPreferences.Editor editor = sp.edit(); + editor.putBoolean("firstTimeOk", true); + editor.apply(); + + this.clearUp(); + } + + public void clearFirstTime() { SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("firstTime", false); - editor.putBoolean("shouldRollback", false); editor.apply(); this.clearUp(); @@ -145,33 +162,32 @@ public class UpdateContext { } public String getBundleUrl(String defaultAssetsUrl) { - // Test should rollback. - if (sp.getBoolean("shouldRollback", false)) { - this.rollBack(); - } String currentVersion = getCurrentVersion(); if (currentVersion == null) { return defaultAssetsUrl; } - if (sp.getBoolean("firstTime", false)) { - SharedPreferences.Editor editor = sp.edit(); - editor.putBoolean("shouldRollback", true); - editor.apply(); + // Test should rollback. + if (!sp.getBoolean("firstTime", false)) { + if (!sp.getBoolean("firstTimeOk", true)) { + // Not firstTime, but not ok, so we roll back. + currentVersion = this.rollBack(); + } } return (new File(rootDir, currentVersion+"/index.bundlejs").toString()); } - private void rollBack() { + private String rollBack() { String lastVersion = sp.getString("lastVersion", null); if (lastVersion == null) { throw new Error("This should never happen"); } SharedPreferences.Editor editor = sp.edit(); editor.putString("currentVersion", lastVersion); - editor.putBoolean("shouldRollback", false); + editor.putBoolean("firstTimeOk", true); editor.putBoolean("firstTime", false); editor.putBoolean("rolledBack", true); editor.apply(); + return lastVersion; } private void clearUp() { diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java index be68b69..77505ca 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java @@ -34,7 +34,11 @@ public class UpdateModule extends ReactContextBaseJavaModule{ constants.put("downloadRootDir", updateContext.getRootDir()); constants.put("packageVersion", updateContext.getPackageVersion()); constants.put("currentVersion", updateContext.getCurrentVersion()); - constants.put("isFirstTime", updateContext.isFirstTime()); + boolean isFirstTime = updateContext.isFirstTime(); + constants.put("isFirstTime", isFirstTime); + if (isFirstTime) { + updateContext.clearFirstTime(); + } boolean isRolledBack = updateContext.isRolledBack(); constants.put("isRolledBack", isRolledBack); if (isRolledBack) { @@ -129,11 +133,11 @@ public class UpdateModule extends ReactContextBaseJavaModule{ } @ReactMethod - public void clearFirstTimeMark() { + public void markSuccess() { UiThreadUtil.runOnUiThread(new Runnable() { @Override public void run() { - updateContext.clearFirstTimeMark(); + updateContext.markSuccess(); } }); } diff --git a/lib/index.js b/lib/index.js index 7995477..9ba14d7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -87,6 +87,6 @@ export async function switchVersionLater(hash) { HotUpdate.setNeedUpdate({hashName:hash}); } -export function clearFirstTimeMark() { - HotUpdate.clearFirstTimeMark(); +export function markSuccess() { + HotUpdate.markSuccess(); } diff --git a/local-cli/src/app.js b/local-cli/src/app.js index 811a8f1..d64261c 100644 --- a/local-cli/src/app.js +++ b/local-cli/src/app.js @@ -88,7 +88,7 @@ export const commands = { selectApp: async function({args, options}) { const {platform} = options; checkPlatform(platform); - const id = args[0] || (await chooseApp()).id; + const id = args[0] || (await chooseApp(platform)).id; let updateInfo = {}; if (await fs.exists('update.json')) {