sycing cards

This commit is contained in:
Roland Osborne 2022-09-15 15:12:06 -07:00
parent 160fa45373
commit 630d8a99bb
3 changed files with 117 additions and 45 deletions

View File

@ -28,7 +28,6 @@ export function useCardContext() {
// get and store
const delta = await getCards(server, appToken, setRevision.current);
console.log("DELTA:", delta);
for (let card of delta) {
if (card.data) {
@ -36,42 +35,53 @@ console.log("DELTA:", delta);
await store.actions.setCardItem(guid, card);
}
else {
const { detailRevision, profileRevision, cardDetail, cardProfile, notifiedView, notifiedArticle, notifiedProfile, notifiedChannel } = channel.data;
const view = await store.actions.getCardItemView(guid, card.id);
if (view.detailRevision != detailRevision) {
const detail = await getCardDetail(server, appToken, card.id);
await store.actions.setCardItemDetail(guid, card.id, detailRevision, detail);
}
if (view.profileRevision != profileRevision) {
const profile = await getCardProfile(server, appToken, card.id);
await store.actions.setCardItemProfile(guid, card.id, profileRevision, profile);
}
if (view.notifiedView != notifiedView) {
// TODO clear contact and channels
// TODO get articles
await store.actions.setCardNotifiedArticle(guid, card.id, notifiedArticle);
// TODO get channels
await store.actions.setCardNotifiedChannel(guid, card.id, notifiedChannel);
await store.actions.setCardNotifiedView(guid, card.id, notifiedView);
if (view == null) {
console.log('alert: expected card not synced');
let assembled = JSON.parse(JSON.stringify(card));
assembled.data.cardDetail = await getCardDetail(server, appToken, card.id);
assembled.data.cardProfile = await getCardProfile(server, appToken, card.id);
await store.actions.setCardItem(guid, assembled);
}
else {
if (view.notifiedArticle != notifiedArticle) {
// TODO get article delta
await store.actions.setCardNotifiedArticle(guid, card.id, notifiedArticle);
if (view.detailRevision != detailRevision) {
const detail = await getCardDetail(server, appToken, card.id);
await store.actions.setCardItemDetail(guid, card.id, detailRevision, detail);
}
if (view.notifiedChannel != notifiedChannel) {
// TODO get channel delta
await store.actions.setCardNotifiedChannel(guid, card.id, notifiedChannel);
if (view.profileRevision != profileRevision) {
const profile = await getCardProfile(server, appToken, card.id);
await store.actions.setCardItemProfile(guid, card.id, profileRevision, profile);
}
}
if (view.notifiedProflile != notifiedProfile) {
// TODO update contact profile
await store.actions.setCardNotifiedProfile(guid, card.id, notifiedProfile);
}
const status = await store.actions.getCardItemStatus(guid, card.id);
if (status.detail.status === 'connected') {
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
if (status.notifiedView !== notifiedView) {
// TODO clear contact and channels
// TODO get articles
await store.actions.setCardItemNotifiedArticle(guid, card.id, notifiedArticle);
// TODO get channels
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
}
else {
if (status.notifiedChannel != notifiedChannel) {
// TODO get channel delta
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
}
}
if (status.notifiedProflile != notifiedProfile) {
// TODO update contact profile if different
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
}
}
}
else {
//TODO clear card channel topics
await store.actions.clearCardChannelItems(guid, card.id);
await store.actions.clearCardItem(guid, card.id);
}
}

View File

@ -36,15 +36,24 @@ export function useChannelContext() {
else {
const { detailRevision, topicRevision, channelDetail, channelSummary } = channel.data;
const view = await store.actions.getChannelItemView(guid, channel.id);
if (view.detailRevision != detailRevision) {
const detail = await getChannelDetail(server, appToken, channel.id);
await store.actions.setChannelItemDetail(guid, channel.id, detailRevision, detail);
if (view == null) {
console.log('alert: expected channel not synced');
let assembled = JSON.parse(JSON.stringify(channel));
assembled.data.channelDetail = await getChannelDetail(server, appToken, channel.id);
assembled.data.channelSummary = await getChannelSummary(server, appToken, channel.id);
await store.actions.setChannelItem(guid, assembled);
}
if (view.topicRevision != topicRevision) {
const summary = await getChannelSummary(server, appToken, channel.id);
await store.actions.setChannelItemSummary(guid, channel.id, topicRevision, summary);
else {
if (view.detailRevision != detailRevision) {
const detail = await getChannelDetail(server, appToken, channel.id);
await store.actions.setChannelItemDetail(guid, channel.id, detailRevision, detail);
}
if (view.topicRevision != topicRevision) {
const summary = await getChannelSummary(server, appToken, channel.id);
await store.actions.setChannelItemSummary(guid, channel.id, topicRevision, summary);
}
await store.actions.setChannelItemRevision(guid, channel.revision);
}
await store.actions.setChannelItemRevision(guid, channel.revision);
}
}
else {

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'databag_v015.db';
const DATABAG_DB = 'databag_v016.db';
export function useStoreContext() {
const [state, setState] = useState({});
@ -80,7 +80,7 @@ export function useStoreContext() {
},
setCardItem: async (guid, card) => {
const { id, revision, data } = card;
await db.current.executeSql(`INSERT OR REPLACE INTO card_${guid} (card_id, revision, detail_revision, profile_revision, detail, profile, notified_view, notified_profile, notified_article, notified_channel) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, [id, revision, data.detailRevision, data.profileRevision, encodeObject(data.cardDetail), encodeObject(data.cardProfile), data.notifiedView, data.notifiedProfile, data.notifiedArticle, data.notifiedChannel]);
await db.current.executeSql(`INSERT OR REPLACE INTO card_${guid} (card_id, revision, detail_revision, profile_revision, detail, profile, notified_view, notified_profile, notified_article, notified_channel) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`, [id, revision, data.detailRevision, data.profileRevision, encodeObject(data.cardDetail), encodeObject(data.cardProfile), null, null, null, null]);
},
clearCardItem: async (guid, cardId) => {
await db.current.executeSql(`DELETE FROM card_${guid} WHERE card_id=?`, [cardId]);
@ -91,7 +91,7 @@ export function useStoreContext() {
setCardItemNotifiedView: async (guid, cardId, notified) => {
await db.current.executeSql(`UPDATE card_${guid} set notified_view=? where card_id=?`, [notified, cardId]);
},
setCardItemNotifiedArtcile: async (guid, cardId, notified) => {
setCardItemNotifiedArticle: async (guid, cardId, notified) => {
await db.current.executeSql(`UPDATE card_${guid} set notified_article=? where card_id=?`, [notified, cardId]);
},
setCardItemNotifiedProfile: async (guid, cardId, notified) => {
@ -106,19 +106,28 @@ export function useStoreContext() {
setCardItemProfile: async (guid, cardId, revision, profile) => {
await db.current.executeSql(`UPDATE card_${guid} set profile_revision=?, profile=? where card_id=?`, [revision, encodeObject(profile), cardId]);
},
getCardItemView: async (guid, cardId) => {
const values = await getAppValues(db.current, `SELECT revision, detail_revision, profile_revision, notifed_view, notified_article, notifed_profile, notified_channel FROM card_${guid} WHERE card_id=?`, [cardId]);
getCardItemStatus: async (guid, cardId) => {
const values = await getAppValues(db.current, `SELECT detail, notified_view, notified_article, notified_profile, notified_channel FROM card_${guid} WHERE card_id=?`, [cardId]);
if (!values.length) {
return {};
return null;
}
return {
detail: decodeObject(values[0].detail),
notifiedView: values[0].notified_view,
notifiedArticle: values[0].notified_article,
notifiedProfile: values[0].notified_profile,
notifiedChannel: values[0].notified_cahnnel,
};
},
getCardItemView: async (guid, cardId) => {
const values = await getAppValues(db.current, `SELECT revision, detail_revision, profile_revision FROM card_${guid} WHERE card_id=?`, [cardId]);
if (!values.length) {
return null;
}
return {
revision: values[0].revision,
detailRevision: values[0].detail_revision,
profileRevision: values[0].profile_revision,
notifiedView: values[0].notified_view,
notifiedArticle: values[0].notified_article,
notifiedProfile: values[0].notified_profile,
notifiedChannel: values[0].notified_cahnnel,
};
},
getCardItems: async (guid) => {
@ -164,7 +173,7 @@ export function useStoreContext() {
getChannelItemView: async (guid, channelId) => {
const values = await getAppValues(db.current, `SELECT revision, detail_revision, topic_revision FROM channel_${guid} WHERE channel_id=?`, [channelId]);
if (!values.length) {
return {};
return null;
}
return {
revision: values[0].revision,
@ -183,6 +192,50 @@ export function useStoreContext() {
summary: decodeObject(channel.summary),
}));
},
setCardChannelItem: async (guid, cardId, channel) => {
const { id, revision, data } = channel;
await db.current.executeSql(`INSERT OR REPLACE INTO card_channel_${guid} (card_id, channel_id, revision, detail_revision, topic_revision, detail, summary) values (?, ?, ?, ?, ?, ?, ?);`, [cardId, id, revision, data.detailRevision, data.topicRevision, encodeObject(data.channelDetail), encodeObject(data.channelSummary)]);
},
clearCardChannelItem: async (guid, cardId, channelId) => {
await db.current.executeSql(`DELETE FROM card_channel_${guid} WHERE card_id=? and channel_id=?`, [cardId, channelId]);
},
setCardChannelItemRevision: async (guid, cardId, channelId, revision) => {
await db.current.executeSql(`UPDATE card_channel_${guid} set revision=? where card_id=? and channel_id=?`, [revision, cardId, channelId]);
},
setCardChannelItemDetail: async (guid, cardId, channelId, revision, detail) => {
await db.current.executeSql(`UPDATE card_channel_${guid} set detail_revision=?, detail=? where card_id=? and channel_id=?`, [revision, encodeObject(detail), cardId, channelId]);
},
setCardChannelItemSummary: async (guid, cardId, channelId, revision, summary) => {
await db.current.executeSql(`UPDATE card_channel_${guid} set topic_revision=?, summary=? where card_id=? and channel_id=?`, [revision, encodeObject(summary), cardId, channelId]);
},
getCardChannelItemView: async (guid, cardId, channelId) => {
const values = await getAppValues(db.current, `SELECT revision, detail_revision, topic_revision FROM card_channel_${guid} WHERE channel_id=?`, [cardId, channelId]);
if (!values.length) {
return null;
}
return {
revision: values[0].revision,
detailRevision: values[0].detail_revision,
topicRevision: values[0].topic_revision,
};
},
getCardChannelItems: async (guid) => {
const values = await getAppValues(db.current, `SELECT card_id, channel_id, revision, detail_revision, topic_revision, detail, summary FROM card_channel_${guid}`, []);
return values.map(channel => ({
cardId: channel.card_id,
channelId: channel.channel_id,
revision: channel.revision,
detailRevision: channel.detail_revision,
topicRevision: channel.topic_revision,
detail: decodeObject(channel.detail),
summary: decodeObject(channel.summary),
}));
},
clearCardChannelItems: async (guid, cardId) => {
await db.current.executeSql(`DELETE FROM card_channel_${guid} WHERE card_id=?`, [cardID]);
},
}
return { state, actions }
}