SignUpScreen.kt 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. package com.vpn.fastestvpnservice.screens
  2. import android.app.Activity
  3. import android.app.LocaleManager
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.content.res.Configuration
  7. import android.location.Location
  8. import android.os.Build
  9. import android.os.LocaleList
  10. import android.util.Log
  11. import androidx.appcompat.app.AppCompatDelegate
  12. import androidx.compose.foundation.Image
  13. import androidx.compose.foundation.background
  14. import androidx.compose.foundation.border
  15. import androidx.compose.foundation.gestures.detectTapGestures
  16. import androidx.compose.foundation.layout.Arrangement
  17. import androidx.compose.foundation.layout.Box
  18. import androidx.compose.foundation.layout.Column
  19. import androidx.compose.foundation.layout.ColumnScope
  20. import androidx.compose.foundation.layout.Row
  21. import androidx.compose.foundation.layout.Spacer
  22. import androidx.compose.foundation.layout.WindowInsets
  23. import androidx.compose.foundation.layout.fillMaxHeight
  24. import androidx.compose.foundation.layout.fillMaxSize
  25. import androidx.compose.foundation.layout.fillMaxWidth
  26. import androidx.compose.foundation.layout.height
  27. import androidx.compose.foundation.layout.padding
  28. import androidx.compose.foundation.layout.size
  29. import androidx.compose.foundation.layout.systemBars
  30. import androidx.compose.foundation.layout.width
  31. import androidx.compose.foundation.layout.windowInsetsPadding
  32. import androidx.compose.foundation.layout.wrapContentHeight
  33. import androidx.compose.foundation.shape.RoundedCornerShape
  34. import androidx.compose.foundation.text.ClickableText
  35. import androidx.compose.foundation.text.KeyboardActions
  36. import androidx.compose.foundation.text.KeyboardOptions
  37. import androidx.compose.material.icons.Icons
  38. import androidx.compose.material.icons.filled.Warning
  39. import androidx.compose.material.icons.outlined.Visibility
  40. import androidx.compose.material3.Button
  41. import androidx.compose.material3.ButtonDefaults
  42. import androidx.compose.material3.CircularProgressIndicator
  43. import androidx.compose.material3.Icon
  44. import androidx.compose.material3.IconButton
  45. import androidx.compose.material3.MaterialTheme
  46. import androidx.compose.material3.Scaffold
  47. import androidx.compose.material3.SnackbarHostState
  48. import androidx.compose.material3.Text
  49. import androidx.compose.material3.TextField
  50. import androidx.compose.material3.TextFieldDefaults
  51. import androidx.compose.runtime.Composable
  52. import androidx.compose.runtime.LaunchedEffect
  53. import androidx.compose.runtime.getValue
  54. import androidx.compose.runtime.livedata.observeAsState
  55. import androidx.compose.runtime.mutableFloatStateOf
  56. import androidx.compose.runtime.mutableStateOf
  57. import androidx.compose.runtime.remember
  58. import androidx.compose.runtime.rememberCoroutineScope
  59. import androidx.compose.runtime.setValue
  60. import androidx.compose.ui.Alignment
  61. import androidx.compose.ui.ExperimentalComposeUiApi
  62. import androidx.compose.ui.Modifier
  63. import androidx.compose.ui.draw.alpha
  64. import androidx.compose.ui.draw.paint
  65. import androidx.compose.ui.graphics.Color
  66. import androidx.compose.ui.graphics.toArgb
  67. import androidx.compose.ui.input.pointer.pointerInput
  68. import androidx.compose.ui.layout.ContentScale
  69. import androidx.compose.ui.platform.LocalContext
  70. import androidx.compose.ui.platform.LocalFocusManager
  71. import androidx.compose.ui.platform.LocalSoftwareKeyboardController
  72. import androidx.compose.ui.platform.LocalView
  73. import androidx.compose.ui.res.colorResource
  74. import androidx.compose.ui.res.painterResource
  75. import androidx.compose.ui.text.AnnotatedString
  76. import androidx.compose.ui.text.TextStyle
  77. import androidx.compose.ui.text.font.FontWeight
  78. import androidx.compose.ui.text.input.ImeAction
  79. import androidx.compose.ui.text.input.KeyboardType
  80. import androidx.compose.ui.text.input.PasswordVisualTransformation
  81. import androidx.compose.ui.text.input.VisualTransformation
  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.core.os.LocaleListCompat
  87. import androidx.lifecycle.viewmodel.compose.viewModel
  88. import androidx.navigation.NavHostController
  89. import androidx.navigation.compose.rememberNavController
  90. import com.vpn.fastestvpnservice.R
  91. import com.vpn.fastestvpnservice.beans.Server
  92. import com.vpn.fastestvpnservice.beans.filterList
  93. import com.vpn.fastestvpnservice.beans.isDarkTheme
  94. import com.vpn.fastestvpnservice.constants.smartConnect
  95. import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
  96. import com.vpn.fastestvpnservice.sealedClass.Screen
  97. import com.vpn.fastestvpnservice.ui.theme.customTypography
  98. import com.vpn.fastestvpnservice.ui.theme.outfitFontFamily
  99. import com.vpn.fastestvpnservice.viewmodels.LoginViewModel
  100. import com.vpn.fastestvpnservice.viewmodels.SearchListViewModel
  101. import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel
  102. import com.vpn.fastestvpnservice.viewmodels.SignUpViewModel
  103. import com.vpn.fastestvpnservice.viewmodels.SplashViewModel
  104. import com.vpn.fastestvpnservice.views.CustomValidation
  105. import com.vpn.fastestvpnservice.views.ShowCustomSnackBar
  106. import com.vpn.fastestvpnservice.views.setCustomLocale
  107. import com.vpn.fastestvpnservice.widgets.SimpleAppWidget
  108. import kotlinx.coroutines.delay
  109. import kotlinx.coroutines.launch
  110. import java.util.Locale
  111. @OptIn(ExperimentalComposeUiApi::class)
  112. @Composable
  113. fun SignUp(navHostController: NavHostController) {
  114. val keyboardController = LocalSoftwareKeyboardController.current
  115. val focusManager = LocalFocusManager.current
  116. val context = LocalContext.current
  117. val signUpViewModel: SignUpViewModel = viewModel()
  118. var isSignUpEnabled = signUpViewModel.liveDataSignUpStatus.observeAsState().value
  119. var textChanged by remember { mutableStateOf("") }
  120. var passwordChanged by remember { mutableStateOf("") }
  121. var passwordVisible by remember { mutableStateOf(false) }
  122. var showErrorEmail by remember { mutableStateOf(false) }
  123. var showErrorEmail2 by remember { mutableStateOf(false) }
  124. var showErrorPass1 by remember { mutableStateOf(false) }
  125. var showErrorPass2 by remember { mutableStateOf(false) }
  126. val snackBarState = remember { SnackbarHostState() }
  127. val snackBarStateRed = remember { SnackbarHostState() }
  128. Scaffold(
  129. content = { padding ->
  130. Box(
  131. modifier = Modifier
  132. .paint(
  133. painter = painterResource(id = if (isDarkTheme.value) R.drawable.bg_app else R.drawable.bg_img3),
  134. contentScale = ContentScale.FillBounds
  135. )
  136. .alpha(if (isSignUpEnabled!!) 0.6F else 1F)
  137. .fillMaxSize()
  138. .pointerInput(Unit) {
  139. detectTapGestures {
  140. focusManager.clearFocus()
  141. keyboardController?.hide()
  142. }
  143. }
  144. .windowInsetsPadding(WindowInsets.systemBars)
  145. ,
  146. ) {
  147. val view = LocalView.current
  148. val window = (view.context as Activity).window
  149. window.statusBarColor = Color.Transparent.toArgb()
  150. window.navigationBarColor = Color.Transparent.toArgb()
  151. ShowCustomSnackBar(snackBarState, R.color.switch_green, R.color.white)
  152. ShowCustomSnackBar(snackBarStateRed, R.color.Red, R.color.white)
  153. if (signUpViewModel.liveDataSignUpStatus.value == true) {
  154. var progress by remember { mutableFloatStateOf(0.1F) }
  155. LaunchedEffect(key1 = Unit) {
  156. while (true) {
  157. for (i in 1..100) {
  158. progress = i.toFloat() / 100F
  159. delay(150)
  160. }
  161. progress = 0.1F
  162. }
  163. }
  164. CircularProgressIndicator(
  165. progress = { progress },
  166. modifier = Modifier
  167. .align(Alignment.Center)
  168. .size(50.dp),
  169. color = colorResource(id = R.color.yellow_text),
  170. strokeWidth = 5.dp,
  171. )
  172. }
  173. // 1st
  174. Column(
  175. modifier = Modifier
  176. .fillMaxWidth()
  177. .wrapContentHeight()
  178. .background(Color.Transparent)
  179. .align(Alignment.TopCenter)
  180. .padding(bottom = 10.dp),
  181. verticalArrangement = Arrangement.SpaceBetween
  182. ) {
  183. IconButton(
  184. onClick = {
  185. if (!isSignUpEnabled) {
  186. navHostController.popBackStack()
  187. }
  188. },
  189. modifier = Modifier
  190. .padding(start = 16.dp, top = 8.dp)
  191. .align(Alignment.Start)
  192. )
  193. {
  194. Icon(
  195. painter = painterResource(
  196. id = R.drawable.back_arrow3x
  197. ),
  198. contentDescription = "Back Button",
  199. tint = colorResource(id = R.color.white),
  200. modifier = Modifier
  201. .height(36.dp)
  202. .width(36.dp)
  203. .align(Alignment.CenterHorizontally),
  204. )
  205. }
  206. Spacer(modifier = Modifier.height(0.dp))
  207. Image(
  208. painter = painterResource(id = R.drawable.fastestapp_logo3x),
  209. contentDescription = "FastestVPN",
  210. modifier = Modifier
  211. .size(75.dp, 102.dp)
  212. .background(Color.Transparent)
  213. .padding(top = 0.dp)
  214. .align(Alignment.CenterHorizontally),
  215. )
  216. }
  217. Spacer(modifier = Modifier.height(0.dp))
  218. // 2nd
  219. Column(
  220. modifier = Modifier
  221. .fillMaxWidth()
  222. .wrapContentHeight()
  223. .background(Color.Transparent)
  224. .align(Alignment.Center)
  225. .padding(top = 12.dp),
  226. verticalArrangement = Arrangement.Center,
  227. horizontalAlignment = Alignment.CenterHorizontally
  228. ) {
  229. Column(
  230. modifier = Modifier
  231. .fillMaxWidth()
  232. .wrapContentHeight(),
  233. verticalArrangement = Arrangement.Center
  234. ) {
  235. Text(
  236. modifier = Modifier
  237. .padding(start = 15.dp, top = 2.dp)
  238. .align(Alignment.Start)
  239. ,
  240. style = MaterialTheme.typography.displayLarge,
  241. text = context.getString(R.string.hello_there),
  242. color = Color.White,
  243. )
  244. Text(
  245. modifier = Modifier
  246. .padding(start = 15.dp, top = 4.dp)
  247. .align(Alignment.Start)
  248. .alpha(0.6F),
  249. style = MaterialTheme.typography.customTypography.labelLarge,
  250. text = context.getString(R.string.please_register),
  251. color = colorResource(id = R.color.white),
  252. )
  253. Spacer(modifier = Modifier.height(8.dp))
  254. val colorEmail = if (showErrorEmail || showErrorEmail2) {
  255. colorResource(id = R.color.red)
  256. } else {
  257. colorResource(id = R.color.white)
  258. }
  259. TextField(
  260. value = textChanged,
  261. onValueChange = {
  262. textChanged = it
  263. },
  264. readOnly = isSignUpEnabled,
  265. textStyle = MaterialTheme.typography.customTypography.bodyMedium,
  266. modifier = Modifier
  267. .padding(start = 15.dp, end = 15.dp, top = 10.dp)
  268. .align(Alignment.Start)
  269. .fillMaxWidth()
  270. .height(60.dp)
  271. .border(
  272. 1.dp,
  273. color = colorEmail,
  274. shape = RoundedCornerShape(16.dp)
  275. ),
  276. shape = RoundedCornerShape(16.dp),
  277. // placeholder = {
  278. // Text(text = "Enter email address",
  279. // color = colorResource(id = R.color.white),
  280. // fontSize = 14.sp,
  281. // )
  282. // },
  283. label = {
  284. Text(text = context.getString(R.string.email),
  285. style = MaterialTheme.typography.customTypography.bodySmall
  286. )
  287. },
  288. leadingIcon = {
  289. Icon(
  290. painter = painterResource(id = R.drawable.sms3x),
  291. contentDescription = "Email Logo",
  292. tint = colorResource(id = R.color.white),
  293. modifier = Modifier
  294. .size(24.dp, 24.dp)
  295. )
  296. },
  297. maxLines = 1,
  298. colors = TextFieldDefaults.colors(
  299. focusedLabelColor = Color.Blue,
  300. unfocusedContainerColor = colorResource(id = R.color.transparent),
  301. focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
  302. focusedIndicatorColor = colorResource(id = R.color.transparent),
  303. disabledIndicatorColor = colorResource(id = R.color.transparent),
  304. unfocusedIndicatorColor = colorResource(id = R.color.transparent),
  305. cursorColor = colorResource(id = R.color.white)
  306. ),
  307. keyboardOptions = KeyboardOptions(
  308. keyboardType = KeyboardType.Email,
  309. imeAction = ImeAction.Done
  310. ),
  311. keyboardActions = KeyboardActions(
  312. onDone = {
  313. focusManager.clearFocus()
  314. keyboardController?.hide()
  315. }
  316. ),
  317. )
  318. Spacer(modifier = Modifier.height(20.dp))
  319. val colorPass = if (showErrorPass1 || showErrorPass2) {
  320. colorResource(id = R.color.red)
  321. } else {
  322. colorResource(id = R.color.white)
  323. }
  324. TextField(
  325. value = passwordChanged,
  326. onValueChange = {
  327. Log.d("onClick_test", "onValueChange -> ")
  328. passwordChanged = it
  329. },
  330. readOnly = isSignUpEnabled,
  331. textStyle = MaterialTheme.typography.customTypography.bodyMedium,
  332. modifier = Modifier
  333. .padding(start = 15.dp, end = 15.dp)
  334. .align(Alignment.Start)
  335. .fillMaxWidth()
  336. .height(60.dp)
  337. .border(
  338. 1.dp,
  339. color = colorPass,
  340. shape = RoundedCornerShape(16.dp)
  341. )
  342. .background(color = colorResource(id = R.color.transparent)),
  343. shape = RoundedCornerShape(16.dp),
  344. // placeholder = {
  345. // Text(text = "Enter password",
  346. // color = colorResource(id = R.color.white))
  347. // },
  348. label = {
  349. Text(text = context.getString(R.string.password),
  350. style = MaterialTheme.typography.customTypography.bodyLarge
  351. )
  352. },
  353. leadingIcon = {
  354. Icon(
  355. painter = painterResource(id = R.drawable.lock3x),
  356. contentDescription = "Password Logo",
  357. tint = colorResource(id = R.color.white),
  358. modifier = Modifier
  359. .size(24.dp, 24.dp)
  360. )
  361. },
  362. maxLines = 1,
  363. colors = TextFieldDefaults.colors(
  364. focusedLabelColor = Color.Blue,
  365. unfocusedContainerColor = colorResource(id = R.color.transparent),
  366. focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
  367. focusedIndicatorColor = colorResource(id = R.color.transparent),
  368. disabledIndicatorColor = colorResource(id = R.color.transparent),
  369. unfocusedIndicatorColor = colorResource(id = R.color.transparent),
  370. cursorColor = colorResource(id = R.color.white)
  371. ),
  372. keyboardOptions = KeyboardOptions(
  373. keyboardType = KeyboardType.Password,
  374. imeAction = ImeAction.Done
  375. ),
  376. keyboardActions = KeyboardActions(
  377. onDone = {
  378. focusManager.clearFocus()
  379. keyboardController?.hide()
  380. }
  381. ),
  382. visualTransformation =
  383. if (passwordVisible) VisualTransformation.None
  384. else PasswordVisualTransformation(),
  385. trailingIcon = {
  386. Log.d("onClick_test", "trailingIcon -> ")
  387. // val image
  388. // = if (passwordVisible) Icons.Filled.Visibility
  389. // else Icons.Filled.VisibilityOff
  390. val description = if (passwordVisible) "Hide Password"
  391. else "Show Password"
  392. IconButton(onClick = {
  393. if (!isSignUpEnabled) {
  394. passwordVisible = !passwordVisible
  395. }
  396. })
  397. {
  398. if (passwordVisible) {
  399. Icon(
  400. imageVector = Icons.Outlined.Visibility,
  401. contentDescription = description,
  402. tint = colorResource(id = R.color.white),
  403. modifier = Modifier.size(24.dp)
  404. )
  405. }
  406. else {
  407. Icon(
  408. painter = painterResource(id = R.drawable.eye_slash3x),
  409. contentDescription = description,
  410. tint = colorResource(id = R.color.white),
  411. modifier = Modifier.size(24.dp)
  412. )
  413. }
  414. }
  415. }
  416. )
  417. Spacer(modifier = Modifier.height(10.dp))
  418. Row (
  419. modifier = Modifier
  420. .padding(start = 15.dp, top = 15.dp)
  421. .fillMaxWidth(),
  422. ){
  423. val colorLogo = if (showErrorEmail || showErrorEmail2 || showErrorPass1 || showErrorPass2) colorResource(
  424. id = R.color.red
  425. ) else colorResource(id = R.color.white)
  426. Icon(
  427. painter = painterResource(id = R.drawable.tick_square3x),
  428. contentDescription = "Logo",
  429. tint = colorLogo,
  430. modifier = Modifier.size(24.dp, 24.dp)
  431. )
  432. Text(
  433. modifier = Modifier
  434. .padding(start = 15.dp),
  435. style = MaterialTheme.typography.customTypography.bodyLarge,
  436. text = context.getString(R.string.by_creating_acc),
  437. color = colorResource(id = R.color.white),
  438. )
  439. }
  440. Spacer(modifier = Modifier.height(0.dp))
  441. Row (
  442. modifier = Modifier
  443. .padding(start = 15.dp)
  444. .fillMaxWidth(),
  445. ){
  446. Icon(
  447. painter = painterResource(id = R.drawable.tick_square3x),
  448. contentDescription = "Logo",
  449. tint = colorResource(id = R.color.transparent),
  450. modifier = Modifier.size(24.dp),
  451. )
  452. ClickableText(
  453. onClick = {
  454. if (!isSignUpEnabled) {
  455. navHostController.navigate(
  456. Screen.TermsAndConditions.route
  457. )
  458. }
  459. },
  460. modifier = Modifier
  461. .padding(start = 15.dp),
  462. text = AnnotatedString(context.getString(R.string.termsndcond)),
  463. style = MaterialTheme.typography.customTypography.titleLarge
  464. )
  465. }
  466. }
  467. }
  468. // 3rd
  469. Column(
  470. modifier = Modifier
  471. .background(Color.Transparent)
  472. .align(Alignment.BottomCenter)
  473. .padding(top = 5.dp),
  474. ) {
  475. SignUpButton(
  476. signUpViewModel,
  477. textChanged,
  478. passwordChanged,
  479. navHostController,
  480. showErrorEmail1 = {
  481. showErrorEmail = it
  482. },
  483. showErrorEmail,
  484. showErrorPass1 = {
  485. showErrorPass1 = it
  486. },
  487. showErrorPass1,
  488. showErrorPass2 = {
  489. showErrorPass2 = it
  490. },
  491. showErrorPass2,
  492. showErrorEmail2 = {
  493. showErrorEmail2 = it
  494. },
  495. showErrorEmail2,
  496. snackBarState,
  497. snackBarStateRed
  498. )
  499. Row(
  500. modifier = Modifier
  501. .padding(bottom = 25.dp)
  502. .align(Alignment.CenterHorizontally),
  503. horizontalArrangement = Arrangement.Center
  504. ) {
  505. Text(
  506. modifier = Modifier
  507. .padding(0.dp),
  508. style = MaterialTheme.typography.customTypography.labelLarge,
  509. text = "${context.getString(R.string.already_have_acc)} ",
  510. color = Color.White,
  511. )
  512. ClickableText(
  513. onClick = {
  514. if (!isSignUpEnabled) {
  515. navHostController.popBackStack()
  516. if (navHostController.currentDestination?.id != null &&
  517. navHostController.currentDestination?.route != null
  518. ) {
  519. Log.d(
  520. "test_signup",
  521. "From Sign In -> id = " + navHostController.currentDestination?.id
  522. + " route = " + navHostController.currentDestination?.route
  523. )
  524. } else {
  525. Log.d(
  526. "test_signup",
  527. "From Sign Up -> id = " + navHostController.currentDestination?.id
  528. + " route = " + navHostController.currentDestination?.route
  529. )
  530. navHostController.navigate(Screen.Login.route)
  531. }
  532. }
  533. },
  534. modifier = Modifier
  535. .padding(0.dp),
  536. style = MaterialTheme.typography.customTypography.titleMedium,
  537. text = AnnotatedString(" ${context.getString(R.string.signin)}"),
  538. )
  539. }
  540. }
  541. }
  542. }
  543. )
  544. }
  545. @Composable
  546. fun ColumnScope.ShowErrorRowSignUp(
  547. errorText: String
  548. ) {
  549. Row(
  550. verticalAlignment = Alignment.CenterVertically,
  551. modifier = Modifier
  552. .align(Alignment.Start)
  553. .padding(start = 16.dp, top = 8.dp)
  554. ) {
  555. Icon(imageVector = Icons.Default.Warning,
  556. contentDescription = "Error",
  557. tint = colorResource(id = R.color.red),
  558. modifier = Modifier.size(14.dp)
  559. )
  560. Text(text = errorText,
  561. style = TextStyle(
  562. fontFamily = outfitFontFamily,
  563. fontWeight = FontWeight.Normal,
  564. fontSize = 14.sp,
  565. color = colorResource(id = R.color.red)
  566. ),
  567. modifier = Modifier.padding(start = 5.dp)
  568. )
  569. }
  570. }
  571. @Composable
  572. fun ColumnScope.SignUpButton(
  573. signUpViewModel: SignUpViewModel,
  574. email: String,
  575. password: String,
  576. navHostController: NavHostController,
  577. showErrorEmail1: (Boolean) -> Unit,
  578. isErrorEmail1: Boolean,
  579. showErrorPass1: (Boolean) -> Unit,
  580. isErrorPass1: Boolean,
  581. showErrorPass2: (Boolean) -> Unit,
  582. isErrorPass2: Boolean,
  583. showErrorEmail2: (Boolean) -> Unit,
  584. isErrorEmail2: Boolean,
  585. snackBarState: SnackbarHostState,
  586. snackBarStateRed: SnackbarHostState
  587. ) {
  588. val loginViewModel: LoginViewModel = viewModel()
  589. val context = LocalContext.current
  590. val prefHelper = BasePreferenceHelper(context)
  591. val customValidation = CustomValidation()
  592. val coroutineScope = rememberCoroutineScope()
  593. if (isErrorEmail1) {
  594. if (email.isNotEmpty()) {
  595. showErrorEmail1(false)
  596. }
  597. }
  598. if (isErrorEmail2) {
  599. if (email.isNotEmpty()) {
  600. val emailFormat = customValidation.isValidEmail(email)
  601. if (emailFormat) {
  602. showErrorEmail2(false)
  603. }
  604. }
  605. }
  606. if (isErrorPass1) {
  607. if (password.isNotEmpty()) {
  608. showErrorPass1(false)
  609. }
  610. }
  611. if (isErrorPass2) {
  612. if (password.isNotEmpty()) {
  613. val isErrorPassSize = customValidation.isValidPassword(password)
  614. if (isErrorPassSize) {
  615. showErrorPass2(false)
  616. }
  617. }
  618. }
  619. Button(
  620. onClick = {
  621. // navHostController.popBackStack()
  622. // navHostController.navigate(Screen.BottomBarMainScreen.route)
  623. val isErrors = customValidation.isValidText(email, "Email")
  624. showErrorEmail1(!isErrors)
  625. val isErrors4 = customValidation.isValidEmail(email)
  626. if (email.isNotEmpty()) {
  627. showErrorEmail2(!isErrors4)
  628. }
  629. val isErrors2 = customValidation.isValidText(password, "Password")
  630. showErrorPass1(!isErrors2)
  631. val isError3 = customValidation.isValidPassword(password)
  632. if (password.isNotEmpty()) {
  633. showErrorPass2(!isError3)
  634. }
  635. if (!isErrors) {
  636. // ShowErrorRow(errorText = "Email is Empty")
  637. coroutineScope.launch {
  638. snackBarStateRed.showSnackbar("Email is Empty")
  639. }
  640. }
  641. else if (!isErrors4) {
  642. // ShowErrorRow(errorText = "Email format incorrect")
  643. coroutineScope.launch {
  644. snackBarStateRed.showSnackbar("Email format incorrect")
  645. }
  646. }
  647. else if (!isErrors2) {
  648. // ShowErrorRow(errorText = "Password is Empty")
  649. coroutineScope.launch {
  650. snackBarStateRed.showSnackbar("Password is Empty")
  651. }
  652. }
  653. else if (!isError3) {
  654. // ShowErrorRow(errorText = "Should be 3 or more!")
  655. coroutineScope.launch {
  656. snackBarStateRed.showSnackbar("Password should be 3 or more!")
  657. }
  658. }
  659. if (signUpViewModel.liveDataSignUpStatus.value == false) {
  660. Log.d("test_button", "onClick")
  661. if (customValidation.isValidText(email, "Email") &&
  662. customValidation.isValidText(password, "Password") &&
  663. customValidation.isValidPassword(password)) {
  664. if (customValidation.isValidEmail(email)) {
  665. signUpViewModel.setSignUpStatus(true)
  666. signUpViewModel.signUp(email, password)
  667. }
  668. }
  669. }
  670. },
  671. modifier = Modifier
  672. .padding(
  673. start = 15.dp, end = 15.dp,
  674. bottom = 25.dp
  675. )
  676. .background(colorResource(id = R.color.transparent))
  677. .fillMaxWidth()
  678. .height(60.dp),
  679. shape = RoundedCornerShape(15.dp),
  680. // border = BorderStroke(25.dp, colorResource(id = R.color.black)),
  681. colors = ButtonDefaults.buttonColors(
  682. contentColor = MaterialTheme.colorScheme.primaryContainer,
  683. containerColor = MaterialTheme.colorScheme.onSecondaryContainer,
  684. ),
  685. )
  686. {
  687. Text(text = context.getString(R.string.signup),
  688. style = MaterialTheme.typography.titleMedium,
  689. textAlign = TextAlign.Center
  690. )
  691. val signUpData = signUpViewModel.liveDataSignUp.observeAsState().value
  692. signUpData?.let { response ->
  693. signUpViewModel.setSignUpStatus(false)
  694. if (response.status) {
  695. signUpViewModel.setSignUpStatus(true)
  696. loginViewModel.loginRequest(
  697. email, password, "android", "11", "3.2.4"
  698. )
  699. } else {
  700. response.message?.let {
  701. coroutineScope.launch {
  702. snackBarStateRed.showSnackbar(it)
  703. }
  704. }
  705. // Toast.makeText(context, response.message, Toast.LENGTH_SHORT).show()
  706. }
  707. signUpViewModel.mutableLiveData.value = null
  708. }
  709. val loginData by loginViewModel.liveDataUserResponse.observeAsState()
  710. loginData?.let { response ->
  711. Log.d("test_api_response live", "live: ${loginData?.status} ${loginData?.message}")
  712. signUpViewModel.setSignUpStatus(false)
  713. if (response.status) {
  714. response.data?.let {
  715. prefHelper.setLoggedInState(true)
  716. prefHelper.savePassword(password)
  717. prefHelper.saveUser(it)
  718. it.wireguard?.let { it1 -> prefHelper.saveWireGuard(it1) }
  719. it.product?.let { it1 -> prefHelper.saveProduct(it1) }
  720. prefHelper.saveEnabledProtocols(it.enabled_protocols)
  721. prefHelper.saveAvailableProtocols(it.available_protocols)
  722. prefHelper.saveXPlatformToken(it.userinfo?.email + "_" + System.currentTimeMillis())
  723. prefHelper.saveAdBlockState(false)
  724. // prefHelper.saveTheme(themesList[0])
  725. prefHelper.saveFilterList(filterList[0])
  726. prefHelper.saveSmartList(smartConnect[0])
  727. it.servers?.let {
  728. prefHelper.saveServerData(it)
  729. }
  730. Log.d("bearer_token", it.token.toString())
  731. // upgradePriceViewModel.getProducts()
  732. prefHelper.getFcmToken().let {
  733. loginViewModel.sendFcmToken(it)
  734. Log.d("Refreshed token: ","SignUp: $it")
  735. }
  736. }
  737. val smartLocationList: MutableList<Server> = ArrayList<Server>()
  738. prefHelper.getServerData().get(0).servers?.let {
  739. val serverDataLocation = it
  740. val distinctdatanotnull =
  741. serverDataLocation.filter { // servers's lt and lt not be null
  742. it.lt != null && it.lg != null
  743. }
  744. val distinctdata =
  745. distinctdatanotnull.distinctBy { // servers's lt filter, no same lt of a server
  746. it.lt
  747. }
  748. val result = FloatArray(1)
  749. val ipinfo = prefHelper.getIpinfo()
  750. distinctdata.forEachIndexed { index, server ->
  751. val lat1 = ipinfo?.latitute
  752. val lon1 = ipinfo?.longitude
  753. val lat2 = server.lt
  754. val lon2 = server.lg
  755. if (lat1 != null && lat2 != null && lon1 != null && lon2 != null) {
  756. Location.distanceBetween(lat1, lon1, lat2, lon2, result)
  757. }
  758. val distance: Float = result[0]
  759. distinctdata.get(index).distance = distance
  760. }
  761. val sortedDistance = distinctdata.sortedBy {
  762. it.distance
  763. }
  764. if (sortedDistance.isNotEmpty()) {
  765. for (i in 0..0) {
  766. smartLocationList.add(sortedDistance.get(i))
  767. }
  768. }
  769. smartLocationList.forEach {
  770. // prefHelper.setSmartServerObject(it)
  771. prefHelper.setRecommendedServerObject(it)
  772. Log.d("smartLocationList", "S:: server = ${it.server_name}")
  773. }
  774. }
  775. setCustomLocale(context)
  776. splashViewModelSplash = viewModel {
  777. SplashViewModel(context)
  778. }
  779. serverListViewModelSplash = viewModel {
  780. ServerListViewModel(context = context)
  781. }
  782. searchListViewModelSplash = viewModel {
  783. SearchListViewModel(context, serverListViewModelSplash, splashViewModelSplash)
  784. }
  785. serverListViewModelSplash.setRecommendedSmartServers()
  786. serverListViewModelSplash.setCountryData()
  787. val widgetIntent = Intent(context, SimpleAppWidget::class.java)
  788. widgetIntent.action = SimpleAppWidget.ACTION_LOGIN
  789. context.sendBroadcast(widgetIntent)
  790. navHostController.popBackStack()
  791. navHostController.navigate(Screen.BottomBarMainScreen.route)
  792. }
  793. loginViewModel.mutableLiveDataUserResponse.value = null
  794. }
  795. }
  796. }
  797. @Preview
  798. @Composable
  799. fun SignUpPreview() {
  800. }
  801. @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
  802. @Composable
  803. fun SignUpPreviewDark() {
  804. }