|
@@ -2,6 +2,7 @@ package com.vpn.fastestvpnservice.viewmodels
|
|
|
|
|
|
import android.content.Context
|
|
|
import android.content.pm.ActivityInfo
|
|
|
+import android.location.Location
|
|
|
import android.util.Log
|
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
@@ -9,6 +10,7 @@ import androidx.lifecycle.ViewModel
|
|
|
import com.google.gson.Gson
|
|
|
import com.google.gson.reflect.TypeToken
|
|
|
import com.vpn.fastestvpnservice.beans.DataResponseServers
|
|
|
+import com.vpn.fastestvpnservice.beans.Server
|
|
|
import com.vpn.fastestvpnservice.beans.ServerData
|
|
|
import com.vpn.fastestvpnservice.constants.AppEnum
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
|
|
@@ -17,6 +19,7 @@ import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
|
|
|
import com.vpn.fastestvpnservice.retrofit.WebServiceFactory
|
|
|
import com.vpn.fastestvpnservice.screens.isServerModelInitialized
|
|
|
import com.vpn.fastestvpnservice.screens.serverListViewModelSplash
|
|
|
+import com.vpn.fastestvpnservice.screens.splashViewModelSplash
|
|
|
import com.vpn.fastestvpnservice.utils.StaticMethods
|
|
|
import retrofit2.Call
|
|
|
import java.io.BufferedReader
|
|
@@ -89,11 +92,14 @@ class SplashViewModel constructor(context: Context): ViewModel() {
|
|
|
if (StaticMethods.isTV(context)) {
|
|
|
Log.d("test_api_response_s", "serverDataApi TV")
|
|
|
serverListViewModelSplash.setCountryDataTV()
|
|
|
+ setRecommendedServer()
|
|
|
} else {
|
|
|
Log.d("test_api_response_s", "serverDataApi Android")
|
|
|
serverListViewModelSplash.setRecommendedSmartServers()
|
|
|
serverListViewModelSplash.setCountryData()
|
|
|
+ setRecommendedServer()
|
|
|
}
|
|
|
+ mutableLiveDataLanguageStatus.value = false
|
|
|
}
|
|
|
|
|
|
// Log.d("test_api_response", data.data.toString())
|
|
@@ -121,6 +127,57 @@ class SplashViewModel constructor(context: Context): ViewModel() {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ fun setRecommendedServer() {
|
|
|
+ val smartLocationList: MutableList<Server> = ArrayList<Server>()
|
|
|
+ preferenceHelper.getServerData().get(0).servers?.let {
|
|
|
+ val serverDataLocation = it
|
|
|
+
|
|
|
+ val distinctdatanotnull =
|
|
|
+ serverDataLocation.filter { // servers's lt and lt not be null
|
|
|
+ it.lt != null && it.lg != null
|
|
|
+ }
|
|
|
+
|
|
|
+ val distinctdata =
|
|
|
+ distinctdatanotnull.distinctBy { // servers's lt filter, no same lt of a server
|
|
|
+ it.lt
|
|
|
+ }
|
|
|
+
|
|
|
+ val result = FloatArray(1)
|
|
|
+ val ipinfo = preferenceHelper.getIpinfo()
|
|
|
+
|
|
|
+ distinctdata.forEachIndexed { index, server ->
|
|
|
+ val lat1 = ipinfo?.latitute
|
|
|
+ val lon1 = ipinfo?.longitude
|
|
|
+ val lat2 = server.lt
|
|
|
+ val lon2 = server.lg
|
|
|
+
|
|
|
+ if (lat1 != null && lat2 != null && lon1 != null && lon2 != null) {
|
|
|
+ Location.distanceBetween(lat1, lon1, lat2, lon2, result)
|
|
|
+ }
|
|
|
+
|
|
|
+ val distance: Float = result[0]
|
|
|
+ distinctdata.get(index).distance = distance
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ val sortedDistance = distinctdata.sortedBy {
|
|
|
+ it.distance
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sortedDistance.isNotEmpty()) {
|
|
|
+ for (i in 0..0) {
|
|
|
+ smartLocationList.add(sortedDistance.get(i))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ smartLocationList.forEach {
|
|
|
+// prefHelper.setSmartServerObject(it)
|
|
|
+ preferenceHelper.setRecommendedServerObject(it)
|
|
|
+ Log.d("smartLocationList", "L:: server = ${it.server_name}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private fun getJsonFileDetails(): String? {
|
|
|
val protocol = AppEnum.UDP_PROTOCOL.key
|
|
|
var conf: InputStream? = null
|