From ca5007665d75f4e01a0e299fb409415ed717a3bd Mon Sep 17 00:00:00 2001 From: arafath Date: Mon, 24 Feb 2020 13:19:59 +0530 Subject: [PATCH 1/5] Issue #SB-17879 fix: 'cordova' platform to map to 'sharedPreferences' and 'web' platform to 'localStorage' --- src/sdk.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdk.ts b/src/sdk.ts index c7b6ebf6e..629fc716e 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -234,10 +234,10 @@ export class SunbirdSdk { switch (sdkConfig.platform) { case 'cordova': this._container.bind(InjectionTokens.SHARED_PREFERENCES) - .to(SharedPreferencesLocalStorage).inSingletonScope(); + .to(SharedPreferencesAndroid).inSingletonScope(); break; case 'web': this._container.bind(InjectionTokens.SHARED_PREFERENCES) - .to(SharedPreferencesAndroid).inSingletonScope(); + .to(SharedPreferencesLocalStorage).inSingletonScope(); break; default: throw new Error('FATAL_ERROR: Invalid platform'); } From 37c9bb683f3480533d077c2757046186801f7555 Mon Sep 17 00:00:00 2001 From: arafath Date: Mon, 24 Feb 2020 14:17:13 +0530 Subject: [PATCH 2/5] Issue #SB-17879 added: to normalise sharedPreferences with localStorage values --- .../impl/shared-preferences-android.spec.ts | 78 +++++++++++++++++++ .../impl/shared-preferences-android.ts | 21 +++++ .../impl/shared-preferences-local-storage.ts | 4 +- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/util/shared-preferences/impl/shared-preferences-android.spec.ts b/src/util/shared-preferences/impl/shared-preferences-android.spec.ts index 9ac567489..00c283485 100644 --- a/src/util/shared-preferences/impl/shared-preferences-android.spec.ts +++ b/src/util/shared-preferences/impl/shared-preferences-android.spec.ts @@ -34,6 +34,32 @@ describe('SharedPreferencesAndroid', () => { }); }); + it('should normalise and resolve from localStorage first', (done) => { + const putStringFunc = jest.fn((a, b, c, d) => { + setTimeout(() => { + c(); + }, 0); + }); + + spyOn(window['plugins'].SharedPreferences, 'getInstance').and.returnValue({ + getString: (a, b, c, d) => { + setTimeout(() => { + c('SOME_VALUE'); + }, 0); + }, + putString: putStringFunc + }); + + sharedPreferences = container.get(InjectionTokens.SHARED_PREFERENCES); + localStorage.setItem('SOME_KEY', 'SOME__LOCALSTORAGE_VALUE'); + + sharedPreferences.getString('SOME_KEY').subscribe((v) => { + expect(v).toBe('SOME__LOCALSTORAGE_VALUE'); + expect(putStringFunc).toBeCalledWith('SOME_KEY', 'SOME__LOCALSTORAGE_VALUE', expect.anything(), expect.anything()) + done(); + }); + }); + it('should delegate to cordova sharedPreferences', (done) => { spyOn(window['plugins'].SharedPreferences, 'getInstance').and.returnValue({ getString: (a, b, c, d) => { @@ -121,6 +147,58 @@ describe('SharedPreferencesAndroid', () => { done(); }); }); + + it('should normalise and resolve from localStorage first for falsy values', (done) => { + const putBooleanFunc = jest.fn((a, b, c, d) => { + setTimeout(() => { + c(true); + }, 0); + }); + + spyOn(window['plugins'].SharedPreferences, 'getInstance').and.returnValue({ + getBoolean: (a, b, c, d) => { + setTimeout(() => { + d('SOME_ERROR'); + }, 0); + }, + putBoolean: putBooleanFunc + }); + + sharedPreferences = container.get(InjectionTokens.SHARED_PREFERENCES); + localStorage.setItem('SOME_KEY', 'falsy_value'); + + sharedPreferences.getBoolean('SOME_KEY').subscribe((v) => { + expect(v).toBe(false); + expect(putBooleanFunc).toBeCalledWith('SOME_KEY', false, expect.anything(), expect.anything()); + done(); + }); + }); + + it('should normalise and resolve from localStorage first for \'true\' values', (done) => { + const putBooleanFunc = jest.fn((a, b, c, d) => { + setTimeout(() => { + c(true); + }, 0); + }); + + spyOn(window['plugins'].SharedPreferences, 'getInstance').and.returnValue({ + getBoolean: (a, b, c, d) => { + setTimeout(() => { + d('SOME_ERROR'); + }, 0); + }, + putBoolean: putBooleanFunc + }); + + sharedPreferences = container.get(InjectionTokens.SHARED_PREFERENCES); + localStorage.setItem('SOME_KEY', 'true'); + + sharedPreferences.getBoolean('SOME_KEY').subscribe((v) => { + expect(v).toBe(true); + expect(putBooleanFunc).toBeCalledWith('SOME_KEY', true, expect.anything(), expect.anything()) + done(); + }); + }); }); describe('putBoolean()', () => { diff --git a/src/util/shared-preferences/impl/shared-preferences-android.ts b/src/util/shared-preferences/impl/shared-preferences-android.ts index 67d03542d..9c60507da 100644 --- a/src/util/shared-preferences/impl/shared-preferences-android.ts +++ b/src/util/shared-preferences/impl/shared-preferences-android.ts @@ -1,6 +1,7 @@ import {SharedPreferences} from '..'; import {Observable} from 'rxjs'; import {injectable} from 'inversify'; +import {mapTo} from 'rxjs/operators'; @injectable() export class SharedPreferencesAndroid implements SharedPreferences { @@ -10,6 +11,16 @@ export class SharedPreferencesAndroid implements SharedPreferences { private sharedPreferences = plugins.SharedPreferences.getInstance(SharedPreferencesAndroid.sharedPreferncesName); public getString(key: string): Observable { + const value = localStorage.getItem(key); + + if (value) { + localStorage.setItem(key, ''); + + return this.putString(key, value).pipe( + mapTo(value) + ); + } + return new Observable((observer) => { this.sharedPreferences.getString(key, '', (value) => { observer.next(value); @@ -43,6 +54,16 @@ export class SharedPreferencesAndroid implements SharedPreferences { } public getBoolean(key: string): Observable { + const value = localStorage.getItem(key); + + if (value) { + localStorage.setItem(key, ''); + + return this.putBoolean(key, value === 'true').pipe( + mapTo(value === 'true') + ); + } + return new Observable((observer) => { this.sharedPreferences.getBoolean(key, false, (value) => { observer.next(value); diff --git a/src/util/shared-preferences/impl/shared-preferences-local-storage.ts b/src/util/shared-preferences/impl/shared-preferences-local-storage.ts index 54444e31d..f43baff4e 100644 --- a/src/util/shared-preferences/impl/shared-preferences-local-storage.ts +++ b/src/util/shared-preferences/impl/shared-preferences-local-storage.ts @@ -25,6 +25,8 @@ export class SharedPreferencesLocalStorage implements SharedPreferences { } public getBoolean(key: string): Observable { - return defer(() => of(Boolean(localStorage.getItem(key)))); + return defer(() => of( + localStorage.getItem(key) === 'true' + )); } } From 2052cf396acf4cff86c991021d4158515fc96de5 Mon Sep 17 00:00:00 2001 From: arafath Date: Mon, 24 Feb 2020 14:34:47 +0530 Subject: [PATCH 3/5] Issue #SB-17879 added: to normalise sharedPreferences with localStorage values --- .../shared-preferences/impl/shared-preferences-android.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/shared-preferences/impl/shared-preferences-android.ts b/src/util/shared-preferences/impl/shared-preferences-android.ts index 9c60507da..de8d1f93e 100644 --- a/src/util/shared-preferences/impl/shared-preferences-android.ts +++ b/src/util/shared-preferences/impl/shared-preferences-android.ts @@ -22,8 +22,8 @@ export class SharedPreferencesAndroid implements SharedPreferences { } return new Observable((observer) => { - this.sharedPreferences.getString(key, '', (value) => { - observer.next(value); + this.sharedPreferences.getString(key, '', (v) => { + observer.next(v); observer.complete(); }, (e) => { observer.error(e); @@ -65,8 +65,8 @@ export class SharedPreferencesAndroid implements SharedPreferences { } return new Observable((observer) => { - this.sharedPreferences.getBoolean(key, false, (value) => { - observer.next(value); + this.sharedPreferences.getBoolean(key, false, (v) => { + observer.next(v); observer.complete(); }, (e) => { observer.error(e); From 2cc337eea25eb627aee22e43429c1113162bf279 Mon Sep 17 00:00:00 2001 From: arafath Date: Mon, 24 Feb 2020 14:39:49 +0530 Subject: [PATCH 4/5] Issue #SB-17879 added: to normalise sharedPreferences with localStorage values --- .../impl/shared-preferences-android.ts | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/util/shared-preferences/impl/shared-preferences-android.ts b/src/util/shared-preferences/impl/shared-preferences-android.ts index de8d1f93e..72a330dbb 100644 --- a/src/util/shared-preferences/impl/shared-preferences-android.ts +++ b/src/util/shared-preferences/impl/shared-preferences-android.ts @@ -19,16 +19,16 @@ export class SharedPreferencesAndroid implements SharedPreferences { return this.putString(key, value).pipe( mapTo(value) ); - } - - return new Observable((observer) => { - this.sharedPreferences.getString(key, '', (v) => { - observer.next(v); - observer.complete(); - }, (e) => { - observer.error(e); + } else { + return new Observable((observer) => { + this.sharedPreferences.getString(key, '', (v) => { + observer.next(v); + observer.complete(); + }, (e) => { + observer.error(e); + }); }); - }); + } } public putString(key: string, value: string): Observable { @@ -62,15 +62,15 @@ export class SharedPreferencesAndroid implements SharedPreferences { return this.putBoolean(key, value === 'true').pipe( mapTo(value === 'true') ); - } - - return new Observable((observer) => { - this.sharedPreferences.getBoolean(key, false, (v) => { - observer.next(v); - observer.complete(); - }, (e) => { - observer.error(e); + } else { + return new Observable((observer) => { + this.sharedPreferences.getBoolean(key, false, (v) => { + observer.next(v); + observer.complete(); + }, (e) => { + observer.error(e); + }); }); - }); + } } } From c0fb2ebf838036fd92b9a6bb290dedb2a55a5523 Mon Sep 17 00:00:00 2001 From: arafath Date: Mon, 24 Feb 2020 14:45:20 +0530 Subject: [PATCH 5/5] Issue #SB-17879 added: to normalise sharedPreferences with localStorage values --- .../shared-preferences/impl/shared-preferences-android.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/shared-preferences/impl/shared-preferences-android.ts b/src/util/shared-preferences/impl/shared-preferences-android.ts index 72a330dbb..bf1a9597a 100644 --- a/src/util/shared-preferences/impl/shared-preferences-android.ts +++ b/src/util/shared-preferences/impl/shared-preferences-android.ts @@ -14,7 +14,7 @@ export class SharedPreferencesAndroid implements SharedPreferences { const value = localStorage.getItem(key); if (value) { - localStorage.setItem(key, ''); + localStorage.removeItem(key); return this.putString(key, value).pipe( mapTo(value) @@ -57,7 +57,7 @@ export class SharedPreferencesAndroid implements SharedPreferences { const value = localStorage.getItem(key); if (value) { - localStorage.setItem(key, ''); + localStorage.removeItem(key); return this.putBoolean(key, value === 'true').pipe( mapTo(value === 'true')