Improve event handling + restarts

This commit is contained in:
Mo Bitar
2020-02-12 14:21:58 -06:00
parent 0fcfd98e5d
commit a364a9ec03
19 changed files with 1202 additions and 814 deletions

View File

@@ -1,4 +1,6 @@
import { ApplicationEvents } from 'snjs';
export class PureCtrl {
/* @ngInject */
constructor(
$scope,
$timeout,
@@ -14,13 +16,22 @@ export class PureCtrl {
this.application = application;
this.state = {};
this.props = {};
this.addAppStateObserver();
this.addAppEventObserver();
$scope.$on('$destroy', () => {
this.unsubApp();
this.unsubState();
});
}
$onInit() {
this.addAppStateObserver();
this.addAppEventObserver();
}
/** @private */
async resetState() {
this.state = {};
await this.setState({});
}
async setState(state) {
return new Promise((resolve) => {
@@ -45,12 +56,28 @@ export class PureCtrl {
}
addAppEventObserver() {
this.unsubApp = this.application.addEventObserver((eventName) => {
this.onApplicationEvent(eventName);
if (this.application.isStarted()) {
this.onAppStart();
}
if (!this.appState.isLocked()) {
this.onAppUnlock();
}
this.unsubApp = this.application.addEventObserver(async (eventName) => {
this.onAppEvent(eventName);
if (eventName === ApplicationEvents.Started) {
await this.resetState();
await this.onAppStart();
} else if (eventName === ApplicationEvents.Unlocked) {
await this.onAppUnlock();
} else if (eventName === ApplicationEvents.CompletedSync) {
this.onAppSync();
} else if (eventName === ApplicationEvents.KeyStatusChange) {
this.onAppKeyChange();
}
});
}
onApplicationEvent(eventName) {
onAppEvent(eventName) {
/** Optional override */
}
@@ -58,4 +85,19 @@ export class PureCtrl {
/** Optional override */
}
async onAppStart() {
/** Optional override */
}
async onAppUnlock() {
/** Optional override */
}
async onAppKeyChange() {
/** Optional override */
}
onAppSync() {
/** Optional override */
}
}

View File

@@ -81,18 +81,18 @@ class EditorCtrl extends PureCtrl {
onReady: () => this.reloadPreferences()
};
this.addSyncStatusObserver();
this.registerKeyboardShortcuts();
application.onUnlock(() => {
this.streamItems();
this.registerComponentHandler();
});
this.registerKeyboardShortcuts();
/** Used by .pug template */
this.prefKeyMonospace = PrefKeys.EditorMonospaceEnabled;
this.prefKeySpellcheck = PrefKeys.EditorSpellcheck;
this.prefKeyMarginResizers = PrefKeys.EditorResizersEnabled;
}
onAppUnlock() {
super.onAppUnlock();
this.streamItems();
this.registerComponentHandler();
}
/** @override */
onAppStateEvent(eventName, data) {
@@ -107,7 +107,7 @@ class EditorCtrl extends PureCtrl {
}
/** @override */
onApplicationEvent(eventName) {
onAppEvent(eventName) {
if (!this.state.note) {
return;
}

View File

@@ -46,23 +46,24 @@ class FooterCtrl extends PureCtrl {
this.arbitraryStatusMessage = string;
});
});
}
application.onUnlock(() => {
this.application.hasPasscode().then((value) => {
this.setState({
hasPasscode: value
});
onAppUnlock() {
super.onAppUnlock();
this.application.hasPasscode().then((value) => {
this.setState({
hasPasscode: value
});
this.godService.checkForSecurityUpdate().then((available) => {
this.securityUpdateAvailable = available;
});
this.user = this.application.getUser();
this.updateOfflineStatus();
this.findErrors();
this.streamItems();
this.registerComponentHandler();
});
this.godService.checkForSecurityUpdate().then((available) => {
this.securityUpdateAvailable = available;
});
this.user = this.application.getUser();
this.updateOfflineStatus();
this.findErrors();
this.streamItems();
this.registerComponentHandler();
}
addRootScopeListeners() {
@@ -109,7 +110,7 @@ class FooterCtrl extends PureCtrl {
}
/** @override */
onApplicationEvent(eventName) {
onAppEvent(eventName) {
if (eventName === ApplicationEvents.EnteredOutOfSync) {
this.outOfSync = true;
} else if (eventName === ApplicationEvents.ExitedOutOfSync) {

View File

@@ -17,6 +17,7 @@ class LockScreenCtrl extends PureCtrl {
}
$onInit() {
super.$onInit();
this.puppet.focusInput = () => {
this.passcodeInput.focus();
};

View File

@@ -76,10 +76,12 @@ class NotesCtrl extends PureCtrl {
angular.element(document).ready(() => {
this.reloadPreferences();
});
application.onUnlock(() => {
this.streamNotesAndTags();
this.reloadPreferences();
});
}
onAppUnlock() {
super.onAppUnlock();
this.streamNotesAndTags();
this.reloadPreferences();
}
/** @override */
@@ -97,7 +99,7 @@ class NotesCtrl extends PureCtrl {
}
/** @override */
onApplicationEvent(eventName) {
onAppEvent(eventName) {
if (eventName === ApplicationEvents.SignedIn) {
/** Delete dummy note if applicable */
if (this.state.selectedNote && this.state.selectedNote.dummy) {
@@ -430,10 +432,17 @@ class NotesCtrl extends PureCtrl {
});
}
if (note.errorDecrypting) {
flags.push({
text: "Missing Keys",
class: 'danger'
});
if(note.waitingForKeys) {
flags.push({
text: "Waiting For Keys",
class: 'info'
});
} else {
flags.push({
text: "Missing Keys",
class: 'danger'
});
}
}
if (note.deleted) {
flags.push({

View File

@@ -36,20 +36,27 @@ class RootCtrl extends PureCtrl {
this.statusManager = statusManager;
this.themeManager = themeManager;
this.platformString = getPlatformString();
this.state = {
ready: false,
appClass: ''
};
this.state = { appClass: '' };
this.loadApplication();
this.addDragDropHandlers();
application.onUnlock(() => {
this.handleAutoSignInFromParams();
});
this.lockScreenPuppet = {
focusInput: () => { }
};
}
onAppStart() {
super.onAppStart();
this.setState({ ready: false });
}
onAppUnlock() {
super.onAppUnlock();
this.setState({ ready: true, needsUnlock: false });
this.application.componentManager.setDesktopManager(this.desktopManager);
this.application.registerService(this.themeManager);
this.handleAutoSignInFromParams();
}
async watchLockscreenValue() {
return new Promise((resolve) => {
const onLockscreenValue = (value) => {
@@ -86,24 +93,12 @@ class RootCtrl extends PureCtrl {
}
});
await this.application.launch();
this.setState({ ready: true });
// this.addSyncStatusObserver();
// this.addSyncEventHandler();
}
onUpdateAvailable() {
this.$rootScope.$broadcast('new-update-available');
};
/** @override */
async onApplicationEvent(eventName) {
if (eventName === ApplicationEvents.ApplicationUnlocked) {
this.setState({ needsUnlock: false });
this.application.componentManager.setDesktopManager(this.desktopManager);
this.application.registerService(this.themeManager);
}
}
/** @override */
async onAppStateEvent(eventName, data) {
if (eventName === AppStateEvents.PanelResized) {
@@ -118,7 +113,7 @@ class RootCtrl extends PureCtrl {
if (this.tagsCollapsed) { appClass += " collapsed-tags"; }
this.setState({ appClass });
} else if (eventName === AppStateEvents.WindowDidFocus) {
if (!(await this.application.isPasscodeLocked())) {
if (!(await this.application.isLocked())) {
this.application.sync();
}
}

View File

@@ -23,28 +23,31 @@ class TagsPanelCtrl extends PureCtrl {
onReady: () => this.loadPreferences()
};
this.state = {
tags: [],
smartTags: [],
noteCounts: {}
noteCounts: {},
};
}
onAppStart() {
super.onAppStart();
this.registerComponentHandler();
}
$onInit() {
this.application.onStart(() => {
this.registerComponentHandler();
});
this.application.onUnlock(() => {
this.loadPreferences();
this.beginStreamingItems();
const smartTags = this.application.getSmartTags();
this.setState({
smartTags: smartTags,
});
this.selectTag(smartTags[0]);
onAppUnlock() {
super.onAppUnlock();
this.loadPreferences();
this.beginStreamingItems();
const smartTags = this.application.getSmartTags();
this.setState({
smartTags: smartTags,
});
this.selectTag(smartTags[0]);
}
this.application.onSync(() => {
this.reloadNoteCounts();
});
onAppSync() {
super.onAppSync();
this.reloadNoteCounts();
}
beginStreamingItems() {
@@ -58,10 +61,10 @@ class TagsPanelCtrl extends PureCtrl {
this.reloadNoteCounts();
if (this.state.selectedTag) {
/** If the selected tag has been deleted, revert to All view. */
const selectedTag = items.find((tag) => {
const matchingTag = items.find((tag) => {
return tag.uuid === this.state.selectedTag.uuid;
});
if (selectedTag && selectedTag.deleted) {
if (!matchingTag || matchingTag.deleted) {
this.selectTag(this.state.smartTags[0]);
}
}