SignUpScreen.kt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package com.vpn.fastestvpnservice.screens
  2. import android.content.res.Configuration
  3. import android.util.Log
  4. import android.widget.Toast
  5. import androidx.compose.foundation.Image
  6. import androidx.compose.foundation.background
  7. import androidx.compose.foundation.border
  8. import androidx.compose.foundation.gestures.detectTapGestures
  9. import androidx.compose.foundation.isSystemInDarkTheme
  10. import androidx.compose.foundation.layout.Arrangement
  11. import androidx.compose.foundation.layout.Box
  12. import androidx.compose.foundation.layout.BoxScope
  13. import androidx.compose.foundation.layout.Column
  14. import androidx.compose.foundation.layout.ColumnScope
  15. import androidx.compose.foundation.layout.Row
  16. import androidx.compose.foundation.layout.Spacer
  17. import androidx.compose.foundation.layout.fillMaxHeight
  18. import androidx.compose.foundation.layout.fillMaxSize
  19. import androidx.compose.foundation.layout.fillMaxWidth
  20. import androidx.compose.foundation.layout.height
  21. import androidx.compose.foundation.layout.padding
  22. import androidx.compose.foundation.layout.size
  23. import androidx.compose.foundation.layout.width
  24. import androidx.compose.foundation.shape.RoundedCornerShape
  25. import androidx.compose.foundation.text.ClickableText
  26. import androidx.compose.foundation.text.KeyboardActions
  27. import androidx.compose.foundation.text.KeyboardOptions
  28. import androidx.compose.material.icons.Icons
  29. import androidx.compose.material.icons.filled.Done
  30. import androidx.compose.material.icons.filled.DoneOutline
  31. import androidx.compose.material.icons.filled.Email
  32. import androidx.compose.material.icons.filled.Lock
  33. import androidx.compose.material.icons.filled.Visibility
  34. import androidx.compose.material.icons.filled.VisibilityOff
  35. import androidx.compose.material.icons.filled.Warning
  36. import androidx.compose.material.icons.outlined.Visibility
  37. import androidx.compose.material.icons.sharp.Visibility
  38. import androidx.compose.material3.Button
  39. import androidx.compose.material3.ButtonDefaults
  40. import androidx.compose.material3.CircularProgressIndicator
  41. import androidx.compose.material3.Icon
  42. import androidx.compose.material3.IconButton
  43. import androidx.compose.material3.MaterialTheme
  44. import androidx.compose.material3.Scaffold
  45. import androidx.compose.material3.SnackbarHostState
  46. import androidx.compose.material3.Text
  47. import androidx.compose.material3.TextField
  48. import androidx.compose.material3.TextFieldDefaults
  49. import androidx.compose.runtime.Composable
  50. import androidx.compose.runtime.LaunchedEffect
  51. import androidx.compose.runtime.getValue
  52. import androidx.compose.runtime.livedata.observeAsState
  53. import androidx.compose.runtime.mutableFloatStateOf
  54. import androidx.compose.runtime.mutableStateOf
  55. import androidx.compose.runtime.remember
  56. import androidx.compose.runtime.rememberCoroutineScope
  57. import androidx.compose.runtime.setValue
  58. import androidx.compose.ui.Alignment
  59. import androidx.compose.ui.ExperimentalComposeUiApi
  60. import androidx.compose.ui.Modifier
  61. import androidx.compose.ui.draw.alpha
  62. import androidx.compose.ui.draw.paint
  63. import androidx.compose.ui.graphics.Color
  64. import androidx.compose.ui.graphics.vector.ImageVector
  65. import androidx.compose.ui.input.pointer.pointerInput
  66. import androidx.compose.ui.layout.ContentScale
  67. import androidx.compose.ui.platform.LocalContext
  68. import androidx.compose.ui.platform.LocalFocusManager
  69. import androidx.compose.ui.platform.LocalSoftwareKeyboardController
  70. import androidx.compose.ui.res.colorResource
  71. import androidx.compose.ui.res.painterResource
  72. import androidx.compose.ui.res.vectorResource
  73. import androidx.compose.ui.text.AnnotatedString
  74. import androidx.compose.ui.text.TextStyle
  75. import androidx.compose.ui.text.font.FontStyle
  76. import androidx.compose.ui.text.font.FontWeight
  77. import androidx.compose.ui.text.input.ImeAction
  78. import androidx.compose.ui.text.input.KeyboardType
  79. import androidx.compose.ui.text.input.PasswordVisualTransformation
  80. import androidx.compose.ui.text.input.VisualTransformation
  81. import androidx.compose.ui.text.style.LineHeightStyle
  82. import androidx.compose.ui.text.style.TextAlign
  83. import androidx.compose.ui.tooling.preview.Preview
  84. import androidx.compose.ui.unit.dp
  85. import androidx.compose.ui.unit.sp
  86. import androidx.lifecycle.viewmodel.compose.viewModel
  87. import androidx.navigation.NavHostController
  88. import androidx.navigation.compose.rememberNavController
  89. import com.vpn.fastestvpnservice.R
  90. import com.vpn.fastestvpnservice.beans.themesList
  91. import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
  92. import com.vpn.fastestvpnservice.sealedClass.Screen
  93. import com.vpn.fastestvpnservice.ui.theme.customTypography
  94. import com.vpn.fastestvpnservice.ui.theme.outfitFontFamily
  95. import com.vpn.fastestvpnservice.viewmodels.LoginViewModel
  96. import com.vpn.fastestvpnservice.viewmodels.SignUpViewModel
  97. import com.vpn.fastestvpnservice.views.CustomValidation
  98. import com.vpn.fastestvpnservice.views.ShowCustomSnackBar
  99. import kotlinx.coroutines.delay
  100. import kotlinx.coroutines.launch
  101. @OptIn(ExperimentalComposeUiApi::class)
  102. @Composable
  103. fun SignUp(navHostController: NavHostController) {
  104. val keyboardController = LocalSoftwareKeyboardController.current
  105. val focusManager = LocalFocusManager.current
  106. val signUpViewModel: SignUpViewModel = viewModel()
  107. var isSignUpEnabled = signUpViewModel.liveDataSignUpStatus.observeAsState().value
  108. var textChanged by remember { mutableStateOf("") }
  109. var passwordChanged by remember { mutableStateOf("") }
  110. var passwordVisible by remember { mutableStateOf(false) }
  111. var showErrorEmail by remember { mutableStateOf(false) }
  112. var showErrorEmail2 by remember { mutableStateOf(false) }
  113. var showErrorPass1 by remember { mutableStateOf(false) }
  114. var showErrorPass2 by remember { mutableStateOf(false) }
  115. val snackBarState = remember { SnackbarHostState() }
  116. Scaffold(
  117. content = { padding ->
  118. Box(
  119. modifier = Modifier
  120. .paint(
  121. painter = painterResource(id = R.drawable.bg_img3),
  122. contentScale = ContentScale.FillBounds
  123. )
  124. .alpha(if (isSignUpEnabled!!) 0.6F else 1F)
  125. // .background(
  126. // if (isSystemInDarkTheme()) Color.Black
  127. // else Color.Transparent
  128. // )
  129. .fillMaxSize()
  130. .pointerInput(Unit) {
  131. detectTapGestures {
  132. focusManager.clearFocus()
  133. keyboardController?.hide()
  134. }
  135. },
  136. ) {
  137. ShowCustomSnackBar(snackBarState)
  138. Column (
  139. modifier = Modifier
  140. .fillMaxHeight()
  141. .fillMaxWidth()
  142. ,
  143. verticalArrangement = Arrangement.Top,
  144. horizontalAlignment = Alignment.CenterHorizontally
  145. ) {
  146. Spacer(modifier = Modifier.height(50.dp))
  147. IconButton(
  148. onClick = {
  149. if (!isSignUpEnabled) {
  150. navHostController.popBackStack()
  151. }
  152. },
  153. modifier = Modifier
  154. .padding(start = 16.dp, top = 25.dp)
  155. .align(Alignment.Start)
  156. )
  157. {
  158. Icon(painter = painterResource(
  159. id = R.drawable.back_arrow3x),
  160. contentDescription = "Back Button",
  161. tint = colorResource(id = R.color.white),
  162. modifier = Modifier
  163. .height(36.dp)
  164. .width(36.dp)
  165. .align(Alignment.Start),
  166. )
  167. }
  168. Spacer(modifier = Modifier.height(44.dp))
  169. Image(
  170. painter = painterResource(id = R.drawable.fastestapp_logo3x),
  171. contentDescription = "FastestVPN",
  172. modifier = Modifier
  173. .size(75.dp, 102.dp)
  174. .background(Color.Transparent)
  175. .padding(top = 0.dp),
  176. )
  177. Spacer(modifier = Modifier.height(50.dp))
  178. Text(
  179. modifier = Modifier
  180. .padding(start = 15.dp)
  181. .align(Alignment.Start)
  182. ,
  183. style = MaterialTheme.typography.displayLarge,
  184. text = "Hello There!",
  185. color = Color.White,
  186. )
  187. Text(
  188. modifier = Modifier
  189. .padding(start = 15.dp)
  190. .align(Alignment.Start)
  191. .alpha(0.6F),
  192. style = MaterialTheme.typography.customTypography.labelLarge,
  193. text = "Please register your account.",
  194. color = colorResource(id = R.color.white),
  195. )
  196. Spacer(modifier = Modifier.height(20.dp))
  197. val colorEmail = if (showErrorEmail || showErrorEmail2) {
  198. colorResource(id = R.color.red)
  199. } else {
  200. colorResource(id = R.color.white)
  201. }
  202. TextField(
  203. value = textChanged,
  204. onValueChange = {
  205. textChanged = it
  206. },
  207. readOnly = isSignUpEnabled,
  208. textStyle = MaterialTheme.typography.customTypography.bodyMedium,
  209. modifier = Modifier
  210. .padding(start = 15.dp, end = 15.dp, top = 10.dp)
  211. .align(Alignment.Start)
  212. .fillMaxWidth()
  213. .height(60.dp)
  214. .border(
  215. 1.dp,
  216. color = colorEmail,
  217. shape = RoundedCornerShape(16.dp)
  218. ),
  219. // placeholder = {
  220. // Text(text = "Enter email address",
  221. // color = colorResource(id = R.color.white),
  222. // fontSize = 14.sp,
  223. // )
  224. // },
  225. label = {
  226. Text(text = "Email",
  227. style = MaterialTheme.typography.customTypography.bodySmall
  228. )
  229. },
  230. leadingIcon = {
  231. Icon(
  232. painter = painterResource(id = R.drawable.sms3x),
  233. contentDescription = "Email Logo",
  234. tint = colorResource(id = R.color.white),
  235. modifier = Modifier
  236. .size(24.dp, 24.dp)
  237. )
  238. },
  239. maxLines = 1,
  240. colors = TextFieldDefaults.colors(
  241. focusedLabelColor = Color.Blue,
  242. unfocusedContainerColor = colorResource(id = R.color.transparent),
  243. focusedContainerColor = colorResource(id = R.color.transparent),
  244. focusedIndicatorColor = colorResource(id = R.color.transparent),
  245. disabledIndicatorColor = colorResource(id = R.color.transparent),
  246. unfocusedIndicatorColor = colorResource(id = R.color.transparent),
  247. cursorColor = colorResource(id = R.color.white)
  248. ),
  249. keyboardOptions = KeyboardOptions(
  250. keyboardType = KeyboardType.Email,
  251. imeAction = ImeAction.Done
  252. ),
  253. keyboardActions = KeyboardActions(
  254. onDone = {
  255. focusManager.clearFocus()
  256. keyboardController?.hide()
  257. }
  258. ),
  259. )
  260. if (showErrorEmail) {
  261. ShowErrorRow(errorText = "Email is Empty")
  262. }
  263. else if (showErrorEmail2) {
  264. ShowErrorRow(errorText = "Email format incorrect")
  265. }
  266. Spacer(modifier = Modifier.height(20.dp))
  267. val colorPass = if (showErrorPass1 || showErrorPass2) {
  268. colorResource(id = R.color.red)
  269. } else {
  270. colorResource(id = R.color.grey_password_field)
  271. }
  272. TextField(
  273. value = passwordChanged,
  274. onValueChange = {
  275. Log.d("onClick_test", "onValueChange -> ")
  276. passwordChanged = it
  277. },
  278. readOnly = isSignUpEnabled,
  279. textStyle = MaterialTheme.typography.customTypography.bodyMedium,
  280. modifier = Modifier
  281. .padding(start = 15.dp, end = 15.dp)
  282. .align(Alignment.Start)
  283. .fillMaxWidth()
  284. .height(60.dp)
  285. .border(
  286. 1.dp,
  287. color = colorPass,
  288. shape = RoundedCornerShape(16.dp)
  289. )
  290. .background(color = colorResource(id = R.color.transparent)),
  291. shape = RoundedCornerShape(15.dp),
  292. // placeholder = {
  293. // Text(text = "Enter password",
  294. // color = colorResource(id = R.color.white))
  295. // },
  296. label = {
  297. Text(text = "Password",
  298. style = MaterialTheme.typography.customTypography.bodyLarge
  299. )
  300. },
  301. leadingIcon = {
  302. Icon(
  303. painter = painterResource(id = R.drawable.lock3x),
  304. contentDescription = "Password Logo",
  305. tint = colorResource(id = R.color.white),
  306. modifier = Modifier
  307. .size(24.dp, 24.dp)
  308. )
  309. },
  310. maxLines = 1,
  311. colors = TextFieldDefaults.colors(
  312. focusedLabelColor = Color.Blue,
  313. unfocusedContainerColor = colorResource(id = R.color.grey_password_field),
  314. focusedContainerColor = colorResource(id = R.color.grey_password_field),
  315. focusedIndicatorColor = colorResource(id = R.color.transparent),
  316. disabledIndicatorColor = colorResource(id = R.color.transparent),
  317. unfocusedIndicatorColor = colorResource(id = R.color.transparent),
  318. cursorColor = colorResource(id = R.color.white)
  319. ),
  320. keyboardOptions = KeyboardOptions(
  321. keyboardType = KeyboardType.Password,
  322. imeAction = ImeAction.Done
  323. ),
  324. keyboardActions = KeyboardActions(
  325. onDone = {
  326. focusManager.clearFocus()
  327. keyboardController?.hide()
  328. }
  329. ),
  330. visualTransformation =
  331. if (passwordVisible) VisualTransformation.None
  332. else PasswordVisualTransformation(),
  333. trailingIcon = {
  334. Log.d("onClick_test", "trailingIcon -> ")
  335. // val image
  336. // = if (passwordVisible) Icons.Filled.Visibility
  337. // else Icons.Filled.VisibilityOff
  338. val description = if (passwordVisible) "Hide Password"
  339. else "Show Password"
  340. IconButton(onClick = {
  341. if (!isSignUpEnabled) {
  342. passwordVisible = !passwordVisible
  343. }
  344. })
  345. {
  346. if (passwordVisible) {
  347. Icon(
  348. imageVector = Icons.Outlined.Visibility,
  349. contentDescription = description,
  350. tint = colorResource(id = R.color.white),
  351. modifier = Modifier.size(24.dp)
  352. )
  353. }
  354. else {
  355. Icon(
  356. painter = painterResource(id = R.drawable.eye_slash3x),
  357. contentDescription = description,
  358. tint = colorResource(id = R.color.white),
  359. modifier = Modifier.size(24.dp)
  360. )
  361. }
  362. }
  363. }
  364. )
  365. if (showErrorPass1) {
  366. ShowErrorRow(errorText = "Password is Empty")
  367. }
  368. else if (showErrorPass2) {
  369. ShowErrorRow(errorText = "Should be 3 or more!")
  370. }
  371. Spacer(modifier = Modifier.height(15.dp))
  372. Row (
  373. modifier = Modifier
  374. .padding(start = 15.dp, top = 16.dp)
  375. .fillMaxWidth(),
  376. ){
  377. val colorLogo = if (showErrorEmail || showErrorEmail2 || showErrorPass1 || showErrorPass2) colorResource(
  378. id = R.color.red
  379. ) else colorResource(id = R.color.white)
  380. Icon(
  381. painter = painterResource(id = R.drawable.tick_square3x),
  382. contentDescription = "Logo",
  383. tint = colorLogo,
  384. modifier = Modifier.size(24.dp, 24.dp)
  385. )
  386. Text(
  387. modifier = Modifier
  388. .padding(start = 15.dp),
  389. style = MaterialTheme.typography.customTypography.bodyLarge,
  390. text = "By creating your account, you agree to",
  391. color = colorResource(id = R.color.white),
  392. )
  393. }
  394. Spacer(modifier = Modifier.height(0.dp))
  395. Row (
  396. modifier = Modifier
  397. .padding(start = 15.dp)
  398. .fillMaxWidth(),
  399. ){
  400. Icon(
  401. painter = painterResource(id = R.drawable.tick_square3x),
  402. contentDescription = "Logo",
  403. tint = colorResource(id = R.color.transparent),
  404. modifier = Modifier.size(24.dp),
  405. )
  406. ClickableText(
  407. onClick = {
  408. if (!isSignUpEnabled) {
  409. navHostController.navigate(
  410. Screen.TermsAndConditions.route
  411. )
  412. }
  413. },
  414. modifier = Modifier
  415. .padding(start = 15.dp),
  416. text = AnnotatedString("Terms & Conditions"),
  417. style = MaterialTheme.typography.customTypography.titleLarge
  418. )
  419. }
  420. }
  421. SignUpButton(
  422. signUpViewModel,
  423. textChanged,
  424. passwordChanged,
  425. navHostController,
  426. showErrorEmail1 = {
  427. showErrorEmail = it
  428. },
  429. showErrorEmail,
  430. showErrorPass1 = {
  431. showErrorPass1 = it
  432. },
  433. showErrorPass1,
  434. showErrorPass2 = {
  435. showErrorPass2 = it
  436. },
  437. showErrorPass2,
  438. showErrorEmail2 = {
  439. showErrorEmail2 = it
  440. },
  441. showErrorEmail2,
  442. snackBarState
  443. )
  444. Row (
  445. modifier = Modifier
  446. .align(Alignment.BottomCenter)
  447. .padding(40.dp),
  448. ){
  449. Text(
  450. modifier = Modifier
  451. .padding(0.dp),
  452. style = MaterialTheme.typography.customTypography.labelLarge,
  453. text = "Already have an account? ",
  454. color = Color.White,
  455. )
  456. ClickableText(
  457. onClick = {
  458. if (!isSignUpEnabled) {
  459. navHostController.popBackStack()
  460. if (navHostController.currentDestination?.id != null &&
  461. navHostController.currentDestination?.route != null){
  462. Log.d("test_signup", "From Sign In -> id = " + navHostController.currentDestination?.id
  463. + " route = " + navHostController.currentDestination?.route
  464. )
  465. }
  466. else {
  467. Log.d("test_signup", "From Sign Up -> id = " + navHostController.currentDestination?.id
  468. + " route = " + navHostController.currentDestination?.route
  469. )
  470. navHostController.navigate(Screen.Login.route)
  471. }
  472. }
  473. },
  474. modifier = Modifier
  475. .padding(0.dp),
  476. style = MaterialTheme.typography.customTypography.titleMedium,
  477. text = AnnotatedString(" Sign In"),
  478. )
  479. }
  480. }
  481. }
  482. )
  483. }
  484. @Composable
  485. fun ColumnScope.ShowErrorRowSignUp(
  486. errorText: String
  487. ) {
  488. Row(
  489. verticalAlignment = Alignment.CenterVertically,
  490. modifier = Modifier
  491. .align(Alignment.Start)
  492. .padding(start = 16.dp, top = 8.dp)
  493. ) {
  494. Icon(imageVector = Icons.Default.Warning,
  495. contentDescription = "Error",
  496. tint = colorResource(id = R.color.red),
  497. modifier = Modifier.size(14.dp)
  498. )
  499. Text(text = errorText,
  500. style = TextStyle(
  501. fontFamily = outfitFontFamily,
  502. fontWeight = FontWeight.Normal,
  503. fontSize = 14.sp,
  504. color = colorResource(id = R.color.red)
  505. ),
  506. modifier = Modifier.padding(start = 5.dp)
  507. )
  508. }
  509. }
  510. @Composable
  511. fun BoxScope.SignUpButton(
  512. signUpViewModel: SignUpViewModel,
  513. email: String,
  514. password: String,
  515. navHostController: NavHostController,
  516. showErrorEmail1: (Boolean) -> Unit,
  517. isErrorEmail1: Boolean,
  518. showErrorPass1: (Boolean) -> Unit,
  519. isErrorPass1: Boolean,
  520. showErrorPass2: (Boolean) -> Unit,
  521. isErrorPass2: Boolean,
  522. showErrorEmail2: (Boolean) -> Unit,
  523. isErrorEmail2: Boolean,
  524. snackBarState: SnackbarHostState
  525. ) {
  526. val loginViewModel: LoginViewModel = viewModel()
  527. val context = LocalContext.current
  528. val prefHelper = BasePreferenceHelper(context)
  529. val customValidation = CustomValidation()
  530. val coroutineScope = rememberCoroutineScope()
  531. if (isErrorEmail1) {
  532. if (email.isNotEmpty()) {
  533. showErrorEmail1(false)
  534. }
  535. }
  536. if (isErrorEmail2) {
  537. if (email.isNotEmpty()) {
  538. val emailFormat = customValidation.isValidEmail(email)
  539. if (emailFormat) {
  540. showErrorEmail2(false)
  541. }
  542. }
  543. }
  544. if (isErrorPass1) {
  545. if (password.isNotEmpty()) {
  546. showErrorPass1(false)
  547. }
  548. }
  549. if (isErrorPass2) {
  550. if (password.isNotEmpty()) {
  551. val isErrorPassSize = customValidation.isValidPassword(password)
  552. if (isErrorPassSize) {
  553. showErrorPass2(false)
  554. }
  555. }
  556. }
  557. if (signUpViewModel.liveDataSignUpStatus.value == true) {
  558. var progress by remember { mutableFloatStateOf(0.1F) }
  559. LaunchedEffect(key1 = Unit) {
  560. while (true) {
  561. for (i in 1..100) {
  562. progress = i.toFloat()/100F
  563. delay(150)
  564. }
  565. progress = 0.1F
  566. }
  567. }
  568. CircularProgressIndicator(
  569. progress = { progress },
  570. modifier = Modifier
  571. .align(Alignment.Center)
  572. .size(50.dp),
  573. color = colorResource(id = R.color.yellow_text),
  574. strokeWidth = 5.dp,
  575. )
  576. }
  577. Button(
  578. onClick = {
  579. // navHostController.popBackStack()
  580. // navHostController.navigate(Screen.BottomBarMainScreen.route)
  581. val isErrors = customValidation.isValidText(email, "Email")
  582. showErrorEmail1(!isErrors)
  583. val isErrors4 = customValidation.isValidEmail(email)
  584. if (email.isNotEmpty()) {
  585. showErrorEmail2(!isErrors4)
  586. }
  587. val isErrors2 = customValidation.isValidText(password, "Password")
  588. showErrorPass1(!isErrors2)
  589. val isError3 = customValidation.isValidPassword(password)
  590. if (password.isNotEmpty()) {
  591. showErrorPass2(!isError3)
  592. }
  593. if (signUpViewModel.liveDataSignUpStatus.value == false) {
  594. Log.d("test_button", "onClick")
  595. if (customValidation.isValidText(email, "Email") &&
  596. customValidation.isValidText(password, "Password") &&
  597. customValidation.isValidPassword(password)) {
  598. if (customValidation.isValidEmail(email)) {
  599. signUpViewModel.setSignUpStatus(true)
  600. signUpViewModel.signUp(email, password)
  601. }
  602. }
  603. }
  604. },
  605. modifier = Modifier
  606. .padding(
  607. start = 15.dp, end = 15.dp,
  608. bottom = 100.dp
  609. )
  610. .align(Alignment.BottomCenter)
  611. .background(colorResource(id = R.color.transparent))
  612. .fillMaxWidth()
  613. .height(60.dp),
  614. shape = RoundedCornerShape(15.dp),
  615. // border = BorderStroke(25.dp, colorResource(id = R.color.black)),
  616. colors = ButtonDefaults.buttonColors(
  617. contentColor = colorResource(id = R.color.light_blue),
  618. containerColor = colorResource(id = R.color.white),
  619. ),
  620. )
  621. {
  622. Text(text = "Sign Up",
  623. style = MaterialTheme.typography.titleMedium,
  624. textAlign = TextAlign.Center
  625. )
  626. val signUpData = signUpViewModel.liveDataSignUp.observeAsState().value
  627. signUpData?.let { response ->
  628. signUpViewModel.setSignUpStatus(false)
  629. if (response.status) {
  630. signUpViewModel.setSignUpStatus(true)
  631. loginViewModel.loginRequest(
  632. email, password, "android", "11", "3.2.4"
  633. )
  634. } else {
  635. response.message?.let {
  636. coroutineScope.launch {
  637. snackBarState.showSnackbar(it)
  638. }
  639. }
  640. // Toast.makeText(context, response.message, Toast.LENGTH_SHORT).show()
  641. }
  642. signUpViewModel.mutableLiveData.value = null
  643. }
  644. val loginData by loginViewModel.liveDataUserResponse.observeAsState()
  645. loginData?.let { response ->
  646. Log.d("test_api_response live", "live: ${loginData?.status} ${loginData?.message}")
  647. signUpViewModel.setSignUpStatus(false)
  648. if (response.status) {
  649. response.data?.let {
  650. prefHelper.setLoggedInState(true)
  651. prefHelper.savePassword(password)
  652. prefHelper.saveUser(it)
  653. it.wireguard?.let { it1 -> prefHelper.saveWireGuard(it1) }
  654. it.product?.let { it1 -> prefHelper.saveProduct(it1) }
  655. prefHelper.saveEnabledProtocols(it.enabled_protocols)
  656. prefHelper.saveAvailableProtocols(it.available_protocols)
  657. prefHelper.saveXPlatformToken(it.userinfo?.email + "_" + System.currentTimeMillis())
  658. prefHelper.saveAdBlockState(false)
  659. prefHelper.saveTheme(themesList[0])
  660. it.servers?.let {
  661. prefHelper.saveServerData(it)
  662. }
  663. Log.d("bearer_token", it.token.toString())
  664. // upgradePriceViewModel.getProducts()
  665. prefHelper.getFcmToken().let {
  666. loginViewModel.sendFcmToken(it)
  667. Log.d("Refreshed token: ","SignUp: $it")
  668. }
  669. }
  670. navHostController.popBackStack()
  671. navHostController.navigate(Screen.BottomBarMainScreen.route)
  672. }
  673. loginViewModel.mutableLiveDataUserResponse.value = null
  674. }
  675. }
  676. }
  677. @Preview
  678. @Composable
  679. fun SignUpPreview() {
  680. SignUp(rememberNavController())
  681. }
  682. @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
  683. @Composable
  684. fun SignUpPreviewDark() {
  685. SignUp(rememberNavController())
  686. }