Browse Source

saving 5 recent connected servers

Khubaib 1 năm trước cách đây
mục cha
commit
1d9500beaa

+ 1 - 14
.idea/deploymentTargetDropDown.xml

@@ -6,20 +6,7 @@
         <State />
       </entry>
       <entry key="app">
-        <State>
-          <runningDeviceTargetSelectedWithDropDown>
-            <Target>
-              <type value="RUNNING_DEVICE_TARGET" />
-              <deviceKey>
-                <Key>
-                  <type value="SERIAL_NUMBER" />
-                  <value value="1C051FDF60048Z" />
-                </Key>
-              </deviceKey>
-            </Target>
-          </runningDeviceTargetSelectedWithDropDown>
-          <timeTargetWasSelectedWithDropDown value="2024-04-22T14:03:27.720575707Z" />
-        </State>
+        <State />
       </entry>
     </value>
   </component>

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

@@ -470,23 +470,12 @@ fun ColumnScope.ShowRecommendedList(
             val recentLocation: ArrayList<Server> = ArrayList<Server>()
 
             val recentList = prefHelper.getRecentlyList()
-            if (recentList != null)
-            {
-                Log.d("recentList","not null: ${recentList.size}")
-                recentList.forEachIndexed { index, server ->
-                    Log.d("recentList","s => ${server.server_name}")
-                }
-
-            } else {
-                Log.d("recentList", "null::")
-            }
 
             prefHelper.getConnectedServer()?.let {
                 recentLocation.add(it)
             }
 
             recentList?.let {
-                Log.d("recentList","inside items...")
                 items(items = it) { server ->
                     ServerItem(server, navHostController, server.ping)
                 }

+ 33 - 8
app/src/main/java/com/vpn/fastestvpnservice/screens/bottomNavBarScreens/HomeScreen.kt

@@ -5,6 +5,7 @@ import android.content.Intent
 import android.content.res.Configuration
 import android.os.Handler
 import android.util.Log
+import android.widget.Toast
 import androidx.activity.ComponentActivity
 import androidx.compose.foundation.BorderStroke
 import androidx.compose.foundation.ExperimentalFoundationApi
@@ -112,6 +113,9 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
         onClick: () -> Unit, isServerDialogShown: Boolean, server: Server) {
         val wg = VPNConnectionsUtil(context, act ,homeViewModel)
         val basePreferenceHelper = BasePreferenceHelper(context)
+         val serverListViewModel: ServerListViewModel = viewModel{
+             ServerListViewModel(context)
+         }
 
         Log.d("ServerCallbacks", "onServerSelected called!")
 
@@ -130,14 +134,26 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
                     basePreferenceHelper.setServerObject(server)
                 }
             } else {
-                val recentList = basePreferenceHelper.getRecentlyList()
-                val tempList = ArrayList<Server>()
-                recentList?.forEachIndexed { index, server1 ->
-                    tempList.add(server1)
-                }
-                tempList.add(server)
+                serverListViewModel.setRecentlyConnectedServer(server)
 
-                basePreferenceHelper.setRecentlyList(tempList)
+//                val recentList = basePreferenceHelper.getRecentlyList()
+//                val tempList = ArrayList<Server>()
+//                recentList?.let { tempList.addAll(it) }
+//
+//                if (tempList.size == 0) {
+//                    tempList.add(server)
+//                }
+//                else {
+//                        if (!tempList.any {
+//                                it.id == server.id
+//                            }) {
+//                            if (tempList.size != 5 && tempList.size < 5) {
+//                                tempList.add(0, server)
+//                            }
+//                        }
+//                }
+//
+//                basePreferenceHelper.setRecentlyList(tempList)
 
                 basePreferenceHelper.setConnectedServer(server)
                 basePreferenceHelper.setServerObject(server)
@@ -372,7 +388,8 @@ fun Home(
                     )
 
                     Column(
-                        modifier = Modifier.fillMaxWidth()
+                        modifier = Modifier
+                            .fillMaxWidth()
                             .fillMaxHeight()
                             .padding(bottom = 115.dp)
 //                            .offset(y = -(118).dp)
@@ -640,6 +657,9 @@ fun Home(
                             else if (isConnect == App.DISCONNECTED) {
                                 Log.d("isConnect_State_vpn", "startVPN")
                                 basePreferenceHelper.setConnectedServer(smartServer)
+                                if (smartServer != null) {
+                                    serverListViewModel.setRecentlyConnectedServer(smartServer)
+                                }
                                 vpnConnectionsUtil.startVpn()
                             }
 
@@ -713,6 +733,10 @@ fun ColumnScope.ShowServerDialog(
             .wrapContentHeight()
     ) {
         val lastServer = prefHelper.getConnectedServer()
+        val context = LocalContext.current
+        val serverListViewModel: ServerListViewModel = viewModel {
+            ServerListViewModel(context)
+        }
         Surface(
             color = colorResource(id = R.color.white),
             modifier = Modifier
@@ -778,6 +802,7 @@ fun ColumnScope.ShowServerDialog(
                             isServerDialog.value = false
                             prefHelper.setServerObject(serverObj.value)
                             prefHelper.setConnectedServer(serverObj.value)
+                            serverListViewModel.setRecentlyConnectedServer(serverObj.value)
                             wg.stopVpn()
                             Handler().postDelayed(Runnable {
                                 wg.startVpn()

+ 21 - 0
app/src/main/java/com/vpn/fastestvpnservice/viewmodels/ServerListViewModel.kt

@@ -540,4 +540,25 @@ class ServerListViewModel(context: Context): ViewModel() {
         _mutableLiveDataGetFavList.value = getFavList()
     }
 
+    fun setRecentlyConnectedServer(server: Server) {
+        val recentList = preferencesHelper.getRecentlyList()
+        val tempList = ArrayList<Server>()
+        recentList?.let { tempList.addAll(it) }
+
+        if (tempList.size == 0) {
+            tempList.add(server)
+        }
+        else {
+            if (!tempList.any {
+                    it.id == server.id
+                }) {
+                if (tempList.size != 5 && tempList.size < 5) {
+                    tempList.add(0, server)
+                }
+            }
+        }
+
+        preferencesHelper.setRecentlyList(tempList)
+    }
+
 }