Browse Source

Implemented searching on server list screen TV

Khubaib 8 months ago
parent
commit
712e0c1716

+ 2 - 2
.idea/deploymentTargetSelector.xml

@@ -4,10 +4,10 @@
     <selectionStates>
       <SelectionState runConfigName="app">
         <option name="selectionMode" value="DROPDOWN" />
-        <DropdownSelection timestamp="2024-08-08T15:11:14.519629907Z">
+        <DropdownSelection timestamp="2024-08-08T16:58:59.461731583Z">
           <Target type="DEFAULT_BOOT">
             <handle>
-              <DeviceId pluginId="Default" identifier="serial=192.168.109.7:5555;connection=544c5340" />
+              <DeviceId pluginId="LocalEmulator" identifier="path=/home/ubuntu/.android/avd/Television_1080p_API_31.avd" />
             </handle>
           </Target>
         </DropdownSelection>

+ 248 - 101
app/src/main/java/com/vpn/fastestvpnservice/screensTV/ServerListScreenTV.kt

@@ -1,35 +1,63 @@
 package com.vpn.fastestvpnservice.screensTV
 
+import android.util.Log
 import androidx.compose.foundation.background
+import androidx.compose.foundation.border
 import androidx.compose.foundation.focusable
 import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.BoxScope
 import androidx.compose.foundation.layout.Column
 import androidx.compose.foundation.layout.fillMaxHeight
 import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.height
 import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
 import androidx.compose.foundation.lazy.LazyRow
 import androidx.compose.foundation.lazy.itemsIndexed
 import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.foundation.text.KeyboardActions
+import androidx.compose.foundation.text.KeyboardOptions
 import androidx.compose.foundation.verticalScroll
+import androidx.compose.material3.Icon
 import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Surface
 import androidx.compose.material3.Text
+import androidx.compose.material3.TextField
+import androidx.compose.material3.TextFieldDefaults
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.livedata.observeAsState
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.platform.LocalContext
+import androidx.compose.ui.platform.LocalSoftwareKeyboardController
 import androidx.compose.ui.res.colorResource
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.text.input.ImeAction
+import androidx.compose.ui.text.input.KeyboardType
 import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
 import androidx.navigation.NavHostController
 import com.vpn.fastestvpnservice.R
 import com.vpn.fastestvpnservice.customItems.ServerItemTV
