Skip to content

Commit 3c1e9c2

Browse files
committed
chore: simplify friendship protocol, direct messaging, and fix stability issues
1 parent df2b2f9 commit 3c1e9c2

21 files changed

Lines changed: 340 additions & 378 deletions
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package com.p2pchat
22

3+
import android.os.Bundle
4+
import androidx.core.view.WindowCompat
35
import com.facebook.react.ReactActivity
46
import com.facebook.react.ReactActivityDelegate
57
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
68
import com.facebook.react.defaults.DefaultReactActivityDelegate
79

810
class MainActivity : ReactActivity() {
911

10-
/**
11-
* Returns the name of the main component registered from JavaScript. This is used to schedule
12-
* rendering of the component.
13-
*/
14-
override fun getMainComponentName(): String = "P2PChat"
12+
override fun getMainComponentName(): String = "p2p-chat"
13+
14+
override fun onCreate(savedInstanceState: Bundle?) {
15+
super.onCreate(savedInstanceState)
16+
WindowCompat.setDecorFitsSystemWindows(window, true)
17+
}
1518

16-
/**
17-
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
18-
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
19-
*/
2019
override fun createReactActivityDelegate(): ReactActivityDelegate =
2120
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
2221
}

apps/p2p-chat/src/App.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ const App = () => {
2121
} = useChatInstances()
2222

2323
const sandboxRefs = useRef<Record<string, any>>({})
24+
const chatInstancesRef = useRef(chatInstances)
25+
chatInstancesRef.current = chatInstances
2426

25-
const messageHandler = new MessageHandler(
26-
chatInstances,
27-
friendshipManager,
28-
sandboxRefs,
29-
triggerFriendshipUpdate
30-
)
27+
const messageHandler = useRef(
28+
new MessageHandler(
29+
() => chatInstancesRef.current,
30+
friendshipManager,
31+
sandboxRefs,
32+
triggerFriendshipUpdate
33+
)
34+
).current
3135

32-
// Use the scroll color interpolation hook
3336
const {currentBackgroundColor, onScroll} = useScrollColorInterpolation({
3437
chatInstances,
35-
scrollStep: screenWidth, // Now each slide takes full screen width
38+
scrollStep: screenWidth,
3639
onIndexChange: setCurrentIndex,
3740
})
3841

@@ -45,7 +48,6 @@ const App = () => {
4548
<StatusBar
4649
barStyle="light-content"
4750
backgroundColor={currentBackgroundColor}
48-
translucent
4951
/>
5052

5153
<ChatCarousel

apps/p2p-chat/src/ChatApp.tsx

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, {useCallback, useState} from 'react'
2-
import {LogBox, StatusBar, View} from 'react-native'
1+
import React, {useCallback} from 'react'
2+
import {LogBox, View} from 'react-native'
33

44
import {
55
FriendRequestsList,
@@ -16,15 +16,12 @@ import {
1616
import {commonStyles} from './styles'
1717
import {ChatAppProps, MessageData} from './types'
1818

19-
// Utility function to create subtle background from vibrant color
2019
const createSubtleBackground = (vibrantColor: string): string => {
21-
// Convert hex to RGB
2220
const hex = vibrantColor.replace('#', '')
2321
const r = parseInt(hex.substr(0, 2), 16)
2422
const g = parseInt(hex.substr(2, 2), 16)
2523
const b = parseInt(hex.substr(4, 2), 16)
2624

27-
// Create a very light version (90% white + 10% color)
2825
const lightR = Math.round(255 * 0.9 + r * 0.1)
2926
const lightG = Math.round(255 * 0.9 + g * 0.1)
3027
const lightB = Math.round(255 * 0.9 + b * 0.1)
@@ -37,12 +34,23 @@ LogBox.ignoreAllLogs()
3734
const ChatApp: React.FC<ChatAppProps> = ({
3835
userId,
3936
userName,
40-
targetOptions,
41-
potentialFriends,
42-
pendingRequests,
37+
targetOptions: initialTargetOptions,
38+
potentialFriends: initialPotentialFriends,
4339
backgroundColor,
4440
}) => {
45-
const [, setIsConnected] = useState(false)
41+
const {
42+
pendingRequests,
43+
targetOptions,
44+
potentialFriends,
45+
respondToFriendRequest,
46+
handleFriendMessage,
47+
sendFriendRequest,
48+
} = useFriendRequests({
49+
userName,
50+
initialTargetOptions,
51+
initialPotentialFriends,
52+
onSendMessage: (msg: MessageData) => sendMessage(msg),
53+
})
4654

4755
const {sendMessage, sendConnectionMessage} = useCommunication({
4856
userName,
@@ -71,34 +79,18 @@ const ChatApp: React.FC<ChatAppProps> = ({
7179
} = useMessages({
7280
userId,
7381
userName,
74-
onSendMessage: (message: MessageData) => {
75-
const success = sendMessage(message)
76-
if (success) {
77-
setIsConnected(true)
78-
}
79-
return success
80-
},
82+
onSendMessage: (message: MessageData, targetOrigin?: string) =>
83+
sendMessage(message, targetOrigin),
8184
})
8285

83-
const {respondToFriendRequest, handleFriendMessage, sendFriendRequest} =
84-
useFriendRequests({
85-
userName,
86-
onSendMessage: sendMessage,
87-
})
88-
8986
function handleIncomingMessage(data: MessageData) {
9087
console.log(`[${userName}] Processing message type: ${data.type}`)
91-
9288
handleMessageIncoming(data)
9389
handleFriendMessage(data)
94-
if (
95-
data.type === 'chat_message' ||
96-
data.type === 'connection_established'
97-
) {
98-
setIsConnected(true)
99-
}
10090
}
10191

92+
const isSelectedTargetFriend = targetOptions.includes(selectedTarget)
93+
10294
const handleSendMessage = useCallback(() => {
10395
sendChatMessage(selectedTarget)
10496
}, [sendChatMessage, selectedTarget])
@@ -121,8 +113,6 @@ const ChatApp: React.FC<ChatAppProps> = ({
121113

122114
return (
123115
<View style={[commonStyles.container, {backgroundColor: subtleBackground}]}>
124-
<StatusBar backgroundColor={backgroundColor} barStyle="light-content" />
125-
126116
<FriendRequestsList
127117
pendingRequests={pendingRequests}
128118
onRespondToRequest={respondToFriendRequest}
@@ -141,6 +131,7 @@ const ChatApp: React.FC<ChatAppProps> = ({
141131
<MessageInput
142132
inputText={inputText}
143133
selectedTarget={selectedTarget}
134+
isFriend={isSelectedTargetFriend}
144135
onInputChange={setInputText}
145136
onSendMessage={handleSendMessage}
146137
/>

0 commit comments

Comments
 (0)