Skip to content

Commit 0917942

Browse files
committed
updated with HuddleStore changes
1 parent 39d771f commit 0917942

File tree

5 files changed

+84
-45
lines changed

5 files changed

+84
-45
lines changed

kotlin_client/src/main/java/com/huddle01/kotlin_client/HuddleClient.kt

+35-24
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.huddle01.kotlin_client
22

33
import RequestOuterClass.Request
44
import android.content.Context
5-
import com.huddle01.kotlin_client.core.LocalPeer
65
import com.huddle01.kotlin_client.core.Room
6+
import com.huddle01.kotlin_client.core.LocalPeer
77
import com.huddle01.kotlin_client.core.Socket
88
import com.huddle01.kotlin_client.models.enum_class.ConnectionState
99
import com.huddle01.kotlin_client.models.enum_class.RoomStates
@@ -79,7 +79,7 @@ class HuddleClient(projectId: String, context: Context) {
7979

8080
init {
8181
enableLogs()
82-
initSDKConnection(application = context.applicationContext as android.app.Application)
82+
initSDKConnection(application = context.applicationContext as android.app.Application)
8383
Timber.i("✅ Initializing HuddleClient")
8484
this.projectId = projectId
8585
_socket = Socket.getInstance()
@@ -107,41 +107,52 @@ class HuddleClient(projectId: String, context: Context) {
107107
* This method connects to socket, creates a room, and then connects to the room
108108
*/
109109
suspend fun joinRoom(roomId: String, token: String): Room {
110-
Timber.i("Join the room with roomId: $roomId")
110+
when {
111+
localPeer.store.roomInfo.value?.connectionState?.let { it != RoomStates.IDLE } == true -> {
112+
Timber.i("❌ Room state is not IDLE")
113+
return room
114+
}
111115

112-
if (socket.connectionState == ConnectionState.CONNECTING) {
113-
Timber.w("Socket is already connecting, waiting for the connection to be established")
114-
return room
115-
}
116+
socket.connectionState == ConnectionState.CONNECTING -> {
117+
Timber.w("Socket is already connecting, waiting for connection")
118+
return room
119+
}
116120

117-
if (room.state == RoomStates.CONNECTING) {
118-
Timber.w("🔔 Room join already in progress")
119-
return room
120-
}
121+
localPeer.store.roomInfo.value?.connectionState == RoomStates.CONNECTING -> {
122+
Timber.w("🔔 Room join already in progress")
123+
return room
124+
}
121125

122-
if (localPeer.joined) {
123-
Timber.w("Already joined the room")
124-
return room
126+
localPeer.joined -> {
127+
Timber.w("Already joined the room")
128+
return room
129+
}
125130
}
131+
132+
Timber.i("Joining room: $roomId")
133+
126134
return try {
127135
socket.connect(token)
128-
Timber.i("✅ Socket Connection Established")
129-
room.roomId = roomId
130-
Timber.i("🚪 Room Id => ${room.roomId}")
131-
val room: Room = this.room.connect()
132-
Timber.i("🚪 Room Connection Established")
133-
room
136+
Timber.i("✅ Socket connection established")
137+
138+
room.apply {
139+
this.roomId = roomId
140+
Timber.i("🚪 Room ID set: $roomId")
141+
}
142+
143+
room.connect().also {
144+
Timber.i("🚪 Room connection established")
145+
}
134146
} catch (error: Throwable) {
135-
Timber.e("🔴 Error While Joining the Room => $error")
147+
Timber.e("🔴 Error joining room: ${error.message}", error)
136148
throw error
137149
}
138150
}
139151

140-
141152
/**
142153
* Leave the room and disconnect from the socket
143154
*/
144-
suspend fun leaveRoom() {
155+
fun leaveRoom() {
145156
Timber.i("Leaving the room")
146157
socket.close(ESocketCloseCode.NORMAL_CLOSURE.value, null)
147158
}
@@ -158,7 +169,7 @@ class HuddleClient(projectId: String, context: Context) {
158169
Timber.plant(Timber.DebugTree())
159170
}
160171

161-
private fun initSDKConnection(application: android.app.Application) {
172+
private fun initSDKConnection(application: android.app.Application) {
162173
MediasoupClient.initialize(
163174
context = application,
164175
logHandler = object : LogHandler {

kotlin_client/src/main/java/com/huddle01/kotlin_client/core/LocalPeer.kt

+18-15
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import android.os.Handler
2929
import android.os.Looper
3030
import com.google.gson.Gson
3131
import com.google.gson.reflect.TypeToken
32+
import com.huddle01.kotlin_client.models.SendData
3233
import com.huddle01.kotlin_client.common.EnhancedMap
3334
import com.huddle01.kotlin_client.common.ProtoParsing
3435
import com.huddle01.kotlin_client.constants.maxDataMessageSize
3536
import com.huddle01.kotlin_client.live_data.store.HuddleStore
3637
import com.huddle01.kotlin_client.models.ProduceSources
3738
import com.huddle01.kotlin_client.models.RoomConfig
3839
import com.huddle01.kotlin_client.models.RoomStats
39-
import com.huddle01.kotlin_client.models.SendData
4040
import com.huddle01.kotlin_client.models.enum_class.RoomStates
4141
import com.huddle01.kotlin_client.types.HandlerEvents
4242
import com.huddle01.kotlin_client.types.TransportType
@@ -173,6 +173,8 @@ class LocalPeer(
173173
private val activeAudioTrack: MutableMap<String, AudioTrack> = mutableMapOf()
174174
private val activeVideoTrack: MutableMap<String, VideoTrack> = mutableMapOf()
175175

176+
// room store
177+
val store: HuddleStore = HuddleStore.getInstance()
176178

177179
/** Returns the room instance
178180
*/
@@ -365,6 +367,7 @@ class LocalPeer(
365367
}
366368
}
367369

370+
368371
/**
369372
* Stops the underlying producing of a camera stream, stops the local track, and closes the producer.
370373
* NOTE: This will notify all the RemotePeers that this producer has stopped producing and they should stop consuming it.
@@ -612,12 +615,12 @@ class LocalPeer(
612615
fun changeCam() {
613616
localVideoManager?.switchCamera(object : CameraSwitchHandler {
614617
override fun onCameraSwitchDone(b: Boolean) {
615-
HuddleStore.setCamInProgress(false)
618+
store.setCamInProgress(false)
616619
}
617620

618621
override fun onCameraSwitchError(s: String) {
619622
Timber.w("❌ Error Enabling Video $s")
620-
HuddleStore.setCamInProgress(false)
623+
store.setCamInProgress(false)
621624
}
622625
})
623626
}
@@ -860,9 +863,9 @@ class LocalPeer(
860863

861864
CoroutineScope(Dispatchers.Main).launch {
862865
// store me
863-
HuddleStore.setMe(helloResponse.peerId, helloResponse.role)
866+
store.setMe(helloResponse.peerId, helloResponse.role)
864867
// store roomId
865-
HuddleStore.setRoomId(helloResponse.roomId)
868+
store.setRoomId(helloResponse.roomId)
866869
}
867870

868871
this.peerId = helloResponse.peerId
@@ -930,7 +933,7 @@ class LocalPeer(
930933

931934
// store setRoomState
932935
CoroutineScope(Dispatchers.Main).launch {
933-
HuddleStore.setRoomState(RoomStates.CONNECTED)
936+
store.setRoomState(RoomStates.CONNECTED)
934937
}
935938
emit(
936939
"device-created", mapOf(
@@ -1113,7 +1116,7 @@ class LocalPeer(
11131116
put("role", role)
11141117
}
11151118
CoroutineScope(Dispatchers.Main).launch {
1116-
HuddleStore.addPeer(peerId, peersData)
1119+
store.addPeer(peerId, peersData)
11171120
}
11181121
val remotePeer = room.getRemotePeerById(peerId)
11191122
if (_recvTransport == null) {
@@ -1164,8 +1167,8 @@ class LocalPeer(
11641167
Timber.w("onTransportClose for consume")
11651168
// store for removeConsumer
11661169
CoroutineScope(Dispatchers.Main).launch {
1167-
HuddleStore.removeConsumer(consumeResponse.producerPeerId)
1168-
HuddleStore.me.value?.myConsumedTracks?.remove(consumeResponse.producerPeerId)
1170+
store.removeConsumer(consumeResponse.producerPeerId)
1171+
store.me.value?.myConsumedTracks?.remove(consumeResponse.producerPeerId)
11691172
}
11701173
}
11711174
},
@@ -1179,11 +1182,11 @@ class LocalPeer(
11791182
if (consumer != null) {
11801183
CoroutineScope(Dispatchers.Main).launch {
11811184
if (consumer.kind == "video") {
1182-
HuddleStore.setMyConsumedTracks(
1185+
store.setMyConsumedTracks(
11831186
consumeResponse.producerPeerId, consumer.track
11841187
)
11851188
}
1186-
HuddleStore.addConsumer(consumeResponse.producerPeerId, consumer)
1189+
store.addConsumer(consumeResponse.producerPeerId, consumer)
11871190
}
11881191
}
11891192
socket.publish(
@@ -1350,7 +1353,7 @@ class LocalPeer(
13501353
put("role", role)
13511354
}
13521355
CoroutineScope(Dispatchers.Main).launch {
1353-
HuddleStore.addPeer(newPeerId, peersData)
1356+
store.addPeer(newPeerId, peersData)
13541357
}
13551358
val remotePeer = RemotePeer(
13561359
peerId = peerId, role = role
@@ -1549,7 +1552,7 @@ class LocalPeer(
15491552

15501553
CoroutineScope(Dispatchers.Main).launch {
15511554
// store for removePeer
1552-
HuddleStore.removePeer(peerId)
1555+
store.removePeer(peerId)
15531556
}
15541557
val remotePeer = room.getRemotePeerById(peerId)
15551558
val labels = remotePeer.labels
@@ -1776,7 +1779,7 @@ class LocalPeer(
17761779
// store for removePeer
17771780
peer.producersList.orEmpty().forEach { producer ->
17781781
// store for addPeer
1779-
HuddleStore.addPeer(peerId, JSONObject().apply {
1782+
store.addPeer(peerId, JSONObject().apply {
17801783
put("peerId", peerId)
17811784
put("role", peer.role)
17821785
})
@@ -1812,7 +1815,7 @@ class LocalPeer(
18121815
consumers.delete(label, peerId)
18131816
CoroutineScope(Dispatchers.Main).launch {
18141817
// store for removePeer
1815-
HuddleStore.removePeer(peerId)
1818+
store.removePeer(peerId)
18161819
}
18171820
}
18181821
if (label == "video") camCapturer?.stopCapture()

kotlin_client/src/main/java/com/huddle01/kotlin_client/core/Room.kt

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.huddle01.kotlin_client.core
22

33
import RequestOuterClass.Request
44
import ResponseOuterClass
5+
import com.huddle01.kotlin_client.live_data.store.HuddleStore
56
import com.huddle01.kotlin_client.models.LobbyPeer
67
import com.huddle01.kotlin_client.models.RoomConfig
78
import com.huddle01.kotlin_client.models.RoomStats
@@ -202,6 +203,8 @@ class Room(
202203
return _metadata?.let { JsonUtils.toJsonObject(it) }
203204
}
204205

206+
private val store: HuddleStore = HuddleStore.getInstance()
207+
205208
/**
206209
* Update Metadata of the room
207210
* @throws { Error } If Request Failed to Update Metadata
@@ -353,6 +356,7 @@ class Room(
353356
Timber.i("🔔 Connecting to the room")
354357
socket.publish(Request.RequestCase.CONNECT_ROOM, mapOf("roomId" to roomId as String))
355358
state = RoomStates.CONNECTING
359+
store.setRoomState(RoomStates.CONNECTING)
356360
emit("room-connecting")
357361
return roomInstance
358362
}

kotlin_client/src/main/java/com/huddle01/kotlin_client/core/Socket.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class Socket : EventEmitter() {
104104
*/
105105
var token: String? = null
106106

107+
private val store: HuddleStore = HuddleStore.getInstance()
108+
107109
/**
108110
* Returns the underlying WebSocket connection, throws an error if the connection is not initialized
109111
*/
@@ -281,13 +283,13 @@ class Socket : EventEmitter() {
281283
_endpoint = null
282284

283285
emit("token-updated")
286+
store.setRoomState(RoomStates.CLOSED)
284287

285288
connectionState = ConnectionState.CLOSED
286289

287290
emit("closed", code)
288291

289292
Timber.i("🔌 WebSocket Connection closed")
290-
HuddleStore.setRoomState(RoomStates.CLOSED)
291293

292294
return
293295
}
@@ -539,6 +541,7 @@ class Socket : EventEmitter() {
539541
Timber.i("🔔 Socket connection closed emitted")
540542
connectionState = ConnectionState.CLOSED
541543
this.close(code, reason)
544+
store.setRoomState(RoomStates.IDLE)
542545
}
543546

544547
/**

kotlin_client/src/main/java/com/huddle01/kotlin_client/live_data/store/HuddleStore.kt

+23-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,36 @@ import com.huddle01.kotlin_client.live_data.store.models.Peers
77
import com.huddle01.kotlin_client.live_data.store.models.RoomInfo
88
import com.huddle01.kotlin_client.models.enum_class.RoomStates
99
import io.github.crow_misia.mediasoup.Consumer
10-
import kotlinx.coroutines.delay
11-
import kotlinx.coroutines.runBlocking
1210
import org.json.JSONObject
1311
import org.webrtc.MediaStreamTrack
1412

15-
object HuddleStore : ViewModel() {
13+
class HuddleStore : ViewModel() {
14+
1615
val roomInfo = SupplierMutableLiveData { RoomInfo() }
1716
val me =
1817
SupplierMutableLiveData { Me() }
1918
val peers = SupplierMutableLiveData { Peers() }
2019

20+
companion object {
21+
22+
private var instance: HuddleStore? = null
23+
24+
fun getInstance(): HuddleStore {
25+
return instance ?: synchronized(this) {
26+
instance ?: HuddleStore().also { instance = it }
27+
}
28+
}
29+
30+
fun destroy() {
31+
instance?.let {
32+
it.me.postValue { Me() }
33+
it.peers.postValue { Peers() }
34+
it.roomInfo.postValue { RoomInfo() }
35+
instance = null
36+
}
37+
}
38+
}
39+
2140
fun setRoomId(roomId: String) {
2241
roomInfo.postValue {
2342
it.roomId = roomId
@@ -29,8 +48,7 @@ object HuddleStore : ViewModel() {
2948
if (RoomStates.CLOSED == state) {
3049
peers.postValue { it.clear() }
3150
me.postValue { it.clear() }
32-
runBlocking { delay(1500) }
33-
roomInfo.postValue { it.connectionState = RoomStates.IDLE }
51+
destroy()
3452
}
3553
}
3654

0 commit comments

Comments
 (0)