Prefs manager TS

This commit is contained in:
Mo Bitar
2020-04-13 09:35:35 -05:00
parent 2e2634e3a1
commit 3d955e4b7d
9 changed files with 119 additions and 128 deletions

View File

@@ -1,7 +1,7 @@
import { isDesktopApplication, dictToArray } from '@/utils';
import {
SNPredicate,
ContentTypes,
ContentType,
CreateMaxPayloadFromAnyObject,
ApplicationService
} from 'snjs';
@@ -127,7 +127,7 @@ export class NativeExtManager extends ApplicationService {
name: 'Batch Manager',
identifier: this.batchManagerId
};
const allContentTypes = dictToArray(ContentTypes);
const allContentType = dictToArray(ContentType);
const content = {
name: packageInfo.name,
area: 'modal',
@@ -135,7 +135,7 @@ export class NativeExtManager extends ApplicationService {
permissions: [
{
name: STREAM_ITEMS_PERMISSION,
content_types: allContentTypes
content_types: allContentType
}
]
};

View File

@@ -1,80 +0,0 @@
import {
SNPredicate,
ContentTypes,
CreateMaxPayloadFromAnyObject,
ApplicationService
} from 'snjs';
export const PrefKeys = {
TagsPanelWidth: 'tagsPanelWidth',
NotesPanelWidth: 'notesPanelWidth',
EditorWidth: 'editorWidth',
EditorLeft: 'editorLeft',
EditorMonospaceEnabled: 'monospaceFont',
EditorSpellcheck: 'spellcheck',
EditorResizersEnabled: 'marginResizersEnabled',
SortNotesBy: 'sortBy',
SortNotesReverse: 'sortReverse',
NotesShowArchived: 'showArchived',
NotesHidePinned: 'hidePinned',
NotesHideNotePreview: 'hideNotePreview',
NotesHideDate: 'hideDate',
NotesHideTags: 'hideTags'
};
export class PreferencesManager extends ApplicationService {
/** @override */
onAppLaunch() {
super.onAppLaunch();
this.streamPreferences();
this.loadSingleton();
}
streamPreferences() {
this.application.streamItems({
contentType: ContentType.UserPrefs,
stream: () => {
this.loadSingleton();
}
});
}
async loadSingleton() {
const contentType = ContentType.UserPrefs;
const predicate = new SNPredicate('content_type', '=', contentType);
this.userPreferences = await this.application.singletonManager.findOrCreateSingleton({
predicate: predicate,
createPayload: CreateMaxPayloadFromAnyObject({
object: {
content_type: contentType,
content: {}
}
})
});
this.preferencesDidChange();
}
preferencesDidChange() {
this.application.getAppState().setUserPreferences(this.userPreferences);
}
syncUserPreferences() {
if (this.userPreferences) {
this.application.saveItem({ item: this.userPreferences });
}
}
getValue(key, defaultValue) {
if (!this.userPreferences) { return defaultValue; }
const value = this.userPreferences.getAppDataItem(key);
return (value !== undefined && value != null) ? value : defaultValue;
}
setUserPrefValue(key, value, sync) {
this.userPreferences.setAppDataItem(key, value);
if (sync) {
this.syncUserPreferences();
}
}
}

View File

@@ -0,0 +1,75 @@
import { WebApplication } from '@/application';
import {
SNPredicate,
ContentType,
ApplicationService,
SNUserPrefs,
WebPrefKey,
UserPrefsMutator
} from 'snjs';
import { FillItemContent } from '@/../../../../snjs/dist/@types/models/generator';
export class PreferencesManager extends ApplicationService {
private userPreferences!: SNUserPrefs
/** @override */
async onAppLaunch() {
super.onAppLaunch();
this.streamPreferences();
this.loadSingleton();
}
get webApplication() {
return this.application as WebApplication;
}
streamPreferences() {
this.application!.streamItems(
ContentType.UserPrefs,
() => {
this.loadSingleton();
}
);
}
async loadSingleton() {
const contentType = ContentType.UserPrefs;
const predicate = new SNPredicate('content_type', '=', contentType);
this.userPreferences = (await this.application!.singletonManager!.findOrCreateSingleton(
predicate,
contentType,
FillItemContent({})
)) as SNUserPrefs;
this.preferencesDidChange();
}
preferencesDidChange() {
this.webApplication.getAppState().setUserPreferences(this.userPreferences);
}
syncUserPreferences() {
if (this.userPreferences) {
this.application!.saveItem(this.userPreferences.uuid);
}
}
getValue(key: WebPrefKey, defaultValue: any) {
if (!this.userPreferences) { return defaultValue; }
const value = this.userPreferences.getPref(key);
return (value !== undefined && value !== null) ? value : defaultValue;
}
setUserPrefValue(key: WebPrefKey, value: any, sync = false) {
this.application!.changeItem(
this.userPreferences.uuid,
(m) => {
const mutator = m as UserPrefsMutator;
mutator.setWebPref(key, value);
}
)
if (sync) {
this.syncUserPreferences();
}
}
}