From 808437ab06c79793616a0370b7919d846a56ee34 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 27 Feb 2019 09:21:58 -0500 Subject: [PATCH] system service for proc reload and clear clipboard --- src/abstractions/lock.service.ts | 2 - src/abstractions/system.service.ts | 5 +++ src/services/lock.service.ts | 33 +-------------- src/services/system.service.ts | 66 ++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 src/abstractions/system.service.ts create mode 100644 src/services/system.service.ts diff --git a/src/abstractions/lock.service.ts b/src/abstractions/lock.service.ts index e40bc00281b..ede31db0b16 100644 --- a/src/abstractions/lock.service.ts +++ b/src/abstractions/lock.service.ts @@ -6,6 +6,4 @@ export abstract class LockService { setLockOption: (lockOption: number) => Promise; isPinLockSet: () => Promise<[boolean, boolean]>; clear: () => Promise; - startLockReload: () => void; - cancelLockReload: () => void; } diff --git a/src/abstractions/system.service.ts b/src/abstractions/system.service.ts new file mode 100644 index 00000000000..cf4f3e31333 --- /dev/null +++ b/src/abstractions/system.service.ts @@ -0,0 +1,5 @@ +export abstract class SystemService { + startProcessReload: () => void; + cancelProcessReload: () => void; + clearClipboard: (clipboardValue: string, timeoutMs?: number) => void; +} diff --git a/src/services/lock.service.ts b/src/services/lock.service.ts index bf074018610..230fd33d108 100644 --- a/src/services/lock.service.ts +++ b/src/services/lock.service.ts @@ -14,13 +14,12 @@ export class LockService implements LockServiceAbstraction { pinLocked = false; private inited = false; - private reloadInterval: any = null; constructor(private cipherService: CipherService, private folderService: FolderService, private collectionService: CollectionService, private cryptoService: CryptoService, private platformUtilsService: PlatformUtilsService, private storageService: StorageService, private messagingService: MessagingService, private searchService: SearchService, - private lockedCallback: () => Promise = null, private reloadCallback: () => Promise = null) { + private lockedCallback: () => Promise = null) { } init(checkOnInterval: boolean) { @@ -118,34 +117,4 @@ export class LockService implements LockServiceAbstraction { clear(): Promise { return this.storageService.remove(ConstantsService.protectedPin); } - - startLockReload(): void { - if (this.pinLocked || this.reloadInterval != null) { - return; - } - this.reloadInterval = setInterval(async () => { - let doRefresh = false; - const lastActive = await this.storageService.get(ConstantsService.lastActiveKey); - if (lastActive != null) { - const diffSeconds = (new Date()).getTime() - lastActive; - // Don't refresh if they are still active in the window - doRefresh = diffSeconds >= 5000; - } - if (doRefresh) { - clearInterval(this.reloadInterval); - this.reloadInterval = null; - this.messagingService.send('reloadProcess'); - if (this.reloadCallback != null) { - await this.reloadCallback(); - } - } - }, 10000); - } - - cancelLockReload(): void { - if (this.reloadInterval != null) { - clearInterval(this.reloadInterval); - this.reloadInterval = null; - } - } } diff --git a/src/services/system.service.ts b/src/services/system.service.ts new file mode 100644 index 00000000000..73e1dbdbc73 --- /dev/null +++ b/src/services/system.service.ts @@ -0,0 +1,66 @@ +import { LockService } from '../abstractions/lock.service'; +import { MessagingService } from '../abstractions/messaging.service'; +import { PlatformUtilsService } from '../abstractions/platformUtils.service'; +import { StorageService } from '../abstractions/storage.service'; +import { SystemService as SystemServiceAbstraction } from '../abstractions/system.service'; + +import { ConstantsService } from './constants.service'; + +import { Utils } from '../misc/utils'; + +export class SystemService implements SystemServiceAbstraction { + private reloadInterval: any = null; + private clearClipboardTimeout: any = null; + + constructor(private storageService: StorageService, private lockService: LockService, + private messagingService: MessagingService, private platformUtilsService: PlatformUtilsService, + private reloadCallback: () => Promise = null) { + } + + startProcessReload(): void { + if (this.lockService.pinLocked || this.reloadInterval != null) { + return; + } + this.cancelProcessReload(); + this.reloadInterval = setInterval(async () => { + let doRefresh = false; + const lastActive = await this.storageService.get(ConstantsService.lastActiveKey); + if (lastActive != null) { + const diffSeconds = (new Date()).getTime() - lastActive; + // Don't refresh if they are still active in the window + doRefresh = diffSeconds >= 5000; + } + if (doRefresh) { + clearInterval(this.reloadInterval); + this.reloadInterval = null; + this.messagingService.send('reloadProcess'); + if (this.reloadCallback != null) { + await this.reloadCallback(); + } + } + }, 10000); + } + + cancelProcessReload(): void { + if (this.reloadInterval != null) { + clearInterval(this.reloadInterval); + this.reloadInterval = null; + } + } + + clearClipboard(clipboardValue: string, timeoutMs = 5000): void { + if (this.clearClipboardTimeout != null) { + clearTimeout(this.clearClipboardTimeout); + this.clearClipboardTimeout = null; + } + if (Utils.isNullOrWhitespace(clipboardValue)) { + return; + } + this.clearClipboardTimeout = setTimeout(async () => { + const clipboardValueNow = await this.platformUtilsService.readFromClipboard(); + if (clipboardValue === clipboardValueNow) { + this.platformUtilsService.copyToClipboard(''); + } + }, timeoutMs); + } +}