From 72cc4442605908fedfd1504f3bdada3a772eacfd Mon Sep 17 00:00:00 2001 From: Brandon Dyer Date: Fri, 17 Sep 2021 16:39:28 -0500 Subject: [PATCH] Functional save button --- global.d.ts | 7 ++++++- screens/ModalScreen.tsx | 14 ++++++++++++- services/StorageService.ts | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/global.d.ts b/global.d.ts index f968826..d4cbcac 100644 --- a/global.d.ts +++ b/global.d.ts @@ -23,7 +23,7 @@ interface User { interface Paged { items: T[]; - next_page: string; + next_page: string | null; } interface Post { @@ -94,4 +94,9 @@ interface Reply { score: number; } +interface SavedContentId { + id: number; + type: "post" | "reply"; +} + type Refreshable = [T, boolean, () => void]; diff --git a/screens/ModalScreen.tsx b/screens/ModalScreen.tsx index 0f2f8e7..a8f58d4 100644 --- a/screens/ModalScreen.tsx +++ b/screens/ModalScreen.tsx @@ -16,6 +16,7 @@ import { useReplies } from "../hooks/lotide"; import useTheme from "../hooks/useTheme"; import { RootStackScreenProps } from "../types"; import LotideContext from "../store/LotideContext"; +import { savedContentIds } from "../services/StorageService"; export default function ModalScreen({ navigation, @@ -48,7 +49,18 @@ export default function ModalScreen({ showCommunityHost /> - + { + savedContentIds + .store(ctx, post.id, "post") + .then(async () => + console.log(await savedContentIds.query(ctx)), + ); + }} + > + + { diff --git a/services/StorageService.ts b/services/StorageService.ts index 0baef2e..2fd97ab 100644 --- a/services/StorageService.ts +++ b/services/StorageService.ts @@ -32,6 +32,38 @@ export const lotideContextKV = { }, }; +export const savedContentIds = { + async store(ctx: LotideContext, id: number, type: "post" | "reply") { + const [pageId, store] = await this.query(ctx); + store.items = [{ id, type }, ...store.items]; + if (store.items.length > 3) { + store.next_page = Math.random().toString(16).substr(2, 16); + serviceKV.store("@content_id_current_pages", ctx.apiUrl, store.next_page); + } + AsyncStorage.setItem(`@content_id_page[${pageId}]`, JSON.stringify(store)); + }, + + async query( + ctx: LotideContext, + page?: string, + ): Promise<[string, Paged]> { + const pageId = page || (await this.getCurrentPage(ctx)); + const storeStr = await AsyncStorage.getItem(`@content_id_page[${pageId}]`); + const store = storeStr + ? JSON.parse(storeStr) + : { items: [], next_page: null }; + return [pageId, store]; + }, + + async getCurrentPage(ctx: LotideContext): Promise { + return await serviceKV.queryOrStore( + "@content_id_current_pages", + ctx.apiUrl, + () => Math.random().toString(16).substr(2, 16), + ); + }, +}; + const serviceKV = { async store(path: string, k: string, v: T) { const storeStr = await AsyncStorage.getItem(path); @@ -45,6 +77,16 @@ const serviceKV = { return storeStr ? JSON.parse(storeStr)[k] : undefined; }, + async queryOrStore(path: string, k: string, v: () => T): Promise { + const value = await this.query(path, k); + if (value === undefined) { + this.store(path, k, v()); + return v(); + } else { + return value; + } + }, + async listKeys(path: string): Promise { const storeStr = await AsyncStorage.getItem(path); const store = storeStr ? JSON.parse(storeStr) : {};