SignUpScreen.kt 31 KB

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