Don't leak context in model

This commit is contained in:
tzugen 2021-04-21 23:01:59 +02:00
parent 25f072a5d5
commit fe69141e19
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 6 additions and 5 deletions

View File

@ -31,5 +31,5 @@ val appPermanentStorage = module {
single { get<AppDatabase>().serverSettingDao() } single { get<AppDatabase>().serverSettingDao() }
viewModel { ServerSettingsModel(get(), get(), androidContext()) } viewModel { ServerSettingsModel(get(), get(), get()) }
} }

View File

@ -1,9 +1,9 @@
package org.moire.ultrasonic.fragment package org.moire.ultrasonic.fragment
import android.content.Context import android.app.Application
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -22,8 +22,8 @@ import timber.log.Timber
class ServerSettingsModel( class ServerSettingsModel(
private val repository: ServerSettingDao, private val repository: ServerSettingDao,
private val activeServerProvider: ActiveServerProvider, private val activeServerProvider: ActiveServerProvider,
private val context: Context application: Application
) : ViewModel() { ) : AndroidViewModel(application) {
companion object { companion object {
private const val PREFERENCES_KEY_SERVER_MIGRATED = "serverMigrated" private const val PREFERENCES_KEY_SERVER_MIGRATED = "serverMigrated"
@ -54,6 +54,7 @@ class ServerSettingsModel(
if (rowCount == null || rowCount == 0) { if (rowCount == null || rowCount == 0) {
// First time load up the server settings from the Preferences // First time load up the server settings from the Preferences
val dbServerList = mutableListOf<ServerSetting>() val dbServerList = mutableListOf<ServerSetting>()
val context = getApplication<Application>().applicationContext
val settings = PreferenceManager.getDefaultSharedPreferences(context) val settings = PreferenceManager.getDefaultSharedPreferences(context)
val serverNum = settings.getInt(PREFERENCES_KEY_ACTIVE_SERVERS, 0) val serverNum = settings.getInt(PREFERENCES_KEY_ACTIVE_SERVERS, 0)