move settings service to jslib

This commit is contained in:
Kyle Spearrin 2018-01-09 22:47:53 -05:00
parent ce00fcba98
commit 16a3555cd4
5 changed files with 7 additions and 69 deletions

View File

@ -9,6 +9,7 @@ import {
EnvironmentService,
FolderService,
PasswordGenerationService,
SettingsService,
TokenService,
TotpService,
UserService,
@ -24,6 +25,7 @@ import {
MessagingService as MessagingServiceAbstraction,
PasswordGenerationService as PasswordGenerationServiceAbstraction,
PlatformUtilsService as PlatformUtilsServiceAbstraction,
SettingsService as SettingsServiceAbstraction,
StorageService as StorageServiceAbstraction,
TokenService as TokenServiceAbstraction,
TotpService as TotpServiceAbstraction,
@ -49,7 +51,6 @@ import CipherService from '../services/cipher.service';
import CollectionService from '../services/collection.service';
import i18nService from '../services/i18n.service';
import LockService from '../services/lock.service';
import SettingsService from '../services/settings.service';
import SyncService from '../services/sync.service';
export default class MainBackground {
@ -65,7 +66,7 @@ export default class MainBackground {
apiService: ApiServiceAbstraction;
environmentService: EnvironmentServiceAbstraction;
userService: UserServiceAbstraction;
settingsService: SettingsService;
settingsService: SettingsServiceAbstraction;
cipherService: CipherService;
folderService: FolderServiceAbstraction;
collectionService: CollectionService;

View File

@ -6,6 +6,7 @@ import { CryptoService } from 'jslib/abstractions/crypto.service';
import { EnvironmentService } from 'jslib/abstractions/environment.service';
import { FolderService } from 'jslib/abstractions/folder.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { SettingsService } from 'jslib/abstractions/settings.service';
import { StorageService } from 'jslib/abstractions/storage.service';
import { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.service';
@ -34,7 +35,7 @@ export const utilsService = getBackgroundService<UtilsService>('utilsService');
export const appIdService = getBackgroundService<AppIdService>('appIdService');
export const i18nService = getBackgroundService<any>('i18nService');
export const constantsService = getBackgroundService<ConstantsService>('constantsService');
export const settingsService = getBackgroundService<any>('settingsService');
export const settingsService = getBackgroundService<SettingsService>('settingsService');
export const lockService = getBackgroundService<any>('lockService');
export const totpService = getBackgroundService<TotpService>('totpService');
export const environmentService = getBackgroundService<EnvironmentService>('environmentService');

View File

@ -21,12 +21,11 @@ import { ConstantsService } from 'jslib/services';
import {
ApiService,
CryptoService,
SettingsService,
StorageService,
UserService,
} from 'jslib/abstractions';
import SettingsService from './settings.service';
const Keys = {
ciphersPrefix: 'ciphers_',
localData: 'sitesLocalData',

View File

@ -1,63 +0,0 @@
import {
StorageService,
UserService,
} from 'jslib/abstractions';
const Keys = {
settingsPrefix: 'settings_',
equivalentDomains: 'equivalentDomains',
};
export default 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[][]) {
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;
}
}

View File

@ -1,12 +1,12 @@
import CipherService from './cipher.service';
import CollectionService from './collection.service';
import SettingsService from './settings.service';
import {
ApiService,
CryptoService,
FolderService,
MessagingService,
SettingsService,
StorageService,
UserService,
} from 'jslib/abstractions';