Bladeren bron

Saving recommended server on shared prefs of locale language

Khubaib 7 maanden geleden
bovenliggende
commit
7b66072f90

+ 50 - 0
app/src/main/java/com/vpn/fastestvpnservice/screens/bottomNavBarScreens/SettingsScreen.kt

@@ -4,6 +4,7 @@ import android.app.Activity
 import android.app.LocaleManager
 import android.content.Context
 import android.content.Intent
+import android.location.Location
 import android.net.Uri
 import android.os.Build
 import android.os.Bundle
@@ -103,6 +104,7 @@ import androidx.navigation.NavHostController
 import com.vpn.fastestvpnservice.MainActivity
 import com.vpn.fastestvpnservice.R
 import com.vpn.fastestvpnservice.beans.Protocol
+import com.vpn.fastestvpnservice.beans.Server
 import com.vpn.fastestvpnservice.beans.isDarkTheme
 import com.vpn.fastestvpnservice.beans.languages
 import com.vpn.fastestvpnservice.beans.themesList
@@ -263,6 +265,54 @@ fun Settings(navHostController: NavHostController, activity: ComponentActivity)
                         Log.d("test_lingual_splash", "serversResponse - true")
                         serverListViewModelSplash.setRecommendedSmartServers()
                         serverListViewModelSplash.setCountryData()
+                        val smartLocationList: MutableList<Server> = ArrayList<Server>()
+                        basePreferenceHelper.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 = basePreferenceHelper.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)
+                                basePreferenceHelper.setRecommendedServerObject(it)
+                                Log.d("smartLocationList", "L:: server = ${it.server_name}")
+                            }
+                        }
                     }
 
                     splashViewModelSplash.mutableLiveDataServerData.value = null