diff --git a/components/inbox-tab.tsx b/components/inbox-tab.tsx index b7edeb17..6ea8bdc3 100644 --- a/components/inbox-tab.tsx +++ b/components/inbox-tab.tsx @@ -26,6 +26,7 @@ import { Inbox, Conversation, inboxStats, observeInbox } from '../xmpp/xmpp'; import { compareArrays } from '../util/util'; import { TopNavBarButton } from './top-nav-bar-button'; import { listen, unlisten } from '../events/events'; +import { inboxOrder, inboxSection } from '../kv-storage/inbox'; const Stack = createNativeStackNavigator(); @@ -94,8 +95,14 @@ const InboxTab_ = ({navigation}) => { }).start(); }, []); - const setSectionIndex_ = useCallback((value) => setSectionIndex(value), []); - const setSortByIndex_ = useCallback((value) => setSortByIndex(value), []); + const setSectionIndex_ = useCallback((value: number) => { + setSectionIndex(value); + inboxSection(value); + }, []); + const setSortByIndex_ = useCallback((value: number) => { + setSortByIndex(value); + inboxOrder(value); + }, []); const onPressTooMany = useCallback(() => { navigation.navigate( @@ -115,7 +122,17 @@ const InboxTab_ = ({navigation}) => { listRef.current?.refresh && listRef.current.refresh() }, [listRef]); - useEffect(() => observeInbox(setInbox), []); + useEffect(() => { + (async () => { + const _inboxOrder = await inboxOrder(); + const _inboxSection = await inboxSection(); + + setSectionIndex(_inboxSection); + setSortByIndex(_inboxOrder); + + observeInbox(setInbox); + })(); + }, []); useEffect(maybeRefresh, [maybeRefresh, sectionIndex]); useEffect(maybeRefresh, [maybeRefresh, sortByIndex]); diff --git a/kv-storage/inbox.tsx b/kv-storage/inbox.tsx new file mode 100644 index 00000000..3493d9c2 --- /dev/null +++ b/kv-storage/inbox.tsx @@ -0,0 +1,32 @@ +import { storeKv } from './kv-storage'; + +const inboxNumber = async (key: string, value?: number) => { + const loaded = await storeKv( + key, + value === undefined ? undefined : String(value)); + + if (loaded === undefined || loaded === null) { + return 0; + } + + const loadedInt = parseInt(loaded); + + if (isNaN(loadedInt)) { + return 0; + } + + return loadedInt; +}; + +const inboxOrder = async (value?: number) => { + return await inboxNumber('inbox_order', value); +}; + +const inboxSection = async (value?: number) => { + return await inboxNumber('inbox_section', value); +}; + +export { + inboxOrder, + inboxSection, +}