Browse Source

save server on shared prefs when server is selected, remove countryServers from Server as it is producing crashes...

Khubaib 1 year ago
parent
commit
56ae0cb1af

+ 1 - 1
app/src/main/java/com/vpn/fastestvpnservice/beans/Server.kt

@@ -28,7 +28,7 @@ class Server(
     @SerializedName("connection_count") val connection_count: Int = 0,
     @SerializedName("wg_key") val wg_key: String? = null,
     @SerializedName("enable") var enable: Int = 1,
-    var countryServers: List<Server>? = null,
+//    var countryServers: List<Server>? = null,
     var totalServers: Int = 1,
     var enableServers: Int = 1,
     var distance: Float = 0.0f,

+ 50 - 15
app/src/main/java/com/vpn/fastestvpnservice/customItems/CountryItem.kt

@@ -34,6 +34,7 @@ import androidx.compose.runtime.livedata.observeAsState
 import androidx.compose.runtime.mutableIntStateOf
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
 import androidx.compose.runtime.saveable.rememberSaveable
 import androidx.compose.runtime.setValue
 import androidx.compose.ui.Alignment
@@ -66,6 +67,9 @@ import com.stealthcopter.networktools.Ping
 import com.stealthcopter.networktools.ping.PingResult
 import com.stealthcopter.networktools.ping.PingStats
 import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.onServer
+import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
+import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
 import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel
 import com.vpn.fastestvpnservice.viewmodels.SplashViewModel
 import java.lang.Exception
@@ -84,7 +88,7 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
     ) {
         val context = LocalContext.current
         val basePreferenceHelper = BasePreferenceHelper(context)
-        val countrySize by rememberSaveable { mutableStateOf(server.countryServers) }
+        val countrySize by rememberSaveable { mutableStateOf(server.totalServers) }
         var isServerExpanded by rememberSaveable { mutableStateOf(false) }
         val serverListViewModel: ServerListViewModel = viewModel{
             ServerListViewModel(context)
@@ -92,6 +96,7 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
         val splashViewModel: SplashViewModel = viewModel{
             SplashViewModel(context)
         }
+
 //        var countrySize: Int = 0
 
         Log.d("test_countrySize", "countrySize $countrySize")
@@ -104,9 +109,13 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
 //            }
 //        }
 
-            if (countrySize != null){
-                countrySize.let { size ->
-                    if (size?.size!! > 1) {
+        countrySize.let { size ->
+                    if (size > 1) {
+                        var locationsSize: Int = 0
+                        server.totalServers.let {
+                            locationsSize = it
+                        }
+                        Log.d("test_server_size", "${server.server_name} $locationsSize")
                         Column {
                             /* Country Location's Row */
                             Row(
@@ -121,9 +130,12 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
                                         indication = null,
                                         interactionSource = remember { MutableInteractionSource() }
                                     ) {
-                                        Toast.makeText(
-                                            context, server.server_name, Toast.LENGTH_SHORT
-                                        ).show()
+//                                        Toast
+//                                            .makeText(
+//                                                context, server.server_name, Toast.LENGTH_SHORT
+//                                            )
+//                                            .show()
+                                        basePreferenceHelper.setServerObject(server)
                                         navHostController.popBackStack()
                                     }
                             ) {
@@ -153,7 +165,7 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
                                         .align(Alignment.CenterVertically)
                                 )
                                 Spacer(modifier = Modifier.weight(1F))
-                                Text(text = "${countrySize?.size} locations",
+                                Text(text = "${server.totalServers} locations",
                                     style = TextStyle(
                                         fontSize = 16.sp,
                                         color = colorResource(id = R.color.blue_text)
@@ -185,9 +197,12 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
 
                             /* Country Expandable's Row */
                             if (isServerExpanded) {
+                                val countriesServersData = basePreferenceHelper.getServerData().get(0).servers
+                                countriesServersData?.let {
+                                    val serversGroup = serverListViewModel.getServerGroupList(server, it)
+                                    ExpandableRow(server = serversGroup, navHostController)
 
-
-                                ExpandableRow(server = server, navHostController)
+                                }
                             }
                         }
                     }
@@ -223,6 +238,7 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
                                     indication = null,
                                     interactionSource = remember { MutableInteractionSource() }
                                 ) {
+                                    basePreferenceHelper.setServerObject(server)
                                     navHostController.popBackStack()
                                 }
 
@@ -302,7 +318,6 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
                         }
                     }
                 }
-            }
 
             Surface(
                 modifier = Modifier
@@ -319,7 +334,7 @@ fun CountryItem(server: Server, category: String, navHostController: NavHostCont
 }
 
 @Composable
-fun ColumnScope.ExpandableRow(server: Server, navHostController: NavHostController) {
+fun ColumnScope.ExpandableRow(server: ArrayList<Server>, navHostController: NavHostController) {
     val context = LocalContext.current
     val basePreferenceHelper = BasePreferenceHelper(context)
     val serverListViewModel: ServerListViewModel = viewModel{
@@ -329,6 +344,22 @@ fun ColumnScope.ExpandableRow(server: Server, navHostController: NavHostControll
         SplashViewModel(context)
     }
 
+    val scope = rememberCoroutineScope()
+    val homeViewModel: HomeViewModel = viewModel {
+        HomeViewModel(context, scope)
+    }
+    var isExpServerClicked by remember {
+        mutableStateOf(false)
+    }
+
+    if (isExpServerClicked) {
+        Log.d("ServerCallbacks", "isServerClicked $isExpServerClicked")
+        onServer.onServerSelected(context, homeViewModel) {
+            isExpServerClicked = false
+        }
+        navHostController.popBackStack()
+    }
+
     Column(
         modifier = Modifier
             .padding(
@@ -343,7 +374,7 @@ fun ColumnScope.ExpandableRow(server: Server, navHostController: NavHostControll
 
         // Expandable Row
 
-        server.countryServers?.let { serverData ->
+        server.let { serverData ->
                 serverData.forEachIndexed { index, serverInfo ->
                     Row(
                         verticalAlignment = Alignment.CenterVertically,
@@ -358,8 +389,12 @@ fun ColumnScope.ExpandableRow(server: Server, navHostController: NavHostControll
                                 indication = null,
                                 interactionSource = remember { MutableInteractionSource() }
                             ) {
-                                navHostController.popBackStack()
-//                                basePreferenceHelper.setServerObject(serverInfo)
+//                                Toast.makeText(
+//                                    context, serverInfo.server_name, Toast.LENGTH_SHORT
+//                                ).show()
+                                basePreferenceHelper.setServerObject(serverInfo)
+                                isExpServerClicked = true
+//                                navHostController.popBackStack()
                             }
                     ) {
                         Text(text = serverInfo.server_name!!,

+ 4 - 11
app/src/main/java/com/vpn/fastestvpnservice/screens/ServerListScreen.kt

@@ -694,15 +694,14 @@ fun ColumnScope.ShowAllLocationsList(
                     CompositionLocalProvider(
                         LocalOverscrollConfiguration provides null
                     ) {
-                        if (data[serverTabPager].name?.lowercase().toString() == "servers") {
-                            filterServersList = data.get(serverTabPager).servers?.let {
+                        filterServersList = if (data[serverTabPager].name?.lowercase().toString() == "servers") {
+                            data.get(serverTabPager).servers?.let {
                                 serverListViewModel.filterServersByISO(
                                     it
                                 )
                             }!!
-                        }
-                        else {
-                            filterServersList = data.get(serverTabPager).servers?.let {
+                        } else {
+                            data.get(serverTabPager).servers?.let {
                                 serverListViewModel.filterServersByStreamingServers(
                                     it
                                 )
@@ -723,16 +722,10 @@ fun ColumnScope.ShowAllLocationsList(
                                 CountryItem(server = country, data[serverTabPager].name!!, navHostController )
                             }
                         }
-
-
-
                     }
                 }
-
-
             }
         }
-
     }
 }
 

+ 42 - 14
app/src/main/java/com/vpn/fastestvpnservice/viewmodels/ServerListViewModel.kt

@@ -31,6 +31,8 @@ class ServerListViewModel(context: Context): ViewModel() {
     val mutableLiveDataFavourite = MutableLiveData<List<Server>>()
     val mutableLiveDataFavUnFav = MutableLiveData<ServerResponse?>()
 
+    var mutableLiveDataGroupServers = MutableLiveData<ArrayList<Server>>()
+
     var _mutableLiveDataPing = MutableLiveData<Int?>(0)
     var liveDataPing: LiveData<Int?> = _mutableLiveDataPing
 
@@ -119,14 +121,14 @@ class ServerListViewModel(context: Context): ViewModel() {
                         Log.d("test_country_servers",server.country + " " + server.server_name.toString())
                     }
 
-                    distinctBy.get(index).countryServers = entry.value
+//                    distinctBy.get(index).countryServers = entry.value
 
-                    value.countryServers?.forEachIndexed { indexs1, serverInfo ->
-                        calculatePing(serverInfo) {
-                            serverInfo.ping = it
-                            Log.d("test_return_ping","ISO=> " + serverInfo.server_name + serverInfo.ping)
-                        }
-                    }
+//                    value.countryServers?.forEachIndexed { indexs1, serverInfo ->
+//                        calculatePing(serverInfo) {
+//                            serverInfo.ping = it
+//                            Log.d("test_return_ping","ISO=> " + serverInfo.server_name + serverInfo.ping)
+//                        }
+//                    }
 
                     if (total_enables >= 1) {
                         distinctBy.get(index).enableServers = 1
@@ -295,19 +297,19 @@ class ServerListViewModel(context: Context): ViewModel() {
 //                    distinctBy.get(index).totalServers = entry.value.sumBy { it.totalServers }
                     distinctBy.get(index).totalServers = 1
 
-                    distinctBy.get(index).countryServers = entry.value
+//                    distinctBy.get(index).countryServers = entry.value
 
 //                    calculatePing(value) {
 //                        distinctBy.get(index).ping = it
 //                        Log.d("test_return_ping", "value => ${value.server_name} ${value.ping}")
 //                    }
 
-                    value.countryServers?.forEachIndexed { indexs1, serverInfo ->
-                        calculatePing(serverInfo) {
-                            serverInfo.ping = it
-                            Log.d("test_return_ping", "SS=> " +serverInfo.server_name + serverInfo.ping)
-                        }
-                    }
+//                    value.countryServers?.forEachIndexed { indexs1, serverInfo ->
+//                        calculatePing(serverInfo) {
+//                            serverInfo.ping = it
+//                            Log.d("test_return_ping", "SS=> " +serverInfo.server_name + serverInfo.ping)
+//                        }
+//                    }
                 }
             }
         }
@@ -475,6 +477,32 @@ class ServerListViewModel(context: Context): ViewModel() {
     fun getServerData(): ArrayList<ServerData> {
         return preferencesHelper.getServerData()
     }
+
+    fun getServerGroupList(
+        server: Server, serverList: ArrayList<Server>
+    ): ArrayList<Server> {
+
+        val tempList = ArrayList<Server>()
+        for (i in serverList.indices) {
+            val obj = serverList[i]
+            if (obj.iso == server.iso && preferencesHelper.getProtocol().title
+                    .equals(obj.protocol, ignoreCase = true)
+            ) {
+                tempList.add(obj)
+            }
+        }
+        tempList.sortBy {
+            it.server_name
+        }
+
+        tempList.forEachIndexed { index, servers ->
+            Log.d("tempList_servers", "${servers.server_name} , ${servers.country}")
+        }
+        mutableLiveDataGroupServers.value = tempList
+
+        return tempList
+    }
+
     fun updateFavServer(server: Server) {
         server.isFavourited = server.isFavourited != true
         val serverData = preferencesHelper.getServerData()