Skip to content

Commit

Permalink
move more things that don't belong into the viewmodel out of the view…
Browse files Browse the repository at this point in the history
…model
  • Loading branch information
westnordost committed Mar 28, 2024
1 parent b82f5dc commit 8d685df
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.russhwolf.settings.ObservableSettings
import com.russhwolf.settings.SettingsListener
import de.westnordost.streetcomplete.data.CacheTrimmer
import de.westnordost.streetcomplete.data.CleanerWorker
import de.westnordost.streetcomplete.data.Preloader
Expand Down Expand Up @@ -50,9 +51,7 @@ import de.westnordost.streetcomplete.screens.settings.settingsModule
import de.westnordost.streetcomplete.screens.user.userScreenModule
import de.westnordost.streetcomplete.util.CrashReportExceptionHandler
import de.westnordost.streetcomplete.util.getDefaultTheme
import de.westnordost.streetcomplete.util.getSelectedLocale
import de.westnordost.streetcomplete.util.getSystemLocales
import de.westnordost.streetcomplete.util.ktx.addedToFront
import de.westnordost.streetcomplete.util.getSelectedLocales
import de.westnordost.streetcomplete.util.ktx.nowAsEpochMilliseconds
import de.westnordost.streetcomplete.util.logs.AndroidLogger
import de.westnordost.streetcomplete.util.logs.DatabaseLogger
Expand Down Expand Up @@ -84,6 +83,8 @@ class StreetCompleteApplication : Application() {

private val applicationScope = CoroutineScope(SupervisorJob() + CoroutineName("Application"))

private val settingsListeners = mutableListOf<SettingsListener>()

override fun onCreate() {
super.onCreate()

Expand Down Expand Up @@ -139,7 +140,7 @@ class StreetCompleteApplication : Application() {
userLoginStatusController.logOut()
}

setDefaultLocales()
updateDefaultLocales()

crashReportExceptionHandler.install()

Expand All @@ -150,7 +151,7 @@ class StreetCompleteApplication : Application() {

enqueuePeriodicCleanupWork()

setDefaultTheme()
updateDefaultTheme()

resurveyIntervalsUpdater.update()

Expand All @@ -161,6 +162,13 @@ class StreetCompleteApplication : Application() {
onNewVersion()
}
}

settingsListeners += prefs.addStringOrNullListener(Prefs.LANGUAGE_SELECT) {
updateDefaultLocales()
}
settingsListeners += prefs.addStringOrNullListener(Prefs.THEME_SELECT) {
updateDefaultTheme()
}
}

private fun onNewVersion() {
Expand All @@ -187,14 +195,11 @@ class StreetCompleteApplication : Application() {
}
}

private fun setDefaultLocales() {
val locale = getSelectedLocale(prefs)
if (locale != null) {
setDefaultLocales(getSystemLocales().addedToFront(locale))
}
private fun updateDefaultLocales() {
setDefaultLocales(getSelectedLocales(prefs))
}

private fun setDefaultTheme() {
private fun updateDefaultTheme() {
val theme = Prefs.Theme.valueOf(prefs.getStringOrNull(Prefs.THEME_SELECT) ?: getDefaultTheme())
AppCompatDelegate.setDefaultNightMode(theme.appCompatNightMode)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.westnordost.streetcomplete.data.osmnotes.notequests

import com.russhwolf.settings.ObservableSettings
import com.russhwolf.settings.SettingsListener
import de.westnordost.streetcomplete.ApplicationConstants
import de.westnordost.streetcomplete.Prefs
import de.westnordost.streetcomplete.data.osm.mapdata.BoundingBox
Expand Down Expand Up @@ -29,6 +30,8 @@ class OsmNoteQuestController(
private val showOnlyNotesPhrasedAsQuestions: Boolean get() =
!prefs.getBoolean(Prefs.SHOW_NOTES_NOT_PHRASED_AS_QUESTIONS, false)

private val settingsListener: SettingsListener

private val noteUpdatesListener = object : NotesWithEditsSource.Listener {
override fun onUpdated(added: Collection<Note>, updated: Collection<Note>, deleted: Collection<Long>) {
val hiddenNoteIds = getHiddenIds()
Expand Down Expand Up @@ -66,7 +69,7 @@ class OsmNoteQuestController(
init {
noteSource.addListener(noteUpdatesListener)
userLoginStatusSource.addListener(userLoginStatusListener)
prefs.addBooleanListener(Prefs.SHOW_NOTES_NOT_PHRASED_AS_QUESTIONS, false) {
settingsListener = prefs.addBooleanListener(Prefs.SHOW_NOTES_NOT_PHRASED_AS_QUESTIONS, false) {
// a lot of notes become visible/invisible if this option is changed
onInvalidated()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.westnordost.streetcomplete.screens.settings

import com.russhwolf.settings.ObservableSettings
import com.russhwolf.settings.SettingsListener
import de.westnordost.streetcomplete.ApplicationConstants
import de.westnordost.streetcomplete.Prefs
import de.westnordost.streetcomplete.Prefs.ResurveyIntervals.DEFAULT
Expand All @@ -12,19 +13,24 @@ import de.westnordost.streetcomplete.data.elementfilter.filters.RelativeDate
/** This class is just to access the user's preference about which multiplier for the resurvey
* intervals to use */
class ResurveyIntervalsUpdater(private val prefs: ObservableSettings) {
fun update() {
RelativeDate.MULTIPLIER = intervalsPreference.multiplier
}

init {
prefs.addStringOrNullListener(Prefs.RESURVEY_INTERVALS) { update() }
}
private val settingsListener: SettingsListener

private val intervalsPreference: Prefs.ResurveyIntervals get() =
valueOf(prefs.getString(
Prefs.RESURVEY_INTERVALS,
ApplicationConstants.DEFAULT_RESURVEY_INTERVALS
))

fun update() {
RelativeDate.MULTIPLIER = intervalsPreference.multiplier
}

init {
settingsListener = prefs.addStringOrNullListener(Prefs.RESURVEY_INTERVALS) { update() }
}

Check failure on line 32 in app/src/main/java/de/westnordost/streetcomplete/screens/settings/ResurveyIntervalsUpdater.kt

View workflow job for this annotation

GitHub Actions / Kotlin

Unexpected blank line(s) before "}"

Check failure on line 33 in app/src/main/java/de/westnordost/streetcomplete/screens/settings/ResurveyIntervalsUpdater.kt

View workflow job for this annotation

GitHub Actions / Kotlin

Needless blank line(s)
}

private val Prefs.ResurveyIntervals.multiplier: Float get() = when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import org.koin.dsl.module
val settingsModule = module {
single { ResurveyIntervalsUpdater(get()) }

viewModel<SettingsViewModel> { SettingsViewModelImpl(get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel<SettingsViewModel> { SettingsViewModelImpl(get(), get(), get(), get(), get(), get(), get(), get()) }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.westnordost.streetcomplete.screens.settings

import android.content.res.Resources
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.ViewModel
import com.russhwolf.settings.ObservableSettings
import com.russhwolf.settings.SettingsListener
Expand All @@ -20,11 +19,8 @@ import de.westnordost.streetcomplete.data.quest.QuestTypeRegistry
import de.westnordost.streetcomplete.data.visiblequests.QuestPreset
import de.westnordost.streetcomplete.data.visiblequests.QuestPresetsSource
import de.westnordost.streetcomplete.data.visiblequests.VisibleQuestTypeSource
import de.westnordost.streetcomplete.util.getDefaultTheme
import de.westnordost.streetcomplete.util.getSelectedLocales
import de.westnordost.streetcomplete.util.ktx.getYamlObject
import de.westnordost.streetcomplete.util.ktx.launch
import de.westnordost.streetcomplete.util.setDefaultLocales
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -102,14 +98,6 @@ class SettingsViewModelImpl(
osmNoteQuestsHiddenController.addListener(osmNoteQuestsHiddenListener)
osmQuestsHiddenController.addListener(osmQuestsHiddenListener)

listeners += prefs.addStringOrNullListener(Prefs.THEME_SELECT) { theme ->
val themeOrDefault = Prefs.Theme.valueOf(theme ?: getDefaultTheme())
AppCompatDelegate.setDefaultNightMode(themeOrDefault.appCompatNightMode)
}

listeners += prefs.addStringOrNullListener(Prefs.LANGUAGE_SELECT) {
setDefaultLocales(getSelectedLocales(prefs))
}
listeners += prefs.addIntOrNullListener(Prefs.MAP_TILECACHE_IN_MB) { size ->
tileCacheSize.value = size ?: ApplicationConstants.DEFAULT_MAP_CACHE_SIZE_IN_MB
}
Expand Down

0 comments on commit 8d685df

Please sign in to comment.