更新Google登录

This commit is contained in:
2024-12-01 21:27:39 +08:00
parent 784427cfba
commit cc7a65e016
9 changed files with 147 additions and 73 deletions

View File

@@ -68,7 +68,7 @@ fun ResetPasswordScreen() {
LaunchedEffect(Unit) {
try {
dictService.getDictByKey(ConstVars.DIC_KEY_RESET_EMAIL_INTERVAL).let {
countDownMax = it.value.toInt()
countDownMax = it.value as? Int ?: 60
}
} catch (e: Exception) {
countDownMax = 60

View File

@@ -56,7 +56,7 @@ fun PolicyCheckbox(
scope.launch {
try {
val resp = dictService.getDictByKey(ConstVars.DICT_KEY_PRIVATE_POLICY_URL)
policyUrl = resp.value
policyUrl = resp.value as String
showModal = true
} catch (e: Exception) {

View File

@@ -50,11 +50,14 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.AppState
import com.aiosman.ravenow.AppStore
import com.aiosman.ravenow.ConstVars
import com.aiosman.ravenow.data.api.ErrorCode
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.data.AccountServiceImpl
import com.aiosman.ravenow.data.DictService
import com.aiosman.ravenow.data.DictServiceImpl
import com.aiosman.ravenow.data.ServiceException
import com.aiosman.ravenow.data.api.getErrorMessageCode
import com.aiosman.ravenow.data.api.showToast
@@ -75,18 +78,46 @@ fun LoginPage() {
val accountService = AccountServiceImpl()
val statusBarController = rememberSystemUiController()
val AppColors = LocalAppTheme.current
val dictService: DictService = DictServiceImpl()
var showGoogleLogin by remember {
mutableStateOf(false)
}
suspend fun checkGoogleLoginEnable() {
val result = dictService.getDistList(
listOf(
ConstVars.DICT_KEY_ENABLE_GOOGLE_LOGIN,
ConstVars.DICT_KEY_GOOGLE_LOGIN_CLIENT_ID
)
)
val enableDictItem = result.find { it.key == ConstVars.DICT_KEY_ENABLE_GOOGLE_LOGIN }
val clientIdDictItem = result.find { it.key == ConstVars.DICT_KEY_GOOGLE_LOGIN_CLIENT_ID }
if (enableDictItem != null && clientIdDictItem != null) {
AppState.googleClientId = clientIdDictItem.value as? String
showGoogleLogin = enableDictItem.value == true
} else {
showGoogleLogin = false
}
AppState.enableGoogleLogin = showGoogleLogin
}
LaunchedEffect(Unit) {
statusBarController.setStatusBarColor(Color.Transparent, darkIcons = !AppState.darkMode)
checkGoogleLoginEnable()
}
fun googleLogin() {
val clientId = AppState.googleClientId
if (clientId == null) {
Toast.makeText(context, "Google login is not enabled", Toast.LENGTH_SHORT).show()
return
}
coroutineScope.launch {
try {
GoogleLogin(context) {
GoogleLogin(context, clientId) {
coroutineScope.launch {
try {
accountService.regiterUserWithGoogleAccount(it)
}catch (e : ServiceException) {
} catch (e: ServiceException) {
when (e.errorType) {
ErrorCode.USER_EXIST ->
Toast.makeText(
@@ -94,6 +125,7 @@ fun LoginPage() {
context.getErrorMessageCode(e.errorType.code),
Toast.LENGTH_SHORT
).show()
else -> {
e.errorType.showToast(context)
}
@@ -197,23 +229,26 @@ fun LoginPage() {
NavigationRoute.EmailSignUp.route,
)
}
Spacer(modifier = Modifier.height(16.dp))
ActionButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.sign_in_with_google),
color = AppColors.text,
leading = {
Image(
painter = painterResource(id = R.drawable.rider_pro_google),
contentDescription = "Google",
modifier = Modifier.size(36.dp)
)
},
expandText = true,
contentPadding = PaddingValues(vertical = 8.dp, horizontal = 8.dp)
) {
googleLogin()
if (showGoogleLogin) {
Spacer(modifier = Modifier.height(16.dp))
ActionButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.sign_in_with_google),
color = AppColors.text,
leading = {
Image(
painter = painterResource(id = R.drawable.rider_pro_google),
contentDescription = "Google",
modifier = Modifier.size(36.dp)
)
},
expandText = true,
contentPadding = PaddingValues(vertical = 8.dp, horizontal = 8.dp)
) {
googleLogin()
}
}
Spacer(modifier = Modifier.height(24.dp))
Box(
contentAlignment = Alignment.Center,
@@ -404,19 +439,20 @@ fun ImageColumn(
val totalHeight = imageHeight.value * imageCount // 计算总高度
Column(modifier = modifier) {
for (i in 0 until imageCount) {
Box(modifier = Modifier
.width(156.dp)
.height(208.dp)
.scale(1f)
.graphicsLayer {
val translation = if (reverse) {
offset
} else {
offset
Box(
modifier = Modifier
.width(156.dp)
.height(208.dp)
.scale(1f)
.graphicsLayer {
val translation = if (reverse) {
offset
} else {
offset
}
translationY = translation
}
translationY = translation
}
.clip(RoundedCornerShape(16.dp)),
.clip(RoundedCornerShape(16.dp)),
contentAlignment = Alignment.Center) {
Image(
painter = painterResource(id = imageList[(currentImage + i) % imageCount]),

View File

@@ -18,7 +18,11 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
@@ -51,11 +55,17 @@ fun SignupScreen() {
val coroutineScope = rememberCoroutineScope()
val accountService: AccountService = AccountServiceImpl()
val appColor = LocalAppTheme.current
var enableGoogleLogin by remember { mutableStateOf(AppState.enableGoogleLogin) }
fun googleLogin() {
val clientId = AppState.googleClientId
if (clientId == null) {
Toast.makeText(context, "Google client id is not set", Toast.LENGTH_SHORT).show()
return
}
coroutineScope.launch {
try {
GoogleLogin(context) {
GoogleLogin(context,clientId) {
coroutineScope.launch {
try {
accountService.regiterUserWithGoogleAccount(it)
@@ -185,25 +195,28 @@ fun SignupScreen() {
// Spacer(modifier = Modifier.width(8.dp))
// }
// )
Spacer(modifier = Modifier.height(16.dp))
ActionButton(
modifier = Modifier
.width(345.dp)
.height(48.dp),
color = Color.Black,
leading = {
Image(
painter = painterResource(id = R.mipmap.rider_pro_signup_google),
contentDescription = "Google",
modifier = Modifier.size(24.dp)
)
Spacer(modifier = Modifier.width(8.dp))
},
text = stringResource(R.string.sign_in_with_google),
) {
googleLogin()
if (enableGoogleLogin) {
Spacer(modifier = Modifier.height(16.dp))
ActionButton(
modifier = Modifier
.width(345.dp)
.height(48.dp),
color = Color.Black,
leading = {
Image(
painter = painterResource(id = R.mipmap.rider_pro_signup_google),
contentDescription = "Google",
modifier = Modifier.size(24.dp)
)
Spacer(modifier = Modifier.width(8.dp))
},
text = stringResource(R.string.sign_in_with_google),
) {
googleLogin()
}
}
// Spacer(modifier = Modifier.height(16.dp))
// Box(
// modifier = Modifier

View File

@@ -148,9 +148,14 @@ fun UserAuthScreen() {
fun googleLogin() {
val clientId = AppState.googleClientId
if (clientId == null) {
Toast.makeText(context, "Google login not supported", Toast.LENGTH_SHORT).show()
return
}
scope.launch {
try {
GoogleLogin(context) {
GoogleLogin(context,clientId) {
scope.launch {
try {
val authResp = accountService.loginUserWithGoogle(it)
@@ -293,25 +298,28 @@ fun UserAuthScreen() {
) {
onLogin()
}
Spacer(modifier = Modifier.height(16.dp))
Text(stringResource(R.string.or_login_with), color = AppColors.secondaryText)
Spacer(modifier = Modifier.height(16.dp))
ActionButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.sign_in_with_google),
color = AppColors.text,
leading = {
Image(
painter = painterResource(id = R.drawable.rider_pro_google),
contentDescription = "Google",
modifier = Modifier.size(36.dp)
)
},
expandText = true,
contentPadding = PaddingValues(vertical = 8.dp, horizontal = 8.dp)
) {
googleLogin()
if (AppState.enableGoogleLogin) {
Spacer(modifier = Modifier.height(16.dp))
Text(stringResource(R.string.or_login_with), color = AppColors.secondaryText)
Spacer(modifier = Modifier.height(16.dp))
ActionButton(
modifier = Modifier.fillMaxWidth(),
text = stringResource(R.string.sign_in_with_google),
color = AppColors.text,
leading = {
Image(
painter = painterResource(id = R.drawable.rider_pro_google),
contentDescription = "Google",
modifier = Modifier.size(36.dp)
)
},
expandText = true,
contentPadding = PaddingValues(vertical = 8.dp, horizontal = 8.dp)
) {
googleLogin()
}
}
}
}