import { IDropboxAuth } from './idropbox-auth' ; import { Storage , StorageConfig } from '@ionic/storage' ; import { Injectable , Injector } from '@angular/core' ; @ Injectable ( ) export class Settings { private remoteWikiDirectory = '/PersonalWiki/' ; private localWikiDirectory : string ; private initializePromise : Promise < any > ; private syncMinutes : number ; private autoSync : boolean ; private style : string ; private showSearch : boolean ; private dropboxAuthInfo : IDropboxAuth ; private lastPageName : string ; private restoreLast : boolean ; private useCurly : boolean ; private fontSize : number ; private stayActiveInBackground : boolean ; constructor ( private storage ) { this . initializePromise = this . initialize ( ) ; } public waitForInitialize ( ) { return this . initializePromise ; } private initialize ( ) { const promises = [ ] ; promises . push ( this . storage . get ( 'syncMinutes' ) . then ( value => this . syncMinutes = value ) ) ; promises . push ( this . storage . get ( 'autoSync' ) . then ( value => this . autoSync = value ) ) ; promises . push ( this . storage . get ( 'showSearch' ) . then ( value => this . showSearch = value ) ) ; promises . push ( this . storage . get ( 'style' ) . then ( value => this . style = value ) ) ; promises . push ( this . storage . get ( 'dropboxAuth' ) . then ( value => this . dropboxAuthInfo = value ) ) ; promises . push ( this . storage . get ( 'lastPageName' ) . then ( value => this . lastPageName = value ) ) ; promises . push ( this . storage . get ( 'restoreLast' ) . then ( value => this . restoreLast = value ) ) ; promises . push ( this . storage . get ( 'useCurly' ) . then ( value => this . useCurly = value ) ) ; promises . push ( this . storage . get ( 'localWikiDirectory' ) . then ( value => this . localWikiDirectory = value ) ) ; promises . push ( this . storage . get ( 'fontSize' ) . then ( value => this . fontSize = value ) ) ; promises . push ( this . storage . get ( 'stayActiveInBackground' ) . then ( value => this . stayActiveInBackground = value ) ) ; return Promise . all ( promises ) ; } getRemotePath ( fileName ) { return this . remoteWikiDirectory + fileName ; } getLastPageName ( ) { return this . lastPageName ; } setLastPageName ( value ) { this . lastPageName = value ; return this . storage . set ( 'lastPageName' , value ) ; } getLocalWikiDirectory ( ) { return this . localWikiDirectory || 'PersonalWiki' ; } setLocalWikiDirectory ( value ) { this . localWikiDirectory = value ; return this . storage . set ( 'localWikiDirectory' , value ) ; } getSyncMinutes ( ) { const value = this . syncMinutes ; if ( value === 0 ) { return 0 ; } if ( ! value || isNaN ( value ) ) { return 10 ; } return value as number ; } setSyncMinutes ( value ) { if ( typeof ( value ) === 'string' ) { value = parseInt ( value , 10 ) ; } this . syncMinutes = value ; return this . storage . set ( 'syncMinutes' , value ) ; } getFontSize ( ) { const value = this . fontSize ; if ( ! value || isNaN ( value ) ) { return 100 ; } return value as number ; } setFontSize ( value ) { if ( typeof ( value ) === 'string' ) { value = parseInt ( value , 10 ) ; } this . fontSize = value ; return this . storage . set ( 'fontSize' , value ) ; } setRestoreLast ( value ) { this . restoreLast = value ; return this . storage . set ( 'restoreLast' , value ) ; } getRestoreLast ( ) { const value = this . restoreLast ; if ( value === false ) { return false ; } return true ; } setStayActiveInBackground ( value ) { this . stayActiveInBackground = value ; return this . storage . set ( 'stayActiveInBackground' , value ) ; } getStayActiveInBackground ( ) { const value = this . stayActiveInBackground ; if ( value === false ) { return false ; } return true ; } setUseCurly ( value ) { this . useCurly = value ; return this . storage . set ( 'useCurly' , value ) ; } getUseCurly ( ) { const value = this . useCurly ; if ( value === false ) { return false ; } return true ; } setShowSearch ( value ) { this . showSearch = value ; return this . storage . set ( 'showSearch' , value ) ; } getShowSearch ( ) { const value = this . showSearch ; if ( value === false ) { return false ; } return true ; } getAutoSync ( ) { const value = this . autoSync ; if ( value === false ) { return false ; } return true ; } setAutoSync ( value ) { this . autoSync = value ; return this . storage . set ( 'autoSync' , value ) ; } getStyle ( ) { const acceptedStyles = [ 'Wood' , 'Blue' , 'Dark' ] ; if ( acceptedStyles . indexOf ( this . style ) === - 1 ) { return 'Blue' ; } return this . style ; } setStyle ( value ) { this . style = value ; return this . storage . set ( 'style' , value ) ; } getDropboxAuthInfo ( ) { return this . dropboxAuthInfo ; } setDropboxAuthInfo ( auth ) { this . dropboxAuthInfo = auth ; return this . storage . set ( 'dropboxAuth' , auth ) ; } removeDropboxAuthInfo ( ) { this . dropboxAuthInfo = null ; return this . storage . remove ( 'dropboxAuth' ) ; } }