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();
}

View File

@@ -45,9 +45,14 @@ class AccountMenuCtrl extends PureCtrl {
this.archiveManager = archiveManager;
this.godService = godService;
this.lockManager = lockManager;
this.appVersion = appVersion;
this.syncStatus = this.application.getSyncStatus();
}
this.state = {
appVersion: 'v' + (window.electronAppVersion || appVersion),
/** @override */
getInitialState() {
return {
appVersion: 'v' + (window.electronAppVersion || this.appVersion),
passcodeAutoLockOptions: this.lockManager.getAutoLockIntervalOptions(),
user: this.application.getUser(),
formData: {
@@ -56,7 +61,6 @@ class AccountMenuCtrl extends PureCtrl {
},
mutable: {}
};
this.syncStatus = this.application.getSyncStatus();
}
async onAppKeyChange() {

View File

@@ -2,13 +2,13 @@
// An interface used by the Desktop app to interact with SN
import pull from 'lodash/pull';
import { isDesktopApplication } from '@/utils';
import { EncryptionIntents } from 'snjs';
import { EncryptionIntents, ApplicationService, ApplicationEvents } from 'snjs';
const COMPONENT_DATA_KEY_INSTALL_ERROR = 'installError';
const COMPONENT_CONTENT_KEY_PACKAGE_INFO = 'package_info';
const COMPONENT_CONTENT_KEY_LOCAL_URL = 'local_url';
export class DesktopManager {
export class DesktopManager extends ApplicationService {
/* @ngInject */
constructor(
$rootScope,
@@ -16,6 +16,7 @@ export class DesktopManager {
application,
appState,
) {
super(application);
this.$rootScope = $rootScope;
this.$timeout = $timeout;
this.appState = appState;
@@ -23,19 +24,21 @@ export class DesktopManager {
this.componentActivationObservers = [];
this.updateObservers = [];
this.isDesktop = isDesktopApplication();
}
$rootScope.$on('initial-data-loaded', () => {
/** @override */
onAppEvent(eventName) {
super.onAppEvent(eventName);
if (eventName === ApplicationEvents.LocalDataLoaded) {
this.dataLoaded = true;
if (this.dataLoadHandler) {
this.dataLoadHandler();
}
});
$rootScope.$on('major-data-change', () => {
} else if (eventName === ApplicationEvents.MajorDataChange) {
if (this.majorDataChangeHandler) {
this.majorDataChangeHandler();
}
});
}
}
saveBackup() {

View File

@@ -1,24 +1,27 @@
import { isDesktopApplication, dictToArray } from '@/utils';
import {
ApplicationEvents,
SNPredicate,
ContentTypes,
CreateMaxPayloadFromAnyObject
CreateMaxPayloadFromAnyObject,
ApplicationService
} from 'snjs';
const STREAM_ITEMS_PERMISSION = 'stream-items';
/** A class for handling installation of system extensions */
export class NativeExtManager {
export class NativeExtManager extends ApplicationService {
/* @ngInject */
constructor(application) {
super(application);
this.application = application;
this.extManagerId = 'org.standardnotes.extensions-manager';
this.batchManagerId = 'org.standardnotes.batch-manager';
this.unsub = application.addSingleEventObserver(ApplicationEvents.Launched, () => {
this.reload();
});
}
/** @override */
onAppLaunch() {
super.onAppLaunch();
this.reload();
}
get extManagerPred() {

View File

@@ -1,4 +1,10 @@
import { ApplicationEvents, SNPredicate, ContentTypes, CreateMaxPayloadFromAnyObject } from 'snjs';
import {
ApplicationEvents,
SNPredicate,
ContentTypes,
CreateMaxPayloadFromAnyObject,
ApplicationService
} from 'snjs';
export const PrefKeys = {
TagsPanelWidth: 'tagsPanelWidth',
@@ -17,18 +23,21 @@ export const PrefKeys = {
NotesHideTags: 'hideTags'
};
export class PreferencesManager {
export class PreferencesManager extends ApplicationService {
/* @ngInject */
constructor(
appState,
application
) {
this.application = application;
this.appState = appState;
this.unsub = application.addSingleEventObserver(ApplicationEvents.Launched, () => {
this.streamPreferences();
this.loadSingleton();
});
super(application);
this.appState = appState;
}
/** @override */
onAppLaunch() {
super.onAppLaunch();
this.streamPreferences();
this.loadSingleton();
}
streamPreferences() {
@@ -61,7 +70,7 @@ export class PreferencesManager {
syncUserPreferences() {
if (this.userPreferences) {
this.application.saveItem({item: this.userPreferences});
this.application.saveItem({ item: this.userPreferences });
}
}

View File

@@ -3,34 +3,23 @@ import {
ApplicationEvents,
StorageValueModes,
EncryptionIntents,
PureService,
ApplicationService,
} from 'snjs';
import { AppStateEvents } from '@/state';
const CACHED_THEMES_KEY = 'cachedThemes';
export class ThemeManager extends PureService {
export class ThemeManager extends ApplicationService {
/* @ngInject */
constructor(
application,
appState,
desktopManager,
) {
super();
this.application = application;
super(application);
this.appState = appState;
this.desktopManager = desktopManager;
this.activeThemes = [];
if (this.application.isStarted()) {
this.onAppStart();
}
this.unsub = application.addEventObserver((event) => {
if (event === ApplicationEvents.Started) {
this.onAppStart();
} else if (event === ApplicationEvents.SignedOut) {
this.deactivateAllThemes();
}
});
this.unsubState = appState.addObserver((eventName, data) => {
if (eventName === AppStateEvents.DesktopExtsReady) {
this.activateCachedThemes();
@@ -38,18 +27,20 @@ export class ThemeManager extends PureService {
});
}
/** @override */
onAppStart() {
super.onAppStart();
this.registerObservers();
if (!this.desktopManager.isDesktop) {
this.activateCachedThemes();
}
}
/** @override */
async deinit() {
super.deinit();
this.unsubState();
this.activeThemes = [];
onAppEvent(eventName) {
super.onAppEvent(eventName);
if (eventName === ApplicationEvents.SignedOut) {
this.deactivateAllThemes();
}
}
async activateCachedThemes() {

1184
dist/javascripts/app.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long