import { Component , HostListener , OnInit } from '@angular/core' ; import { WalletService } from "./services/wallet.service" ; import { AddressBookService } from "./services/address-book.service" ; import { AppSettingsService } from "./services/app-settings.service" ; import { WebsocketService } from "./services/websocket.service" ; import { PriceService } from "./services/price.service" ; import { NotificationService } from "./services/notification.service" ; import { PowService } from "./services/pow.service" ; import { WorkPoolService } from "./services/work-pool.service" ; import { Router } from "@angular/router" ; import { RepresentativeService } from "./services/representative.service" ; import { NodeService } from "./services/node.service" ; import { LedgerService } from './services' ; @ Component ( { selector : 'app-root' , templateUrl : './app.component.html' , styleUrls : [ './app.component.css' ] } ) export class AppComponent implements OnInit { @ HostListener ( 'window:resize' , [ '$event' ] ) onResize ( e ) { this . windowHeight = e . target . innerHeight ; } ; wallet = this . walletService . wallet ; node = this . nodeService . node ; nanoPrice = this . price . price ; fiatTimeout = 5 * 60 * 1000 ; inactiveSeconds = 0 ; windowHeight = 1000 ; showSearchBar = false ; searchData = '' ; isConfigured = this . walletService . isConfigured ; constructor ( public walletService , private addressBook , public settings , private websocket , private notifications , private pow , public nodeService , private representative , private router , private workPool , private ledger , public price ) { } async ngOnInit ( ) { this . windowHeight = window . innerHeight ; this . settings . loadAppSettings ( ) ; await this . patchXrbToNanoPrefixData ( ) ; this . addressBook . loadAddressBook ( ) ; this . workPool . loadWorkCache ( ) ; await this . walletService . loadStoredWallet ( ) ; this . websocket . connect ( ) ; this . representative . loadRepresentativeList ( ) ; if ( this . wallet . locked && this . walletService . hasPendingTransactions ( ) ) { this . notifications . sendWarning ( `New incoming transaction - unlock the wallet to receive it!` , { length : 0 , identifier : 'pending-locked' } ) ; } if ( this . walletService . isLedgerWallet ( ) && this . ledger . isBrokenBrowser ( ) ) { this . notifications . sendLedgerChromeWarning ( ) ; } window . addEventListener ( "beforeunload" , ( e ) => { if ( this . wallet . locked ) return ; this . walletService . lockWallet ( ) ; } ) ; window . addEventListener ( "unload" , ( e ) => { if ( this . wallet . locked ) return ; this . walletService . lockWallet ( ) ; } ) ; window . addEventListener ( 'protocol-load' , ( e ) => { const protocolText = e . detail ; const stripped = protocolText . split ( '' ) . splice ( 4 ) . join ( '' ) ; if ( stripped . startsWith ( 'xrb_' ) ) { this . router . navigate ( [ 'account' , stripped ] ) ; } } ) ; setInterval ( ( ) => { this . inactiveSeconds += 1 ; if ( ! this . settings . settings . lockInactivityMinutes ) return ; if ( this . wallet . locked || ! this . wallet . password ) return ; if ( this . inactiveSeconds >= this . settings . settings . lockInactivityMinutes * 60 ) { this . walletService . lockWallet ( ) ; this . notifications . sendSuccess ( `Wallet locked after ${ this . settings . settings . lockInactivityMinutes } minutes of inactivity` ) ; } } , 1000 ) ; try { await this . updateFiatPrices ( ) ; } catch ( err ) { this . notifications . sendWarning ( `There was an issue retrieving latest Nano price.  Ensure your AdBlocker is disabled on this page then reload to see accurate FIAT values.` , { length : 0 , identifier : `price-adblock` } ) ; } } async patchXrbToNanoPrefixData ( ) { if ( this . settings . settings . walletVersion >= 2 ) return ; await this . walletService . patchOldSavedData ( ) ; this . addressBook . patchXrbPrefixData ( ) ; this . representative . patchXrbPrefixData ( ) ; this . settings . setAppSetting ( 'walletVersion' , 2 ) ; } toggleSearch ( mobile = false ) { this . showSearchBar = ! this . showSearchBar ; if ( this . showSearchBar ) { setTimeout ( ( ) => document . getElementById ( mobile ? 'search-input-mobile' : 'search-input' ) . focus ( ) , 150 ) ; } } performSearch ( ) { const searchData = this . searchData . trim ( ) ; if ( ! searchData . length ) return ; if ( searchData . startsWith ( 'xrb_' ) || searchData . startsWith ( 'nano_' ) ) { this . router . navigate ( [ 'account' , searchData ] ) ; } else if ( searchData . length === 64 ) { this . router . navigate ( [ 'transaction' , searchData ] ) ; } else { this . notifications . sendWarning ( `Invalid Nano account or transaction hash!` ) ; } this . searchData = '' ; } updateIdleTime ( ) { this . inactiveSeconds = 0 ; } retryConnection ( ) { this . walletService . reloadBalances ( true ) ; this . notifications . sendInfo ( `Attempting to reconnect to Nano node` ) ; } async updateFiatPrices ( ) { const displayCurrency = this . settings . getAppSetting ( `displayCurrency` ) || 'USD' ; await this . price . getPrice ( displayCurrency ) ; this . walletService . reloadFiatBalances ( ) ; setTimeout ( ( ) => this . updateFiatPrices ( ) , this . fiatTimeout ) ; } }