diff --git a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt index 6f0c3ce..c470c52 100644 --- a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt +++ b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/detail_UI/setting_Profile_Page.kt @@ -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() ) { diff --git a/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt new file mode 100644 index 0000000..74c64f3 --- /dev/null +++ b/app/src/main/java/com/example/login_v3/home/setting/setting_detail_page/viewmodel/Profile_Viewmodel.kt @@ -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.Loading) + val uiState: StateFlow = _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 ?: "未知錯誤") + } + } + } +} \ No newline at end of file