浏览代码

Settings dialog cleanup

Danilo Bargen 8 年之前
父节点
当前提交
a98df87c83
共有 3 个文件被更改,包括 42 次插入57 次删除
  1. 15 25
      src/partials/messenger.ts
  2. 25 31
      src/services/settings.ts
  3. 2 1
      src/threema.d.ts

+ 15 - 25
src/partials/messenger.ts

@@ -18,7 +18,6 @@
 import {ContactControllerModel} from '../controller_model/contact';
 import {supportsPassive, throttle} from '../helpers';
 import {ExecuteService} from '../services/execute';
-import {SettingsService} from '../services/settings';
 import {ControllerModelMode} from '../types/enums';
 
 abstract class DialogController {
@@ -85,30 +84,22 @@ class SendFileController extends DialogController {
  */
 class SettingsController {
 
-    public static $inject = ['$mdDialog', 'SettingsService', '$window'];
+    public static $inject = ['$mdDialog', '$window', 'SettingsService'];
 
     public $mdDialog: ng.material.IDialogService;
     public $window: ng.IWindowService;
     public settingsService: threema.SettingsService;
     public activeElement: HTMLElement | null;
 
-    // Setting Variables
-
-    constructor($mdDialog: ng.material.IDialogService , settingsService: threema.SettingsService,
-                $window: ng.IWindowService) {
-
+    constructor($mdDialog: ng.material.IDialogService,
+                $window: ng.IWindowService,
+                settingsService: threema.SettingsService) {
         this.$mdDialog = $mdDialog;
         this.$window = $window;
-        this.activeElement = document.activeElement as HTMLElement;
         this.settingsService = settingsService;
-
-        // Init Setting Variables
-
+        this.activeElement = document.activeElement as HTMLElement;
     }
 
-    // SettingsServices Methods
-
-    // Dialog Methods
     public cancel(): void {
         this.$mdDialog.cancel();
         this.done();
@@ -121,8 +112,7 @@ class SettingsController {
 
     private done(): void {
         if (this.activeElement !== null) {
-            // reset focus
-            // this.$window.location.reload();
+            // Reset focus
             this.activeElement.focus();
         }
     }
@@ -679,15 +669,15 @@ class NavigationController {
      * Show settings dialog.
      */
     public settings(ev): void {
-            this.$mdDialog.show({
-                controller: SettingsController,
-                controllerAs: 'ctrl',
-                templateUrl: 'partials/dialog.settings.html',
-                parent: angular.element(document.body),
-                targetEvent: ev,
-                clickOutsideToClose: true,
-                fullscreen: true,
-            });
+        this.$mdDialog.show({
+            controller: SettingsController,
+            controllerAs: 'ctrl',
+            templateUrl: 'partials/dialog.settings.html',
+            parent: angular.element(document.body),
+            targetEvent: ev,
+            clickOutsideToClose: true,
+            fullscreen: true,
+        });
     }
 
     /**

+ 25 - 31
src/services/settings.ts

@@ -16,57 +16,51 @@
  */
 
 /**
- * The settings service can update variables for settings and persist them to local storage.
+ * The settings service can update variables for settings and persist them to
+ * LocalStorage.
  */
-
 export class SettingsService implements threema.SettingsService {
     private $log: ng.ILogService;
-    private $window: ng.IWindowService;
 
     private static STORAGE_KEY_PREFIX = 'settings-';
     private logTag: string = '[SettingsService]';
 
-    private storage: Storage = null;
-    public blocked = false;
-
-    // Settings Variables
+    private storage: Storage;
 
     public static $inject = ['$log', '$window'];
-    constructor($log: ng.ILogService, $window: ng.IWindowService, themeProvider, $mdTheming) {
+    constructor($log: ng.ILogService, $window: ng.IWindowService) {
         this.$log = $log;
-        this.$window = $window;
-
-        this.storage = this.$window.localStorage;
-
-        // Load Initial Data from LocalStorage to Settings Variables
-
+        this.storage = $window.localStorage;
     }
 
-    // Settings Getters & Setters - also set them in ../threema.d.ts
-
-    // Local Storage , store key-value pair
-    private storeUntrustedKeyValuePair(Key: string, value: string): void {
-        this.$log.debug(this.logTag, 'Storing unencrypted key-value pair for settings');
-        this.storage.setItem(SettingsService.STORAGE_KEY_PREFIX + Key, value);
+    /**
+     * Store settings key-value pair in LocalStorage.
+     */
+    public storeUntrustedKeyValuePair(key: string, value: string): void {
+        this.$log.debug(this.logTag, 'Storing settings key:', key);
+        this.storage.setItem(SettingsService.STORAGE_KEY_PREFIX + key, value);
     }
 
-    // Local Storage , retrieve key-value pair
-    private retrieveUntrustedKeyValuePair(Key: string): string {
-        this.$log.debug(this.logTag, 'Retrieving unencrypted key-value pair for settings');
-        if (this.hasUntrustedKeyValuePair(Key)) {
-            return this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + Key);
+    /**
+     * Retrieve settings key-value pair from LocalStorage.
+     */
+    public retrieveUntrustedKeyValuePair(key: string): string {
+        this.$log.debug(this.logTag, 'Retrieving settings key:', key);
+        if (this.hasUntrustedKeyValuePair(key)) {
+            return this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + key);
         } else {
-            this.$log.debug(this.logTag, 'key-value not set, creating empty one');
-            this.storeUntrustedKeyValuePair(Key, '');
+            this.storeUntrustedKeyValuePair(key, '');
+            return '';
         }
-
     }
 
     /**
-     * Return whether key-value pair is present in localstorage.
+     * Return whether key-value pair is present in LocalStorage.
+     *
+     * Note that this will return `true` for empty values!
      */
-    private hasUntrustedKeyValuePair(Key: string): boolean {
-        const item: string = this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + Key);
+    private hasUntrustedKeyValuePair(key: string): boolean {
+        const item: string = this.storage.getItem(SettingsService.STORAGE_KEY_PREFIX + key);
         return item !== null;
     }
 

+ 2 - 1
src/threema.d.ts

@@ -512,7 +512,8 @@ declare namespace threema {
     }
 
     interface SettingsService {
-
+        storeUntrustedKeyValuePair(key: string, value: string): void;
+        retrieveUntrustedKeyValuePair(key: string): string;
     }
 
     interface WebClientDefault {