Skip to content

Instantly share code, notes, and snippets.

@ova2
Created April 2, 2019 15:28
Show Gist options
  • Save ova2/8b8b8d71427092145b57ca2cdd230729 to your computer and use it in GitHub Desktop.
Save ova2/8b8b8d71427092145b57ca2cdd230729 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable({
providedIn: 'root'
})
export class StoreService {
private readonly _prefix: string;
private readonly AUTH_TOKEN = 'authToken';
private readonly TRACK_NUMBER = 'trackNumber';
private readonly DEVICE_ID = 'deviceId';
constructor(private _storage: Storage) {
this._prefix = 'shuntassistant';
}
public setAuthToken(value: string): Promise<any> {
return this.setValue(this.AUTH_TOKEN, value);
}
public getAuthToken(): Promise<string> {
return this.readValue(this.AUTH_TOKEN);
}
public removeAuthToken(): Promise<any> {
return this.removeValue(this.AUTH_TOKEN);
}
public setTrackNumber(value: string): Promise<any> {
return this.setValue(this.TRACK_NUMBER, value);
}
public getTrackNumber(): Promise<string> {
return this.readValue(this.TRACK_NUMBER);
}
public removeTrackNumber(): Promise<any> {
return this.removeValue(this.TRACK_NUMBER);
}
public setDeviceId(value: string): Promise<any> {
return this.setValue(this.DEVICE_ID, value);
}
public getDeviceId(): Promise<string> {
return this.readValue(this.DEVICE_ID);
}
public removeDeviceId(): Promise<any> {
return this.removeValue(this.DEVICE_ID);
}
private setValue(prop: any, value: any): Promise<any> {
const key = `${this._prefix}-${prop}`;
return this._storage.set(key, value);
}
private readValue(prop: keyof CoreState): Promise<any> {
const key = `${this._prefix}-${prop}`;
return this._storage.get(key);
}
private removeValue(prop: keyof CoreState): Promise<any> {
const key = `${this._prefix}-${prop}`;
return this._storage.remove(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment