This commit is contained in:
Eric85710 2026-05-24 17:26:30 +08:00
parent 816983a45c
commit 32eec439e2
2 changed files with 7 additions and 5 deletions

View file

@ -14,7 +14,6 @@ import javax.inject.Inject
class TokenManager @Inject constructor( class TokenManager @Inject constructor(
private val dataStore: DataStore<Preferences> private val dataStore: DataStore<Preferences>
) { ) {
private val USER_ID_LIST = stringSetPreferencesKey("user_id_list")
// 獲取所有已登入的使用者 ID 清單 // 獲取所有已登入的使用者 ID 清單
val allUserIds: Flow<Set<String>> = dataStore.data.map { preferences -> val allUserIds: Flow<Set<String>> = dataStore.data.map { preferences ->
preferences[USER_ID_LIST] ?: emptySet() preferences[USER_ID_LIST] ?: emptySet()

View file

@ -11,10 +11,12 @@ import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import com.example.login_v3.data.api.api_class.Message import com.example.login_v3.data.api.api_class.Message
import com.example.login_v3.data.api.api_class.fullContactAvatarUrl import com.example.login_v3.data.api.api_class.fullContactAvatarUrl
import com.example.login_v3.data.repository.basic.TokenManager
import com.example.login_v3.home.Message.ViewModel.UserStatus import com.example.login_v3.home.Message.ViewModel.UserStatus
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
//message sending state //message sending state
@ -41,7 +43,8 @@ sealed interface MessagesUiState {
@HiltViewModel @HiltViewModel
class ChatViewModel @Inject constructor( class ChatViewModel @Inject constructor(
private val repository: ChatRoomsRepository private val repository: ChatRoomsRepository,
private val tokenManager: TokenManager
) : ViewModel() { ) : ViewModel() {
//UI state var //UI state var
@ -56,6 +59,9 @@ class ChatViewModel @Inject constructor(
viewModelScope.launch { viewModelScope.launch {
_uiState.value = MessagesUiState.Loading _uiState.value = MessagesUiState.Loading
//isMe
val currentLoggedInUserId = tokenManager.currentUserId.first() ?: ""
// 1. 同時獲取房間資訊與訊息列表 // 1. 同時獲取房間資訊與訊息列表
val roomResult = repository.getChatRoom(roomId) // ⚠️ 需確保 Repository 有提供此方法 val roomResult = repository.getChatRoom(roomId) // ⚠️ 需確保 Repository 有提供此方法
val messagesResult = repository.getChatMessages(roomId) val messagesResult = repository.getChatMessages(roomId)
@ -77,9 +83,6 @@ class ChatViewModel @Inject constructor(
Triple("聊天室", UserStatus.UNKNOWN, R.drawable.avatar_v1) Triple("聊天室", UserStatus.UNKNOWN, R.drawable.avatar_v1)
} }
val currentLoggedInUserId = "user_123"
_uiState.value = MessagesUiState.Success( _uiState.value = MessagesUiState.Success(
roomTitle = title, roomTitle = title,
partnerStatus = status, partnerStatus = status,