Skip to content

Commit

Permalink
ApplicationService integration
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Mar 21, 2020
1 parent 0ec0fd4 commit 0ef1ce8
Show file tree
Hide file tree
Showing 9 changed files with 891 additions and 441 deletions.
57 changes: 12 additions & 45 deletions app/assets/javascripts/controllers/abstract/pure_ctrl.js
@@ -1,5 +1,6 @@
import { ApplicationEvents } from 'snjs';
export class PureCtrl {
import { ApplicationService } from 'snjs';

export class PureCtrl extends ApplicationService {
/* @ngInject */
constructor(
$scope,
Expand All @@ -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 */
Expand Down Expand Up @@ -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();
}
}
1 change: 0 additions & 1 deletion app/assets/javascripts/controllers/root.js
Expand Up @@ -61,7 +61,6 @@ class RootCtrl extends PureCtrl {
onAppLaunch() {
super.onAppLaunch();
this.setState({ needsUnlock: false });
this.application.registerService(this.themeManager);
this.handleAutoSignInFromParams();
}

Expand Down
10 changes: 7 additions & 3 deletions app/assets/javascripts/directives/views/accountMenu.js
Expand Up @@ -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: {
Expand All @@ -56,7 +61,6 @@ class AccountMenuCtrl extends PureCtrl {
},
mutable: {}
};
this.syncStatus = this.application.getSyncStatus();
}

async onAppKeyChange() {
Expand Down
17 changes: 10 additions & 7 deletions app/assets/javascripts/services/desktopManager.js
Expand Up @@ -2,40 +2,43 @@
// 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,
$timeout,
application,
appState,
) {
super(application);
this.$rootScope = $rootScope;
this.$timeout = $timeout;
this.appState = appState;
this.application = application;
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() {
Expand Down
17 changes: 10 additions & 7 deletions app/assets/javascripts/services/nativeExtManager.js
@@ -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() {
Expand Down
27 changes: 18 additions & 9 deletions app/assets/javascripts/services/preferencesManager.js
@@ -1,4 +1,10 @@
import { ApplicationEvents, SNPredicate, ContentTypes, CreateMaxPayloadFromAnyObject } from 'snjs';
import {
ApplicationEvents,
SNPredicate,
ContentTypes,
CreateMaxPayloadFromAnyObject,
ApplicationService
} from 'snjs';

export const PrefKeys = {
TagsPanelWidth: 'tagsPanelWidth',
Expand All @@ -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() {
Expand Down Expand Up @@ -61,7 +70,7 @@ export class PreferencesManager {

syncUserPreferences() {
if (this.userPreferences) {
this.application.saveItem({item: this.userPreferences});
this.application.saveItem({ item: this.userPreferences });
}
}

Expand Down
29 changes: 10 additions & 19 deletions app/assets/javascripts/services/themeManager.js
Expand Up @@ -3,53 +3,44 @@ 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();
}
});
}

/** @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() {
Expand Down

0 comments on commit 0ef1ce8

Please sign in to comment.