Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ fun GlobalSettingsScreen(vm: GlobalSettingsViewModel = viewModel()) {
availableQuery = ""
selectedQuery = ""
activeTab = "AVAILABLE"
vm.loadInstalledAppsIfNeeded()
showAppPicker = true
},
modifier = Modifier.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.lifecycle.viewModelScope
import com.masterdns.vpn.util.GlobalSettings
import com.masterdns.vpn.util.GlobalSettingsStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -28,19 +29,18 @@ class GlobalSettingsViewModel(app: Application) : AndroidViewModel(app) {

private val _installedApps = MutableStateFlow<List<AppEntry>>(emptyList())
val installedApps: StateFlow<List<AppEntry>> = _installedApps

init {
loadInstalledApps()
}
private var installedAppsJob: Job? = null
private var installedAppsLoaded = false

fun save(settings: GlobalSettings) {
viewModelScope.launch {
GlobalSettingsStore.save(getApplication(), settings)
}
}

private fun loadInstalledApps() {
viewModelScope.launch {
fun loadInstalledAppsIfNeeded() {
if (installedAppsLoaded || installedAppsJob?.isActive == true) return
installedAppsJob = viewModelScope.launch {
val appList = withContext(Dispatchers.IO) {
val packageManager = getApplication<Application>().packageManager
val launcherIntent = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
Expand Down Expand Up @@ -69,6 +69,7 @@ class GlobalSettingsViewModel(app: Application) : AndroidViewModel(app) {
.toList()
}
_installedApps.value = appList
installedAppsLoaded = true
}
}
}
Loading