move settings service to jslib

This commit is contained in:
Kyle Spearrin 2018-01-09 22:47:58 -05:00
parent 4ea1a5f239
commit e85eb143bf
4 changed files with 70 additions and 0 deletions

View File

@ -6,6 +6,7 @@ export { FolderService } from './folder.service';
export { MessagingService } from './messaging.service'; export { MessagingService } from './messaging.service';
export { PasswordGenerationService } from './passwordGeneration.service'; export { PasswordGenerationService } from './passwordGeneration.service';
export { PlatformUtilsService } from './platformUtils.service'; export { PlatformUtilsService } from './platformUtils.service';
export { SettingsService } from './settings.service';
export { StorageService } from './storage.service'; export { StorageService } from './storage.service';
export { TokenService } from './token.service'; export { TokenService } from './token.service';
export { TotpService } from './totp.service'; export { TotpService } from './totp.service';

View File

@ -0,0 +1,6 @@
export interface SettingsService {
clearCache(): void;
getEquivalentDomains(): Promise<any>;
setEquivalentDomains(equivalentDomains: string[][]): Promise<any>;
clear(userId: string): Promise<void>;
}

View File

@ -6,6 +6,7 @@ export { CryptoService } from './crypto.service';
export { EnvironmentService } from './environment.service'; export { EnvironmentService } from './environment.service';
export { FolderService } from './folder.service'; export { FolderService } from './folder.service';
export { PasswordGenerationService } from './passwordGeneration.service'; export { PasswordGenerationService } from './passwordGeneration.service';
export { SettingsService } from './settings.service';
export { TokenService } from './token.service'; export { TokenService } from './token.service';
export { TotpService } from './totp.service'; export { TotpService } from './totp.service';
export { UserService } from './user.service'; export { UserService } from './user.service';

View File

@ -0,0 +1,62 @@
import { SettingsService as SettingsServiceInterface } from '../abstractions/settings.service';
import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service';
const Keys = {
settingsPrefix: 'settings_',
equivalentDomains: 'equivalentDomains',
};
export class SettingsService {
private settingsCache: any;
constructor(private userService: UserService, private storageService: StorageService) {
}
clearCache(): void {
this.settingsCache = null;
}
getEquivalentDomains(): Promise<any> {
return this.getSettingsKey(Keys.equivalentDomains);
}
async setEquivalentDomains(equivalentDomains: string[][]): Promise<void> {
await this.setSettingsKey(Keys.equivalentDomains, equivalentDomains);
}
async clear(userId: string): Promise<void> {
await this.storageService.remove(Keys.settingsPrefix + userId);
this.settingsCache = null;
}
// Helpers
private async getSettings(): Promise<any> {
if (this.settingsCache == null) {
const userId = await this.userService.getUserId();
this.settingsCache = this.storageService.get(Keys.settingsPrefix + userId);
}
return this.settingsCache;
}
private async getSettingsKey(key: string): Promise<any> {
const settings = await this.getSettings();
if (settings != null && settings[key]) {
return settings[key];
}
return null;
}
private async setSettingsKey(key: string, value: any): Promise<void> {
const userId = await this.userService.getUserId();
let settings = await this.getSettings();
if (!settings) {
settings = {};
}
settings[key] = value;
await this.storageService.save(Keys.settingsPrefix + userId, settings);
this.settingsCache = settings;
}
}