import { Component , OnInit } from 'angular-ts-decorators' ; import autobind from 'autobind-decorator' ; import { ApiServiceStatus } from '../../../shared/api/api.enum' ; import { ApiServiceInfo } from '../../../shared/api/api.interface' ; import { BookmarkHelperService } from '../../../shared/bookmark/bookmark-helper/bookmark-helper.service' ; import { PlatformService } from '../../../shared/global-shared.interface' ; import { NetworkService } from '../../../shared/network/network.service' ; import { StoreKey } from '../../../shared/store/store.enum' ; import { StoreService } from '../../../shared/store/store.service' ; import { SyncService } from '../../../shared/sync/sync.service' ; import { UtilityService } from '../../../shared/utility/utility.service' ; import { WorkingService } from '../../../shared/working/working.service' ; import { AppEventType , RoutePath } from '../../app.enum' ; import { AppHelperService } from '../../shared/app-helper/app-helper.service' ; @ autobind @ Component ( { controllerAs : 'vm' , selector : 'syncSettings' , styles : [ require ( './sync-settings.component.scss' ) ] , template : require ( './sync-settings.component.html' ) } ) export class SyncSettingsComponent implements OnInit { Strings = require ( '../../../../../res/strings/en.json' ) ; $q : ng . IQService ; $timeout : ng . ITimeoutService ; appHelperSvc : AppHelperService ; bookmarkHelperSvc : BookmarkHelperService ; networkSvc : NetworkService ; platformSvc : PlatformService ; storeSvc : StoreService ; syncSvc : SyncService ; utilitySvc : UtilityService ; workingSvc : WorkingService ; apiServiceStatus = ApiServiceStatus ; dataUsageProgressWidth = 0 ; displayQr = false ; lastUpdated : string ; nextUpdate : string ; serviceInfo : ApiServiceInfo ; syncDataSize : number ; syncDataUsed : number ; syncEnabled : boolean ; syncId : string ; syncIdCopied = false ; updatesAvailable : boolean ; static $inject = [ '$q' , '$timeout' , '$scope' , 'AppHelperService' , 'BookmarkHelperService' , 'NetworkService' , 'PlatformService' , 'StoreService' , 'SyncService' , 'UtilityService' , 'WorkingService' ] ; constructor ( $q , $timeout , $scope , AppHelperSvc , BookmarkHelperSvc , NetworkSvc , PlatformSvc , StoreSvc , SyncSvc , UtilitySvc , WorkingSvc ) { this . $q = $q ; this . $timeout = $timeout ; this . appHelperSvc = AppHelperSvc ; this . bookmarkHelperSvc = BookmarkHelperSvc ; this . networkSvc = NetworkSvc ; this . platformSvc = PlatformSvc ; this . storeSvc = StoreSvc ; this . syncSvc = SyncSvc ; this . utilitySvc = UtilitySvc ; this . workingSvc = WorkingSvc ; $scope . $on ( AppEventType . SyncDisabled , ( ) => { this . syncEnabled = false ; } ) ; $scope . $on ( AppEventType . RefreshSyncDataUsage , ( ) => this . refreshSyncDataUsage ( ) ) ; } checkForSyncUpdates ( ) { return this . $q . all ( [ this . syncSvc . checkForUpdates ( ) . catch ( ( ) => { } ) , this . appHelperSvc . getNextScheduledSyncUpdateCheck ( ) , this . appHelperSvc . getSyncQueueLength ( ) , this . storeSvc . get < string > ( StoreKey . LastUpdated ) ] ) . then ( ( data ) => { const [ updatesAvailable , nextUpdateDate , syncQueueLength , lastUpdated ] = data ; if ( updatesAvailable || syncQueueLength > 0 ) { this . updatesAvailable = true ; this . nextUpdate = this . platformSvc . getI18nString ( this . Strings . View . Settings . Sync . UpdatesAvailable . True ) . replace ( '{date}' , nextUpdateDate . toLocaleTimeString ( ) ) ; } else { this . updatesAvailable = false ; const lastUpdatedDate = new Date ( lastUpdated ) ; this . lastUpdated = this . platformSvc . getI18nString ( this . Strings . View . Settings . Sync . UpdatesAvailable . False ) . replace ( '{date}' , lastUpdatedDate . toLocaleString ( ) ) ; } } ) ; } closeQrPanel ( ) { this . displayQr = false ; } disableSync ( ) { this . platformSvc . disableSync ( ) . then ( ( ) => this . appHelperSvc . switchView ( RoutePath . Login ) ) ; } displayQrPanel ( ) { this . displayQr = true ; } ngOnInit ( ) { this . $q . all ( [ this . storeSvc . get < string > ( StoreKey . SyncId ) , this . utilitySvc . getServiceUrl ( ) , this . utilitySvc . isSyncEnabled ( ) ] ) . then ( ( data ) => { const [ syncId , serviceUrl , syncEnabled ] = data ; this . syncId = syncId ; this . serviceInfo = { url : serviceUrl } ; this . syncEnabled = syncEnabled ; if ( this . syncEnabled && ! this . utilitySvc . isMobilePlatform ( this . platformSvc . platformName ) ) { this . checkForSyncUpdates ( ) ; } this . refreshServiceStatus ( ) . then ( ( ) => this . appHelperSvc . attachClickEventsToNewTabLinks ( document . querySelector ( '.service-message' ) ) ) . then ( ( ) => this . refreshSyncDataUsage ( ) ) ; } ) ; } refreshServiceStatus ( ) { return this . appHelperSvc . formatServiceInfo ( ) . then ( ( formattedServiceInfo ) => { Object . assign ( this . serviceInfo , formattedServiceInfo ) ; } ) ; } refreshSyncDataUsage ( ) { return this . utilitySvc . isSyncEnabled ( ) . then ( ( syncEnabled ) => { if ( ! syncEnabled ) { return ; } return this . syncSvc . getSyncSize ( ) . then ( ( bookmarksSyncSize ) => { this . syncDataSize = bookmarksSyncSize / 1024 ; this . syncDataUsed = Math . ceil ( ( this . syncDataSize / this . serviceInfo . maxSyncSize ) * 150 ) ; this . $timeout ( ( ) => { this . dataUsageProgressWidth = this . syncDataUsed ; } , 250 ) ; } ) ; } ) ; } syncUpdates ( ) { this . workingSvc . show ( ) ; return this . platformSvc . queueSync ( ) . then ( ( ) => this . appHelperSvc . syncBookmarksSuccess ( ) ) . catch ( ( err ) => { return this . appHelperSvc . syncBookmarksFailed ( err ) . then ( ( ) => { throw err ; } ) ; } ) ; } }