This commit is contained in:
Eric85710 2026-05-27 05:46:03 +08:00
parent e0b522e0d7
commit b14cd522fe
2 changed files with 37 additions and 3 deletions

View file

@ -247,4 +247,25 @@ class ChatRoomsRepository @Inject constructor(
Result.failure(e) Result.failure(e)
} }
} }
// 新增訊息的 Emoji 反應
suspend fun addMessageReaction(
roomId: String,
messageId: String,
emoji: String
): Result<Unit> {
return try {
val response = api.addMessageReaction(roomId, messageId, emoji)
if (response.isSuccessful) {
// 204 成功時沒有 body,直接回傳 Unit
Result.success(Unit)
} else {
val errorMsg = response.errorBody()?.string() ?: "未知錯誤"
Result.failure(Exception("新增反應失敗,錯誤碼: ${response.code()}, 訊息: $errorMsg"))
}
} catch (e: Exception) {
Result.failure(e)
}
}
} }

View file

@ -193,9 +193,7 @@ class ChatViewModel @Inject constructor(
private val _reactionUsersState = MutableStateFlow<ReactionUsersState>(ReactionUsersState.Idle) private val _reactionUsersState = MutableStateFlow<ReactionUsersState>(ReactionUsersState.Idle)
val reactionUsersState: StateFlow<ReactionUsersState> = _reactionUsersState.asStateFlow() val reactionUsersState: StateFlow<ReactionUsersState> = _reactionUsersState.asStateFlow()
/** //get emoji reaction
* 💡 新增:載入點擊特定 Emoji 的使用者名單
*/
fun loadMessageReactionUsers(roomId: String, messageId: String, emoji: String) { fun loadMessageReactionUsers(roomId: String, messageId: String, emoji: String) {
viewModelScope.launch { viewModelScope.launch {
_reactionUsersState.value = ReactionUsersState.Loading _reactionUsersState.value = ReactionUsersState.Loading
@ -214,6 +212,21 @@ class ChatViewModel @Inject constructor(
} }
} }
} }
//add emoji reaction
fun addMessageReaction(roomId: String, messageId: String, emoji: String) {
viewModelScope.launch {
val result = repository.addMessageReaction(roomId, messageId, emoji)
result.onSuccess {
// 😎【超讚體驗優化】:點擊成功後,除了通知後端,
// 也可以選擇直接呼叫 loadMessages(roomId) 來重新拉取最新帶有按讚數量的訊息列表
loadMessages(roomId)
}.onFailure { error ->
Log.e("ChatViewModel", "Add reaction failed", error)
// 這裡可以依需求決定要不要用 Toast 提示使用者點擊失敗
}
}
}
// 💡 請確保這段程式碼有確實待在 ChatViewModel 裡面 // 💡 請確保這段程式碼有確實待在 ChatViewModel 裡面
fun resetReactionUsersState() { fun resetReactionUsersState() {