Browse Source

save smart server, so tapping on smart button will connect smart server...

Khubaib 1 year ago
parent
commit
a9331cdca8

+ 3 - 1
app/src/main/java/com/vpn/fastestvpnservice/helpers/BasePreferenceHelper.kt

@@ -19,7 +19,9 @@ class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
         removePreference(context, KEY_FILENAME, KEY_USER)
         removePreference(context, KEY_FILENAME, KEY_USER_PASSWORD)
         removePreference(context, KEY_FILENAME, KEY_SERVERS_DATA)
-//        removePreference(context, KEY_FILENAME, KEY_SERVERS)
+
+        removePreference(context, KEY_FILENAME, KEY_SERVERS)
+
         removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS)
         removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NO_VPN)
         removePreference(context, KEY_FILENAME, KEY_SELECTED_APPS_NOT_ALLOW)

+ 41 - 26
app/src/main/java/com/vpn/fastestvpnservice/screens/bottomNavBarScreens/HomeScreen.kt

@@ -97,6 +97,7 @@ import androidx.lifecycle.LifecycleOwner
 import androidx.lifecycle.viewmodel.compose.viewModel
 import androidx.navigation.NavHostController
 import androidx.navigation.compose.rememberNavController
+import com.vpn.fastestvpnservice.MainActivity
 import de.blinkt.openvpn.core.App
 import com.vpn.fastestvpnservice.beans.Protocol
 import com.vpn.fastestvpnservice.beans.Server
@@ -162,6 +163,7 @@ lateinit var homeViewModel1: HomeViewModel
             wg.startVpn()
             Log.d("isConnect_State", "onServerSelected ${homeViewModel.isConnect.value}")
         }
+         MainActivity.isSelectedServersShown = true
 
         onClick()
 
@@ -278,20 +280,26 @@ fun Home(navHostController: NavHostController, activity: ComponentActivity) {
                 var filterServerByConnectionCount = Server()
                 if (prefHelper.getServerObject() != null) {
                     prefHelper.getServerObject()?.let {
+                        Log.d("test_smart_server_n", "server nn = ${it.server_name}")
                         filterServerByConnectionCount = it
                     }
                 } else {
-                    prefHelper.getServerData().get(0).servers?.let {
-
-                        val temp = it.filter { server ->
-                            server.protocol.equals(prefHelper.getProtocol().title, ignoreCase = true)
-                        }
-
-                        if (temp.isNotEmpty()) {
-                            filterServerByConnectionCount = serverListViewModel.filterServerByConnectionCount(
-                                it
-                            )
-                        }
+//                    prefHelper.getServerData().get(0).servers?.let {
+//
+//                        val temp = it.filter { server ->
+//                            server.protocol.equals(prefHelper.getProtocol().title, ignoreCase = true)
+//                        }
+//
+//                        if (temp.isNotEmpty()) {
+//                            filterServerByConnectionCount = serverListViewModel.filterServerByConnectionCount(
+//                                it
+//                            )
+//                        }
+//                    }
+                    val smartServer = basePreferenceHelper.getSmartServerObject()
+                    smartServer?.let {
+                        Log.d("test_smart_server_n", "server = ${it.server_name}")
+                        filterServerByConnectionCount = it
                     }
                 }
                 prefHelper.setServerObject(filterServerByConnectionCount)
@@ -1074,26 +1082,33 @@ fun BoxScope.AddRowSmart(
     val ipInfo = basePreferenceHelper.getIpinfo()
     val selectedSmartList = basePreferenceHelper.getSmartList()
     var selectedServer = basePreferenceHelper.getServerObject()
-    
+
     Log.d("smartLocationList", "Home:: server = ${smart?.server_name}")
     var icon = Utils.getDrawable(context, selectedServer?.iso)
 
-    when(selectedSmartList) {
-        smartConnect[0] -> {
-            icon = Utils.getDrawable(context, smart?.iso)
-            selectedServer = smart
-        }
-        smartConnect[1] -> {
-            icon = Utils.getDrawable(context, recently?.iso)
-            selectedServer = recently
-        }
-        smartConnect[2] -> {
-            icon = Utils.getDrawable(context, anySpecific?.iso)
-            selectedServer = anySpecific
+    if (MainActivity.isSelectedServersShown) {
+        selectedServer = basePreferenceHelper.getServerObject()
+        icon = Utils.getDrawable(context, selectedServer?.iso)
+    } else {
+        when(selectedSmartList) {
+            smartConnect[0] -> {
+                icon = Utils.getDrawable(context, smart?.iso)
+                selectedServer = smart
+            }
+            smartConnect[1] -> {
+                icon = Utils.getDrawable(context, recently?.iso)
+                selectedServer = recently
+            }
+            smartConnect[2] -> {
+                icon = Utils.getDrawable(context, anySpecific?.iso)
+                selectedServer = anySpecific
+            }
+            else -> {}
         }
-        else -> {}
     }
-    
+
+    basePreferenceHelper.setSmartServerObject(selectedServer)
+
     Row(
         modifier = Modifier
             .fillMaxWidth()