From ce4f683b966d3394963c4b5b256034e7f6859942 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 26 Jan 2018 22:16:35 -0500 Subject: [PATCH] added i18n sorting to folders, ciphers, collection --- src/abstractions/i18n.service.ts | 3 +++ src/services/cipher.service.ts | 11 ++++++++++- src/services/collection.service.ts | 11 ++++++++++- src/services/folder.service.ts | 16 +++++++++++++++- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/abstractions/i18n.service.ts b/src/abstractions/i18n.service.ts index 0a89e6eeba..372659d81d 100644 --- a/src/abstractions/i18n.service.ts +++ b/src/abstractions/i18n.service.ts @@ -1,4 +1,7 @@ export abstract class I18nService { + locale: string; + translationLocale: string; + collator: Intl.Collator; t: (id: string) => string; translate: (id: string) => string; } diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index 784c3d8841..a25d02cf38 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -29,6 +29,7 @@ import { ConstantsService } from './constants.service'; import { ApiService } from '../abstractions/api.service'; import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service'; import { CryptoService } from '../abstractions/crypto.service'; +import { I18nService } from '../abstractions/i18n.service'; import { SettingsService } from '../abstractions/settings.service'; import { StorageService } from '../abstractions/storage.service'; import { UserService } from '../abstractions/user.service'; @@ -84,7 +85,7 @@ export class CipherService implements CipherServiceAbstraction { constructor(private cryptoService: CryptoService, private userService: UserService, private settingsService: SettingsService, private apiService: ApiService, - private storageService: StorageService) { + private storageService: StorageService, private i18nService: I18nService) { } clearCache(): void { @@ -189,6 +190,7 @@ export class CipherService implements CipherServiceAbstraction { }); await Promise.all(promises); + decCiphers.sort(this.getLocaleSortingFunction()) this.decryptedCipherCache = decCiphers; return this.decryptedCipherCache; } @@ -445,6 +447,13 @@ export class CipherService implements CipherServiceAbstraction { // Helpers + private getLocaleSortingFunction(): (a: CipherView, b: CipherView) => number { + return (a, b) => { + return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) : + a.name.localeCompare(b.name); + }; + } + private async encryptObjProperty(model: V, obj: D, map: any, key: SymmetricCryptoKey): Promise { const promises = []; diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts index 5cdd393261..f860f5d9d3 100644 --- a/src/services/collection.service.ts +++ b/src/services/collection.service.ts @@ -6,6 +6,7 @@ import { CollectionView } from '../models/view/collectionView'; import { CollectionService as CollectionServiceAbstraction } from '../abstractions/collection.service'; import { CryptoService } from '../abstractions/crypto.service'; +import { I18nService } from '../abstractions/i18n.service'; import { StorageService } from '../abstractions/storage.service'; import { UserService } from '../abstractions/user.service'; @@ -17,7 +18,7 @@ export class CollectionService implements CollectionServiceAbstraction { decryptedCollectionCache: CollectionView[]; constructor(private cryptoService: CryptoService, private userService: UserService, - private storageService: StorageService) { + private storageService: StorageService, private i18nService: I18nService) { } clearCache(): void { @@ -66,6 +67,7 @@ export class CollectionService implements CollectionServiceAbstraction { }); await Promise.all(promises); + decCollections.sort(this.getLocaleSortingFunction()); this.decryptedCollectionCache = decCollections; return this.decryptedCollectionCache; } @@ -122,4 +124,11 @@ export class CollectionService implements CollectionServiceAbstraction { await this.storageService.save(Keys.collectionsPrefix + userId, collections); this.decryptedCollectionCache = null; } + + private getLocaleSortingFunction(): (a: CollectionView, b: CollectionView) => number { + return (a, b) => { + return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) : + a.name.localeCompare(b.name); + }; + } } diff --git a/src/services/folder.service.ts b/src/services/folder.service.ts index c185e20f14..acc9b39c17 100644 --- a/src/services/folder.service.ts +++ b/src/services/folder.service.ts @@ -11,6 +11,7 @@ import { FolderView } from '../models/view/folderView'; import { ApiService } from '../abstractions/api.service'; import { CryptoService } from '../abstractions/crypto.service'; import { FolderService as FolderServiceAbstraction } from '../abstractions/folder.service'; +import { I18nService } from '../abstractions/i18n.service'; import { StorageService } from '../abstractions/storage.service'; import { UserService } from '../abstractions/user.service'; @@ -23,7 +24,7 @@ export class FolderService implements FolderServiceAbstraction { constructor(private cryptoService: CryptoService, private userService: UserService, private noneFolder: () => string, private apiService: ApiService, - private storageService: StorageService) { + private storageService: StorageService, private i18nService: I18nService) { } clearCache(): void { @@ -83,6 +84,7 @@ export class FolderService implements FolderServiceAbstraction { }); await Promise.all(promises); + decFolders.sort(this.getLocaleSortingFunction()); this.decryptedFolderCache = decFolders; return this.decryptedFolderCache; } @@ -160,4 +162,16 @@ export class FolderService implements FolderServiceAbstraction { await this.apiService.deleteFolder(id); await this.delete(id); } + + private getLocaleSortingFunction(): (a: FolderView, b: FolderView) => number { + return (a, b) => { + if (a.id == null) { + // No folder is always last + return Number.MAX_SAFE_INTEGER; + } + + return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) : + a.name.localeCompare(b.name); + }; + } }