refactor: improve device interface types (#996)

This commit is contained in:
Mo
2022-04-22 13:54:34 -05:00
committed by GitHub
parent 68ad0f17ae
commit abb85b3f11
22 changed files with 296 additions and 235 deletions

View File

@@ -1,4 +1,3 @@
import { Bridge } from '@/Services/Bridge'
import { storage, StorageKey } from '@/Services/LocalStorage'
import { WebApplication, WebAppEvent } from '@/UIModels/Application'
import { AccountMenuState } from '@/UIModels/AppState/AccountMenuState'
@@ -32,6 +31,7 @@ import { SearchOptionsState } from './SearchOptionsState'
import { SubscriptionState } from './SubscriptionState'
import { SyncState } from './SyncState'
import { TagsState } from './TagsState'
import { WebOrDesktopDevice } from '@/Device/WebOrDesktopDevice'
export enum AppStateEvent {
TagChanged,
@@ -91,7 +91,7 @@ export class AppState {
private readonly tagChangedDisposer: IReactionDisposer
constructor(application: WebApplication, private bridge: Bridge) {
constructor(application: WebApplication, private device: WebOrDesktopDevice) {
this.application = application
this.notes = new NotesState(
application,
@@ -120,7 +120,7 @@ export class AppState {
}
this.registerVisibilityObservers()
if (this.bridge.appVersion.includes('-beta')) {
if (this.device.appVersion.includes('-beta')) {
this.showBetaWarning = storage.get(StorageKey.ShowBetaWarning) ?? true
} else {
this.showBetaWarning = false
@@ -198,7 +198,7 @@ export class AppState {
}
public get version(): string {
return this.bridge.appVersion
return this.device.appVersion
}
async openNewNote(title?: string) {

View File

@@ -379,8 +379,9 @@ export class NotesViewState {
paginate = () => {
this.notesToDisplay += this.pageSize
this.reloadNotes()
if (this.searchSubmitted) {
this.application.getDesktopService().searchText(this.noteFilterText)
this.application.getDesktopService()?.searchText(this.noteFilterText)
}
}
@@ -482,8 +483,9 @@ export class NotesViewState {
})
.catch(console.error)
}
if (this.isFiltering) {
this.application.getDesktopService().searchText(this.noteFilterText)
this.application.getDesktopService()?.searchText(this.noteFilterText)
}
}
@@ -496,9 +498,13 @@ export class NotesViewState {
handleTagChange = () => {
this.resetScrollPosition()
this.setShowDisplayOptionsMenu(false)
this.setNoteFilterText('')
this.application.getDesktopService().searchText()
this.application.getDesktopService()?.searchText()
this.resetPagination()
/* Capture db load state before beginning reloadNotes,
@@ -525,7 +531,8 @@ export class NotesViewState {
* enter before highlighting desktop search results.
*/
this.searchSubmitted = true
this.application.getDesktopService().searchText(this.noteFilterText)
this.application.getDesktopService()?.searchText(this.noteFilterText)
}
handleFilterTextChanged = () => {

View File

@@ -2,13 +2,13 @@ import { WebCrypto } from '@/Crypto'
import { AlertService } from '@/Services/AlertService'
import { ArchiveManager } from '@/Services/ArchiveManager'
import { AutolockService } from '@/Services/AutolockService'
import { Bridge } from '@/Services/Bridge'
import { DesktopDeviceInterface, isDesktopDevice } from '@/Device/DesktopDeviceInterface'
import { DesktopManager } from '@/Services/DesktopManager'
import { IOService } from '@/Services/IOService'
import { StatusManager } from '@/Services/StatusManager'
import { ThemeManager } from '@/Services/ThemeManager'
import { AppState } from '@/UIModels/AppState'
import { WebDeviceInterface } from '@/WebDeviceInterface'
import { WebOrDesktopDevice } from '@/Device/WebOrDesktopDevice'
import {
DeinitSource,
Platform,
@@ -21,7 +21,7 @@ import {
type WebServices = {
appState: AppState
desktopService: DesktopManager
desktopService?: DesktopManager
autolockService: AutolockService
archiveService: ArchiveManager
statusManager: StatusManager
@@ -44,26 +44,26 @@ export class WebApplication extends SNApplication {
public iconsController: IconsController
constructor(
deviceInterface: WebDeviceInterface,
deviceInterface: WebOrDesktopDevice,
platform: Platform,
identifier: string,
defaultSyncServerHost: string,
public bridge: Bridge,
webSocketUrl: string,
runtime: Runtime,
) {
super({
environment: bridge.environment,
environment: deviceInterface.environment,
platform: platform,
deviceInterface: deviceInterface,
crypto: WebCrypto,
alertService: new AlertService(),
identifier,
defaultHost: defaultSyncServerHost,
appVersion: bridge.appVersion,
appVersion: deviceInterface.appVersion,
webSocketUrl: webSocketUrl,
runtime,
})
deviceInterface.setApplication(this)
this.noteControllerGroup = new NoteGroupController(this)
this.iconsController = new IconsController()
@@ -80,7 +80,7 @@ export class WebApplication extends SNApplication {
if ('deinit' in service) {
service.deinit?.(source)
}
;(service as any).application = undefined
;(service as { application?: WebApplication }).application = undefined
}
this.webServices = {} as WebServices
@@ -88,7 +88,7 @@ export class WebApplication extends SNApplication {
this.webEventObservers.length = 0
if (source === DeinitSource.SignOut) {
this.bridge.onSignOut()
isDesktopDevice(this.deviceInterface) && this.deviceInterface.onSignOut()
}
} catch (error) {
console.error('Error while deiniting application', error)
@@ -116,7 +116,7 @@ export class WebApplication extends SNApplication {
return this.webServices.appState
}
public getDesktopService(): DesktopManager {
public getDesktopService(): DesktopManager | undefined {
return this.webServices.desktopService
}
@@ -128,6 +128,14 @@ export class WebApplication extends SNApplication {
return this.webServices.archiveService
}
public get desktopDevice(): DesktopDeviceInterface | undefined {
if (isDesktopDevice(this.deviceInterface)) {
return this.deviceInterface
}
return undefined
}
getStatusManager() {
return this.webServices.statusManager
}
@@ -145,11 +153,14 @@ export class WebApplication extends SNApplication {
}
downloadBackup(): void | Promise<void> {
return this.bridge.downloadBackup()
if (isDesktopDevice(this.deviceInterface)) {
return this.deviceInterface.downloadBackup()
}
}
async signOutAndDeleteLocalBackups(): Promise<void> {
await this.bridge.deleteLocalBackups()
isDesktopDevice(this.deviceInterface) && (await this.deviceInterface.deleteLocalBackups())
return this.user.signOut()
}
}

View File

@@ -1,15 +1,12 @@
import { WebDeviceInterface } from '@/WebDeviceInterface'
import { WebApplication } from './Application'
import {
ApplicationDescriptor,
SNApplicationGroup,
DeviceInterface,
Platform,
Runtime,
InternalEventBus,
} from '@standardnotes/snjs'
import { AppState } from '@/UIModels/AppState'
import { Bridge } from '@/Services/Bridge'
import { getPlatform, isDesktopApplication } from '@/Utils'
import { ArchiveManager } from '@/Services/ArchiveManager'
import { DesktopManager } from '@/Services/DesktopManager'
@@ -17,15 +14,17 @@ import { IOService } from '@/Services/IOService'
import { AutolockService } from '@/Services/AutolockService'
import { StatusManager } from '@/Services/StatusManager'
import { ThemeManager } from '@/Services/ThemeManager'
import { WebOrDesktopDevice } from '@/Device/WebOrDesktopDevice'
import { isDesktopDevice } from '@/Device/DesktopDeviceInterface'
export class ApplicationGroup extends SNApplicationGroup {
export class ApplicationGroup extends SNApplicationGroup<WebOrDesktopDevice> {
constructor(
private defaultSyncServerHost: string,
private bridge: Bridge,
private device: WebOrDesktopDevice,
private runtime: Runtime,
private webSocketUrl: string,
) {
super(new WebDeviceInterface(bridge))
super(device)
}
override async initialize(): Promise<void> {
@@ -34,7 +33,7 @@ export class ApplicationGroup extends SNApplicationGroup {
})
if (isDesktopApplication()) {
Object.defineProperty(window, 'desktopManager', {
Object.defineProperty(window, 'desktopCommunicationReceiver', {
get: () => (this.primaryApplication as WebApplication).getDesktopService(),
})
}
@@ -42,34 +41,36 @@ export class ApplicationGroup extends SNApplicationGroup {
private createApplication = (
descriptor: ApplicationDescriptor,
deviceInterface: DeviceInterface,
deviceInterface: WebOrDesktopDevice,
) => {
const platform = getPlatform()
const application = new WebApplication(
deviceInterface as WebDeviceInterface,
deviceInterface,
platform,
descriptor.identifier,
this.defaultSyncServerHost,
this.bridge,
this.webSocketUrl,
this.runtime,
)
const appState = new AppState(application, this.bridge)
const appState = new AppState(application, this.device)
const archiveService = new ArchiveManager(application)
const desktopService = new DesktopManager(application, this.bridge)
const io = new IOService(platform === Platform.MacWeb || platform === Platform.MacDesktop)
const autolockService = new AutolockService(application, new InternalEventBus())
const statusManager = new StatusManager()
const themeService = new ThemeManager(application)
application.setWebServices({
appState,
archiveService,
desktopService,
desktopService: isDesktopDevice(this.device)
? new DesktopManager(application, this.device)
: undefined,
io,
autolockService,
statusManager,
themeService,
})
return application
}
}