ui: revert to sync style

This commit is contained in:
Aaron Greenspan
2021-01-20 13:04:32 -07:00
committed by Aiden McClelland
parent 833941b031
commit e96ef695a3
8 changed files with 72 additions and 91 deletions

View File

@@ -167,7 +167,6 @@ export class MockApiService extends ApiService {
}
async patchServerConfig (attr: string, value: any): Promise<EmptyResponse> {
console.log('huh', attr, value)
await mockPatchServerConfig()
this.serverModel.update({ [attr]: value })
return { }

View File

@@ -48,6 +48,17 @@ export class OsUpdateService {
)
}
updateIsAvailable (vi: string, vl: string): boolean {
if (!vi || !vl) return false
if (this.emver.compare(vi, vl) === -1) {
this.$updateAvailable$.next(vl)
return true
} else {
this.$updateAvailable$.next(undefined)
return false
}
}
async checkForAppsUpdate (): Promise<boolean> {
const availableApps = await this.apiService.getAvailableApps()
return !!availableApps.find(app => this.emver.compare(app.versionInstalled, app.versionLatest) === -1)

View File

@@ -1,76 +1,74 @@
import { Injectable } from '@angular/core'
import { AlertController, ModalController, NavController } from '@ionic/angular'
import { combineLatest, Observable, of } from 'rxjs'
import { concatMap, debounceTime, distinctUntilChanged, filter, map, switchMap, take } from 'rxjs/operators'
import { OSWelcomePage } from '../modals/os-welcome/os-welcome.page'
import { ServerModel, ServerModelState } from '../models/server-model'
import { exists, traceWheel } from '../util/misc.util'
import { S9Server } from '../models/server-model'
import { ApiService } from './api/api.service'
import { ConfigService } from './config.service'
import { Emver } from './emver.service'
import { LoaderService } from './loader.service'
import { OsUpdateService } from './os-update.service'
@Injectable({ providedIn: 'root' })
export class GlobalAlertsNotifier {
export class StartupAlertsNotifier {
constructor (
private readonly osUpdateService: OsUpdateService,
private readonly alertCtrl: AlertController,
private readonly navCtrl: NavController,
private readonly loader: LoaderService,
private readonly config: ConfigService,
private readonly modalCtrl: ModalController,
private readonly server: ServerModel,
private readonly apiService: ApiService,
) {
private readonly emver: Emver,
private readonly osUpdateService: OsUpdateService,
) { }
displayedWelcomeMessage = false
checkedForUpdates = false
welcomeSetAutoUpdateCheck = false
async handleSpecial (server: Readonly<S9Server>): Promise<void> {
this.handleOSWelcome(server)
if (!this.displayedWelcomeMessage) this.handleUpdateCheck(server)
}
init () {
console.log('init')
of({ }).pipe(
concatMap(
() => this.welcomeNeeded$().pipe(
take(1), // we only show welcome at most once per app instance
concatMap(vi => vi ? this.presentOsWelcome(vi) : of({ })),
),
),
concatMap(
() => this.osUpdateAlertNeeded$().pipe(
concatMap(vl => vl ? this.presentUpdateDailogues(vl) : of({ })),
),
),
).subscribe()
private async handleOSWelcome (server: Readonly<S9Server>) {
if (server.welcomeAck || server.versionInstalled !== this.config.version || this.displayedWelcomeMessage) return
this.displayedWelcomeMessage = true
const modal = await this.modalCtrl.create({
backdropDismiss: false,
component: OSWelcomePage,
presentingElement: await this.modalCtrl.getTop(),
componentProps: {
version: server.versionInstalled,
},
})
modal.onDidDismiss().then(res => {
this.welcomeSetAutoUpdateCheck = res.data.autoCheckUpdates
this.apiService.acknowledgeOSWelcome(this.config.version)
this.handleUpdateCheck(server)
})
await modal.present()
}
// emits versionInstalled when welcomeAck is false and f.e and b.e have same version
private welcomeNeeded$ (): Observable<string | undefined> {
const { welcomeAck, versionInstalled } = this.server.watch()
private async handleUpdateCheck (server: Readonly<S9Server>) {
if (this.displayedWelcomeMessage && !this.welcomeSetAutoUpdateCheck) return
if (!this.displayedWelcomeMessage && (!server.autoCheckUpdates || this.checkedForUpdates)) return
return combineLatest([ this.server.$modelState$, welcomeAck, versionInstalled ]).pipe(
filter(([ms, _, vi]) => ms === ServerModelState.LIVE && !!vi),
map(([_, wa, vi]) => !wa && vi === this.config.version ? vi : undefined),
)
}
// emits versionLatest whenever autoCheckUpdates becomes true and checkForUpdates yields a new version
private osUpdateAlertNeeded$ (): Observable<string | undefined> {
return this.server.watch().autoCheckUpdates.pipe(
distinctUntilChanged(),
filter(exists), //
concatMap(() => this.osUpdateService.checkForUpdates$()),
)
}
private async presentUpdateDailogues (vl : string): Promise<any> {
const { update } = await this.presentAlertNewOS(vl)
if (update) {
return this.loader.displayDuringP(
this.osUpdateService.updateEmbassyOS(vl),
).catch(e => alert(e))
this.checkedForUpdates = true
if (this.osUpdateService.updateIsAvailable(server.versionInstalled, server.versionLatest)) {
const { update } = await this.presentAlertNewOS(server.versionLatest)
if (update) {
return this.loader
.displayDuringP(this.osUpdateService.updateEmbassyOS(server.versionLatest))
.catch(e => alert(e))
}
}
try {
const newApps = await this.osUpdateService.checkForAppsUpdate()
if (newApps) {
const availableApps = await this.apiService.getAvailableApps()
if (!!availableApps.find(app => this.emver.compare(app.versionInstalled, app.versionLatest) === -1)) {
return this.presentAlertNewApps()
}
} catch (e) {
@@ -78,23 +76,6 @@ export class GlobalAlertsNotifier {
}
}
private async presentOsWelcome (vi: string): Promise<void> {
return new Promise(async resolve => {
const modal = await this.modalCtrl.create({
backdropDismiss: false,
component: OSWelcomePage,
presentingElement: await this.modalCtrl.getTop(),
componentProps: { version: vi },
})
//kick this off async
this.apiService.acknowledgeOSWelcome(this.config.version).catch(e => {
console.error(`Unable to acknowledge OS welcome`, e)
})
await modal.present()
modal.onDidDismiss().then(() => resolve())
})
}
private async presentAlertNewApps () {
const alert = await this.alertCtrl.create({
backdropDismiss: true,

View File

@@ -6,6 +6,7 @@ import { AppModel } from '../models/app-model'
import { SyncNotifier } from './sync.notifier'
import { BehaviorSubject, Observable, of, from, Subject, EMPTY } from 'rxjs'
import { switchMap, concatMap, catchError, delay, tap } from 'rxjs/operators'
import { StartupAlertsNotifier } from './startup-alerts.notifier'
@Injectable({
providedIn: 'root',
@@ -22,6 +23,7 @@ export class SyncDaemon {
private readonly serverModel: ServerModel,
private readonly appModel: AppModel,
private readonly syncNotifier: SyncNotifier,
private readonly startupAlertsNotifier: StartupAlertsNotifier,
) {
this.$sync$.pipe(
switchMap(go => go
@@ -36,6 +38,7 @@ export class SyncDaemon {
sync (): Observable<void> {
return from(this.getServerAndApps()).pipe(
concatMap(() => this.syncNotifier.handleSpecial(this.serverModel.peek())),
concatMap(() => this.startupAlertsNotifier.handleSpecial(this.serverModel.peek())),
tap(() => this.$synced$.next()),
catchError(e => of(console.error(`Exception in sync service`, e))),
)
@@ -54,7 +57,7 @@ export class SyncDaemon {
switch (serverRes.result) {
case 'resolve': {
this.serverModel.sync(serverRes.value, now)
this.serverModel.update(serverRes.value, now)
break
}
case 'reject': {