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

Commit

Permalink
fix: save sorting settings after logout -> login (#610)
Browse files Browse the repository at this point in the history
* fix: save sorting settings after logout -> login

* chore: upgrade snjs version
  • Loading branch information
vardan-arm committed May 26, 2022
1 parent 0ca9d78 commit 9ad0985
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/Lib/ApplicationState.ts
Expand Up @@ -10,6 +10,7 @@ import {
isNullOrUndefined,
NoteViewController,
PayloadEmitSource,
PrefKey,
removeFromArray,
SmartView,
SNNote,
Expand Down Expand Up @@ -37,7 +38,6 @@ import VersionInfo from 'react-native-version-info'
import pjson from '../../package.json'
import { MobileApplication } from './Application'
import { associateComponentWithNote } from './ComponentManager'
import { PrefKey } from './PreferencesManager'
const { PlatformConstants } = NativeModules

export enum AppStateType {
Expand Down Expand Up @@ -146,7 +146,7 @@ export class ApplicationState extends ApplicationService {
if (this.selectedTagRestored) {
return
}
const savedTagUuid: string | undefined = this.prefService.getValue(PrefKey.SelectedTagUuid, undefined)
const savedTagUuid: string | undefined = this.prefService.getValue(PrefKey.MobileSelectedTagUuid, undefined)

if (isNullOrUndefined(savedTagUuid)) {
this.selectedTagRestored = true
Expand Down Expand Up @@ -428,7 +428,7 @@ export class ApplicationState extends ApplicationService {
this.selectedTag = tag

if (saveSelection) {
void this.application.getLocalPreferences().setUserPrefValue(PrefKey.SelectedTagUuid, tag.uuid)
void this.application.getLocalPreferences().setUserPrefValue(PrefKey.MobileSelectedTagUuid, tag.uuid)
}

this.notifyOfStateChange(AppStateType.TagChanged, {
Expand Down
21 changes: 7 additions & 14 deletions src/Lib/PreferencesManager.ts
@@ -1,18 +1,6 @@
import { ApplicationService, isNullOrUndefined, removeFromArray } from '@standardnotes/snjs'
import { ApplicationService, isNullOrUndefined, PrefKey, removeFromArray } from '@standardnotes/snjs'
import { MobileApplication } from './Application'

export enum PrefKey {
SortNotesBy = 'sortBy',
SortNotesReverse = 'sortReverse',
NotesHideNotePreview = 'hideNotePreview',
NotesHideDate = 'hideDate',
NotesHideTags = 'hideTags',
LastExportDate = 'lastExportDate',
DoNotShowAgainUnsupportedEditors = 'doNotShowAgainUnsupportedEditors',
SelectedTagUuid = 'selectedTagUuid',
NotesHideEditorIcon = 'hideEditorIcon',
}

type Preferences = Record<PrefKey, any>
type PreferencesObserver = () => Promise<void> | void
export const LAST_EXPORT_DATE_KEY = 'LastExportDateKey'
Expand Down Expand Up @@ -63,16 +51,21 @@ export class PreferencesManager extends ApplicationService {
return this.application.setValue(PREFS_KEY, this.userPreferences)
}

private async savePreference(key: PrefKey, value: any) {
return this.application.setPreference(key, value)
}

getValue(key: PrefKey, defaultValue?: any) {
if (!this.userPreferences) {
return defaultValue
}
const value = this.userPreferences[key]
const value = this.application.getPreference(key)
return !isNullOrUndefined(value) ? value : defaultValue
}

async setUserPrefValue(key: PrefKey, value: any) {
this.userPreferences[key] = value
await this.saveSingleton()
await this.savePreference(key, value)
}
}
7 changes: 3 additions & 4 deletions src/Screens/Compose/ComponentView.tsx
@@ -1,10 +1,9 @@
import { ComponentLoadingError } from '@Lib/ComponentManager'
import { PrefKey } from '@Lib/PreferencesManager'
import { useFocusEffect, useNavigation } from '@react-navigation/native'
import { ApplicationContext } from '@Root/ApplicationContext'
import { AppStackNavigationProp } from '@Root/AppStack'
import { SCREEN_NOTES } from '@Root/Screens/screens'
import { ButtonType, ComponentViewer } from '@standardnotes/snjs'
import { ButtonType, ComponentViewer, PrefKey } from '@standardnotes/snjs'
import { ThemeServiceContext } from '@Style/ThemeService'
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'
import { Platform } from 'react-native'
Expand Down Expand Up @@ -109,7 +108,7 @@ export const ComponentView = ({

const doNotShowAgainUnsupportedEditors = application
?.getLocalPreferences()
.getValue(PrefKey.DoNotShowAgainUnsupportedEditors, false)
.getValue(PrefKey.MobileDoNotShowAgainUnsupportedEditors, false)

if (!doNotShowAgainUnsupportedEditors) {
const alertText =
Expand All @@ -126,7 +125,7 @@ export const ComponentView = ({
)

if (confirmed) {
void application?.getLocalPreferences().setUserPrefValue(PrefKey.DoNotShowAgainUnsupportedEditors, true)
void application?.getLocalPreferences().setUserPrefValue(PrefKey.MobileDoNotShowAgainUnsupportedEditors, true)
}
}
}
Expand Down
38 changes: 26 additions & 12 deletions src/Screens/Notes/Notes.tsx
@@ -1,14 +1,15 @@
import { AppStateType } from '@Lib/ApplicationState'
import { PrefKey } from '@Lib/PreferencesManager'
import { useSignedIn, useSyncStatus } from '@Lib/SnjsHelperHooks'
import { useFocusEffect, useNavigation } from '@react-navigation/native'
import { AppStackNavigationProp } from '@Root/AppStack'
import { useSafeApplicationContext } from '@Root/Hooks/useSafeApplicationContext'
import { SCREEN_COMPOSE, SCREEN_NOTES, SCREEN_VIEW_PROTECTED_NOTE } from '@Root/Screens/screens'
import {
ApplicationEvent,
CollectionSort,
CollectionSortProperty,
ContentType,
PrefKey,
SmartView,
SNNote,
SNTag,
Expand Down Expand Up @@ -50,19 +51,19 @@ export const Notes = React.memo(

// State
const [sortBy, setSortBy] = useState<CollectionSortProperty>(() =>
application.getLocalPreferences().getValue(PrefKey.SortNotesBy, CollectionSort.CreatedAt)
application.getLocalPreferences().getValue(PrefKey.MobileSortNotesBy, CollectionSort.CreatedAt)
)
const [sortReverse, setSortReverse] = useState<boolean>(() =>
application.getLocalPreferences().getValue(PrefKey.SortNotesReverse, false)
application.getLocalPreferences().getValue(PrefKey.MobileSortNotesReverse, false)
)
const [hideDates, setHideDates] = useState<boolean>(() =>
application.getLocalPreferences().getValue(PrefKey.NotesHideDate, false)
application.getLocalPreferences().getValue(PrefKey.MobileNotesHideDate, false)
)
const [hidePreviews, setHidePreviews] = useState<boolean>(() =>
application.getLocalPreferences().getValue(PrefKey.NotesHideNotePreview, false)
application.getLocalPreferences().getValue(PrefKey.MobileNotesHideNotePreview, false)
)
const [hideEditorIcon, setHideEditorIcon] = useState<boolean>(() =>
application.getLocalPreferences().getValue(PrefKey.NotesHideEditorIcon, false)
application.getLocalPreferences().getValue(PrefKey.MobileNotesHideEditorIcon, false)
)
const [notes, setNotes] = useState<SNNote[]>([])
const [selectedNoteId, setSelectedNoteId] = useState<SNNote['uuid']>()
Expand Down Expand Up @@ -403,13 +404,16 @@ export const Notes = React.memo(
}, [application, reloadNotes, reloadNotesDisplayOptions])

const reloadPreferences = useCallback(async () => {
const newSortBy = application.getLocalPreferences().getValue(PrefKey.SortNotesBy, CollectionSort.CreatedAt)
let displayOptionsChanged = false
let newSortBy = application.getLocalPreferences().getValue(PrefKey.MobileSortNotesBy, CollectionSort.CreatedAt)

const newSortReverse = application.getLocalPreferences().getValue(PrefKey.SortNotesReverse, false)
const newHidePreview = application.getLocalPreferences().getValue(PrefKey.NotesHideNotePreview, false)
const newHideDate = application.getLocalPreferences().getValue(PrefKey.NotesHideDate, false)
const newHideEditorIcon = application.getLocalPreferences().getValue(PrefKey.NotesHideEditorIcon, false)
if (newSortBy === CollectionSort.UpdatedAt || (newSortBy as string) === 'client_updated_at') {
newSortBy = CollectionSort.UpdatedAt
}
let displayOptionsChanged = false
const newSortReverse = application.getLocalPreferences().getValue(PrefKey.MobileSortNotesReverse, false)
const newHidePreview = application.getLocalPreferences().getValue(PrefKey.MobileNotesHideNotePreview, false)
const newHideDate = application.getLocalPreferences().getValue(PrefKey.MobileNotesHideDate, false)
const newHideEditorIcon = application.getLocalPreferences().getValue(PrefKey.MobileNotesHideEditorIcon, false)

if (sortBy !== newSortBy) {
setSortBy(newSortBy)
Expand Down Expand Up @@ -464,6 +468,16 @@ export const Notes = React.memo(
[reloadNotes, reloadNotesDisplayOptions]
)

useEffect(() => {
const removeEventObserver = application?.addSingleEventObserver(ApplicationEvent.PreferencesChanged, async () => {
await reloadPreferences()
})

return () => {
removeEventObserver?.()
}
}, [application, reloadPreferences])

useFocusEffect(
useCallback(() => {
void reloadPreferences()
Expand Down
7 changes: 3 additions & 4 deletions src/Screens/Settings/Sections/OptionsSection.tsx
@@ -1,4 +1,3 @@
import { PrefKey } from '@Lib/PreferencesManager'
import { useSignedIn } from '@Lib/SnjsHelperHooks'
import { useNavigation } from '@react-navigation/native'
import { ApplicationContext } from '@Root/ApplicationContext'
Expand All @@ -9,7 +8,7 @@ import { SectionHeader } from '@Root/Components/SectionHeader'
import { TableSection } from '@Root/Components/TableSection'
import { ModalStackNavigationProp } from '@Root/ModalStack'
import { SCREEN_MANAGE_SESSIONS, SCREEN_SETTINGS } from '@Root/Screens/screens'
import { ButtonType } from '@standardnotes/snjs'
import { ButtonType, PrefKey } from '@standardnotes/snjs'
import moment from 'moment'
import React, { useCallback, useContext, useMemo, useState } from 'react'
import { Platform } from 'react-native'
Expand All @@ -31,7 +30,7 @@ export const OptionsSection = ({ title, encryptionAvailable }: Props) => {
const [importing, setImporting] = useState(false)
const [exporting, setExporting] = useState(false)
const [lastExportDate, setLastExportDate] = useState<Date | undefined>(() =>
application?.getLocalPreferences().getValue(PrefKey.LastExportDate, undefined)
application?.getLocalPreferences().getValue(PrefKey.MobileLastExportDate, undefined)
)

const lastExportData = useMemo(() => {
Expand Down Expand Up @@ -95,7 +94,7 @@ export const OptionsSection = ({ title, encryptionAvailable }: Props) => {
if (result) {
const exportDate = new Date()
setLastExportDate(exportDate)
void application?.getLocalPreferences().setUserPrefValue(PrefKey.LastExportDate, exportDate)
void application?.getLocalPreferences().setUserPrefValue(PrefKey.MobileLastExportDate, exportDate)
}
setExporting(false)
},
Expand Down
29 changes: 14 additions & 15 deletions src/Screens/Settings/Sections/PreferencesSection.tsx
@@ -1,30 +1,29 @@
import { PrefKey } from '@Lib/PreferencesManager'
import { ApplicationContext } from '@Root/ApplicationContext'
import { SectionedAccessoryTableCell } from '@Root/Components/SectionedAccessoryTableCell'
import { SectionHeader } from '@Root/Components/SectionHeader'
import { TableSection } from '@Root/Components/TableSection'
import { CollectionSort, CollectionSortProperty } from '@standardnotes/snjs'
import React, { useContext, useMemo, useState } from 'react'
import { useSafeApplicationContext } from '@Root/Hooks/useSafeApplicationContext'
import { CollectionSort, CollectionSortProperty, PrefKey } from '@standardnotes/snjs'
import React, { useMemo, useState } from 'react'

export const PreferencesSection = () => {
// Context
const application = useContext(ApplicationContext)
const application = useSafeApplicationContext()

// State
const [sortBy, setSortBy] = useState<CollectionSortProperty>(() =>
application!.getLocalPreferences().getValue(PrefKey.SortNotesBy, CollectionSort.CreatedAt)
application.getPreference(PrefKey.MobileSortNotesBy, CollectionSort.CreatedAt)
)
const [sortReverse, setSortReverse] = useState<boolean>(() =>
application!.getLocalPreferences().getValue(PrefKey.SortNotesReverse, false)
application.getLocalPreferences().getValue(PrefKey.MobileSortNotesReverse, false)
)
const [hideDates, setHideDates] = useState<boolean>(() =>
application!.getLocalPreferences().getValue(PrefKey.NotesHideDate, false)
application.getLocalPreferences().getValue(PrefKey.MobileNotesHideDate, false)
)
const [hideEditorIcon, setHideEditorIcon] = useState<boolean>(() =>
application!.getLocalPreferences().getValue(PrefKey.NotesHideEditorIcon, false)
application.getLocalPreferences().getValue(PrefKey.MobileNotesHideEditorIcon, false)
)
const [hidePreviews, setHidePreviews] = useState<boolean>(() =>
application!.getLocalPreferences().getValue(PrefKey.NotesHideNotePreview, false)
application.getLocalPreferences().getValue(PrefKey.MobileNotesHideNotePreview, false)
)

const sortOptions = useMemo(() => {
Expand All @@ -36,24 +35,24 @@ export const PreferencesSection = () => {
}, [])

const toggleReverseSort = () => {
void application?.getLocalPreferences().setUserPrefValue(PrefKey.SortNotesReverse, !sortReverse)
void application.getLocalPreferences().setUserPrefValue(PrefKey.MobileSortNotesReverse, !sortReverse)
setSortReverse(value => !value)
}

const changeSortOption = (key: CollectionSortProperty) => {
void application?.getLocalPreferences().setUserPrefValue(PrefKey.SortNotesBy, key)
void application.getLocalPreferences().setUserPrefValue(PrefKey.MobileSortNotesBy, key)
setSortBy(key)
}
const toggleNotesPreviewHidden = () => {
void application?.getLocalPreferences().setUserPrefValue(PrefKey.NotesHideNotePreview, !hidePreviews)
void application.getLocalPreferences().setUserPrefValue(PrefKey.MobileNotesHideNotePreview, !hidePreviews)
setHidePreviews(value => !value)
}
const toggleNotesDateHidden = () => {
void application?.getLocalPreferences().setUserPrefValue(PrefKey.NotesHideDate, !hideDates)
void application.getLocalPreferences().setUserPrefValue(PrefKey.MobileNotesHideDate, !hideDates)
setHideDates(value => !value)
}
const toggleNotesEditorIconHidden = () => {
void application?.getLocalPreferences().setUserPrefValue(PrefKey.NotesHideEditorIcon, !hideEditorIcon)
void application.getLocalPreferences().setUserPrefValue(PrefKey.MobileNotesHideEditorIcon, !hideEditorIcon)
setHideEditorIcon(value => !value)
}

Expand Down

0 comments on commit 9ad0985

Please sign in to comment.