Skip to content

Commit

Permalink
Store inbox tab settings in local storage (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
duogenesis authored Nov 21, 2023
1 parent 152f74d commit f602398
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
23 changes: 20 additions & 3 deletions components/inbox-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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(
Expand All @@ -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]);
Expand Down
32 changes: 32 additions & 0 deletions kv-storage/inbox.tsx
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit f602398

Please sign in to comment.