dm-183
This commit is contained in:
parent
530bdfa427
commit
eb22555b62
2 changed files with 29 additions and 11 deletions
|
|
@ -80,4 +80,8 @@ class TokenManager @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun switchAccount(userId: String) {
|
||||||
|
// 目前暫不實作
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -43,26 +43,38 @@ class AppViewModel @Inject constructor(
|
||||||
private val _currentScreen = MutableStateFlow<AppScreen>(AppScreen.Loading)
|
private val _currentScreen = MutableStateFlow<AppScreen>(AppScreen.Loading)
|
||||||
val currentScreen: StateFlow<AppScreen> = _currentScreen.asStateFlow()
|
val currentScreen: StateFlow<AppScreen> = _currentScreen.asStateFlow()
|
||||||
|
|
||||||
// 修改:監聽當前活躍帳號的 Token
|
// 監聽當前活躍帳號的 Token
|
||||||
val userToken: StateFlow<String?> = tokenManager.currentAccessToken
|
val userToken: StateFlow<String?> = tokenManager.currentAccessToken
|
||||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)
|
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), null)
|
||||||
|
|
||||||
// 修改:監聽「是否至少有一個帳號登入中」
|
// 監聽「是否至少有一個帳號登入中」
|
||||||
private val hasAccountLoggedIn: Flow<Boolean> = tokenManager.allUserIds.map { it.isNotEmpty() }
|
private val hasAccountLoggedIn: Flow<Boolean> = tokenManager.allUserIds.map { it.isNotEmpty() }
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
// 1. App 啟動時先檢查一次狀態,決定落腳點
|
||||||
checkLoginStatus()
|
checkLoginStatus()
|
||||||
|
// 2. 額外監聽:處理在 App 使用過程中,因為過期被強制登出的狀況
|
||||||
|
observeGlobalLogout()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkLoginStatus() {
|
private fun checkLoginStatus() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
// 修改:根據「帳號清單是否為空」來決定初始頁面
|
// 🌟 改用 first():只在 App 剛打開時決定是要去主頁 (ScreensTab) 還是歡迎頁 (PreReg)
|
||||||
|
val hasAccount = hasAccountLoggedIn.first()
|
||||||
|
if (hasAccount) {
|
||||||
|
_currentScreen.value = AppScreen.ScreensTab
|
||||||
|
} else {
|
||||||
|
_currentScreen.value = AppScreen.PreReg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun observeGlobalLogout() {
|
||||||
|
viewModelScope.launch {
|
||||||
|
// 監聽全域狀態。如果使用者在操作過程中(例如 TokenAuthenticator 刷新失敗觸發 logout),
|
||||||
|
// 導致帳號清單變空了,就把畫面強制踢回歡迎頁。
|
||||||
hasAccountLoggedIn.collect { hasAccount ->
|
hasAccountLoggedIn.collect { hasAccount ->
|
||||||
if (hasAccount) {
|
if (!hasAccount && _currentScreen.value == AppScreen.ScreensTab) {
|
||||||
// 只要清單不為空,就進入主頁面
|
|
||||||
_currentScreen.value = AppScreen.ScreensTab
|
|
||||||
} else {
|
|
||||||
// 如果清單空了(所有人都登出了),回首頁/註冊頁
|
|
||||||
_currentScreen.value = AppScreen.PreReg
|
_currentScreen.value = AppScreen.PreReg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +87,7 @@ class AppViewModel @Inject constructor(
|
||||||
Log.d("NavTest", "Current state value: ${_currentScreen.value}")
|
Log.d("NavTest", "Current state value: ${_currentScreen.value}")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改:登出當前帳號
|
// 登出當前帳號
|
||||||
fun onLogout() {
|
fun onLogout() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
// 先獲取目前的 ID
|
// 先獲取目前的 ID
|
||||||
|
|
@ -83,7 +95,9 @@ class AppViewModel @Inject constructor(
|
||||||
currentId?.let {
|
currentId?.let {
|
||||||
tokenManager.logout(it)
|
tokenManager.logout(it)
|
||||||
}
|
}
|
||||||
// logout 完後,hasAccountLoggedIn 的 collect 會自動觸發頁面跳轉邏輯
|
|
||||||
|
// 🌟 登出成功後,主動導回起點頁面
|
||||||
|
_currentScreen.value = AppScreen.PreReg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue