dm-67
This commit is contained in:
parent
63c4f30659
commit
8644930f42
2 changed files with 31 additions and 6 deletions
|
|
@ -113,6 +113,9 @@ fun MessageMessaging(
|
||||||
// 💡 1. 取得 ViewModel 裡的 Reaction 狀態
|
// 💡 1. 取得 ViewModel 裡的 Reaction 狀態
|
||||||
val reactionState by viewModel.reactionUsersState.collectAsStateWithLifecycle()
|
val reactionState by viewModel.reactionUsersState.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
|
//message that selected
|
||||||
|
val actionMessage by viewModel.actionMessage.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
|
|
||||||
//進入時重整
|
//進入時重整
|
||||||
LaunchedEffect(roomId) {
|
LaunchedEffect(roomId) {
|
||||||
|
|
@ -332,6 +335,7 @@ fun MessageRow(
|
||||||
var showEmojiPanel by remember { mutableStateOf(false) }
|
var showEmojiPanel by remember { mutableStateOf(false) }
|
||||||
val commonEmojis = listOf("👍", "❤️", "😂", "😮", "😢", "🙏")
|
val commonEmojis = listOf("👍", "❤️", "😂", "😮", "😢", "🙏")
|
||||||
|
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,29 @@ class ChatViewModel @Inject constructor(
|
||||||
private val tokenManager: TokenManager
|
private val tokenManager: TokenManager
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
|
// --- 訊息長按動作選單狀態 (Action Menu State) ---
|
||||||
|
|
||||||
|
// 1. 私有的 MutableStateFlow,用來控制長按選中的訊息
|
||||||
|
private val _actionMessage = MutableStateFlow<Message?>(null)
|
||||||
|
|
||||||
|
// 2. 公開給 Compose UI 觀察的唯讀 StateFlow
|
||||||
|
val actionMessage: StateFlow<Message?> = _actionMessage.asStateFlow()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 設定目前哪一則訊息被長按選取(開啟動作選單)
|
||||||
|
*/
|
||||||
|
fun setActionMessage(message: Message?) {
|
||||||
|
_actionMessage.value = message
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除目前選取的訊息狀態(關閉動作選單)
|
||||||
|
*/
|
||||||
|
fun clearActionMessage() {
|
||||||
|
_actionMessage.value = null
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//reply function
|
//reply function
|
||||||
private val _replyingMessage = MutableStateFlow<Message?>(null)
|
private val _replyingMessage = MutableStateFlow<Message?>(null)
|
||||||
val replyingMessage: StateFlow<Message?> = _replyingMessage.asStateFlow()
|
val replyingMessage: StateFlow<Message?> = _replyingMessage.asStateFlow()
|
||||||
|
|
@ -264,23 +287,21 @@ class ChatViewModel @Inject constructor(
|
||||||
result.onSuccess {
|
result.onSuccess {
|
||||||
_deleteMessageState.value = DeleteMessageState.Success
|
_deleteMessageState.value = DeleteMessageState.Success
|
||||||
|
|
||||||
// 😎【超讚的體驗優化】:不重新 call loadMessages,直接在本地修改該訊息的狀態
|
// ✨【優化】:刪除成功後,自動關閉長按選單
|
||||||
|
clearActionMessage()
|
||||||
|
|
||||||
val currentState = _uiState.value
|
val currentState = _uiState.value
|
||||||
if (currentState is MessagesUiState.Success) {
|
if (currentState is MessagesUiState.Success) {
|
||||||
|
|
||||||
val updatedMessages = currentState.messages.map { message ->
|
val updatedMessages = currentState.messages.map { message ->
|
||||||
if (message.id == messageId) {
|
if (message.id == messageId) {
|
||||||
// 將該則訊息改為已刪除狀態(利用 copy 複製,並帶入對應欄位)
|
|
||||||
message.copy(
|
message.copy(
|
||||||
isDeleted = true,
|
isDeleted = true,
|
||||||
content = "此訊息已被刪除" // 可選擇在這裡直接改 content,或交由 UI 根據 isDeleted 顯示對應文字
|
content = "此訊息已被刪除"
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
message
|
message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新 uiState,Compose 畫面會立刻連動刷新
|
|
||||||
_uiState.value = currentState.copy(messages = updatedMessages)
|
_uiState.value = currentState.copy(messages = updatedMessages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue