feat: Enhance socket service for club management and event handling
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 46s
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 46s
- Implemented club connection management in SocketService to handle joining and leaving clubs. - Added event handling for various real-time updates including participant changes and diary notes. - Updated AppRoot and DiaryDetailScreen to utilize new socket service features for real-time data synchronization. - Introduced member portrait upload functionality in DiaryDetailScreen. - Improved clipboard management across multiple screens for better user experience. - Updated versioning in libs.versions.toml for app version increment. - Refactored navigation icons to use AutoMirrored icons for better compatibility.
This commit is contained in:
@@ -8,45 +8,90 @@ import org.json.JSONObject
|
||||
|
||||
class SocketService(private val socketUrl: String) {
|
||||
private var socket: Socket? = null
|
||||
private var currentClubId: Int? = null
|
||||
|
||||
private val _events = MutableSharedFlow<Pair<String, JSONObject>>()
|
||||
private val _events = MutableSharedFlow<Pair<String, JSONObject>>(extraBufferCapacity = 64)
|
||||
val events = _events.asSharedFlow()
|
||||
|
||||
fun connect(clubId: Int) {
|
||||
val existing = socket
|
||||
if (existing != null) {
|
||||
val previousClubId = currentClubId
|
||||
if (previousClubId != null && previousClubId != clubId) {
|
||||
existing.emit("leave-club", previousClubId)
|
||||
}
|
||||
currentClubId = clubId
|
||||
if (existing.connected()) {
|
||||
existing.emit("join-club", clubId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val options = IO.Options().apply {
|
||||
path = "/socket.io/"
|
||||
transports = arrayOf("polling", "websocket")
|
||||
reconnection = true
|
||||
reconnectionAttempts = Int.MAX_VALUE
|
||||
reconnectionDelay = 1_000
|
||||
reconnectionDelayMax = 5_000
|
||||
timeout = 20_000
|
||||
forceNew = false
|
||||
}
|
||||
|
||||
socket = IO.socket(socketUrl, options)
|
||||
currentClubId = clubId
|
||||
socket = IO.socket(normalizedSocketUrl(), options)
|
||||
|
||||
socket?.on(Socket.EVENT_CONNECT) {
|
||||
println("✅ Connected to Socket.IO")
|
||||
socket?.emit("join-club", clubId)
|
||||
}
|
||||
|
||||
socket?.on("participant:added") { args ->
|
||||
val data = args[0] as JSONObject
|
||||
_events.tryEmit("participant:added" to data)
|
||||
}
|
||||
|
||||
socket?.on("diary:note:added") { args ->
|
||||
val data = args[0] as JSONObject
|
||||
_events.tryEmit("diary:note:added" to data)
|
||||
currentClubId?.let { socket?.emit("join-club", it) }
|
||||
}
|
||||
|
||||
socket?.on("schedule:match:updated") { args ->
|
||||
val data = args[0] as JSONObject
|
||||
_events.tryEmit("schedule:match:updated" to data)
|
||||
socket?.on(Socket.EVENT_DISCONNECT) {
|
||||
println("❌ Socket.IO disconnected")
|
||||
}
|
||||
|
||||
// Add more events as needed
|
||||
|
||||
realtimeEvents.forEach { eventName ->
|
||||
socket?.on(eventName) { args ->
|
||||
val data = args.firstOrNull() as? JSONObject ?: JSONObject()
|
||||
_events.tryEmit(eventName to data)
|
||||
}
|
||||
}
|
||||
|
||||
socket?.connect()
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
currentClubId?.let { socket?.emit("leave-club", it) }
|
||||
socket?.disconnect()
|
||||
socket?.off()
|
||||
socket = null
|
||||
currentClubId = null
|
||||
}
|
||||
|
||||
private fun normalizedSocketUrl(): String =
|
||||
socketUrl
|
||||
.replaceFirst("wss://", "https://")
|
||||
.replaceFirst("ws://", "http://")
|
||||
|
||||
private companion object {
|
||||
val realtimeEvents = listOf(
|
||||
"participant:added",
|
||||
"participant:removed",
|
||||
"participant:updated",
|
||||
"diary:note:added",
|
||||
"diary:note:updated",
|
||||
"diary:note:deleted",
|
||||
"diary:tag:added",
|
||||
"diary:tag:removed",
|
||||
"diary:date:updated",
|
||||
"activity:member:added",
|
||||
"activity:member:removed",
|
||||
"activity:changed",
|
||||
"member:changed",
|
||||
"group:changed",
|
||||
"tournament:changed",
|
||||
"schedule:match:updated",
|
||||
"schedule:match-report:submitted",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user