Przeglądaj źródła

Worked on room database, adding all servers and deleting when initializing serverlist view model

Khubaib 6 miesięcy temu
rodzic
commit
20a7de41bc

+ 1 - 1
.idea/deploymentTargetSelector.xml

@@ -4,7 +4,7 @@
     <selectionStates>
       <SelectionState runConfigName="app">
         <option name="selectionMode" value="DROPDOWN" />
-        <DropdownSelection timestamp="2024-11-06T10:48:05.874169337Z">
+        <DropdownSelection timestamp="2024-11-13T09:43:27.565511023Z">
           <Target type="DEFAULT_BOOT">
             <handle>
               <DeviceId pluginId="PhysicalDevice" identifier="serial=1C051FDF60048Z" />

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

@@ -1,24 +1,27 @@
 package com.vpn.fastestvpnservice.beans
 
+import androidx.room.Entity
+import androidx.room.PrimaryKey
 import com.google.gson.annotations.SerializedName
 
+@Entity
 class Server(
-
+    @PrimaryKey(autoGenerate = false)
     @SerializedName("id") var id: Int? = null,
     @SerializedName("continent") val continent: String? = null,
     @SerializedName("country") val country: String? = null,
     @SerializedName("state") val state: String? = null,
-    @SerializedName("city") val city: Any? = null,
+    @SerializedName("city") val city: String? = null,
     @SerializedName("name") val name: String? = null,
     @SerializedName("dns") val dns: String? = null,
     @SerializedName("iso") val iso: String? = null,
     @SerializedName("lt") val lt: Double? = null,
     @SerializedName("lg") var lg: Double? = null,
-    @SerializedName("ip") val ip: Any? = null,
+    @SerializedName("ip") val ip: String? = null,
     @SerializedName("port") val port: Int? = null,
     @SerializedName("protocol") val protocol: String? = null,
-    @SerializedName("ipsec") val ipsec: Any? = null,
-    @SerializedName("remote_id") val remoteId: Any? = null,
+    @SerializedName("ipsec") val ipsec: String? = null,
+    @SerializedName("remote_id") val remoteId: String? = null,
     @SerializedName("is_trial") val isTrial: Boolean? = null,
     @SerializedName("active") val active: Boolean? = null,
     @SerializedName("flag") val flag: String? = null,

+ 9 - 3
app/src/main/java/com/vpn/fastestvpnservice/roomDB/ServerDao.kt

@@ -2,18 +2,24 @@ package com.vpn.fastestvpnservice.roomDB
 
 import androidx.lifecycle.LiveData
 import androidx.room.Dao
+import androidx.room.Delete
 import androidx.room.Insert
 import androidx.room.OnConflictStrategy
 import androidx.room.Query
+import com.vpn.fastestvpnservice.beans.Server
 import com.vpn.fastestvpnservice.beans.ServerData
+import com.vpn.fastestvpnservice.constants.AppEnum
 
 @Dao
 interface ServerDao {
 
-    @Query("SELECT * FROM ServerDataRoom")
-    fun getAllServers(): LiveData<List<ServerDataRoom>>
+    @Query("SELECT * FROM Server")
+    fun getAllServers(): LiveData<List<Server>>
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    fun addServers(servers : List<ServerDataRoom>)
+    fun addServers(server : Server)
+
+    @Query("DELETE FROM Server")
+    fun deleteServers()
 
 }

+ 1 - 1
app/src/main/java/com/vpn/fastestvpnservice/roomDB/ServerDataRoom.kt

@@ -8,6 +8,6 @@ data class ServerDataRoom(
     @PrimaryKey(autoGenerate = false)
     var id: Int? = 0,
     var name: String? = null,
-    var servers: List<ServerRoom>? = null,
+    var servers: Int? = null,
     var country_sort: Int? = 0
 )

+ 2 - 1
app/src/main/java/com/vpn/fastestvpnservice/roomDB/ServerDatabase.kt

@@ -3,9 +3,10 @@ package com.vpn.fastestvpnservice.roomDB
 import androidx.room.Database
 import androidx.room.RoomDatabase
 import androidx.room.TypeConverters
+import com.vpn.fastestvpnservice.beans.Server
 
 @Database(
-    entities = [ServerRoom::class, ServerDataRoom::class], version = 2
+    entities = [ServerRoom::class, ServerDataRoom::class, Server::class], version = 3
 )
 //@TypeConverters(Converters::class)
 abstract class ServerDatabase : RoomDatabase() {

+ 3 - 1
app/src/main/java/com/vpn/fastestvpnservice/screens/bottomNavBarScreens/HomeScreen.kt

@@ -308,9 +308,11 @@ fun Home(
         }
     }
 
+    Log.d("test_room_db_svm", "serversListRoom")
     val serversListRoom = serverListViewModelSplash.serversListRoom.observeAsState()
+
     serversListRoom.value?.forEachIndexed { index, serverDataRoom ->
-        Log.d("serverDataRoom", "$index , ${serverDataRoom.name} , ${serverDataRoom.servers?.size}")
+        Log.d("serverDataRoom", "$index , ${serverDataRoom.server_name} , ${serverDataRoom.protocol}")
     }
 
     vpnConnectionsUtil = VPNConnectionsUtil(context, activity, homeViewModel)

+ 28 - 15
app/src/main/java/com/vpn/fastestvpnservice/viewmodels/ServerListViewModel.kt

@@ -80,14 +80,24 @@ class ServerListViewModel(context: Context): ViewModel() {
     var mutableLiveDataAllServers = MutableLiveData<ArrayList<Server>>()
     var liveDataAllServers: LiveData<ArrayList<Server>> = mutableLiveDataAllServers
 
-    var serverDao: ServerDao = App.serverDatabase.getServerDao()
-
-    val serversListRoom : LiveData<List<ServerDataRoom>> = serverDao.getAllServers()
-
     fun setPagerIndex(index: Int) {
         _mutableLiveDataPagerIndex.value = index
     }
 
+    companion object {
+        lateinit var serverDao: ServerDao
+    }
+
+    init {
+        serverDao = App.serverDatabase.getServerDao()
+        viewModelScope.launch(Dispatchers.IO) {
+            Log.d("test_room_db_svm", "serverDao.deleteServers()")
+            serverDao.deleteServers()
+        }
+    }
+
+    val serversListRoom : LiveData<List<Server>> = serverDao.getAllServers()
+
     fun setRecommendedSmartServers() {
         Log.d("test_api_response_s", "setRecommendedSmartServers")
         val smartLocationList: MutableList<Server> = ArrayList<Server>()
@@ -255,17 +265,13 @@ class ServerListViewModel(context: Context): ViewModel() {
         _mutableLiveDataGetServersGlobal.value = serversListGlobal
         mutableLiveDataAllServers.value = serversListAllGlobal
 
-        val serversDataRoom = ArrayList<ServerDataRoom>()
-
-        serversListGlobal.forEachIndexed { index, serverDataGlobal ->
-            val servers = serverDataGlobal?.servers?.let { convertServerListToServerRoomList(it) }
-
-            serversDataRoom.add(index, ServerDataRoom(index, serverDataGlobal?.name, servers, serverDataGlobal?.country_sort))
-        }
-
-        viewModelScope.launch(Dispatchers.IO) {
-            serverDao.addServers(serversDataRoom)
-        }
+//        serversListGlobal.forEachIndexed { index, serverDataGlobal ->
+//            serverDataGlobal?.servers?.forEachIndexed { index1, server ->
+//                viewModelScope.launch(Dispatchers.IO) {
+//                    serverDao.addServers(server)
+//                }
+//            }
+//        }
     }
 
     fun convertServerListToServerRoomList(serverList: ArrayList<Server>): List<ServerRoom> {
@@ -615,6 +621,9 @@ class ServerListViewModel(context: Context): ViewModel() {
 
                     calculatePing(value) {
                         distinctBy.get(index).ping = it
+                        viewModelScope.launch(Dispatchers.IO) {
+                            serverDao.addServers(distinctBy.get(index))
+                        }
                     }
 
 //                    distinctBy.get(index).countryServers = entry.value
@@ -896,8 +905,12 @@ class ServerListViewModel(context: Context): ViewModel() {
             calculatePing(server1){
 //                Log.d("serverInfo_ping", "In: ${server1.server_name} $it")
                 server1.ping = it
+                viewModelScope.launch(Dispatchers.IO) {
+                    serverDao.addServers(server1)
+                }
             }
             tempList2.add(server1)
+
         }
 
         serversGroupListGlobal.add(index, ServerDataGlobal(server.country, tempList2, server.country_sort))

+ 3 - 1
app/src/main/java/de/blinkt/openvpn/core/App.java

@@ -174,7 +174,9 @@ public class App extends /*com.orm.SugarApp*/ Application {
                 getApplicationContext(),
                 ServerDatabase.class,
                 ServerDatabase.NAME
-        ).fallbackToDestructiveMigration().build();
+        )
+                .fallbackToDestructiveMigration()
+                .build();
 
 //        initializeSSLContext();