Selaa lähdekoodia

Resolved UninitializedPropertyAccessException of searchListViewModelSplash on server list screen TV

Khubaib 3 kuukautta sitten
vanhempi
commit
be6bc8f196

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

@@ -302,9 +302,9 @@ fun ColumnScope.ShowRecommendedList(
         var favList = serverListViewModel.liveDataGetFavList.observeAsState().value
         favList = serverListViewModel.getFavList(true)
 
-        favList.forEachIndexed { index, server ->
-            Log.d("test_fav_lang", "SLS -> ${server.server_name} ${server.id}")
-        }
+//        favList.forEachIndexed { index, server ->
+//            Log.d("test_fav_lang", "SLS -> ${server.server_name} ${server.id}")
+//        }
 
 //        var ping by rememberSaveable { mutableIntStateOf(0) }
 //        fun updatePing(newPing: Int) {

+ 24 - 7
app/src/main/java/com/vpn/fastestvpnservice/screensTV/ServerListScreenTV.kt

@@ -44,12 +44,17 @@ import androidx.compose.ui.text.input.ImeAction
 import androidx.compose.ui.text.input.KeyboardType
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
+import androidx.lifecycle.viewmodel.compose.viewModel
 import androidx.navigation.NavHostController
 import com.vpn.fastestvpnservice.R
 import com.vpn.fastestvpnservice.customItems.ServerItemTV
+import com.vpn.fastestvpnservice.screens.isSearchModelInitialized
 import com.vpn.fastestvpnservice.screens.searchListViewModelSplash
 import com.vpn.fastestvpnservice.screens.serverListViewModelSplash
 import com.vpn.fastestvpnservice.ui.theme.customTypography
+import com.vpn.fastestvpnservice.viewmodels.SearchListViewModel
+import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel
+import com.vpn.fastestvpnservice.viewmodels.SplashViewModel
 import kotlinx.coroutines.delay
 
 @Composable
@@ -58,6 +63,18 @@ fun ServerListTV(navHostController: NavHostController) {
     var textChanged by remember { mutableStateOf("") }
     val keyboardController = LocalSoftwareKeyboardController.current
 
+    if (!isSearchModelInitialized) {
+        splashViewModelSplash = viewModel {
+            SplashViewModel(context)
+        }
+        serverListViewModelSplash = viewModel {
+            ServerListViewModel(context = context)
+        }
+        searchListViewModelSplash = viewModel {
+            SearchListViewModel(context, serverListViewModelSplash, splashViewModelSplash)
+        }
+    }
+
     Box(
         modifier = Modifier
             .fillMaxSize()
@@ -251,18 +268,18 @@ fun ServerListTV(navHostController: NavHostController) {
                 }
             }
         } else {
-            val searchListViewModel = searchListViewModelSplash
+//            val searchListViewModel = searchListViewModelSplash
             LaunchedEffect(key1 = textChanged) {
-                Log.d("test_search_logic", "LaunchedEffect $textChanged")
+//                Log.d("test_search_logic", "LaunchedEffect $textChanged")
                 val delay = if (textChanged.isEmpty()) 0L else 300L
                 delay(delay)
-                searchListViewModel.getCountries(textChanged, true)
+                searchListViewModelSplash.getCountries(textChanged, true)
             }
 
-            val searchServersList = searchListViewModel.countriesList.observeAsState().value
-            searchServersList?.forEachIndexed { index, server ->
-                Log.d("test_search", "SLS_ ${server.server_name} ${server.ping}")
-            }
+            val searchServersList = searchListViewModelSplash.countriesList.observeAsState().value
+//            searchServersList?.forEachIndexed { index, server ->
+//                Log.d("test_search", "SLS_ ${server.server_name} ${server.ping}")
+//            }
 
             if (searchServersList != null) {
                 val fraction = if (searchServersList.size > 0) 1f else if (textChanged.isNotEmpty()) 1f else 0f

+ 1 - 1
app/src/main/java/com/vpn/fastestvpnservice/viewmodels/HomeViewModel.kt

@@ -100,7 +100,7 @@ class HomeViewModel constructor(context: Context, scope: CoroutineScope): ViewMo
                         val type = object : TypeToken<DataResponse<TokenResponse?>?>() {}.type
                         val data = gson.fromJson<DataResponse<TokenResponse>>(jsonString, type)
                         _mutableLiveDataValidate.value = data
-                        Log.d("test_api_validate", "data = ${data.status} ${data.message}")
+//                        Log.d("test_api_validate", "data = ${data.status} ${data.message}")
 
                     } catch (ex: Exception) {
                         Log.d("test_api_response", "validatePassword onSuccess: catch")

+ 3 - 3
app/src/main/java/com/vpn/fastestvpnservice/viewmodels/SearchListViewModel.kt

@@ -82,7 +82,7 @@ class SearchListViewModel constructor(
         val emptyArray: ArrayList<Server> = ArrayList<Server>()
         val getServersIdList = basePreferenceHelper.getServersId()
 
-        Log.d("test_search_logic", "getCountries() $text")
+//        Log.d("test_search_logic", "getCountries() $text")
 
         serversData.forEachIndexed { index, serverData ->
             serverData.servers?.forEachIndexed { indexes, server ->
@@ -91,7 +91,7 @@ class SearchListViewModel constructor(
         }
 
         val allServers2 = serverListViewModelSplash.liveDataAllServers.value
-        Log.d("test_serversall_p", "allServers2 = ${allServers2?.size}")
+//        Log.d("test_serversall_p", "allServers2 = ${allServers2?.size}")
         val searchServersList = allServers2?.let { serverListViewModelSplash.filterServersAllCategory(it) }
 
         searchServersList?.forEachIndexed { index, server ->
@@ -122,7 +122,7 @@ class SearchListViewModel constructor(
 
         filtersArray.forEachIndexed { index, server ->
             if (getServersIdList?.any { it == server.id } == true) {
-                Log.d("serversListGlobal", "match - ${server.server_name} ${server.id}")
+//                Log.d("serversListGlobal", "match - ${server.server_name} ${server.id}")
                 server.isFavourited = true
             }
             else {

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

@@ -221,7 +221,7 @@ class ServerListViewModel(context: Context): ViewModel() {
             it.forEach {
               it?.servers?.forEachIndexed { index, server ->
                   if (getServersIdList?.any { it == server.id } == true) {
-                      Log.d("serversListGlobal", "match - ${server.server_name} ${server.id}")
+//                      Log.d("serversListGlobal", "match - ${server.server_name} ${server.id}")
                       server.isFavourited = true
                   }
               }
@@ -266,7 +266,7 @@ class ServerListViewModel(context: Context): ViewModel() {
             serverDataGlobal?.servers?.let { serversListAllGlobal.addAll(it) }
         }
 
-        Log.d("test_lingual_splash", "serversListGlobalTV - ${serversListGlobalTV.size}")
+//        Log.d("test_lingual_splash", "serversListGlobalTV - ${serversListGlobalTV.size}")
 
         _mutableLiveDataGetServersGlobal.value = serversListGlobalTV
         mutableLiveDataAllServers.value = serversListAllGlobal
@@ -735,7 +735,7 @@ class ServerListViewModel(context: Context): ViewModel() {
 //            it.protocol.equals(preferencesHelper.getProtocol().title, ignoreCase = true)
 //        }
 
-        Log.d("test_fav_lang", "SLVM -> serverList = ${serverList.size}")
+//        Log.d("test_fav_lang", "SLVM -> serverList = ${serverList.size}")
 //        Log.d("test_fav_lang", "SLVM -> serverProtocol = ${serverProtocol.size}")
 //        Log.d("test_fav_lang", "SLVM -> distinctBy = ${distinctBy.size}")