From 94d52bb4151ec2624e6ed801c20378f34c04e447 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Tue, 5 Oct 2021 14:08:56 +0800 Subject: [PATCH] Add set local hash info for ios --- .../modules/update/UpdateContext.java | 5 ++- ios/RCTPushy/RCTPushy.h | 8 ---- ios/RCTPushy/RCTPushy.m | 41 +++++++++++++------ ios/RCTPushy/RCTPushyDownloader.h | 8 ---- ios/RCTPushy/RCTPushyDownloader.m | 8 ---- ios/RCTPushy/RCTPushyManager.h | 8 ---- ios/RCTPushy/RCTPushyManager.m | 8 ---- lib/index.js | 2 +- 8 files changed, 33 insertions(+), 55 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 c536c7d..39fe830 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java @@ -147,7 +147,7 @@ public class UpdateContext { String lastVersion = getCurrentVersion(); SharedPreferences.Editor editor = sp.edit(); editor.putString("currentVersion", hash); - if (lastVersion != null && lastVersion != hash) { + if (lastVersion != null && !lastVersion.equals(hash)) { editor.putString("lastVersion", lastVersion); } editor.putBoolean("firstTime", true); @@ -189,7 +189,8 @@ public class UpdateContext { SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("firstTimeOk", true); String lastVersion = sp.getString("lastVersion", null); - if (lastVersion != null) { + String curVersion = sp.getString("currentVersion", null); + if (lastVersion != null && !lastVersion.equals(curVersion)) { editor.remove("lastVersion"); editor.remove("hash_" + lastVersion); } diff --git a/ios/RCTPushy/RCTPushy.h b/ios/RCTPushy/RCTPushy.h index 28d15bb..a44bb22 100644 --- a/ios/RCTPushy/RCTPushy.h +++ b/ios/RCTPushy/RCTPushy.h @@ -1,11 +1,3 @@ -// -// RCTPushy.h -// RCTPushy -// -// Created by LvBingru on 2/19/16. -// Copyright © 2016 erica. All rights reserved. -// - #import #import diff --git a/ios/RCTPushy/RCTPushy.m b/ios/RCTPushy/RCTPushy.m index f49f222..40a22cd 100644 --- a/ios/RCTPushy/RCTPushy.m +++ b/ios/RCTPushy/RCTPushy.m @@ -1,11 +1,3 @@ -// -// RCTPushy.m -// RCTPushy -// -// Created by LvBingru on 2/19/16. -// Copyright © 2016 erica. All rights reserved. -// - #import "RCTPushy.h" #import "RCTPushyDownloader.h" #import "RCTPushyManager.h" @@ -23,6 +15,7 @@ static NSString *const paramIsFirstTime = @"isFirstTime"; static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK"; static NSString *const keyBlockUpdate = @"REACTNATIVECN_PUSHY_BLOCKUPDATE"; static NSString *const keyUuid = @"REACTNATIVECN_PUSHY_UUID"; +static NSString *const keyHashInfo = @"REACTNATIVECN_PUSHY_HASH_"; static NSString *const keyFirstLoadMarked = @"REACTNATIVECN_PUSHY_FIRSTLOADMARKED_KEY"; static NSString *const keyRolledBackMarked = @"REACTNATIVECN_PUSHY_ROLLEDBACKMARKED_KEY"; static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_PUSHY_ISPACKAGEUPDATEDMARKED_KEY"; @@ -212,6 +205,24 @@ RCT_EXPORT_METHOD(setUuid:(NSString *)uuid) [defaults synchronize]; } +RCT_EXPORT_METHOD(setLocalHashInfo:(NSString *)hash + value:(NSString *)value) +{ + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults setObject:value forKey:[keyHashInfo stringByAppendingString:hash]]; + [defaults synchronize]; +} + + +RCT_EXPORT_METHOD(getLocalHashInfo:(NSString *)hash + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + resolve([defaults stringForKey:[keyHashInfo stringByAppendingString:hash]]); +} + RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) @@ -304,10 +315,16 @@ RCT_EXPORT_METHOD(markSuccess) { // up package info NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - NSMutableDictionary *packageInfo = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:keyPushyInfo]]; - [packageInfo setObject:@(NO) forKey:paramIsFirstTime]; - [packageInfo setObject:@(YES) forKey:paramIsFirstLoadOk]; - [defaults setObject:packageInfo forKey:keyPushyInfo]; + NSMutableDictionary *pushyInfo = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:keyPushyInfo]]; + [pushyInfo setObject:@(NO) forKey:paramIsFirstTime]; + [pushyInfo setObject:@(YES) forKey:paramIsFirstLoadOk]; + + NSString *lastVersion = pushyInfo[paramLastVersion]; + NSString *curVersion = pushyInfo[paramCurrentVersion]; + if (lastVersion != nil && ![lastVersion isEqualToString:curVersion]) { + [pushyInfo removeObjectForKey:[keyHashInfo stringByAppendingString:lastVersion]]; + } + [defaults setObject:pushyInfo forKey:keyPushyInfo]; [defaults synchronize]; // clear other package dir diff --git a/ios/RCTPushy/RCTPushyDownloader.h b/ios/RCTPushy/RCTPushyDownloader.h index 7ca873b..6089e7c 100644 --- a/ios/RCTPushy/RCTPushyDownloader.h +++ b/ios/RCTPushy/RCTPushyDownloader.h @@ -1,11 +1,3 @@ -// -// RCTPushyDownloader.h -// RCTPushy -// -// Created by lvbingru on 16/2/23. -// Copyright © 2016年 erica. All rights reserved. -// - #import @interface RCTPushyDownloader : NSObject diff --git a/ios/RCTPushy/RCTPushyDownloader.m b/ios/RCTPushy/RCTPushyDownloader.m index 4248299..b862d99 100644 --- a/ios/RCTPushy/RCTPushyDownloader.m +++ b/ios/RCTPushy/RCTPushyDownloader.m @@ -1,11 +1,3 @@ -// -// RCTPushyDownloader.m -// RCTPushy -// -// Created by lvbingru on 16/2/23. -// Copyright © 2016年 erica. All rights reserved. -// - #import "RCTPushyDownloader.h" @interface RCTPushyDownloader() diff --git a/ios/RCTPushy/RCTPushyManager.h b/ios/RCTPushy/RCTPushyManager.h index 68ea3c0..379cd21 100644 --- a/ios/RCTPushy/RCTPushyManager.h +++ b/ios/RCTPushy/RCTPushyManager.h @@ -1,11 +1,3 @@ -// -// RCTPushyManager.h -// RCTPushy -// -// Created by lvbingru on 16/4/1. -// Copyright © 2016年 erica. All rights reserved. -// - #import @interface RCTPushyManager : NSObject diff --git a/ios/RCTPushy/RCTPushyManager.m b/ios/RCTPushy/RCTPushyManager.m index 87b9799..5ca3099 100644 --- a/ios/RCTPushy/RCTPushyManager.m +++ b/ios/RCTPushy/RCTPushyManager.m @@ -1,11 +1,3 @@ -// -// RCTPushyManager.m -// RCTPushy -// -// Created by lvbingru on 16/4/1. -// Copyright © 2016年 erica. All rights reserved. -// - #import "RCTPushyManager.h" #import "ZipArchive.h" #import "HDiffPatch.h" diff --git a/lib/index.js b/lib/index.js index 1a87c32..9c576b0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,7 +51,7 @@ export async function getCurrentVersionInfo() { const eventEmitter = new NativeEventEmitter(Pushy); if (!uuid) { - uuid = require('nanoid/non-secure')(); + uuid = require('nanoid/non-secure').nanoid(); Pushy.setUuid(uuid); }