From 167cfe43e29864e9889f3f503a728a240a3f5742 Mon Sep 17 00:00:00 2001 From: "Wonko T. Sane" <42@wonko.de> Date: Sun, 28 May 2017 13:21:11 +0200 Subject: [PATCH] settings provider --- src/src/app/app.component.ts | 3 ++- src/src/app/app.module.ts | 21 +++------------- src/src/pages/settings/settings.html | 2 +- src/src/pages/settings/settings.ts | 4 ++- src/src/providers/lectures-provider.ts | 1 - src/src/providers/settings.ts | 35 ++++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 src/src/providers/settings.ts diff --git a/src/src/app/app.component.ts b/src/src/app/app.component.ts index 0c1859b..cc6ae01 100644 --- a/src/src/app/app.component.ts +++ b/src/src/app/app.component.ts @@ -11,6 +11,7 @@ import { Settings } from '../pages/settings/settings'; import { Sponsors } from '../pages/sponsors/sponsors'; import { LecturesProvider } from '../providers/lectures-provider'; +import { SettingsProvider } from '../providers/settings'; @Component({ templateUrl: 'app.html' @@ -22,7 +23,7 @@ export class MyApp { pages: Array<{title: string, component: any}>; - constructor(public platform: Platform, private statusBar: StatusBar, private splashScreen: SplashScreen, private storage: Storage, private lecturesProvider: LecturesProvider) { + constructor(public platform: Platform, private statusBar: StatusBar, private splashScreen: SplashScreen, private storage: Storage, private lecturesProvider: LecturesProvider, private settings: SettingsProvider) { this.initializeApp(); // used for an example of ngFor and navigation diff --git a/src/src/app/app.module.ts b/src/src/app/app.module.ts index 2d76c7b..d3338fd 100644 --- a/src/src/app/app.module.ts +++ b/src/src/app/app.module.ts @@ -19,8 +19,8 @@ import { Settings } from '../pages/settings/settings'; import { LecturesProvider } from '../providers/lectures-provider'; -import { LecturesProviderFactory } from '../providers/lectures-provider'; import { FacebookProvider } from '../providers/facebook-provider'; +import { SettingsProvider } from '../providers/settings'; let pages = [ MyApp, Tabs, @@ -33,22 +33,6 @@ Sponsors, Settings ]; -export function provideSettings(storage: Storage) { - /** - * The Settings provider takes a set of default settings for your app. - * - * You can add new settings options at any time. Once the settings are saved, - * these values will not overwrite the saved values (this can be done manually if desired). - */ -/** return new Settings(storage, { - option1: true, - option2: 'Ionitron J. Framework', - option3: '3', - option4: 'Hello' - }); - */ -} - export function declarations() { return pages; @@ -64,8 +48,9 @@ export function providers() { StatusBar, FacebookProvider, LecturesProvider, + SettingsProvider, // { provide: LecturesProvider, useFactory: LecturesProviderFactory, deps: [Http,Storage] }, - { provide: Settings, useFactory: provideSettings, deps: [Storage] }, + //{ provide: Settings, useFactory: provideSettings, deps: [Storage] }, // Keep this to enable Ionic's runtime error handling during development { provide: ErrorHandler, useClass: IonicErrorHandler } ]; diff --git a/src/src/pages/settings/settings.html b/src/src/pages/settings/settings.html index 2d6a327..f5b6faf 100644 --- a/src/src/pages/settings/settings.html +++ b/src/src/pages/settings/settings.html @@ -21,7 +21,7 @@ <ion-item>Version: {{lecturesProvider.lecturesVersion}}</ion-item> <ion-item>Source: {{lecturesProvider.source}}</ion-item> <ion-item>driver: {{lecturesProvider.storage.driver}}</ion-item> - + <ion-item>url: {{settings.data.lecturesURL}}</ion-item> <ion-item> <ion-label>Boromir</ion-label> <ion-toggle color="royal"></ion-toggle> diff --git a/src/src/pages/settings/settings.ts b/src/src/pages/settings/settings.ts index 1560f80..b18709a 100644 --- a/src/src/pages/settings/settings.ts +++ b/src/src/pages/settings/settings.ts @@ -1,6 +1,7 @@ import { Component } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import { LecturesProvider } from '../../providers/lectures-provider'; +import { SettingsProvider } from '../../providers/settings'; @Component({ selector: 'page-settings', @@ -8,7 +9,8 @@ import { LecturesProvider } from '../../providers/lectures-provider'; }) export class Settings { - constructor(public navCtrl: NavController, public navParams: NavParams, public lecturesProvider: LecturesProvider) { + constructor(public navCtrl: NavController, public navParams: NavParams, public lecturesProvider: LecturesProvider, public settings: SettingsProvider) { + } ionViewDidLoad() { diff --git a/src/src/providers/lectures-provider.ts b/src/src/providers/lectures-provider.ts index 9c3a66c..cfcb532 100644 --- a/src/src/providers/lectures-provider.ts +++ b/src/src/providers/lectures-provider.ts @@ -7,7 +7,6 @@ import { Storage } from '@ionic/storage'; @Injectable() export class LecturesProvider { - private isInitialized: boolean = false; times: any[] = [] lecturesVersion: string; lecturesData: any; diff --git a/src/src/providers/settings.ts b/src/src/providers/settings.ts new file mode 100644 index 0000000..e3b02e5 --- /dev/null +++ b/src/src/providers/settings.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { Storage } from '@ionic/storage'; + +@Injectable() +export class SettingsProvider { + initial: any = null; + version: number = null; + data: {[setting: string]:string} = null; + constructor(public storage: Storage) { + if (!this.initial){ + storage.ready().then(() => { + + storage.get("version").then((version) => { + if (version){ + console.log('settings version: '+ version); + this.version = version; + storage.get("data").then((settings) => { + this.data = JSON.parse(settings); + }); + } else { + console.log('no settings data: new install'); + this.version = version; + this.data = {}; + this.data['lecturesURL'] = "https://test.wonko.de/lectures.json"; + this.version = 1; + storage.set('data',JSON.stringify(this.data)); + storage.set('version',this.version); + } + }); + }); + + } + } + +} -- GitLab