Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
refactor: local user preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
radko93 committed Oct 3, 2020
1 parent 513230e commit 7096511
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 98 deletions.
77 changes: 22 additions & 55 deletions src/lib/PreferencesManager.ts
@@ -1,26 +1,26 @@
import {
ApplicationService,
ContentType,
FillItemContent,
MobilePrefKey,
SNPredicate,
SNUserPrefs,
UserPrefsMutator,
} from 'snjs';
import { ApplicationService, isNullOrUndefined } from 'snjs';
import { MobileApplication } from './application';

export enum PrefKey {
SortNotesBy = 'sortBy',
SortNotesReverse = 'sortReverse',
NotesHideNotePreview = 'hideNotePreview',
NotesHideDate = 'hideDate',
LastExportDate = 'lastExportDate',
}

export const LAST_EXPORT_DATE_KEY = 'LastExportDateKey';
const PREFS_KEY = 'preferencesKey';

export class PreferencesManager extends ApplicationService {
private userPreferences!: SNUserPrefs;
private userPreferences!: Record<PrefKey, any>;
private loadingPrefs = false;
private unubscribeStreamItems?: () => void;

/** @override */
async onAppLaunch() {
super.onAppLaunch();
this.loadSingleton();
this.streamPreferences();
}

deinit() {
Expand All @@ -31,61 +31,28 @@ export class PreferencesManager extends ApplicationService {
return this.application as MobileApplication;
}

streamPreferences() {
this.unubscribeStreamItems = this.application!.streamItems(
ContentType.UserPrefs,
() => {
this.loadSingleton();
}
);
}

private async loadSingleton(forceSave?: boolean) {
private async loadSingleton() {
if (this.loadingPrefs) {
return;
}
this.loadingPrefs = true;
const contentType = ContentType.UserPrefs;
const predicate = new SNPredicate('content_type', '=', contentType);
const previousRef = this.userPreferences;
this.userPreferences = (await this.application!.singletonManager!.findOrCreateSingleton(
predicate,
contentType,
FillItemContent({})
)) as SNUserPrefs;
this.loadingPrefs = false;
const didChange =
!previousRef ||
this.userPreferences.lastSyncBegan?.getTime() !==
previousRef?.lastSyncBegan?.getTime();
if (forceSave || didChange) {
this.mobileApplication
.getAppState()
.setUserPreferences(this.userPreferences);
}
const preferences = await this.application.getValue(PREFS_KEY);
this.userPreferences = preferences ?? {};
}

syncUserPreferences() {
if (this.userPreferences) {
this.application!.saveItem(this.userPreferences.uuid);
}
private async saveSingleton() {
return this.application.setValue(PREFS_KEY, this.userPreferences);
}

getValue(key: MobilePrefKey, defaultValue?: any) {
getValue(key: PrefKey, defaultValue?: any) {
if (!this.userPreferences) {
return defaultValue;
}
const value = this.userPreferences.getPref(key);
return value !== undefined && value !== null ? value : defaultValue;
const value = this.userPreferences[key];
return !isNullOrUndefined(value) ? value : defaultValue;
}

async setUserPrefValue(key: MobilePrefKey, value: any, sync = false) {
await this.application!.changeItem(this.userPreferences.uuid, m => {
const mutator = m as UserPrefsMutator;
mutator.setMobilePref(key, value);
});
if (sync) {
this.syncUserPreferences();
}
async setUserPrefValue(key: PrefKey, value: any) {
this.userPreferences[key] = value;
await this.saveSingleton();
}
}
29 changes: 10 additions & 19 deletions src/screens/Notes/Notes.tsx
@@ -1,4 +1,5 @@
import { AppStateType } from '@Lib/ApplicationState';
import { PrefKey } from '@Lib/PreferencesManager';
import { useSignedIn, useSyncStatus } from '@Lib/snjsHooks';
import { useFocusEffect, useNavigation } from '@react-navigation/native';
import { ApplicationContext } from '@Root/ApplicationContext';
Expand All @@ -8,13 +9,7 @@ import { ICON_ADD } from '@Style/icons';
import { StyleKit } from '@Style/StyleKit';
import React, { useCallback, useContext, useRef, useState } from 'react';
import FAB from 'react-native-fab';
import {
CollectionSort,
ContentType,
MobilePrefKey,
Platform,
SNNote,
} from 'snjs';
import { CollectionSort, ContentType, Platform, SNNote } from 'snjs';
import { ThemeContext } from 'styled-components/native';
import { NoteList } from './NoteList';
import { StyledIcon } from './Notes.styled';
Expand Down Expand Up @@ -43,20 +38,16 @@ export const Notes: React.FC<Props> = ({ onNoteCreate, onNoteSelect }) => {
const [sortBy, setSortBy] = useState<CollectionSort>(() =>
application!
.getPrefsService()
.getValue(MobilePrefKey.SortNotesBy, CollectionSort.UpdatedAt)
.getValue(PrefKey.SortNotesBy, CollectionSort.UpdatedAt)
);
const [sortReverse, setSortReverse] = useState<boolean>(() =>
application!
.getPrefsService()
.getValue(MobilePrefKey.SortNotesReverse, false)
application!.getPrefsService().getValue(PrefKey.SortNotesReverse, false)
);
const [hideDates, setHideDates] = useState<boolean>(() =>
application!.getPrefsService().getValue(MobilePrefKey.NotesHideDate, false)
application!.getPrefsService().getValue(PrefKey.NotesHideDate, false)
);
const [hidePreviews, setHidePreviews] = useState<boolean>(() =>
application!
.getPrefsService()
.getValue(MobilePrefKey.NotesHideNotePreview, false)
application!.getPrefsService().getValue(PrefKey.NotesHideNotePreview, false)
);
const [notes, setNotes] = useState<SNNote[]>([]);
const [selectedNoteId, setSelectedNoteId] = useState<SNNote['uuid']>();
Expand Down Expand Up @@ -238,7 +229,7 @@ export const Notes: React.FC<Props> = ({ onNoteCreate, onNoteSelect }) => {
const reloadPreferences = useCallback(async () => {
let newSortBy = application
?.getPrefsService()
.getValue(MobilePrefKey.SortNotesBy, NoteSortKey.UserUpdatedAt);
.getValue(PrefKey.SortNotesBy, NoteSortKey.UserUpdatedAt);
if (
newSortBy === NoteSortKey.UpdatedAt ||
newSortBy === NoteSortKey.ClientUpdatedAt
Expand All @@ -250,13 +241,13 @@ export const Notes: React.FC<Props> = ({ onNoteCreate, onNoteSelect }) => {

const newSortReverse = application
?.getPrefsService()
.getValue(MobilePrefKey.SortNotesReverse, false);
.getValue(PrefKey.SortNotesReverse, false);
const newHidePreview = application!
.getPrefsService()
.getValue(MobilePrefKey.NotesHideNotePreview, false);
.getValue(PrefKey.NotesHideNotePreview, false);
const newHideDate = application!
.getPrefsService()
.getValue(MobilePrefKey.NotesHideDate, false);
.getValue(PrefKey.NotesHideDate, false);

if (sortBy !== newSortBy) {
setSortBy(newSortBy);
Expand Down
9 changes: 4 additions & 5 deletions src/screens/Settings/Sections/OptionsSection.tsx
Expand Up @@ -3,6 +3,7 @@ import { SectionedAccessoryTableCell } from '@Components/SectionedAccessoryTable
import { SectionedOptionsTableCell } from '@Components/SectionedOptionsTableCell';
import { SectionHeader } from '@Components/SectionHeader';
import { TableSection } from '@Components/TableSection';
import { PrefKey } from '@Lib/PreferencesManager';
import { useSignedIn } from '@Lib/snjsHooks';
import { useFocusEffect, useNavigation } from '@react-navigation/native';
import { ApplicationContext } from '@Root/ApplicationContext';
Expand All @@ -15,7 +16,7 @@ import {
} from '@Screens/screens';
import moment from 'moment';
import React, { useCallback, useContext, useMemo, useState } from 'react';
import { ButtonType, MobilePrefKey, ProtectedAction } from 'snjs';
import { ButtonType, ProtectedAction } from 'snjs';

type Props = {
title: string;
Expand All @@ -35,9 +36,7 @@ export const OptionsSection = ({ title, encryptionAvailable }: Props) => {
const [awaitingUnlock, setAwaitingUnlock] = useState(false);
const [encryptedBackup, setEncryptedBackp] = useState(false);
const [lastExportDate, setLastExportDate] = useState<Date | undefined>(() =>
application
?.getPrefsService()
.getValue(MobilePrefKey.LastExportDate, undefined)
application?.getPrefsService().getValue(PrefKey.LastExportDate, undefined)
);

const lastExportData = useMemo(() => {
Expand Down Expand Up @@ -117,7 +116,7 @@ export const OptionsSection = ({ title, encryptionAvailable }: Props) => {
setLastExportDate(exportDate);
application
?.getPrefsService()
.setUserPrefValue(MobilePrefKey.LastExportDate, exportDate);
.setUserPrefValue(PrefKey.LastExportDate, exportDate);
}
setExporting(false);
},
Expand Down
29 changes: 10 additions & 19 deletions src/screens/Settings/Sections/PreferencesSection.tsx
@@ -1,9 +1,10 @@
import { SectionedAccessoryTableCell } from '@Components/SectionedAccessoryTableCell';
import { SectionHeader } from '@Components/SectionHeader';
import { TableSection } from '@Components/TableSection';
import { PrefKey } from '@Lib/PreferencesManager';
import { ApplicationContext } from '@Root/ApplicationContext';
import React, { useContext, useMemo, useState } from 'react';
import { CollectionSort, MobilePrefKey } from 'snjs';
import { CollectionSort } from 'snjs';

export const PreferencesSection = () => {
// Context
Expand All @@ -13,20 +14,16 @@ export const PreferencesSection = () => {
const [sortBy, setSortBy] = useState<CollectionSort>(() =>
application!
.getPrefsService()
.getValue(MobilePrefKey.SortNotesBy, CollectionSort.UpdatedAt)
.getValue(PrefKey.SortNotesBy, CollectionSort.UpdatedAt)
);
const [sortReverse, setSortReverse] = useState<boolean>(() =>
application!
.getPrefsService()
.getValue(MobilePrefKey.SortNotesReverse, false)
application!.getPrefsService().getValue(PrefKey.SortNotesReverse, false)
);
const [hideDates, setHideDates] = useState<boolean>(() =>
application!.getPrefsService().getValue(MobilePrefKey.NotesHideDate, false)
application!.getPrefsService().getValue(PrefKey.NotesHideDate, false)
);
const [hidePreviews, setHidePreviews] = useState<boolean>(() =>
application!
.getPrefsService()
.getValue(MobilePrefKey.NotesHideNotePreview, false)
application!.getPrefsService().getValue(PrefKey.NotesHideNotePreview, false)
);

const sortOptions = useMemo(() => {
Expand All @@ -40,30 +37,24 @@ export const PreferencesSection = () => {
const toggleReverseSort = () => {
application
?.getPrefsService()
.setUserPrefValue(MobilePrefKey.SortNotesReverse, !sortReverse, true);
.setUserPrefValue(PrefKey.SortNotesReverse, !sortReverse);
setSortReverse(value => !value);
};

const changeSortOption = (key: CollectionSort) => {
application
?.getPrefsService()
.setUserPrefValue(MobilePrefKey.SortNotesBy, key, true);
application?.getPrefsService().setUserPrefValue(PrefKey.SortNotesBy, key);
setSortBy(key);
};
const toggleNotesPreviewHidden = () => {
application
?.getPrefsService()
.setUserPrefValue(
MobilePrefKey.NotesHideNotePreview,
!hidePreviews,
true
);
.setUserPrefValue(PrefKey.NotesHideNotePreview, !hidePreviews);
setHidePreviews(value => !value);
};
const toggleNotesDateHidden = () => {
application
?.getPrefsService()
.setUserPrefValue(MobilePrefKey.NotesHideDate, !hideDates, true);
.setUserPrefValue(PrefKey.NotesHideDate, !hideDates);
setHideDates(value => !value);
};

Expand Down

0 comments on commit 7096511

Please sign in to comment.