support adding basic messages

This commit is contained in:
Roland Osborne 2022-10-06 15:34:29 -07:00
parent c471a76914
commit 2bdd2c4a68
8 changed files with 90 additions and 14 deletions

View File

@ -327,7 +327,7 @@ export function useCardContext() {
await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, topicRevision, summary);
setCardChannelSummary(cardId, channel.id, summary, topicRevision);
}
await store.actions.setCardChannelItemRevision(guid, cardId, channel.revision);
await store.actions.setCardChannelItemRevision(guid, cardId, channel.id, channel.revision);
setCardChannelRevision(cardId, channel.id, channel.revision);
}
}
@ -466,7 +466,13 @@ export function useCardContext() {
},
addChannelTopic: async (cardId, channelId, message, assets) => {
const { detail, profile } = getCard(cardId);
return await addChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, message, assets);
if (assets?.length > 0) {
console.log("UPLOAD");
}
else {
await addContactChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, message, []);
// sync channel
}
},
setChannelTopicSubject: async (cardId, channelId, topicId, data) => {
const { detail, profile } = getCard(cardId);

View File

@ -210,7 +210,13 @@ export function useChannelContext() {
},
addTopic: async (channelId, message, assets) => {
const { server, appToken } = session.current;
return await addChannelTopic(server, appToken, channelId, message, assets);
if (assets?.length) {
console.log("UPLOAD");
}
else {
await addChannelTopic(server, appToken, channelId, message, []);
//sync channels
}
},
setTopicSubject: async (channelId, topicId, data) => {
const { server, appToken } = session.current;

View File

@ -18,6 +18,7 @@ export function useConversationContext() {
const profile = useContext(ProfileContext);
const topics = useRef(null);
const revision = useRef(0);
const force = useRef(false);
const detailRevision = useRef(0);
const syncing = useRef(false);
const conversationId = useRef(null);
@ -107,7 +108,7 @@ export function useConversationContext() {
if (conversationId.current) {
const { cardId, channelId } = conversationId.current;
const channelItem = getChannel(cardId, channelId);
if (channelItem && (channelItem.revision !== revision.current)) {
if (channelItem && (channelItem.revision !== revision.current || force.current)) {
syncing.current = true;
try {
@ -129,7 +130,8 @@ export function useConversationContext() {
}
// sync from server
if (channelItem.topicRevision !== channelItem.syncRevision) {
if (channelItem.topicRevision != channelItem.syncRevision || force.current) {
force.current = false;
const res = await getTopics(cardId, channelId, channelItem.syncRevision)
for (const topic of res.topics) {
if (!topic.data) {
@ -147,7 +149,7 @@ export function useConversationContext() {
topics.current.set(id, { topicId: id, revision: revision, detailRevision: topic.data.detailRevision, detail: topic.data.topicDetail });
}
}
await setSyncRevision(cardId, channelId, channelItem.topicRevision);
await setSyncRevision(cardId, channelId, res.revision);
}
// update revision
@ -299,6 +301,19 @@ export function useConversationContext() {
}
return null;
},
addTopic: async (message, files) => {
if (conversationId.current) {
const { cardId, channelId } = conversationId.current;
if (cardId) {
await card.actions.addChannelTopic(cardId, channelId, message, files);
}
else {
await channel.actions.addTopic(channelId, message, files);
}
force.current = true;
sync();
}
},
}
return { state, actions }

View File

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

View File

@ -72,7 +72,7 @@ export function ConversationBody() {
<View style={styles.latchbar}>
{ !state.latched && (
<TouchableOpacity style={styles.latch} onPress={latch}>
<Ionicons name="downcircleo" size={24} color={Colors.primary} />
<Ionicons name="unlock" size={16} color={Colors.primary} />
</TouchableOpacity>
)}
</View>

View File

@ -80,7 +80,7 @@ export const styles = StyleSheet.create({
},
latchbar: {
position: 'absolute',
top: -16,
top: -26,
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
@ -89,6 +89,9 @@ export const styles = StyleSheet.create({
latch: {
backgroundColor: Colors.formBackground,
borderRadius: 12,
borderWidth: 1,
padding: 4,
borderColor: Colors.primary,
},
})

View File

@ -1,4 +1,4 @@
import { Modal, Image, FlatList, TextInput, Alert, View, TouchableOpacity, Text, } from 'react-native';
import { ActivityIndicator, Modal, Image, FlatList, TextInput, Alert, View, TouchableOpacity, Text, } from 'react-native';
import { useState, useRef } from 'react';
import { useAddTopic } from './useAddTopic.hook';
import { styles } from './AddTopic.styled';
@ -28,6 +28,19 @@ export function AddTopic() {
}
}
const sendMessage = async () => {
try {
await actions.addTopic();
}
catch (err) {
console.log(err);
Alert.alert(
'Failed to Send Message',
'Please try again.',
)
}
}
const addVideo = async () => {
try {
const full = await ImagePicker.openPicker({ mediaType: 'video' });
@ -122,8 +135,16 @@ export function AddTopic() {
<MaterialIcons name="palette-outline" size={20} color={Colors.text} />
</TouchableOpacity>
<View style={styles.space} />
<TouchableOpacity style={styles.addButton}>
<MaterialIcons name="send-outline" size={20} color={Colors.text} />
<TouchableOpacity style={styles.addButton} onPress={sendMessage}>
{ state.busy && (
<ActivityIndicator color={Colors.white} />
)}
{ !state.busy && (state.message || state.assets.length > 0) && (
<MaterialIcons name="send-outline" size={20} color={Colors.text} />
)}
{ !state.busy && !(state.message || state.assets.length > 0) && (
<MaterialIcons name="send-outline" size={20} color={Colors.lightgrey} />
)}
</TouchableOpacity>
</View>
<Modal

View File

@ -11,7 +11,10 @@ export function useAddTopic(cardId, channelId) {
fontSize: false,
fontColor: false,
size: 'medium',
sizeSet: false,
color: Colors.text,
colorSet: false,
busy: false,
});
const assetId = useRef(0);
@ -76,10 +79,32 @@ export function useAddTopic(cardId, channelId) {
updateState({ fontSize: false });
},
setFontSize: (size) => {
updateState({ size });
updateState({ size, sizeSet: true });
},
setFontColor: (color) => {
updateState({ color });
updateState({ color, colorSet: true });
},
addTopic: async () => {
if (!state.busy) {
try {
updateState({ busy: true });
let message = {
text: state.message,
textColor: state.colorSet ? state.color : null,
textSize: state.sizeSet ? state.size : null,
};
await conversation.actions.addTopic(message, state.assets);
updateState({ busy: false, assets: [], message: null,
size: 'medium', sizeSet: false,
color: Colors.text, colorSet: false,
});
}
catch(err) {
console.log(err);
updateState({ busy: false });
throw new Error("failed to add message");
}
}
},
};