From 40a29e17c33d6c3868bbddcdc000616b582a29ec Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Thu, 29 Sep 2022 16:00:48 -0700 Subject: [PATCH] removing delta methods from store --- app/mobile/src/context/useCardContext.hook.js | 29 +++----- .../src/context/useChannelContext.hook.js | 8 +-- .../context/useConversationContext.hook.js | 67 ++++++++++--------- .../src/context/useStoreContext.hook.js | 28 ++------ app/mobile/src/session/profile/Profile.jsx | 2 - 5 files changed, 52 insertions(+), 82 deletions(-) diff --git a/app/mobile/src/context/useCardContext.hook.js b/app/mobile/src/context/useCardContext.hook.js index 3c1ce56e..613a90c5 100644 --- a/app/mobile/src/context/useCardContext.hook.js +++ b/app/mobile/src/context/useCardContext.hook.js @@ -440,13 +440,9 @@ export function useCardContext() { const { guid } = session.current; return await store.actions.getCardChannelTopicItems(guid, cardId, channelId); }, - getChannelTopicDeltaItems: async (cardId, channelId, revision) => { + setChannelTopicItem: async (cardId, channelId, topicId, topic) => { const { guid } = session.current; - return await store.actions.getCardChannelTopicDeltaItems(guid, cardId, channelId, revision); - }, - setChannelTopicItem: async (cardId, channelId, topicId, channelRevision, topic) => { - const { guid } = session.current; - return await store.actions.setCardChannelTopicItem(guid, cardId, channelId, topicId, channelRevision, topic); + return await store.actions.setCardChannelTopicItem(guid, cardId, channelId, topicId, topic); }, clearChannelTopicItem: async (cardId, channelId, topicId) => { const { guid } = session.current; @@ -457,39 +453,32 @@ export function useCardContext() { return await store.actions.clearCardChannelTopicItems(guid, cardId, channelId); }, getChannelTopic: async (cardId, channelId, topicId) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return await getContactChannelTopic(profile.node, detail.token, channelId, topicId); + return await getContactChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId); }, getChannelTopics: async (cardId, channelId, revision) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return await getContactChannelTopics(profile.node, detail.token, channelId, revision); + return await getContactChannelTopics(profile.node, `${profile.guid}.${detail.token}`, channelId, revision); }, getChannelTopicAssetUrl: (cardId, channelId, topicId, assetId) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return getContactChannelTopicAssetUrl(profile.node, detail.token, channelId, topicId, assetId); + return getContactChannelTopicAssetUrl(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId, assetId); }, addChannelTopic: async (cardId, channelId, message, assets) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return await addChannelTopic(profile.node, detail.token, channelId, message, assets); + return await addChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, message, assets); }, setChannelTopicSubject: async (cardId, channelId, topicId, data) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return await setContactChannelTopicSubject(profile.node, detail.token, channelId, topicId, data); + return await setContactChannelTopicSubject(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId, data); }, removeChannel: async (cardId, channelId) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return await removeChannel(profile.node, detail.token, channelId); + return await removeChannel(profile.node, `${profile.guid}.${detail.token}`, channelId); }, removeChannelTopic: async (cardId, channelId, topicId) => { - const { guid } = session.current; const { detail, profile } = getCard(cardId); - return await removeChannelTopic(profile.node, detail.token, channelId, topicId); + return await removeChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId); }, } diff --git a/app/mobile/src/context/useChannelContext.hook.js b/app/mobile/src/context/useChannelContext.hook.js index 0e4c1db6..d82ec815 100644 --- a/app/mobile/src/context/useChannelContext.hook.js +++ b/app/mobile/src/context/useChannelContext.hook.js @@ -184,13 +184,9 @@ export function useChannelContext() { const { guid } = session.current; return await store.actions.getChannelTopicItems(guid, channelId); }, - getTopicDeltaItems: async (channelId, revision) => { + setTopicItem: async (channelId, topicId, topic) => { const { guid } = session.current; - return await store.actions.getChannelTopicDeltaItems(guid, channelId, revision); - }, - setTopicItem: async (channelId, topicId, channelRevision, topic) => { - const { guid } = session.current; - return await store.actions.setChannelTopicItem(guid, channelId, topicId, channelRevision, topic); + return await store.actions.setChannelTopicItem(guid, channelId, topicId, topic); }, clearTopicItem: async (channelId, topicId) => { const { guid } = session.current; diff --git a/app/mobile/src/context/useConversationContext.hook.js b/app/mobile/src/context/useConversationContext.hook.js index 1bd0c243..f5e9da23 100644 --- a/app/mobile/src/context/useConversationContext.hook.js +++ b/app/mobile/src/context/useConversationContext.hook.js @@ -102,42 +102,47 @@ export function useConversationContext() { if (item && (item.revision !== revision.current || item.syncRevision != syncRevision.current)) { syncing.current = true; - // set channel details - if (detailRevision.current != item.detailRevision) { + try { + // set channel details + if (detailRevision.current != item.detailRevision) { + if (curView === setView.current) { + setChannel(item); + detailRevision.current = item.detailRevision; + } + } + + // set channel topics + if (syncRevision.current != item.syncRevision) { + if (syncRevision.current) { + const topics = await getTopicDeltaItems(cardId, channelId); + } + else { + const topics = await getTopicItems(cardId, channelId); + } + if (curView === setView.current) { + syncRevision.current = item.syncRevision; + } + } + + // sync from server to store + if (item.topicRevision !== item.syncRevision) { + const res = await getTopics(cardId, channelId, item.syncRevision) + } + + // update revision if (curView === setView.current) { - setChannel(item); - detailRevision.current = item.detailRevision; + revision.current = item.revision; + //TODO set to synced state } - } - // set channel topics - if (syncRevision.current != item.syncRevision) { - if (syncRevision.current) { - const topics = await getTopicDeltaItems(cardId, channelId); - } - else { - const topics = await getTopicItems(cardId, channelId); - } - if (curView === setView.current) { - syncRevision.current = item.syncRevision; - } + syncing.current = false; + sync(); } - - // sync from server to store - if (item.topicRevision !== item.syncRevision) { - const res = await getTopics(cardId, channelId, item.syncRevision) -res.topics.forEach(topic => { - console.log(topic.data); -}); + catch(err) { + console.log(err); + syncing.current = false; + //TODO set to unsynced state } - - // update revision - if (curView === setView.current) { - revision.current = item.revision; - } - - syncing.current = false; - sync(); } } } diff --git a/app/mobile/src/context/useStoreContext.hook.js b/app/mobile/src/context/useStoreContext.hook.js index 4eada7aa..b494e9ae 100644 --- a/app/mobile/src/context/useStoreContext.hook.js +++ b/app/mobile/src/context/useStoreContext.hook.js @@ -1,7 +1,7 @@ import { useEffect, useState, useRef, useContext } from 'react'; import SQLite from "react-native-sqlite-storage"; -const DATABAG_DB = 'databag_v036.db'; +const DATABAG_DB = 'databag_v037.db'; export function useStoreContext() { const [state, setState] = useState({}); @@ -13,10 +13,10 @@ export function useStoreContext() { const initSession = async (guid) => { await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_${guid} (channel_id text, revision integer, detail_revision integer, topic_revision integer, sync_revision integer, detail text, summary text, offsync integer, read_revision integer, unique(channel_id))`); - await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_topic_${guid} (channel_id text, topic_id text, channel_revision integer, revision integer, detail_revision integer, detail text, unique(channel_id, topic_id))`); + await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_topic_${guid} (channel_id text, topic_id text, revision integer, detail_revision integer, detail text, unique(channel_id, topic_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_${guid} (card_id text, revision integer, detail_revision integer, profile_revision integer, detail text, profile text, notified_view integer, notified_article integer, notified_profile integer, notified_channel integer, offsync integer, blocked integer, unique(card_id))`); await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_${guid} (card_id text, channel_id text, revision integer, detail_revision integer, topic_revision integer, sync_revision integer, detail text, summary text, offsync integer, blocked integer, read_revision integer, unique(card_id, channel_id))`); - await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_topic_${guid} (card_id text, channel_id text, topic_id text, channel_revision integer, revision integer, detail_revision integer, detail text, unique(card_id, channel_id, topic_id))`); + await db.current.executeSql(`CREATE TABLE IF NOT EXISTS card_channel_topic_${guid} (card_id text, channel_id text, topic_id text, revision integer, detail_revision integer, detail text, unique(card_id, channel_id, topic_id))`); } const actions = { @@ -230,16 +230,7 @@ export function useStoreContext() { detail: decodeObject(topic.detail), })); }, - getChannelTopicDeltaItems: async (guid, channelId, revision) => { - const values = await getAppValues(db.current, `SELECT topic_id, revision, detail_revision, detail FROM channel_topic_${guid} WHERE channel_id=? AND channel_revision>?`, [channelId, revision]); - return values.map(topic => ({ - topicId: topic.topic_id, - revision: topic.revision, - detailRevision: topic.detail_revision, - detail: decodeObject(topic.detail), - })); - }, - setChannelTopicItem: async (guid, channelId, topicId, channelRevision, topic) => { + setChannelTopicItem: async (guid, channelId, topicId, topic) => { const { id, revision, data } = channel; await db.current.executeSql(`INSERT OR REPLACE INTO channel_topic_${guid} (channel_id, topic_id, revision, channel_revision, detail_revision, detail) values (?, ?, ?, ?, ?, ?);`, [channelId, id, revision, channelRevision, data.detailRevision, encodeObject(data.topicDetail)]); }, @@ -311,16 +302,7 @@ export function useStoreContext() { detail: decodeObject(topic.detail), })); }, - getCardChannelTopicDeltaItems: async (guid, cardId, channelId, revision) => { - const values = await getAppValues(db.current, `SELECT topic_id, revision, detail_revision, detail FROM card_channel_topic_${guid} WHERE card_id=? AND channel_id=? AND channel_revision>?`, [cardId, channelId, revision]); - return values.map(topic => ({ - topicId: topic.topic_id, - revision: topic.revision, - detailRevision: topic.detail_revision, - detail: decodeObject(topic.detail), - })); - }, - setCardChannelTopicItem: async (guid, cardId, channelId, topicId, channelRevision, topic) => { + setCardChannelTopicItem: async (guid, cardId, channelId, topicId, topic) => { const { id, revision, data } = channel; await db.current.executeSql(`INSERT OR REPLACE INTO channel_topic_${guid} (card_id, channel_id, topic_id, revision, channel_revision, detail_revision, detail) values (?, ?, ?, ?, ?, ?, ?);`, [cardId, channelId, id, revision, channelRevision, data.detailRevision, encodeObject(data.topicDetail)]); }, diff --git a/app/mobile/src/session/profile/Profile.jsx b/app/mobile/src/session/profile/Profile.jsx index c192aa4a..e4584b5b 100644 --- a/app/mobile/src/session/profile/Profile.jsx +++ b/app/mobile/src/session/profile/Profile.jsx @@ -23,8 +23,6 @@ export function Profile() { const { state, actions } = useProfile(); -console.log(state.imageSource); - const setVisible = async (visible) => { try { await actions.setVisible(visible);