loading more of conversation

This commit is contained in:
balzack 2023-03-05 15:51:44 -08:00
parent ed0a4df44f
commit a655313b12
6 changed files with 41 additions and 5 deletions

View File

@ -225,7 +225,7 @@ export function useChannelContext() {
}, },
setTopicMarker: async (channelId, marker) => { setTopicMarker: async (channelId, marker) => {
const { guid } = access.current || {}; const { guid } = access.current || {};
await store.actions.setChannelItemTopicMarker(guid, channelId, revision); await store.actions.setChannelItemTopicMarker(guid, channelId, marker);
setChannelField(channelId, 'topicMarker', marker); setChannelField(channelId, 'topicMarker', marker);
}, },
setMarkerAndSync: async (channelId, marker, revision) => { setMarkerAndSync: async (channelId, marker, revision) => {

View File

@ -93,7 +93,7 @@ export function useConversationContext() {
curSyncRevision.current = delta.revision; curSyncRevision.current = delta.revision;
updateState({ loaded: true, offsync: false, topics: topics.current, card: cardValue, channel: channelValue }); updateState({ loaded: true, offsync: false, topics: topics.current, card: cardValue, channel: channelValue });
} }
else if (loadMore && marker) { else if (loadMore) {
const delta = await getTopicDelta(cardId, channelId, null, COUNT, null, curTopicMarker.current); const delta = await getTopicDelta(cardId, channelId, null, COUNT, null, curTopicMarker.current);
const marker = delta.marker ? delta.marker : 1; const marker = delta.marker ? delta.marker : 1;
await setTopicDelta(cardId, channelId, delta.topics); await setTopicDelta(cardId, channelId, delta.topics);
@ -333,9 +333,9 @@ export function useConversationContext() {
const { cardId, channelId } = conversationId.current || {}; const { cardId, channelId } = conversationId.current || {};
return getTopicAssetUrl(cardId, channelId, topicId, assetId); return getTopicAssetUrl(cardId, channelId, topicId, assetId);
}, },
loadMore: () => { loadMore: async () => {
more.current = true; more.current = true;
sync(); await sync();
}, },
resync: () => { resync: () => {
force.current = true; force.current = true;

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useRef, useContext } from 'react'; import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage"; import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'db_v_126.db'; const DATABAG_DB = 'db_v_131.db';
export function useStoreContext() { export function useStoreContext() {
const [state, setState] = useState({}); const [state, setState] = useState({});

View File

@ -13,6 +13,19 @@ export function Conversation({ navigation, cardId, channelId, closeConversation,
const { state, actions } = useConversation(); const { state, actions } = useConversation();
const loadMore = async () => {
try {
await actions.loadMore();
}
catch (err) {
console.log(err);
Alert.alert(
'Failed to Load More Messages',
'Please try again',
)
}
}
const updateTopic = async () => { const updateTopic = async () => {
try { try {
await actions.updateTopic(); await actions.updateTopic();
@ -77,11 +90,16 @@ export function Conversation({ navigation, cardId, channelId, closeConversation,
<ActivityIndicator color={Colors.grey} size="large" /> <ActivityIndicator color={Colors.grey} size="large" />
</View> </View>
)} )}
{ state.moreBusy && state.topics.length > 32 && (
<ActivityIndicator style={styles.more} color={Colors.primary} />
)}
{ state.loaded && state.topics.length !== 0 && ( { state.loaded && state.topics.length !== 0 && (
<FlatList style={{ ...styles.conversation, transform: [{rotate: '180deg'}]}} <FlatList style={{ ...styles.conversation, transform: [{rotate: '180deg'}]}}
contentContainerStyle={styles.topics} contentContainerStyle={styles.topics}
data={state.topics} data={state.topics}
initialNumToRender={16} initialNumToRender={16}
onEndReached={loadMore}
onEndReachedThreshold={0.1}
renderItem={({item}) => <TopicItem item={item} focused={item.topicId === state.focus} renderItem={({item}) => <TopicItem item={item} focused={item.topicId === state.focus}
focus={() => actions.setFocus(item.topicId)} hosting={state.host == null} focus={() => actions.setFocus(item.topicId)} hosting={state.host == null}
remove={actions.removeTopic} update={actions.editTopic} block={actions.blockTopic} remove={actions.removeTopic} update={actions.editTopic} block={actions.blockTopic}

View File

@ -10,6 +10,9 @@ export const styles = StyleSheet.create({
alignItems: 'center', alignItems: 'center',
flexShrink: 1, flexShrink: 1,
}, },
more: {
marginTop: 8,
},
header: { header: {
display: 'flex', display: 'flex',
flexDirection: 'row', flexDirection: 'row',

View File

@ -20,6 +20,7 @@ export function useConversation() {
editMessage: null, editMessage: null,
editData: null, editData: null,
updateBusy: false, updateBusy: false,
moreBusy: false,
}); });
const updateState = (value) => { const updateState = (value) => {
@ -143,6 +144,20 @@ export function useConversation() {
removeTopic: async (topicId) => { removeTopic: async (topicId) => {
await conversation.actions.removeTopic(topicId); await conversation.actions.removeTopic(topicId);
}, },
loadMore: async () => {
if (!state.moreBusy) {
try {
updateState({ moreBusy: true });
await conversation.actions.loadMore();
updateState({ moreBusy: false });
}
catch(err) {
console.log(err);
updateState({ moreBusy: false });
throw new Error("failed to load more");
}
}
},
}; };
return { state, actions }; return { state, actions };