Skip to content

Commit

Permalink
Issue #SB-17879 added: to normalise sharedPreferences with localStora…
Browse files Browse the repository at this point in the history
…ge values
  • Loading branch information
arafath committed Feb 24, 2020
1 parent ca50076 commit 37c9bb6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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()', () => {
Expand Down
21 changes: 21 additions & 0 deletions src/util/shared-preferences/impl/shared-preferences-android.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,6 +11,16 @@ export class SharedPreferencesAndroid implements SharedPreferences {
private sharedPreferences = plugins.SharedPreferences.getInstance(SharedPreferencesAndroid.sharedPreferncesName);

public getString(key: string): Observable<string | undefined> {
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);
Expand Down Expand Up @@ -43,6 +54,16 @@ export class SharedPreferencesAndroid implements SharedPreferences {
}

public getBoolean(key: string): Observable<boolean> {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class SharedPreferencesLocalStorage implements SharedPreferences {
}

public getBoolean(key: string): Observable<boolean> {
return defer(() => of(Boolean(localStorage.getItem(key))));
return defer(() => of(
localStorage.getItem(key) === 'true'
));
}
}

0 comments on commit 37c9bb6

Please sign in to comment.