package com.vpn.fastestvpnservice.screens import android.content.res.Configuration import android.gesture.Gesture import android.util.Log import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ColumnScope import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight 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.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.ClickableText import androidx.compose.foundation.text.KeyboardActionScope import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Email import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.Lock import androidx.compose.material.icons.filled.Visibility import androidx.compose.material.icons.filled.VisibilityOff import androidx.compose.material.icons.outlined.Visibility import androidx.compose.material3.Button import androidx.compose.material3.ButtonColors import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.ButtonElevation import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.ElevatedButton import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.material3.TextFieldColors import androidx.compose.material3.TextFieldDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.setValue import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCompositionContext import androidx.compose.ui.Alignment import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.paint import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavController import androidx.navigation.NavHostController import androidx.navigation.compose.rememberNavController import com.vpn.fastestvpnservice.R import com.vpn.fastestvpnservice.beans.DataResponse import com.vpn.fastestvpnservice.beans.UserResponse import com.vpn.fastestvpnservice.beans.themesList import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.AddText import com.vpn.fastestvpnservice.sealedClass.Screen import com.vpn.fastestvpnservice.viewmodels.LoginViewModel import kotlinx.coroutines.delay import kotlin.coroutines.coroutineContext import kotlin.random.Random @OptIn(ExperimentalComposeUiApi::class) @Composable fun Login(navHostController: NavHostController) { val keyboardController = LocalSoftwareKeyboardController.current val focusManager = LocalFocusManager.current val random = Random.nextInt(1,999) val loginViewModel: LoginViewModel = viewModel() val readOnly = loginViewModel.liveDataLoginStatus.observeAsState().value var textChanged by remember { mutableStateOf("") } var passwordChanged by remember { mutableStateOf("") } var passwordVisible by remember { mutableStateOf(false) } val context = LocalContext.current var basePreferenceHelper = BasePreferenceHelper(context) Box( modifier = Modifier .fillMaxSize() .paint( painter = painterResource(id = R.drawable.bg_img3), contentScale = ContentScale.FillBounds ) .alpha(if (readOnly == true) 0.6F else 1F) // .background( // if (isSystemInDarkTheme()) Color.Black // else Color.Transparent // ) .pointerInput(Unit) { detectTapGestures { focusManager.clearFocus() keyboardController?.hide() } } , // contentAlignment = Alignment.Center ) { Column ( modifier = Modifier .fillMaxHeight() .fillMaxWidth() , verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally ) { Spacer(modifier = Modifier.height(132.dp)) Image( painter = painterResource( id = R.drawable.fastestapp_logo3x), contentDescription = "FastestVPN", modifier = Modifier .size(width = 75.dp, height = 102.dp) .background(Color.Transparent) .padding(top = 0.dp) , ) Spacer(modifier = Modifier.height(50.dp)) TextMsg( fontSize = 28.sp, text = "Welcome Back!", color = Color.White ) TextMsg( fontSize = 14.sp, text = "Please login to your account.", color = colorResource(id = R.color.light_grey) ) Spacer(modifier = Modifier.height(20.dp)) TextField( value = textChanged, onValueChange = { textChanged = it }, readOnly = readOnly!!, textStyle = TextStyle( fontSize = 20.sp, color = colorResource(id = R.color.white), ), modifier = Modifier .padding(start = 15.dp, end = 15.dp) .align(Alignment.Start) .fillMaxWidth() .height(60.dp) .border( 1.dp, color = colorResource(id = R.color.white), shape = RoundedCornerShape(16.dp) ) .clickable() { }, // placeholder = { // Text(text = "Enter email address", // color = colorResource(id = R.color.white)) // }, label = { Text(text = "Email", style = TextStyle( colorResource(id = R.color.white)) ) }, leadingIcon = { Icon( painter = painterResource(id = R.drawable.sms3x), contentDescription = "Email Logo", tint = colorResource(id = R.color.white), modifier = Modifier .size(24.dp, 24.dp) ) }, maxLines = 1, colors = TextFieldDefaults.colors( focusedLabelColor = Color.Blue, unfocusedContainerColor = colorResource(id = R.color.transparent), focusedContainerColor = colorResource(id = R.color.transparent), focusedIndicatorColor = colorResource(id = R.color.transparent), disabledIndicatorColor = colorResource(id = R.color.transparent), unfocusedIndicatorColor = colorResource(id = R.color.transparent), cursorColor = colorResource(id = R.color.white) ), keyboardOptions = KeyboardOptions( keyboardType = KeyboardType.Email, imeAction = ImeAction.Done ), keyboardActions = KeyboardActions( onDone = { focusManager.clearFocus() keyboardController?.hide() } ), ) Spacer(modifier = Modifier.height(20.dp)) TextField( value = passwordChanged, onValueChange = { Log.d("onClick_test", "onValueChange -> ") passwordChanged = it }, readOnly = readOnly, textStyle = TextStyle( fontSize = 20.sp, color = colorResource(id = R.color.white), ), modifier = Modifier .padding(start = 15.dp, end = 15.dp) .align(Alignment.Start) .fillMaxWidth() .height(60.dp) .border( 1.dp, color = colorResource(id = R.color.grey_password_field), shape = RoundedCornerShape(16.dp) ) .background(color = colorResource(id = R.color.transparent)) .clickable() { }, shape = RoundedCornerShape(15.dp), // placeholder = { // Text(text = "Enter password", // color = colorResource(id = R.color.white)) // }, label = { Text(text = "Password", style = TextStyle( colorResource(id = R.color.white)) ) }, leadingIcon = { Icon( painter = painterResource(id = R.drawable.lock3x), contentDescription = "Password Logo", tint = colorResource(id = R.color.white), modifier = Modifier .size(24.dp, 24.dp) ) }, maxLines = 1, colors = TextFieldDefaults.colors( focusedLabelColor = Color.Blue, unfocusedContainerColor = colorResource(id = R.color.grey_password_field), focusedContainerColor = colorResource(id = R.color.grey_password_field), focusedIndicatorColor = colorResource(id = R.color.transparent), disabledIndicatorColor = colorResource(id = R.color.transparent), unfocusedIndicatorColor = colorResource(id = R.color.transparent), cursorColor = colorResource(id = R.color.white) ), keyboardOptions = KeyboardOptions( keyboardType = KeyboardType.Password, imeAction = ImeAction.Done ), keyboardActions = KeyboardActions( onDone = { focusManager.clearFocus() keyboardController?.hide() } ), visualTransformation = if (passwordVisible) VisualTransformation.Companion.None else PasswordVisualTransformation(), trailingIcon = { // val image // = if (passwordVisible) Icons.Filled.Visibility // else Icons.Filled.VisibilityOff val description = if (passwordVisible) "Hide Password" else "Show Password" IconButton(onClick = { if (!readOnly) { passwordVisible = !passwordVisible } }) { if (passwordVisible) { Icon( imageVector = Icons.Outlined.Visibility, contentDescription = description, tint = colorResource(id = R.color.white), modifier = Modifier.size(24.dp) ) } else { Icon( painter = painterResource(id = R.drawable.eye_slash3x), contentDescription = description, tint = colorResource(id = R.color.white), modifier = Modifier.size(24.dp) ) } } } ) Spacer(modifier = Modifier.height(15.dp)) ClickableText( onClick = { if (!readOnly) { Log.d("onClick_test", "Forgot Password Clicked") navHostController.navigate(Screen.ForgotPassword.route) } }, modifier = Modifier .padding(end = 15.dp) .align(Alignment.End), style = TextStyle( fontSize = 16.sp, fontWeight = FontWeight.Normal, color = colorResource(id = R.color.yellow_text), ), text = AnnotatedString("Forgot Password"), ) } SignInButton( navHostController = navHostController, textChanged, passwordChanged, loginViewModel = loginViewModel, basePreferenceHelper ) Row ( modifier = Modifier .align(Alignment.BottomCenter) .padding(57.dp), ){ TextMsgSignUp(navHostController = navHostController, loginViewModel, readOnly!!) // Text( // modifier = Modifier // .padding(0.dp), // fontSize = 14.sp, // fontWeight = FontWeight.Normal, // fontStyle = FontStyle.Normal, // text = "Don't have an account? ", // color = Color.White, // ) // ClickableText( // onClick = { // navHostController.navigate(Screen.SignUp.route) // }, // modifier = Modifier // .padding(0.dp), // style = TextStyle( // fontSize = 14.sp, // fontStyle = FontStyle.Normal, // color = colorResource(id = R.color.yellow_text), // ), // text = AnnotatedString(" Sign Up"), // ) } } } @Composable fun RowScope.TextMsgSignUp( navHostController: NavHostController, loginViewModel: LoginViewModel, isEnabled: Boolean ) { Text( modifier = Modifier .padding(0.dp), fontSize = 14.sp, fontWeight = FontWeight.Normal, fontStyle = FontStyle.Normal, text = "Don't have an account? ", color = Color.White, ) ClickableText( onClick = { if (isEnabled == false) { navHostController.navigate(Screen.SignUp.route) } }, modifier = Modifier .padding(0.dp), style = TextStyle( fontSize = 14.sp, fontStyle = FontStyle.Normal, color = colorResource(id = R.color.yellow_text), ), text = AnnotatedString(" Sign Up"), ) } @Composable fun BoxScope.SignInButton( navHostController: NavHostController, email: String, password: String, loginViewModel: LoginViewModel, prefHelper: BasePreferenceHelper ) { // var showLoader by remember { mutableStateOf(false) } if (loginViewModel.liveDataLoginStatus.value == true) { var progress by remember { mutableFloatStateOf(0.1F) } LaunchedEffect(key1 = Unit) { while (true) { for (i in 1..100) { progress = i.toFloat()/100F delay(150) } progress = 0.1F } } CircularProgressIndicator( progress = { progress }, modifier = Modifier .align(Alignment.Center) .size(50.dp), color = colorResource(id = R.color.yellow_text), strokeWidth = 5.dp, ) } Button( modifier = Modifier .padding( start = 15.dp, end = 15.dp, bottom = 109.dp ) .align(Alignment.BottomCenter) .background(colorResource(id = R.color.transparent)) .fillMaxWidth() .height(60.dp) .clickable() { }, onClick = { Log.d("test_api_response live", "Login Clicked:") // showLoader = true if (loginViewModel.liveDataLoginStatus.value == false) { Log.d("test_api_response live", "SignIn: $email $password") loginViewModel.setLoginStatus(true) loginViewModel.loginRequest( email, password, "android", "11", "3.2.4" ) } }, shape = RoundedCornerShape(15.dp), // border = BorderStroke(25.dp, colorResource(id = R.color.black)), colors = ButtonDefaults.buttonColors( contentColor = colorResource(id = R.color.blue_text), containerColor = colorResource(id = R.color.white), ), ) { Text(text = "Sign In", fontSize = 18.sp, fontWeight = FontWeight.Medium, fontStyle = FontStyle.Normal ) val loginData by loginViewModel.liveDataUserResponse.observeAsState() loginData?.let { response -> Log.d("test_api_response live", "live: ${loginData?.status} ${loginData?.message}") loginViewModel.setLoginStatus(false) // showLoader = false if (response.status) { response.data?.let { prefHelper.setLoggedInState(true) prefHelper.savePassword(password) prefHelper.saveUser(it) it.wireguard?.let { it1 -> prefHelper.saveWireGuard(it1) } it.product?.let { it1 -> prefHelper.saveProduct(it1) } prefHelper.saveEnabledProtocols(it.enabled_protocols) prefHelper.saveAvailableProtocols(it.available_protocols) prefHelper.saveXPlatformToken(it.userinfo?.email + "_" + System.currentTimeMillis()) prefHelper.saveAdBlockState(false) prefHelper.saveTheme(themesList[0]) it.servers?.let { prefHelper.saveServerData(it) } Log.d("bearer_token", it.token.toString()) // upgradePriceViewModel.getProducts() // prefHelper.getFcmToken().let { // loginViewModel.sendFcmToken(it) // Log.d("fcm token get", prefHelper.getFcmToken()) // } prefHelper.saveRadioBtnSplitPos(0) navHostController.popBackStack() navHostController.navigate(Screen.BottomBarMainScreen.route) } } loginViewModel.mutableLiveDataUserResponse.value = null } } } @Composable fun ColumnScope.TextMsg(fontSize: TextUnit, text: String, color: Color) { Text( modifier = Modifier .padding(start = 15.dp) .align(Alignment.Start), fontSize = fontSize, fontWeight = FontWeight.Normal, text = text, color = color, ) } @Preview @Composable fun LoginPreview() { Login(rememberNavController()) } @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) @Composable fun LoginPreviewDark() { Login(rememberNavController()) }