auth to server-V3
This commit is contained in:
parent
8c26b4c5a1
commit
eeba3524fa
2 changed files with 108 additions and 15 deletions
|
|
@ -5,37 +5,43 @@ import androidx.lifecycle.viewModelScope
|
||||||
import com.example.login_v3.data.api.api_class.RegisterRequest
|
import com.example.login_v3.data.api.api_class.RegisterRequest
|
||||||
import com.example.login_v3.data.repository.basic.AuthRepository
|
import com.example.login_v3.data.repository.basic.AuthRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
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 kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
// 在 RegisterViewModel.kt 中修改
|
||||||
|
data class RegisterUiState(
|
||||||
|
val isLoading: Boolean = false,
|
||||||
|
val isSuccess: Boolean = false,
|
||||||
|
val errorMessage: String? = null
|
||||||
|
)
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class RegisterViewModel @Inject constructor(
|
class RegisterViewModel @Inject constructor(
|
||||||
private val repository: AuthRepository
|
private val repository: AuthRepository
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
fun handleRegister() {
|
// 使用 StateFlow 管理 UI 狀態
|
||||||
val request = RegisterRequest(
|
private val _uiState = MutableStateFlow(RegisterUiState())
|
||||||
display_name = "測試員",
|
val uiState: StateFlow<RegisterUiState> = _uiState.asStateFlow()
|
||||||
email = "test@example.com",
|
|
||||||
password = "password123",
|
fun handleRegister(displayName: String, email: String, pass: String, user: String) {
|
||||||
username = "tester01"
|
val request = RegisterRequest(displayName, email, pass, user)
|
||||||
)
|
|
||||||
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
_uiState.value = RegisterUiState(isLoading = true) // 開始載入
|
||||||
try {
|
try {
|
||||||
val response = repository.register(request)
|
val response = repository.register(request)
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
// 註冊成功,處理 response.body()
|
_uiState.value = RegisterUiState(isSuccess = true)
|
||||||
val successMsg = response.body()?.message
|
|
||||||
println("註冊成功: $successMsg")
|
|
||||||
} else {
|
} else {
|
||||||
// 註冊失敗,處理錯誤訊息 (例如 400 重複註冊)
|
val errorMsg = response.errorBody()?.string() ?: "註冊失敗"
|
||||||
val errorJson = response.errorBody()?.string()
|
_uiState.value = RegisterUiState(errorMessage = errorMsg)
|
||||||
println("註冊失敗: $errorJson")
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// 處理網路連線異常
|
_uiState.value = RegisterUiState(errorMessage = "網路異常: ${e.message}")
|
||||||
println("網路錯誤: ${e.message}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.example.login_v3.auth.reg
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.Button
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun RegisterScreen(viewModel: RegisterViewModel = hiltViewModel()) {
|
||||||
|
// 監聽狀態
|
||||||
|
val uiState by viewModel.uiState.collectAsState()
|
||||||
|
|
||||||
|
// 這裡定義 TextField 的輸入狀態
|
||||||
|
var displayName by remember { mutableStateOf("") }
|
||||||
|
var email by remember { mutableStateOf("") }
|
||||||
|
var username by remember { mutableStateOf("") }
|
||||||
|
var password by remember { mutableStateOf("") }
|
||||||
|
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(16.dp),
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
verticalArrangement = Arrangement.Center
|
||||||
|
) {
|
||||||
|
Text(text = "創建新帳號", style = MaterialTheme.typography.headlineMedium)
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
OutlinedTextField(value = displayName, onValueChange = { displayName = it }, label = { Text("顯示名稱") })
|
||||||
|
OutlinedTextField(value = email, onValueChange = { email = it }, label = { Text("電子信箱") })
|
||||||
|
OutlinedTextField(value = username, onValueChange = { username = it }, label = { Text("帳號") })
|
||||||
|
OutlinedTextField(
|
||||||
|
value = password,
|
||||||
|
onValueChange = { password = it },
|
||||||
|
label = { Text("密碼") },
|
||||||
|
visualTransformation = PasswordVisualTransformation() // 密碼隱碼
|
||||||
|
)
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(24.dp))
|
||||||
|
|
||||||
|
if (uiState.isLoading) {
|
||||||
|
CircularProgressIndicator() // 顯示載入圈圈
|
||||||
|
} else {
|
||||||
|
Button(
|
||||||
|
onClick = { viewModel.handleRegister(displayName, email, password, username) },
|
||||||
|
modifier = Modifier.fillMaxWidth()
|
||||||
|
) {
|
||||||
|
Text("註冊")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 顯示錯誤訊息
|
||||||
|
uiState.errorMessage?.let {
|
||||||
|
Text(text = it, color = Color.Red, modifier = Modifier.padding(top = 8.dp))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 註冊成功跳轉邏輯
|
||||||
|
LaunchedEffect(uiState.isSuccess) {
|
||||||
|
if (uiState.isSuccess) {
|
||||||
|
// 這裡執行導向登入頁面或是主頁面的動作
|
||||||
|
println("導航至登入頁面")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue