|
@@ -7,6 +7,7 @@ import android.util.Log
|
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
+import androidx.lifecycle.viewModelScope
|
|
|
import com.stealthcopter.networktools.Ping
|
|
|
import com.stealthcopter.networktools.ping.PingResult
|
|
|
import com.stealthcopter.networktools.ping.PingStats
|
|
@@ -17,6 +18,9 @@ import com.vpn.fastestvpnservice.beans.ServerDataGlobal
|
|
|
import com.vpn.fastestvpnservice.beans.ServerProtocol
|
|
|
import com.vpn.fastestvpnservice.beans.ServerResponse
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
|
|
|
+import com.vpn.fastestvpnservice.roomDB.ServerDao
|
|
|
+import com.vpn.fastestvpnservice.roomDB.ServerDataRoom
|
|
|
+import com.vpn.fastestvpnservice.roomDB.ServerRoom
|
|
|
import com.vpn.fastestvpnservice.screens.isAlphabetList
|
|
|
import com.vpn.fastestvpnservice.screens.recommendedListFinalGlobal
|
|
|
import com.vpn.fastestvpnservice.screens.recommendedListGlobal
|
|
@@ -37,6 +41,9 @@ import com.vpn.fastestvpnservice.screensTV.locations
|
|
|
import com.vpn.fastestvpnservice.screensTV.p2p
|
|
|
import com.vpn.fastestvpnservice.screensTV.serversListGlobalTV
|
|
|
import com.vpn.fastestvpnservice.screensTV.streaming
|
|
|
+import de.blinkt.openvpn.core.App
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
|
class ServerListViewModel(context: Context): ViewModel() {
|
|
|
|
|
@@ -73,6 +80,10 @@ 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
|
|
|
}
|
|
@@ -243,8 +254,55 @@ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun convertServerListToServerRoomList(serverList: ArrayList<Server>): List<ServerRoom> {
|
|
|
+ return serverList.map { server ->
|
|
|
+ ServerRoom(
|
|
|
+ id = server.id,
|
|
|
+ continent = server.continent,
|
|
|
+ country = server.country,
|
|
|
+ state = server.state,
|
|
|
+ city = server.city as? String,
|
|
|
+ name = server.name,
|
|
|
+ dns = server.dns,
|
|
|
+ iso = server.iso,
|
|
|
+ lt = server.lt,
|
|
|
+ lg = server.lg,
|
|
|
+ ip = server.ip as? String,
|
|
|
+ port = server.port,
|
|
|
+ protocol = server.protocol,
|
|
|
+ ipsec = server.ipsec as? String,
|
|
|
+ remoteId = server.remoteId as? String,
|
|
|
+ isTrial = server.isTrial,
|
|
|
+ active = server.active,
|
|
|
+ flag = server.flag,
|
|
|
+ server_name = server.server_name,
|
|
|
+ country_sort = server.country_sort,
|
|
|
+ connection_count = server.connection_count,
|
|
|
+ wg_key = server.wg_key,
|
|
|
+ enable = server.enable,
|
|
|
+ totalServers = server.totalServers,
|
|
|
+ enableServers = server.enableServers,
|
|
|
+ distance = server.distance,
|
|
|
+ ping = server.ping
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
fun setCountryDataTV() {
|
|
|
val data = preferencesHelper.getServerData()
|
|
|
|