From 2817f1daf89aad7ffac9004cac74c950ed766167 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 18 Feb 2019 13:17:30 +0100 Subject: [PATCH] add StorageProvider & SettingsPage infrastructure for persisting settings & data, needed for upcoming stuff --- src/app/app.module.ts | 2 ++ src/pages/settings/settings.html | 21 +++++++++++++++++ src/pages/settings/settings.module.ts | 15 ++++++++++++ src/pages/settings/settings.scss | 3 +++ src/pages/settings/settings.ts | 23 +++++++++++++++++++ src/providers/storage/storage.ts | 33 +++++++++++++++++++++++++++ 6 files changed, 97 insertions(+) create mode 100644 src/pages/settings/settings.html create mode 100644 src/pages/settings/settings.module.ts create mode 100644 src/pages/settings/settings.scss create mode 100644 src/pages/settings/settings.ts create mode 100644 src/providers/storage/storage.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 9900050..05b61c7 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -10,6 +10,7 @@ import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { openSenseApp } from './app.component'; import { OtaWizardPageModule } from '../pages/ota-wizard/ota-wizard.module'; import { BlocklyPageModule } from '../pages/blockly/blockly.module'; +import { StorageProvider } from '../providers/storage/storage'; // For AoT compilation (production builds) we need to have a factory for the loader of translation files. // @TODO: we possibly could optimize this by using a static loader in combination with webpack: @@ -44,6 +45,7 @@ export function createTranslateLoader(http: HttpClient) { StatusBar, SplashScreen, {provide: ErrorHandler, useClass: IonicErrorHandler}, + StorageProvider, ] }) export class AppModule {} diff --git a/src/pages/settings/settings.html b/src/pages/settings/settings.html new file mode 100644 index 0000000..51688c5 --- /dev/null +++ b/src/pages/settings/settings.html @@ -0,0 +1,21 @@ + + + + {{ 'SETTINGS.TITLE' | translate }} + + + + + + + + +

SETTINGS.LOG_OPTIN.TITLE

+

+
+ +
+
+
diff --git a/src/pages/settings/settings.module.ts b/src/pages/settings/settings.module.ts new file mode 100644 index 0000000..529640f --- /dev/null +++ b/src/pages/settings/settings.module.ts @@ -0,0 +1,15 @@ +import { NgModule } from '@angular/core'; +import { IonicPageModule } from 'ionic-angular'; +import { SettingsPage } from './settings'; +import { TranslateModule } from '@ngx-translate/core'; + +@NgModule({ + declarations: [ + SettingsPage, + ], + imports: [ + TranslateModule, + IonicPageModule.forChild(SettingsPage), + ], +}) +export class SettingsPageModule {} diff --git a/src/pages/settings/settings.scss b/src/pages/settings/settings.scss new file mode 100644 index 0000000..ee78e1c --- /dev/null +++ b/src/pages/settings/settings.scss @@ -0,0 +1,3 @@ +page-settings { + +} diff --git a/src/pages/settings/settings.ts b/src/pages/settings/settings.ts new file mode 100644 index 0000000..eba0c5a --- /dev/null +++ b/src/pages/settings/settings.ts @@ -0,0 +1,23 @@ +import { Component } from '@angular/core'; +import { IonicPage } from 'ionic-angular'; + +import { StorageProvider, SETTINGS } from '../../providers/storage/storage'; + + +@IonicPage() +@Component({ + selector: 'page-settings', + templateUrl: 'settings.html', +}) +export class SettingsPage { + settings = {} + + constructor(private storage: StorageProvider) { + this.settings = this.storage.get(SETTINGS) + } + + onSettingsChange (name, value) { + this.settings[name] = value + this.storage.set(SETTINGS, this.settings) + } +} diff --git a/src/providers/storage/storage.ts b/src/providers/storage/storage.ts new file mode 100644 index 0000000..a59e495 --- /dev/null +++ b/src/providers/storage/storage.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@angular/core'; + +export const SETTINGS = 'appsettings' + +@Injectable() +export class StorageProvider { + private cache: Map = new Map() + + constructor () { + this.registerKey(SETTINGS, { + logOptin: false, + }) + } + + registerKey (key, defaultValue) { + const stored = localStorage.getItem(key) + if (!stored) { + localStorage.setItem(key, JSON.stringify(defaultValue)) + this.cache[key] = defaultValue + } else { + this.cache[key] = JSON.parse(stored) + } + } + + get (key) { + return this.cache[key] + } + + set (key, value) { + localStorage.setItem(key, JSON.stringify(value)) + this.cache[key] = value + } +}