Skip to content

Instantly share code, notes, and snippets.

@NathanWalker
Last active June 6, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanWalker/8885f24e35a07f7511a728355d9f1b5a to your computer and use it in GitHub Desktop.
Save NathanWalker/8885f24e35a07f7511a728355d9f1b5a to your computer and use it in GitHub Desktop.
xplat storage service which uses localStorage on the web and ApplicationSettings on mobile.
// /xplat/nativescript/core/core.module.ts
import { MobileStorageService } from './services/mobile-storage.service';
// Add this to providers, for example:
@NgModule({
imports: [
NativeScriptModule,
...
CoreModule.forRoot([
...
{
provide: StorageService,
useClass: MobileStorageService
},
...
// This file should be located in /xplat/nativescript/core/services/mobile-storage.service.ts
import { Injectable } from '@angular/core';
import { WindowService, LogService } from '@workspace/core';
import { ApplicationSettings } from '@nativescript/core';
@Injectable()
export class MobileStorageService {
public storageType: any;
constructor(public win: WindowService, protected log: LogService) {
// not used however AoT needs same arguments as provided service at runtime
}
public setItem(key: string, data: any): void {
if (typeof data === 'undefined') {
data = null;
} else {
data = JSON.stringify(data);
}
ApplicationSettings.setString(key, data);
}
public getItem(key: string, _default = null): any {
const item = ApplicationSettings.getString(key);
if (!item || item === 'null') {
return _default;
}
try {
return JSON.parse(item);
} catch (e) {
this.log.debug(e);
return _default;
}
}
public removeItem(key: string): void {
ApplicationSettings.remove(key);
}
public clearAll(): void {
ApplicationSettings.clear();
}
public isAvailable(): boolean {
return true;
}
public key(index: number): string {
return null;
}
}
// This file should be located in /libs/core/services/storage.service.ts
import { Injectable } from '@angular/core';
import { LogService } from './log.service';
import { WindowService } from './window.service';
// unique prefix you want to use for your applications
export const storagePrefix = `abc.`;
// all the keys ever persisted across web and mobile
export interface IStorageKeys {
COUNTRY_CODES?: string;
LOCALE: string;
USER: string;
}
export const StorageKeys: IStorageKeys = {
COUNTRY_CODES: `${storagePrefix}country-codes`,
LOCALE: `${storagePrefix}locale`,
USER: `${storagePrefix}user`
};
@Injectable()
export class StorageService {
public storageType: any;
constructor(public win: WindowService, protected log: LogService) {
this.storageType = this.win && this.win.localStorage != null ? this.win.localStorage : null;
}
public setItem(key: string, value: any): void {
try {
if (this.storageType) {
this.storageType.setItem(key, JSON.stringify(value));
}
} catch (err) {
this.log.debug(err);
}
}
public getItem(key: string, _default?: any): any {
try {
if (this.storageType) {
const item = this.storageType.getItem(key);
if (item) {
try {
return JSON.parse(item);
} catch (err) {
this.log.debug(err);
}
}
}
return _default;
} catch (err) {
this.log.debug(err);
return _default;
}
}
public removeItem(key: string): void {
try {
if (this.storageType) {
this.storageType.removeItem(key);
}
} catch (err) {
this.log.debug(err);
}
}
public clearAll(): void {
try {
if (this.storageType) {
this.storageType.clear();
}
} catch (err) {
this.log.debug(err);
}
}
public isAvailable(): boolean {
try {
if (this.storageType) {
const x = `${storagePrefix}__test__`;
this.storageType.setItem(x, x);
this.storageType.removeItem(x);
return true;
}
} catch (e) {
return false;
}
return false;
}
}
@NathanWalker
Copy link
Author

NathanWalker commented Jun 6, 2020

Usage instructions:

  • Always use import { StorageService } from '@workspace/core'; and inject anytime you need to use storage:
constructor(private storage: StorageService) { }

Web/Mobile now have exact same api to interact with storage.

You can use the StorageKeys as a convenience to isolate all storage key's to one place and use them everywhere, ie:

this.storage.setItem(StorageKeys.USER, user);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment