auth to server-V29
This commit is contained in:
parent
e96e3d9843
commit
a283099137
2 changed files with 57 additions and 1 deletions
|
|
@ -20,6 +20,8 @@ import androidx.compose.material3.Divider
|
|||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.blur
|
||||
|
|
@ -30,10 +32,17 @@ import androidx.compose.ui.res.painterResource
|
|||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.example.login_v3.R
|
||||
import com.example.login_v3.home.setting.setting_detail_page.viewmodel.PersonalProfileViewModel
|
||||
|
||||
@Composable
|
||||
fun setting_profile_page(){
|
||||
fun setting_profile_page(
|
||||
viewModel: PersonalProfileViewModel = hiltViewModel()
|
||||
){
|
||||
val uiState by viewModel.uiState.collectAsState()
|
||||
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package com.example.login_v3.home.setting.setting_detail_page.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.login_v3.data.api.api_class.UserProfile
|
||||
import com.example.login_v3.data.repository.basic.PersonalProfileRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
// 定義 UI 狀態
|
||||
sealed class ProfileUiState {
|
||||
object Loading : ProfileUiState()
|
||||
data class Success(val profile: UserProfile) : ProfileUiState()
|
||||
data class Error(val message: String) : ProfileUiState()
|
||||
}
|
||||
|
||||
@HiltViewModel
|
||||
class PersonalProfileViewModel @Inject constructor(
|
||||
private val repository: PersonalProfileRepository
|
||||
) : ViewModel() {
|
||||
|
||||
private val _uiState = MutableStateFlow<ProfileUiState>(ProfileUiState.Loading)
|
||||
val uiState: StateFlow<ProfileUiState> = _uiState.asStateFlow()
|
||||
|
||||
init {
|
||||
// 初始化時自動抓取
|
||||
fetchProfile()
|
||||
}
|
||||
|
||||
fun fetchProfile() {
|
||||
viewModelScope.launch {
|
||||
_uiState.value = ProfileUiState.Loading
|
||||
try {
|
||||
val profile = repository.getMyProfile()
|
||||
_uiState.value = ProfileUiState.Success(profile)
|
||||
} catch (e: Exception) {
|
||||
// 如果是 401,Interceptor 會處理登出,
|
||||
// 這裡處理一般的網路錯誤或 API 錯誤
|
||||
_uiState.value = ProfileUiState.Error(e.localizedMessage ?: "未知錯誤")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue