import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import * as Loki from 'lokijs' ; import { StoreConfig } from './store.config' ; import { Settings } from 'app/settings' ; import { Account } from '@signumjs/core' ; import { adjustLegacyAddressPrefix } from '../util/adjustLegacyAddressPrefix' ; const CollectionName = { Account : 'accounts' , Settings : 'settings' , } ; @ Injectable ( { providedIn : 'root' } ) export class StoreService { private store : any ; public ready : BehaviorSubject < any > = new BehaviorSubject ( false ) ; public settings : BehaviorSubject < any > = new BehaviorSubject ( false ) ; constructor ( private storeConfig ) { this . store = new Loki ( storeConfig . databaseName , { autoload : true , autoloadCallback : this . init . bind ( this ) , adapter : storeConfig . persistenceAdapter } ) ; } public init ( ) { let accounts = this . store . getCollection ( CollectionName . Account ) ; if ( ! accounts ) { accounts = this . store . addCollection ( CollectionName . Account , { unique : [ 'account' ] } ) ; } let settings = this . store . getCollection ( CollectionName . Settings ) ; if ( ! settings ) { settings = this . store . addCollection ( CollectionName . Settings , { unique : [ 'id' ] } ) ; settings . insert ( new Settings ( ) ) ; } this . store . saveDatabase ( ) ; this . setReady ( true ) ; this . getSettings ( ) . then ( s => { this . setSettings ( s ) ; } ) . catch ( error => { this . setSettings ( new Settings ( ) ) ; } ) ; } public setReady ( state ) { this . ready . next ( state ) ; } public setSettings ( state ) { this . settings . next ( state ) ; } public saveAccount ( account ) { account = adjustLegacyAddressPrefix ( account ) ; return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { this . store . loadDatabase ( { } , ( ) => { const accounts = this . store . getCollection ( CollectionName . Account ) ; const rs = accounts . find ( { account : account . account } ) ; if ( rs . length === 0 ) { accounts . insert ( account ) ; } else { accounts . chain ( ) . find ( { account : account . account } ) . update ( w => { w . balanceNQT = account . balanceNQT ; w . unconfirmedBalanceNQT = account . unconfirmedBalanceNQT ; w . committedBalanceNQT = account . committedBalanceNQT ; w . accountRSExtended = account . accountRSExtended ; w . assetBalances = account . assetBalances ; w . type = account . type ; w . selected = account . selected ; w . name = account . name ; w . description = account . description ; w . keys = account . keys ; w . transactions = account . transactions ; w . confirmed = account . confirmed ; } ) ; } this . store . saveDatabase ( ) ; resolve ( account ) ; } ) ; } else { reject ( undefined ) ; } } ) ; } public getSelectedAccount ( ) { return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { this . store . loadDatabase ( { } , ( ) => { const accounts = this . store . getCollection ( CollectionName . Account ) ; let rs = accounts . find ( { selected : true } ) ; if ( rs . length > 0 ) { const account = new Account ( rs [ 0 ] ) ; resolve ( adjustLegacyAddressPrefix ( account ) ) ; } else { rs = accounts . find ( ) ; if ( rs . length > 0 ) { accounts . chain ( ) . find ( { account : rs [ 0 ] . account } ) . update ( a => { a . selected = true ; } ) ; const storedAccount = new Account ( rs [ 0 ] ) ; this . store . saveDatabase ( ) ; resolve ( adjustLegacyAddressPrefix ( storedAccount ) ) ; } else { reject ( undefined ) ; } reject ( undefined ) ; } } ) ; } else { reject ( undefined ) ; } } ) ; } public selectAccount ( account ) { return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { this . store . loadDatabase ( { } , ( ) => { account . selected = true ; const accounts = this . store . getCollection ( CollectionName . Account ) ; accounts . chain ( ) . find ( { selected : true } ) . update ( w => { w . selected = false ; } ) ; accounts . chain ( ) . find ( { account : account . account } ) . update ( w => { w . selected = true ; } ) ; this . store . saveDatabase ( ) ; resolve ( account ) ; } ) ; } else { reject ( undefined ) ; } } ) ; } public getAllAccounts ( ) { return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { const accounts = this . store . getCollection ( CollectionName . Account ) ; const rs = accounts . find ( ) ; const ws = [ ] ; rs . map ( storedAccount => { const account = adjustLegacyAddressPrefix ( new Account ( storedAccount ) ) ; ws . push ( account ) ; } ) ; resolve ( ws ) ; } else { reject ( [ ] ) ; } } ) ; } public findAccount ( accountId ) { return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { const accounts = this . store . getCollection ( CollectionName . Account ) ; const rs = accounts . find ( { account : accountId } ) ; if ( accountId && rs . length > 0 ) { const storedAccount = new Account ( rs [ 0 ] ) ; resolve ( adjustLegacyAddressPrefix ( storedAccount ) ) ; } else { resolve ( undefined ) ; } } else { reject ( undefined ) ; } } ) ; } public removeAccount ( account ) { return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { this . store . loadDatabase ( { } , ( ) => { const accounts = this . store . getCollection ( CollectionName . Account ) ; const rs = accounts . chain ( ) . find ( { account : account . account } ) . remove ( ) ; this . store . saveDatabase ( ) ; resolve ( true ) ; } ) ; } else { reject ( false ) ; } } ) ; } public saveSettings ( newSettings ) { return new Promise ( ( resolve , reject ) => { if ( ! this . ready . value ) { return reject ( ) ; } this . store . loadDatabase ( { } , ( ) => { const settings = this . store . getCollection ( CollectionName . Settings ) ; const rs = settings . find ( { id : newSettings . id } ) ; if ( rs . length > 0 ) { settings . update ( newSettings ) ; } else { settings . insert ( newSettings ) ; } this . store . saveDatabase ( ) ; this . setSettings ( newSettings ) ; resolve ( newSettings ) ; } ) ; } ) ; } public getSettings ( ) { return new Promise ( ( resolve , reject ) => { if ( this . ready . value ) { const settings = this . store . getCollection ( CollectionName . Settings ) ; const rs = settings . find ( ) ; resolve ( rs [ 0 ] ) ; } else { resolve ( new Settings ( ) ) ; } } ) ; } }