Skip to content

Commit

Permalink
Issue #SB-17879 merge: Merge pull request #296 from arfath-gwl/releas…
Browse files Browse the repository at this point in the history
…e-2.7.0

Issue #SB-17879 fix: 'cordova' platform to map to 'sharedPreferences'…
  • Loading branch information
AmiableAnil authored Feb 24, 2020
2 parents 9953903 + c0fb2eb commit e57df38
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ export class SunbirdSdk {

switch (sdkConfig.platform) {
case 'cordova': this._container.bind<SharedPreferences>(InjectionTokens.SHARED_PREFERENCES)
.to(SharedPreferencesLocalStorage).inSingletonScope();
.to(SharedPreferencesAndroid).inSingletonScope();
break;
case 'web': this._container.bind<SharedPreferences>(InjectionTokens.SHARED_PREFERENCES)
.to(SharedPreferencesAndroid).inSingletonScope();
.to(SharedPreferencesLocalStorage).inSingletonScope();
break;
default: throw new Error('FATAL_ERROR: Invalid platform');
}
Expand Down
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
49 changes: 35 additions & 14 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,14 +11,24 @@ export class SharedPreferencesAndroid implements SharedPreferences {
private sharedPreferences = plugins.SharedPreferences.getInstance(SharedPreferencesAndroid.sharedPreferncesName);

public getString(key: string): Observable<string | undefined> {
return new Observable((observer) => {
this.sharedPreferences.getString(key, '', (value) => {
observer.next(value);
observer.complete();
}, (e) => {
observer.error(e);
const value = localStorage.getItem(key);

if (value) {
localStorage.removeItem(key);

return this.putString(key, value).pipe(
mapTo(value)
);
} 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<undefined> {
Expand All @@ -43,13 +54,23 @@ export class SharedPreferencesAndroid implements SharedPreferences {
}

public getBoolean(key: string): Observable<boolean> {
return new Observable((observer) => {
this.sharedPreferences.getBoolean(key, false, (value) => {
observer.next(value);
observer.complete();
}, (e) => {
observer.error(e);
const value = localStorage.getItem(key);

if (value) {
localStorage.removeItem(key);

return this.putBoolean(key, value === 'true').pipe(
mapTo(value === 'true')
);
} else {
return new Observable((observer) => {
this.sharedPreferences.getBoolean(key, false, (v) => {
observer.next(v);
observer.complete();
}, (e) => {
observer.error(e);
});
});
});
}
}
}
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 e57df38

Please sign in to comment.