+import com.vpn.fastestvpnservice.screens.searchListViewModelSplash
+import com.vpn.fastestvpnservice.screens.serverListViewModelSplash
+import com.vpn.fastestvpnservice.ui.theme.customTypography
+import kotlinx.coroutines.delay
 
 @Composable
 fun ServerListTV(navHostController: NavHostController) {
     val context = LocalContext.current
+    var textChanged by remember { mutableStateOf("") }
+    val keyboardController = LocalSoftwareKeyboardController.current
+
     Box(
         modifier = Modifier
             .fillMaxSize()
@@ -41,6 +69,225 @@ fun ServerListTV(navHostController: NavHostController) {
     ) {
         HeaderRowSL(navHostController = navHostController, title = "Locations")
 
+        TextField(
+            value = textChanged,
+            onValueChange = {
+                textChanged = it
+            },
+            textStyle = MaterialTheme.typography.customTypography.bodyMedium.copy(
+                color = colorResource(id = R.color.dark_blue_gray_text)
+            ),
+            modifier = Modifier
+                .padding(top = 60.dp, end = 0.dp)
+                .padding(horizontal = 0.dp)
+                .fillMaxWidth(fraction = 0.5f)
+                .align(Alignment.TopCenter)
+                .height(50.dp)
+                .border(
+                    1.dp,
+                    colorResource(id = R.color.white),
+                    shape = RoundedCornerShape(24.dp)
+                )
+                .background(color = colorResource(id = R.color.transparent))
+            ,
+//                .focusRequester(focusRequester1)
+//                            .onFocusChanged {
+////                                isTextFieldFocused = it.isFocused
+//                                focusRequester2.requestFocus()
+//                            }
+            shape = RoundedCornerShape(24.dp),
+            placeholder = {
+                Text(
+                    text = "Search Location",
+                    style = MaterialTheme.typography.customTypography.titleSmall.copy(
+                        color = colorResource(id = R.color.dark_blue_gray_text),
+                        fontSize = 14.sp
+                    )
+                )
+            },
+//            label = {
+//                Text(
+//                    text = "Search Location",
+//                    style = MaterialTheme.typography.customTypography.bodyLarge.copy(
+//                        color = colorResource(id = R.color.dark_blue_gray_text),
+//                        fontSize = 14.sp
+//                    )
+//                )
+//            },
+            leadingIcon = {
+                Icon(
+                    painter = painterResource(id = R.drawable.search3x),
+                    contentDescription = "Email Logo",
+                    tint = colorResource(id = R.color.dark_blue_gray_text),
+                    modifier = Modifier
+                        .size(20.dp, 20.dp)
+                )
+            },
+            maxLines = 1,
+            colors = TextFieldDefaults.colors(
+                focusedLabelColor = colorResource(id = R.color.dark_blue_gray_text),
+                unfocusedTextColor = colorResource(id = R.color.dark_blue_gray_text),
+                focusedTextColor = colorResource(id = R.color.dark_blue_gray_text),
+                disabledTextColor = colorResource(id = R.color.dark_blue_gray_text),
+                unfocusedContainerColor = colorResource(id = R.color.white),
+                focusedContainerColor = colorResource(id = R.color.white),
+                focusedIndicatorColor = colorResource(id = R.color.transparent),
+                disabledIndicatorColor = colorResource(id = R.color.transparent),
+                unfocusedIndicatorColor = colorResource(id = R.color.transparent),
+                cursorColor = colorResource(id = R.color.dark_blue_gray_text),
+            ),
+            keyboardOptions = KeyboardOptions(
+                keyboardType = KeyboardType.Email,
+                imeAction = ImeAction.Done
+            ),
+            keyboardActions = KeyboardActions(
+                onDone = {
+//                    focusManager.clearFocus()
+                    keyboardController?.hide()
+
+                }
+            ),
+        )
+
+        if (textChanged == "") {
+            Column(
+                modifier = Modifier
+                    .fillMaxSize()
+                    .padding(top = 130.dp, start = 24.dp, end = 24.dp, bottom = 24.dp)
+                    .verticalScroll(rememberScrollState())
+                    .background(Color.Transparent)
+            ) {
+                val topPadding = 25.dp
+
+                if (isLocationsEnabled.value) {
+                    Text(
+                        text = "Top Locations",
+                        style = MaterialTheme.typography.labelLarge,
+                        color = colorResource(id = R.color.dark_blue_gray_text),
+                        modifier = Modifier
+                            .padding(top = 0.dp, start = 0.dp)
+                            .focusable()
+                    )
+                    LazyRow(
+                        modifier = Modifier
+                            .padding(top = 10.dp, start = 0.dp, end = 0.dp)
+                            .background(Color.Transparent)
+                    ) {
+                        itemsIndexed(items = locations, itemContent = { index, server ->
+                            ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
+                        })
+                    }
+                }
+                if (isDedicatedIpEnabled.value) {
+                    Text(
+                        text = "Dedicated IP",
+                        style = MaterialTheme.typography.labelLarge,
+                        color = colorResource(id = R.color.dark_blue_gray_text),
+                        modifier = Modifier
+                            .padding(top = topPadding, start = 0.dp)
+                    )
+                    LazyRow(
+                        modifier = Modifier
+                            .padding(top = 10.dp, start = 0.dp, end = 0.dp)
+                            .background(Color.Transparent)
+                    ) {
+                        itemsIndexed(items = dedicatedIP, itemContent = { index, server ->
+                            ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
+                        })
+                    }
+                }
+                if (isStreamingEnabled.value) {
+                    Text(
+                        text = "Streaming",
+                        style = MaterialTheme.typography.labelLarge,
+                        color = colorResource(id = R.color.dark_blue_gray_text),
+                        modifier = Modifier
+                            .padding(top = topPadding, start = 0.dp)
+                    )
+                    LazyRow(
+                        modifier = Modifier
+                            .padding(top = 10.dp, start = 0.dp, end = 0.dp)
+                            .background(Color.Transparent)
+                    ) {
+                        itemsIndexed(items = streaming, itemContent = { index, server ->
+                            ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
+                        })
+                    }
+                }
+                if (isDvpnEnabled.value) {
+                    Text(
+                        text = "D-VPN",
+                        style = MaterialTheme.typography.labelLarge,
+                        color = colorResource(id = R.color.dark_blue_gray_text),
+                        modifier = Modifier
+                            .padding(top = topPadding, start = 0.dp)
+                    )
+                    LazyRow(
+                        modifier = Modifier
+                            .padding(top = 10.dp, start = 0.dp, end = 0.dp)
+                            .background(Color.Transparent)
+                    ) {
+                        itemsIndexed(items = dvpn, itemContent = { index, server ->
+                            ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
+                        })
+                    }
+                }
+                if (isP2pEnabled.value) {
+                    Text(
+                        text = "P2P",
+                        style = MaterialTheme.typography.labelLarge,
+                        color = colorResource(id = R.color.dark_blue_gray_text),
+                        modifier = Modifier
+                            .padding(top = topPadding, start = 0.dp)
+                    )
+                    LazyRow(
+                        modifier = Modifier
+                            .padding(top = 10.dp, start = 0.dp, end = 0.dp)
+                            .background(Color.Transparent)
+                    ) {
+                        itemsIndexed(items = p2p, itemContent = { index, server ->
+                            ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
+                        })
+                    }
+                }
+            }
+        } else {
+            val searchListViewModel = searchListViewModelSplash
+            LaunchedEffect(key1 = textChanged) {
+                Log.d("test_search_logic", "LaunchedEffect $textChanged")
+                val delay = if (textChanged.isEmpty()) 0L else 300L
+                delay(delay)
+                searchListViewModel.getCountries(textChanged, true)
+            }
+
+            val searchServersList = searchListViewModel.countriesList.observeAsState().value
+            searchServersList?.forEachIndexed { index, server ->
+                Log.d("test_search", "SLS_ ${server.server_name} ${server.ping}")
+            }
+
+            if (searchServersList != null) {
+                val fraction = if (searchServersList.size > 0) 1f else if (textChanged.isNotEmpty()) 1f else 0f
+                Column(
+                    modifier = Modifier
+                        .fillMaxSize()
+                        .padding(top = 130.dp, start = 24.dp, end = 24.dp, bottom = 24.dp)
+                        .verticalScroll(rememberScrollState())
+                        .background(Color.Transparent)
+                ) {
+                    LazyRow(
+                        modifier = Modifier
+                            .padding(top = 10.dp, start = 0.dp, end = 0.dp)
+                            .background(Color.Transparent)
+                    ) {
+                        itemsIndexed(items = searchServersList, itemContent = { index, server ->
+                            ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
+                        })
+                    }
+                }
+            }
+
+        }
+
 //        var locations by remember { mutableStateOf(ArrayList<Server>()) }
 //        var dedicatedIP by remember { mutableStateOf(ArrayList<Server>()) }
 //        var streaming by remember { mutableStateOf(ArrayList<Server>()) }
@@ -80,107 +327,7 @@ fun ServerListTV(navHostController: NavHostController) {
 //            }
 //        }
 
-        Column(
-            modifier = Modifier
-                .fillMaxSize()
-                .padding(top = 60.dp, start = 24.dp, end = 24.dp, bottom = 24.dp)
-                .verticalScroll(rememberScrollState())
-                .background(Color.Transparent)
-        ) {
-            val topPadding = 25.dp
-
-            if (isLocationsEnabled.value) {
-                Text(
-                    text = "Top Locations",
-                    style = MaterialTheme.typography.labelLarge,
-                    color = colorResource(id = R.color.dark_blue_gray_text),
-                    modifier = Modifier
-                        .padding(top = 0.dp, start = 0.dp)
-                        .focusable()
-                )
-                LazyRow(
-                    modifier = Modifier
-                        .padding(top = 10.dp, start = 0.dp, end = 0.dp)
-                        .background(Color.Transparent)
-                ) {
-                    itemsIndexed(items = locations, itemContent = { index, server ->
-                        ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
-                    })
-                }
-            }
-            if (isDedicatedIpEnabled.value) {
-                Text(
-                    text = "Dedicated IP",
-                    style = MaterialTheme.typography.labelLarge,
-                    color = colorResource(id = R.color.dark_blue_gray_text),
-                    modifier = Modifier
-                        .padding(top = topPadding, start = 0.dp)
-                )
-                LazyRow(
-                    modifier = Modifier
-                        .padding(top = 10.dp, start = 0.dp, end = 0.dp)
-                        .background(Color.Transparent)
-                ) {
-                    itemsIndexed(items = dedicatedIP, itemContent = { index, server ->
-                        ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
-                    })
-                }
-            }
-            if (isStreamingEnabled.value) {
-                Text(
-                    text = "Streaming",
-                    style = MaterialTheme.typography.labelLarge,
-                    color = colorResource(id = R.color.dark_blue_gray_text),
-                    modifier = Modifier
-                        .padding(top = topPadding, start = 0.dp)
-                )
-                LazyRow(
-                    modifier = Modifier
-                        .padding(top = 10.dp, start = 0.dp, end = 0.dp)
-                        .background(Color.Transparent)
-                ) {
-                    itemsIndexed(items = streaming, itemContent = { index, server ->
-                        ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
-                    })
-                }
-            }
-            if (isDvpnEnabled.value) {
-                Text(
-                    text = "D-VPN",
-                    style = MaterialTheme.typography.labelLarge,
-                    color = colorResource(id = R.color.dark_blue_gray_text),
-                    modifier = Modifier
-                        .padding(top = topPadding, start = 0.dp)
-                )
-                LazyRow(
-                    modifier = Modifier
-                        .padding(top = 10.dp, start = 0.dp, end = 0.dp)
-                        .background(Color.Transparent)
-                ) {
-                    itemsIndexed(items = dvpn, itemContent = { index, server ->
-                        ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
-                    })
-                }
-            }
-            if (isP2pEnabled.value) {
-                Text(
-                    text = "P2P",
-                    style = MaterialTheme.typography.labelLarge,
-                    color = colorResource(id = R.color.dark_blue_gray_text),
-                    modifier = Modifier
-                        .padding(top = topPadding, start = 0.dp)
-                )
-                LazyRow(
-                    modifier = Modifier
-                        .padding(top = 10.dp, start = 0.dp, end = 0.dp)
-                        .background(Color.Transparent)
-                ) {
-                    itemsIndexed(items = p2p, itemContent = { index, server ->
-                        ServerItemTV(server = server, navHostController = navHostController, serverPing = server.ping)
-                    })
-                }
-            }
-        }
+
     }
 }