ServerItem.kt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. package com.vpn.fastestvpnservice.customItems
  2. import android.content.res.Configuration
  3. import android.util.Log
  4. import androidx.compose.foundation.background
  5. import androidx.compose.foundation.border
  6. import androidx.compose.foundation.clickable
  7. import androidx.compose.foundation.interaction.MutableInteractionSource
  8. import androidx.compose.foundation.layout.Arrangement
  9. import androidx.compose.foundation.layout.Box
  10. import androidx.compose.foundation.layout.Row
  11. import androidx.compose.foundation.layout.Spacer
  12. import androidx.compose.foundation.layout.fillMaxWidth
  13. import androidx.compose.foundation.layout.height
  14. import androidx.compose.foundation.layout.padding
  15. import androidx.compose.foundation.layout.size
  16. import androidx.compose.foundation.layout.wrapContentWidth
  17. import androidx.compose.foundation.shape.CircleShape
  18. import androidx.compose.material3.Icon
  19. import androidx.compose.material3.IconButton
  20. import androidx.compose.material3.MaterialTheme
  21. import androidx.compose.material3.Surface
  22. import androidx.compose.material3.Text
  23. import androidx.compose.runtime.Composable
  24. import androidx.compose.runtime.getValue
  25. import androidx.compose.runtime.mutableStateOf
  26. import androidx.compose.runtime.remember
  27. import androidx.compose.runtime.rememberCoroutineScope
  28. import androidx.compose.runtime.setValue
  29. import androidx.compose.ui.Alignment
  30. import androidx.compose.ui.Modifier
  31. import androidx.compose.ui.draw.alpha
  32. import androidx.compose.ui.draw.clip
  33. import androidx.compose.ui.draw.paint
  34. import androidx.compose.ui.graphics.Color
  35. import androidx.compose.ui.layout.ContentScale
  36. import androidx.compose.ui.platform.LocalContext
  37. import androidx.compose.ui.res.colorResource
  38. import androidx.compose.ui.res.painterResource
  39. import androidx.compose.ui.text.style.TextOverflow
  40. import androidx.compose.ui.tooling.preview.Preview
  41. import androidx.compose.ui.unit.dp
  42. import androidx.compose.ui.unit.sp
  43. import androidx.lifecycle.viewmodel.compose.viewModel
  44. import androidx.navigation.NavHostController
  45. import androidx.navigation.compose.rememberNavController
  46. import com.vpn.fastestvpnservice.R
  47. import com.vpn.fastestvpnservice.beans.Server
  48. import com.vpn.fastestvpnservice.beans.favListServer
  49. import com.vpn.fastestvpnservice.beans.toChangeServer
  50. import com.vpn.fastestvpnservice.constants.AppEnum
  51. import com.vpn.fastestvpnservice.constants.smartConnect
  52. import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
  53. import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.onServer
  54. import com.vpn.fastestvpnservice.screens.serverListViewModelSplash
  55. import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
  56. import com.vpn.fastestvpnservice.sealedClass.Screen
  57. import com.vpn.fastestvpnservice.utils.Utils
  58. import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
  59. @Composable
  60. fun ServerItem(server: Server, navHostController: NavHostController, serverPing: Int = 0) {
  61. val context = LocalContext.current
  62. val scope = rememberCoroutineScope()
  63. var isServerClicked by remember { mutableStateOf(false) }
  64. val homeViewModel: HomeViewModel = viewModel{ HomeViewModel(context, scope) }
  65. val basePreferenceHelper = BasePreferenceHelper(context)
  66. var isFavorite by remember { mutableStateOf(server.isFavourited) }
  67. val isServerFavourited: Boolean = server.isFavourited == true
  68. Log.d("test-server_fav_d", "$isFavorite")
  69. Box(
  70. modifier = Modifier
  71. .fillMaxWidth()
  72. .background(color = MaterialTheme.colorScheme.background)
  73. .padding(bottom = 1.dp)
  74. ) {
  75. // val serverListViewModel: ServerListViewModel = viewModel{
  76. // ServerListViewModel(context)
  77. // }
  78. // val splashViewModel: SplashViewModel = viewModel{
  79. // SplashViewModel(context)
  80. // }
  81. if (isServerClicked) {
  82. Log.d("ServerCallbacks", "isServerClicked $isServerClicked")
  83. onServer.onServerSelected(
  84. context,
  85. homeViewModel,
  86. onClick = { isServerClicked = false },
  87. true,
  88. server
  89. )
  90. navHostController.popBackStack()
  91. }
  92. // val icon = if (server.enable == 1) Utils.getDrawable(context, server.iso)
  93. // else Utils.getDrawableGray(context, server.iso)
  94. val icon = Utils.getDrawable(context, server.iso)
  95. var isFreeAccount: Boolean = false
  96. basePreferenceHelper.getProduct()?.identifier?.let {
  97. if (it == AppEnum.FREE.key) {
  98. isFreeAccount = true
  99. } else {
  100. isFreeAccount = false
  101. }
  102. }
  103. Box(
  104. // verticalAlignment = Alignment.Top,
  105. // horizontalArrangement = Arrangement.Start,
  106. modifier = Modifier
  107. .fillMaxWidth()
  108. .padding(start = 12.dp, end = 7.dp, top = 12.dp)
  109. .background(Color.Transparent)
  110. .clickable(
  111. indication = null,
  112. interactionSource = remember { MutableInteractionSource() }
  113. ) {
  114. if (!isFreeAccount) {
  115. if (toChangeServer.value) {
  116. basePreferenceHelper.saveSmartList(smartConnect[2])
  117. basePreferenceHelper.setSmartServerObject(server)
  118. navHostController.popBackStack()
  119. } else {
  120. isServerClicked = true
  121. }
  122. // basePreferenceHelper.setServerObject(server)
  123. } else {
  124. basePreferenceHelper.setSubscriptionServerObject(server)
  125. Screen.Subscription.isTrue = true
  126. navHostController.navigate(
  127. Screen.Subscription.route
  128. )
  129. }
  130. },
  131. contentAlignment = Alignment.CenterStart
  132. ) {
  133. // var ping by rememberSaveable { mutableIntStateOf(0) }
  134. //
  135. // fun updatePing(newPing: Int) {
  136. // ping = newPing
  137. // }
  138. //
  139. // LaunchedEffect(key1 = Unit) {
  140. // Log.d("test_new_ping", "Inside LE")
  141. //// while (true) {
  142. // Ping.onAddress(server.ip as String).setTimeOutMillis(1000).doPing(
  143. // object : Ping.PingListener{
  144. // override fun onResult(pingResult: PingResult?) {
  145. // pingResult?.let {
  146. // Log.d("test_new_ping_t", "pingResult = ${it.timeTaken.toInt()}, ping = $ping")
  147. // if (ping == 0) updatePing(it.timeTaken.toInt())
  148. // }
  149. // }
  150. // override fun onError(e: Exception?) {}
  151. // override fun onFinished(pingStats: PingStats?) {}
  152. // }
  153. // )
  154. //// delay(3 * 1000)
  155. //// }
  156. // }
  157. if (icon != 0) {
  158. Icon(
  159. painter = painterResource(id = icon),
  160. contentDescription = "Server Logo",
  161. tint = Color.Unspecified,
  162. modifier = Modifier
  163. .padding(top = 0.dp, bottom = 15.dp)
  164. .size(24.dp)
  165. .clip(CircleShape)
  166. .border(1.dp, colorResource(id = R.color.gray_opac_04), CircleShape)
  167. .paint(
  168. painter = painterResource(id = icon),
  169. contentScale = ContentScale.FillBounds
  170. )
  171. .align(Alignment.CenterStart)
  172. )
  173. Surface(
  174. modifier = Modifier
  175. .padding(start = 40.dp, bottom = 15.dp, end = 110.dp)
  176. .align(Alignment.CenterStart)
  177. .background(Color.Transparent),
  178. color = Color.Transparent
  179. ) {
  180. Text(
  181. text = "${server.server_name}",
  182. style = MaterialTheme.typography.labelMedium,
  183. color = MaterialTheme.colorScheme.primary,
  184. maxLines = 1,
  185. overflow = TextOverflow.Ellipsis,
  186. modifier = Modifier
  187. .align(Alignment.CenterStart)
  188. )
  189. }
  190. // Spacer(modifier = Modifier.weight(1F))
  191. Text(
  192. text = "$serverPing ms",
  193. style = MaterialTheme.typography.displayMedium,
  194. color = colorResource(id = R.color.blue_text),
  195. modifier = Modifier
  196. .padding(end = 50.dp, bottom = 15.dp)
  197. .align(Alignment.CenterEnd)
  198. )
  199. // Spacer(modifier = Modifier.weight(1F))
  200. IconButton(
  201. modifier = Modifier
  202. .padding(bottom = 15.dp, end = 4.dp)
  203. .size(25.dp)
  204. .align(Alignment.CenterEnd),
  205. onClick = {
  206. isFavorite = !isFavorite!!
  207. serverListViewModelSplash.favAndUnFav(server)
  208. }
  209. ) {
  210. Icon(
  211. painter = if (isServerFavourited) painterResource(
  212. id = R.drawable.fav_server3x
  213. ) else painterResource(
  214. id = R.drawable.unfav_server3x
  215. ),
  216. contentDescription = "Server Logo",
  217. tint = Color.Unspecified,
  218. modifier = Modifier.size(22.dp, 21.dp)
  219. )
  220. }
  221. }
  222. }
  223. if (icon != 0) {
  224. Surface(
  225. modifier = Modifier
  226. .padding(start = 0.dp, end = 0.dp)
  227. .height(1.dp)
  228. .fillMaxWidth()
  229. .alpha(0.6F)
  230. .align(Alignment.BottomCenter),
  231. color = colorResource(id = R.color.gray_icon)
  232. ) {}
  233. }
  234. }
  235. }
  236. @Composable
  237. fun FavoriteServerItem(
  238. server: Server,
  239. navHostController: NavHostController,
  240. serverPing: Int
  241. ) {
  242. val context = LocalContext.current
  243. val basePreferenceHelper = BasePreferenceHelper(context)
  244. // val serverListViewModel: ServerListViewModel = viewModel {
  245. // ServerListViewModel(context = context)
  246. // }
  247. // val splashViewModel: SplashViewModel = viewModel{
  248. // SplashViewModel(context)
  249. // }
  250. val scope = rememberCoroutineScope()
  251. val homeViewModel: HomeViewModel = viewModel {
  252. HomeViewModel(context, scope)
  253. }
  254. var isFavorite by remember { mutableStateOf(server.isFavourited) }
  255. val isServerFavourited: Boolean = server.isFavourited == true
  256. Log.d("test-server_fav_d", "$isFavorite")
  257. var isFavServerClicked by remember { mutableStateOf(false) }
  258. if (isFavServerClicked) {
  259. Log.d("ServerCallbacks", "isServerClicked $isFavServerClicked")
  260. onServer.onServerSelected(
  261. context,
  262. homeViewModel,
  263. onClick = { isFavServerClicked = false },
  264. true,
  265. server
  266. )
  267. navHostController.popBackStack(BottomBarScreen.Home.route, false)
  268. }
  269. Box(
  270. modifier = Modifier
  271. .fillMaxWidth()
  272. .background(MaterialTheme.colorScheme.background)
  273. .padding(bottom = 1.dp)
  274. ) {
  275. // val icon = if (server.enable == 1) Utils.getDrawable(context, server.iso)
  276. // else Utils.getDrawableGray(context, server.iso)
  277. val icon = Utils.getDrawable(context, server.iso)
  278. var isFreeAccount: Boolean = false
  279. basePreferenceHelper.getProduct()?.identifier?.let {
  280. isFreeAccount = if (it == AppEnum.FREE.key) {
  281. true
  282. } else {
  283. false
  284. }
  285. }
  286. Box(
  287. // verticalAlignment = Alignment.Top,
  288. // horizontalArrangement = Arrangement.Start,
  289. modifier = Modifier
  290. .fillMaxWidth()
  291. .padding(start = 12.dp, end = 7.dp, top = 12.dp)
  292. .clickable(
  293. indication = null,
  294. interactionSource = remember { MutableInteractionSource() }
  295. ) {
  296. if (!isFreeAccount) {
  297. isFavServerClicked = true
  298. } else {
  299. basePreferenceHelper.setSubscriptionServerObject(server)
  300. Screen.Subscription.isTrue = true
  301. navHostController.navigate(
  302. Screen.Subscription.route
  303. )
  304. }
  305. }
  306. ) {
  307. // var ping by rememberSaveable { mutableIntStateOf(0) }
  308. // fun updatePing(newPing: Int) {
  309. // ping = newPing
  310. // }
  311. // LaunchedEffect(key1 = Unit) {
  312. // Ping.onAddress(server.ip as String).setTimeOutMillis(1000).doPing(
  313. // object : Ping.PingListener{
  314. // override fun onResult(pingResult: PingResult?) {
  315. // android.os.Handler(Looper.getMainLooper()).post {
  316. // pingResult?.let {
  317. // if (ping == 0) updatePing(it.timeTaken.toInt())
  318. // }
  319. // }
  320. // }
  321. //
  322. // override fun onError(e: Exception?) {}
  323. // override fun onFinished(pingStats: PingStats?) {}
  324. // }
  325. // )
  326. // }
  327. if (icon != 0) {
  328. Icon(
  329. painter = painterResource(id = icon),
  330. contentDescription = "Server Logo",
  331. tint = Color.Unspecified,
  332. modifier = Modifier
  333. .padding(bottom = 15.dp)
  334. .size(24.dp)
  335. .clip(CircleShape)
  336. .border(1.dp, colorResource(id = R.color.gray_opac_04), CircleShape)
  337. .paint(
  338. painter = painterResource(id = icon),
  339. contentScale = ContentScale.FillBounds
  340. )
  341. )
  342. Surface(
  343. modifier = Modifier
  344. .padding(start = 40.dp, bottom = 15.dp, end = 110.dp)
  345. .align(Alignment.CenterStart)
  346. .background(Color.Transparent),
  347. color = Color.Transparent
  348. ) {
  349. Text(
  350. text = server.server_name!!,
  351. color = MaterialTheme.colorScheme.primary,
  352. style = MaterialTheme.typography.labelMedium,
  353. maxLines = 1,
  354. overflow = TextOverflow.Ellipsis,
  355. modifier = Modifier
  356. .align(Alignment.CenterStart)
  357. )
  358. }
  359. // Spacer(modifier = Modifier.weight(1F))
  360. Text(
  361. text = "$serverPing ms",
  362. color = colorResource(id = R.color.blue_text),
  363. style = MaterialTheme.typography.displayMedium,
  364. modifier = Modifier
  365. .padding(end = 50.dp, bottom = 15.dp)
  366. .align(Alignment.CenterEnd)
  367. )
  368. // Spacer(modifier = Modifier.weight(1F))
  369. IconButton(
  370. onClick = {
  371. isFavorite = !isFavorite!!
  372. serverListViewModelSplash.favAndUnFav(server)
  373. },
  374. modifier = Modifier
  375. .padding(bottom = 15.dp, end = 4.dp)
  376. .size(25.dp)
  377. .align(Alignment.CenterEnd),
  378. // .clickable(
  379. // indication = null,
  380. // interactionSource = remember { MutableInteractionSource() }
  381. // ) { },
  382. ) {
  383. Icon(
  384. painter = if (isServerFavourited) painterResource(
  385. id = R.drawable.fav_server3x
  386. ) else painterResource(
  387. id = R.drawable.unfav_server3x
  388. ),
  389. contentDescription = "Server Logo",
  390. tint = Color.Unspecified,
  391. modifier = Modifier.size(22.dp, 21.dp)
  392. )
  393. }
  394. }
  395. }
  396. if (icon != 0) {
  397. Surface(
  398. modifier = Modifier
  399. .padding(start = 0.dp, end = 0.dp)
  400. .height(1.dp)
  401. .fillMaxWidth()
  402. .alpha(0.6F)
  403. .align(Alignment.BottomCenter),
  404. color = colorResource(id = R.color.gray_icon)
  405. ) {}
  406. }
  407. }
  408. }
  409. @Composable
  410. fun ServerSearchItem(
  411. server: Server,
  412. navHostController: NavHostController,
  413. bgColor: Color = Color.White,
  414. textColor: Color = MaterialTheme.colorScheme.primary,
  415. isServerCallbackShown: Boolean = true,
  416. serverPing: Int,
  417. onServerDisable: () -> Unit
  418. ) {
  419. val context = LocalContext.current
  420. val scope = rememberCoroutineScope()
  421. val basePreferenceHelper = BasePreferenceHelper(context)
  422. // val serverListViewModel: ServerListViewModel = viewModel {
  423. // ServerListViewModel(context = context)
  424. // }
  425. // val splashViewModel: SplashViewModel = viewModel{
  426. // SplashViewModel(context)
  427. // }
  428. // val searchListViewModel: SearchListViewModel = viewModel{
  429. // SearchListViewModel(context, serverListViewModel, splashViewModel)
  430. // }
  431. val homeViewModel: HomeViewModel = viewModel{
  432. HomeViewModel(context, scope)
  433. }
  434. var isSearchServerClicked by remember {
  435. mutableStateOf(false)
  436. }
  437. var isFavorite by remember { mutableStateOf(server.isFavourited) }
  438. val isServerFavourited: Boolean = server.isFavourited == true
  439. Log.d("test-server_fav_d", "$isFavorite")
  440. if (isSearchServerClicked) {
  441. Log.d("ServerCallbacks", "isServerClicked $isSearchServerClicked")
  442. onServer.onServerSelected(
  443. context,
  444. homeViewModel,
  445. onClick = { isSearchServerClicked = false },
  446. true,
  447. server
  448. )
  449. navHostController.popBackStack()
  450. }
  451. Box(
  452. modifier = Modifier
  453. .fillMaxWidth()
  454. .background(color = MaterialTheme.colorScheme.onBackground)
  455. .padding(bottom = 1.dp)
  456. ) {
  457. // val icon = if (server.enable == 1) Utils.getDrawable(context, server.iso)
  458. // else Utils.getDrawableGray(context, server.iso)
  459. val icon = Utils.getDrawable(context, server.iso)
  460. var isFreeAccount: Boolean = false
  461. basePreferenceHelper.getProduct()?.identifier?.let {
  462. if (it == AppEnum.FREE.key) {
  463. isFreeAccount = true
  464. } else {
  465. isFreeAccount = false
  466. }
  467. }
  468. Box(
  469. // verticalAlignment = Alignment.Top,
  470. // horizontalArrangement = Arrangement.Start,
  471. modifier = Modifier
  472. .fillMaxWidth()
  473. .padding(start = 12.dp, end = 7.dp, top = 12.dp)
  474. .clickable(
  475. indication = null,
  476. interactionSource = remember { MutableInteractionSource() }
  477. ) {
  478. if (!isFreeAccount) {
  479. if (toChangeServer.value) {
  480. basePreferenceHelper.saveSmartList(smartConnect[2])
  481. basePreferenceHelper.setSmartServerObject(server)
  482. navHostController.popBackStack()
  483. } else {
  484. isSearchServerClicked = isServerCallbackShown
  485. }
  486. // basePreferenceHelper.setServerObject(server)
  487. if (!isServerCallbackShown) {
  488. onServerDisable()
  489. }
  490. } else {
  491. basePreferenceHelper.setSubscriptionServerObject(server)
  492. Screen.Subscription.isTrue = true
  493. navHostController.navigate(
  494. Screen.Subscription.route
  495. )
  496. }
  497. }
  498. )
  499. {
  500. // var ping by rememberSaveable { mutableIntStateOf(0) }
  501. // fun updatePing(newPing: Int) {
  502. // ping = newPing
  503. // }
  504. // LaunchedEffect(key1 = Unit) {
  505. // Log.d("test_new_ping", "Inside LE")
  506. //// while (true) {
  507. // Ping.onAddress(server.ip as String).setTimeOutMillis(1000).doPing(
  508. // object : Ping.PingListener{
  509. // override fun onResult(pingResult: PingResult?) {
  510. // pingResult?.let {
  511. // Log.d("test_new_ping", "pingResult = ${it.timeTaken.toInt()}")
  512. // if (ping == 0) updatePing(it.timeTaken.toInt())
  513. // }
  514. // }
  515. // override fun onError(e: Exception?) {}
  516. // override fun onFinished(pingStats: PingStats?) {}
  517. // }
  518. // )
  519. //// delay(3 * 1000)
  520. //// }
  521. // }
  522. if (icon != 0) {
  523. Icon(
  524. painter = painterResource(id = icon),
  525. contentDescription = "Server Logo",
  526. tint = Color.Unspecified,
  527. modifier = Modifier
  528. .padding(bottom = 15.dp)
  529. .size(24.dp)
  530. .clip(CircleShape)
  531. .border(1.dp, colorResource(id = R.color.gray_opac_04), CircleShape)
  532. .paint(
  533. painter = painterResource(id = icon),
  534. contentScale = ContentScale.FillBounds
  535. )
  536. .align(Alignment.CenterStart)
  537. )
  538. Surface(
  539. modifier = Modifier
  540. .padding(start = 40.dp, bottom = 15.dp, end = 110.dp)
  541. .align(Alignment.CenterStart)
  542. .background(Color.Transparent),
  543. color = Color.Transparent
  544. ) {
  545. Text(
  546. text = server.server_name!!,
  547. color = textColor,
  548. style = MaterialTheme.typography.labelMedium,
  549. maxLines = 1,
  550. overflow = TextOverflow.Ellipsis,
  551. modifier = Modifier
  552. .align(Alignment.CenterStart)
  553. )
  554. }
  555. // Spacer(modifier = Modifier.weight(1F))
  556. Text(
  557. text = "$serverPing ms",
  558. color = colorResource(id = R.color.blue_text),
  559. style = MaterialTheme.typography.displayMedium,
  560. modifier = Modifier
  561. .padding(end = 50.dp, bottom = 15.dp)
  562. .align(Alignment.CenterEnd)
  563. )
  564. IconButton(
  565. modifier = Modifier
  566. .padding(bottom = 15.dp, end = 4.dp)
  567. .size(25.dp)
  568. .align(Alignment.CenterEnd),
  569. onClick = {
  570. isFavorite = !isFavorite!!
  571. serverListViewModelSplash.favAndUnFav(server)
  572. }) {
  573. Icon(
  574. painter = if (isServerFavourited) painterResource(
  575. id = R.drawable.fav_server3x
  576. ) else painterResource(
  577. id = R.drawable.unfav_server3x
  578. ),
  579. contentDescription = "Server Logo",
  580. tint = Color.Unspecified,
  581. modifier = Modifier.size(22.dp, 21.dp),
  582. )
  583. }
  584. }
  585. }
  586. if (icon != 0) {
  587. Surface(
  588. modifier = Modifier
  589. .padding(start = 0.dp, end = 0.dp)
  590. .height(1.dp)
  591. .fillMaxWidth()
  592. .alpha(0.6F)
  593. .align(Alignment.BottomCenter),
  594. color = colorResource(id = R.color.gray_icon)
  595. ) {}
  596. }
  597. }
  598. }
  599. @Composable
  600. fun ServerSpecificItem(
  601. server: Server,
  602. navHostController: NavHostController,
  603. onServerDisable: () -> Unit
  604. ) {
  605. val context = LocalContext.current
  606. val scope = rememberCoroutineScope()
  607. val basePreferenceHelper = BasePreferenceHelper(context)
  608. // val serverListViewModel: ServerListViewModel = viewModel {
  609. // ServerListViewModel(context = context)
  610. // }
  611. // val splashViewModel: SplashViewModel = viewModel{
  612. // SplashViewModel(context)
  613. // }
  614. // val searchListViewModel: SearchListViewModel = viewModel{
  615. // SearchListViewModel(context, serverListViewModel, splashViewModel)
  616. // }
  617. // val homeViewModel: HomeViewModel = viewModel{
  618. // HomeViewModel(context, scope)
  619. // }
  620. var isSearchServerClicked by remember {
  621. mutableStateOf(false)
  622. }
  623. Box(
  624. modifier = Modifier
  625. .fillMaxWidth()
  626. .background(color = MaterialTheme.colorScheme.background)
  627. .padding(bottom = 1.dp)
  628. ) {
  629. // val icon = if (server.enable == 1) Utils.getDrawable(context, server.iso)
  630. // else Utils.getDrawableGray(context, server.iso)
  631. val icon = Utils.getDrawable(context, server.iso)
  632. var isFreeAccount: Boolean = false
  633. basePreferenceHelper.getProduct()?.identifier?.let {
  634. if (it == AppEnum.FREE.key) {
  635. isFreeAccount = true
  636. } else {
  637. isFreeAccount = false
  638. }
  639. }
  640. Row(
  641. verticalAlignment = Alignment.Top,
  642. horizontalArrangement = Arrangement.Start,
  643. modifier = Modifier
  644. .fillMaxWidth()
  645. .padding(start = 12.dp, end = 7.dp, top = 12.dp)
  646. .clickable(
  647. indication = null,
  648. interactionSource = remember { MutableInteractionSource() }
  649. ) {
  650. if (!isFreeAccount) {
  651. // MainActivity.isSelectedServersShown = true
  652. // basePreferenceHelper.setServerObject(server)
  653. basePreferenceHelper.saveSmartList(smartConnect[2])
  654. basePreferenceHelper.setSmartServerObject(server)
  655. onServerDisable()
  656. } else {
  657. basePreferenceHelper.setSubscriptionServerObject(server)
  658. Screen.Subscription.isTrue = true
  659. navHostController.navigate(
  660. Screen.Subscription.route
  661. )
  662. }
  663. }
  664. ) {
  665. if (icon != 0) {
  666. Icon(
  667. painter = painterResource(id = icon),
  668. contentDescription = "Server Logo",
  669. tint = Color.Unspecified,
  670. modifier = Modifier
  671. .padding(bottom = 16.dp)
  672. .size(24.dp)
  673. .clip(CircleShape)
  674. .border(1.dp, colorResource(id = R.color.gray_opac_04), CircleShape)
  675. .paint(
  676. painter = painterResource(id = icon),
  677. contentScale = ContentScale.FillBounds
  678. )
  679. )
  680. server.server_name?.let {
  681. Text(
  682. text = it,
  683. color = MaterialTheme.colorScheme.primary,
  684. style = MaterialTheme.typography.labelMedium,
  685. modifier = Modifier
  686. .padding(start = 16.dp, bottom = 18.dp)
  687. .align(Alignment.CenterVertically)
  688. )
  689. }
  690. }
  691. }
  692. if (icon != 0) {
  693. Surface(
  694. modifier = Modifier
  695. .padding(start = 0.dp, end = 0.dp)
  696. .height(1.dp)
  697. .fillMaxWidth()
  698. .alpha(0.6F)
  699. .align(Alignment.BottomCenter),
  700. color = colorResource(id = R.color.gray_icon)
  701. ) {}
  702. }
  703. }
  704. }
  705. @Preview
  706. @Composable
  707. fun ServerItemPreview() {
  708. ServerItem(server = favListServer[0], rememberNavController())
  709. }
  710. @Preview
  711. @Composable
  712. fun FavoriteServerItemPreview() {
  713. FavoriteServerItem(server = favListServer[0], rememberNavController(), 0)
  714. }
  715. @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
  716. @Composable
  717. fun FavoriteServerItemPreviewDark() {
  718. FavoriteServerItem(server = favListServer[0], rememberNavController(), 0)
  719. }
  720. @Preview
  721. @Composable
  722. fun ServerSearchItemPreview() {
  723. ServerSearchItem(server = favListServer[0], rememberNavController(), serverPing = 0) {}
  724. }