Skip to content

Commit

Permalink
mark viewmodels that are stable as @stable, explicitly enable strong …
Browse files Browse the repository at this point in the history
…skipping
  • Loading branch information
westnordost committed Nov 5, 2024
1 parent 2be4bfa commit 05ebc52
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ android {
namespace = "de.westnordost.streetcomplete"
}

composeCompiler {
enableStrongSkippingMode = true
}

val keystorePropertiesFile = rootProject.file("keystore.properties")
if (keystorePropertiesFile.exists()) {
val props = Properties()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package de.westnordost.streetcomplete.screens.about

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.changelog.Changelog
import de.westnordost.streetcomplete.util.html.HtmlNode
import de.westnordost.streetcomplete.util.ktx.launch
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

@Stable
abstract class ChangelogViewModel : ViewModel() {
/* version name -> html */
abstract val changelog: StateFlow<Map<String, List<HtmlNode>>?>
}

@Stable
class ChangelogViewModelImpl(private val changes: Changelog) : ChangelogViewModel() {
override val changelog = MutableStateFlow<Map<String, List<HtmlNode>>?>(null)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.westnordost.streetcomplete.screens.about

import android.content.res.Resources
import androidx.compose.runtime.Immutable

Check failure on line 4 in app/src/main/java/de/westnordost/streetcomplete/screens/about/CreditsViewModel.kt

View workflow job for this annotation

GitHub Actions / Kotlin

Unused import
import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.util.ktx.getYamlObject
Expand All @@ -10,6 +12,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.serialization.Serializable

@Stable
abstract class CreditsViewModel : ViewModel() {
abstract val credits: StateFlow<Credits?>
}
Expand All @@ -34,6 +37,7 @@ data class Contributor(
val githubLink: String get() = "https://github.com/$githubUsername"
}

@Stable
class CreditsViewModelImpl(private val resources: Resources) : CreditsViewModel() {
override val credits = MutableStateFlow<Credits?>(null)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.about.logs

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.westnordost.streetcomplete.data.logs.LogMessage
Expand All @@ -21,13 +22,15 @@ import kotlinx.coroutines.plus
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.LocalTime

@Stable
abstract class LogsViewModel : ViewModel() {
abstract val filters: StateFlow<LogsFilters>
abstract val logs: StateFlow<List<LogMessage>>

abstract fun setFilters(filters: LogsFilters)
}

@Stable
class LogsViewModelImpl(
private val logsController: LogsController,
) : LogsViewModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ fun MainScreen(
MapAttribution(Modifier.padding(8.dp))
}

if (intersection != null) {
val (offset, angle) = intersection
intersection?.let { (offset, angle) ->
val rotation = angle * 180 / PI
PointerPinButton(
onClick = onClickLocationPointer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.westnordost.streetcomplete.screens.main.map.maplibre.CameraPosition
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

// not @Stable, as not all fields are StateFlows or immutable
abstract class MainViewModel : ViewModel() {
/* error handling */
abstract val lastCrashReport: StateFlow<String?>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.main.edithistory

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.westnordost.osmfeatures.FeatureDictionary
Expand Down Expand Up @@ -28,6 +29,7 @@ import kotlinx.coroutines.withContext
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime

@Stable
abstract class EditHistoryViewModel : ViewModel() {
abstract val editItems: StateFlow<List<EditItem>>
abstract val selectedEdit: StateFlow<Edit?>
Expand All @@ -54,6 +56,7 @@ data class EditItem(
val showTime: Boolean,
)

@Stable
class EditHistoryViewModelImpl(
private val mapDataSource: MapDataWithEditsSource,
private val editHistoryController: EditHistoryController,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.westnordost.streetcomplete.screens.settings

import android.content.res.Resources
import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import com.russhwolf.settings.SettingsListener
import de.westnordost.streetcomplete.R
Expand All @@ -26,6 +27,7 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

@Stable
abstract class SettingsViewModel : ViewModel() {
abstract val selectableLanguageCodes: StateFlow<List<String>?>
abstract val selectedQuestPresetName: StateFlow<String?>
Expand Down Expand Up @@ -53,6 +55,7 @@ abstract class SettingsViewModel : ViewModel() {

data class QuestTypeCount(val total: Int, val enabled: Int)

@Stable
class SettingsViewModelImpl(
private val prefs: Preferences,
private val resources: Resources,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package de.westnordost.streetcomplete.screens.settings.debug

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.quest.QuestType
import de.westnordost.streetcomplete.data.quest.QuestTypeRegistry

@Stable
abstract class ShowQuestFormsViewModel : ViewModel() {
abstract val quests: List<QuestType>
}

@Stable
class ShowQuestFormsViewModelImpl(
private val questTypeRegistry: QuestTypeRegistry
) : ShowQuestFormsViewModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.settings.quest_presets

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.urlconfig.UrlConfigController
import de.westnordost.streetcomplete.data.visiblequests.QuestPreset
Expand All @@ -14,6 +15,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update

@Stable
abstract class QuestPresetsViewModel : ViewModel() {
abstract val presets: StateFlow<List<QuestPresetSelection>>

Expand All @@ -33,6 +35,7 @@ data class QuestPresetSelection(
val url: String? = null
)

@Stable
class QuestPresetsViewModelImpl(
private val questPresetsController: QuestPresetsController,
private val questTypeOrderController: QuestTypeOrderController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fun QuestSelectionScreen(
onClickBack: () -> Unit,
) {
val quests by viewModel.quests.collectAsState()
val selectedQuestPresetName by viewModel.selectedQuestPresetName.collectAsState()

var searchText by remember { mutableStateOf(TextFieldValue()) }

Expand All @@ -33,7 +34,7 @@ fun QuestSelectionScreen(

Column(Modifier.fillMaxSize()) {
QuestSelectionTopAppBar(
currentPresetName = viewModel.selectedQuestPresetName ?: stringResource(R.string.quest_presets_default_name),
currentPresetName = selectedQuestPresetName ?: stringResource(R.string.quest_presets_default_name),
onClickBack = onClickBack,
onUnselectAll = { viewModel.unselectAllQuests() },
onReset = { viewModel.resetQuestSelectionsAndOrder() },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.settings.quest_selection

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.countryboundaries.CountryBoundaries
import de.westnordost.streetcomplete.data.osm.osmquests.OsmElementQuestType
Expand All @@ -9,6 +10,7 @@ import de.westnordost.streetcomplete.data.quest.AllCountriesExcept
import de.westnordost.streetcomplete.data.quest.NoCountriesExcept
import de.westnordost.streetcomplete.data.quest.QuestType
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.QuestTypeOrderController
import de.westnordost.streetcomplete.data.visiblequests.QuestTypeOrderSource
Expand All @@ -22,9 +24,10 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update

@Stable
abstract class QuestSelectionViewModel : ViewModel() {
abstract val selectedQuestPresetName: String?
abstract val currentCountry: String?
abstract val selectedQuestPresetName: StateFlow<String?>
abstract val quests: StateFlow<List<QuestSelection>>

abstract fun selectQuest(questType: QuestType, selected: Boolean)
Expand All @@ -33,6 +36,7 @@ abstract class QuestSelectionViewModel : ViewModel() {
abstract fun resetQuestSelectionsAndOrder()
}

@Stable
class QuestSelectionViewModelImpl(
private val questTypeRegistry: QuestTypeRegistry,
private val questPresetsSource: QuestPresetsSource,
Expand Down Expand Up @@ -73,24 +77,38 @@ class QuestSelectionViewModelImpl(
override fun onQuestTypeOrdersChanged() { initQuests() }
}

private val questPresetsListener = object : QuestPresetsSource.Listener {
override fun onSelectedQuestPresetChanged() { updateSelectedQuestPresetName() }
override fun onAddedQuestPreset(preset: QuestPreset) {}
override fun onRenamedQuestPreset(preset: QuestPreset) {}
override fun onDeletedQuestPreset(presetId: Long) {}
}

override val quests = MutableStateFlow<List<QuestSelection>>(emptyList())

private val currentCountryCodes = countryBoundaries.value
.getIds(prefs.mapPosition)
private val currentCountryCodes = countryBoundaries.value.getIds(prefs.mapPosition)

override val selectedQuestPresetName: String?
get() = questPresetsSource.selectedQuestPresetName
override val selectedQuestPresetName = MutableStateFlow<String?>(null)

override val currentCountry: String?
get() = currentCountryCodes.firstOrNull()

init {
initQuests()
updateSelectedQuestPresetName()
questPresetsSource.addListener(questPresetsListener)
visibleQuestTypeController.addListener(visibleQuestsListener)
questTypeOrderController.addListener(questTypeOrderListener)
}

private fun updateSelectedQuestPresetName() {
launch(IO) {
selectedQuestPresetName.value = questPresetsSource.selectedQuestPresetName
}
}

override fun onCleared() {
questPresetsSource.removeListener(questPresetsListener)
visibleQuestTypeController.removeListener(visibleQuestsListener)
questTypeOrderController.removeListener(questTypeOrderListener)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package de.westnordost.streetcomplete.screens.user

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.user.UserLoginSource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

@Stable
abstract class UserViewModel : ViewModel() {
abstract val isLoggedIn: StateFlow<Boolean>
}

@Stable
class UserViewModelImpl(
private val userLoginSource: UserLoginSource
) : UserViewModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.user.achievements

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.user.achievements.Achievement
import de.westnordost.streetcomplete.data.user.achievements.AchievementsSource
Expand All @@ -9,11 +10,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

@Stable
abstract class AchievementsViewModel : ViewModel() {
abstract val isSynchronizingStatistics: StateFlow<Boolean>
abstract val achievements: StateFlow<List<Pair<Achievement, Int>>?>
}

@Stable
class AchievementsViewModelImpl(
private val achievementsSource: AchievementsSource,
private val statisticsSource: StatisticsSource,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.westnordost.streetcomplete.screens.user.edits

import android.content.res.Resources
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.AllEditTypes
Expand All @@ -13,6 +15,7 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

@Stable
abstract class EditStatisticsViewModel : ViewModel() {

abstract val isSynchronizingStatistics: StateFlow<Boolean>
Expand All @@ -31,8 +34,10 @@ abstract class EditStatisticsViewModel : ViewModel() {
abstract val flagAlignments: StateFlow<Map<String, FlagAlignment>?>
}

@Immutable
data class EditTypeStatistics(val type: EditType, val count: Int)

@Stable
class EditStatisticsViewModelImpl(
private val statisticsSource: StatisticsSource,
private val allEditTypes: AllEditTypes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.user.links

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.user.achievements.AchievementsSource
import de.westnordost.streetcomplete.data.user.achievements.Link
Expand All @@ -9,11 +10,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

@Stable
abstract class LinksViewModel : ViewModel() {
abstract val isSynchronizingStatistics: StateFlow<Boolean>
abstract val links: StateFlow<List<Link>?>
}

@Stable
class LinksViewModelImpl(
private val achievementsSource: AchievementsSource,
private val statisticsSource: StatisticsSource,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.westnordost.streetcomplete.screens.user.login

import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import de.westnordost.streetcomplete.data.UnsyncedChangesCountSource
import de.westnordost.streetcomplete.data.user.OAUTH2_AUTHORIZATION_URL
Expand All @@ -20,6 +21,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update

@Stable
abstract class LoginViewModel : ViewModel() {
abstract val unsyncedChangesCount: StateFlow<Int>

Expand Down Expand Up @@ -53,6 +55,7 @@ enum class LoginError : LoginState {
}
data object LoggedIn : LoginState

@Stable
class LoginViewModelImpl(
private val unsyncedChangesCountSource: UnsyncedChangesCountSource,
private val userLoginController: UserLoginController,
Expand Down
Loading

0 comments on commit 05ebc52

Please sign in to comment.