ApplicationService integration

This commit is contained in:
Mo Bitar
2020-03-20 19:03:02 -05:00
parent 0ec0fd4872
commit 0ef1ce8ef1
9 changed files with 897 additions and 447 deletions

View File

@@ -1,5 +1,6 @@
import { ApplicationEvents } from 'snjs';
export class PureCtrl {
import { ApplicationService } from 'snjs';
export class PureCtrl extends ApplicationService {
/* @ngInject */
constructor(
$scope,
@@ -10,21 +11,23 @@ export class PureCtrl {
if (!$scope || !$timeout || !application || !appState) {
throw 'Invalid PureCtrl construction.';
}
super(application);
this.$scope = $scope;
this.$timeout = $timeout;
this.appState = appState;
this.application = application;
this.state = this.getInitialState();
this.props = {};
/* Allow caller constructor to finish setting instance variables */
setImmediate(() => {
this.state = this.getInitialState();
});
$scope.$on('$destroy', () => {
this.unsubApp();
this.unsubState();
this.deinit();
});
}
$onInit() {
this.addAppStateObserver();
this.addAppEventObserver();
}
/** @private */
@@ -60,49 +63,13 @@ export class PureCtrl {
});
}
addAppEventObserver() {
if (this.application.isStarted()) {
this.onAppStart();
}
if (!this.appState.isLocked()) {
this.onAppLaunch();
}
this.unsubApp = this.application.addEventObserver(async (eventName) => {
this.onAppEvent(eventName);
if (eventName === ApplicationEvents.Started) {
await this.resetState();
await this.onAppStart();
} else if (eventName === ApplicationEvents.Launched) {
await this.onAppLaunch();
} else if (eventName === ApplicationEvents.CompletedSync) {
this.onAppSync();
} else if (eventName === ApplicationEvents.KeyStatusChanged) {
this.onAppKeyChange();
}
});
}
onAppEvent(eventName) {
/** Optional override */
}
onAppStateEvent(eventName, data) {
/** Optional override */
}
/** @override */
async onAppStart() {
/** Optional override */
}
async onAppLaunch() {
/** Optional override */
}
async onAppKeyChange() {
/** Optional override */
}
onAppSync() {
/** Optional override */
await this.resetState();
return super.onAppStart();
}
}

View File

@@ -61,7 +61,6 @@ class RootCtrl extends PureCtrl {
onAppLaunch() {
super.onAppLaunch();
this.setState({ needsUnlock: false });
this.application.registerService(this.themeManager);
this.handleAutoSignInFromParams();
}