Fix circular not downloaded after first start

This commit is contained in:
Matte23
2020-10-30 13:13:54 +01:00
parent 361631b6ac
commit 74ebc7bdc4

View File

@@ -31,16 +31,10 @@ class CircularLetterViewModel internal constructor(
private val circularRepository: CircularRepository, private val circularRepository: CircularRepository,
application: Application application: Application
) : AndroidViewModel(application), SharedPreferences.OnSharedPreferenceChangeListener { ) : AndroidViewModel(application), SharedPreferences.OnSharedPreferenceChangeListener {
private val preferenceManager = PreferenceManager.getDefaultSharedPreferences(application) private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application)
private val schoolID = private val schoolID =
MutableLiveData(preferenceManager.getString("school", "0")?.toInt() ?: 0) MutableLiveData(sharedPreferences.getString("school", "0")?.toInt() ?: 0)
init {
updateCirculars()
preferenceManager.registerOnSharedPreferenceChangeListener(this)
}
val query = MutableLiveData("") val query = MutableLiveData("")
val circulars: LiveData<List<Circular>> = val circulars: LiveData<List<Circular>> =
@@ -59,6 +53,13 @@ class CircularLetterViewModel internal constructor(
val circularsUpdated = MutableLiveData<Boolean>().apply { value = false } val circularsUpdated = MutableLiveData<Boolean>().apply { value = false }
private var isNotUpdating = true private var isNotUpdating = true
init {
if (!sharedPreferences.getBoolean("first_start", true))
updateCirculars()
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
fun updateCirculars() { fun updateCirculars() {
if (isNotUpdating) { if (isNotUpdating) {
viewModelScope.launch { viewModelScope.launch {
@@ -75,7 +76,12 @@ class CircularLetterViewModel internal constructor(
} }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == "school") val firstStart = sharedPreferences?.getBoolean("first_start", true) ?: true
schoolID.postValue(preferenceManager.getString("school", "0")?.toInt() ?: 0)
if (!firstStart && key == "school" || key == "first_start") {
schoolID.postValue(sharedPreferences?.getString("school", "0")?.toInt() ?: 0)
updateCirculars()
}
} }
} }