-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prefer having the viewmodel implementation in the same file as the in…
…terface There is only ever one implementation of the interface. The only reason why an interface exists is so that the StateFlows exposed on the interface are not mutable
- Loading branch information
1 parent
ef6eb16
commit 1be40b8
Showing
17 changed files
with
525 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/de/westnordost/streetcomplete/screens/about/ChangelogViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package de.westnordost.streetcomplete.screens.about | ||
|
||
import android.content.res.Resources | ||
import androidx.lifecycle.ViewModel | ||
import de.westnordost.streetcomplete.R | ||
import de.westnordost.streetcomplete.util.ktx.getRawTextFile | ||
import de.westnordost.streetcomplete.util.ktx.launch | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
abstract class ChangelogViewModel : ViewModel() { | ||
abstract val changelog: StateFlow<String?> | ||
} | ||
|
||
class ChangelogViewModelImpl(resources: Resources) : ChangelogViewModel() { | ||
override val changelog = MutableStateFlow<String?>(null) | ||
|
||
init { | ||
launch(Dispatchers.IO) { changelog.value = resources.getRawTextFile(R.raw.changelog) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
app/src/main/java/de/westnordost/streetcomplete/screens/about/CreditsViewModelImpl.kt
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
app/src/main/java/de/westnordost/streetcomplete/screens/about/LogsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,86 @@ | ||
package de.westnordost.streetcomplete.screens.about | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import de.westnordost.streetcomplete.data.logs.LogMessage | ||
import de.westnordost.streetcomplete.data.logs.LogsController | ||
import de.westnordost.streetcomplete.data.logs.LogsFilters | ||
import de.westnordost.streetcomplete.util.ktx.systemTimeNow | ||
import de.westnordost.streetcomplete.util.ktx.toEpochMilli | ||
import de.westnordost.streetcomplete.util.ktx.toLocalDate | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.shareIn | ||
import kotlinx.coroutines.flow.transformLatest | ||
import kotlinx.coroutines.plus | ||
import kotlinx.datetime.LocalDateTime | ||
import kotlinx.datetime.LocalTime | ||
|
||
abstract class LogsViewModel : ViewModel() { | ||
abstract val filters: StateFlow<LogsFilters> | ||
abstract val logs: StateFlow<List<LogMessage>> | ||
|
||
abstract fun setFilters(filters: LogsFilters) | ||
} | ||
|
||
class LogsViewModelImpl( | ||
private val logsController: LogsController, | ||
) : LogsViewModel() { | ||
|
||
override val filters = MutableStateFlow(LogsFilters( | ||
timestampNewerThan = LocalDateTime(systemTimeNow().toLocalDate(), LocalTime(0, 0, 0)) | ||
)) | ||
|
||
/** | ||
* Produce a call back flow of all incoming logs matching the given [filters]. | ||
*/ | ||
private fun getIncomingLogs(filters: LogsFilters) = callbackFlow { | ||
// Listener that sends the messages matching the filters to the observer | ||
val listener = object : LogsController.Listener { | ||
override fun onAdded(message: LogMessage) { | ||
if (filters.matches(message)) { | ||
trySend(message) // Send it to the observer | ||
} | ||
} | ||
} | ||
logsController.addListener(listener) | ||
awaitClose { logsController.removeListener(listener) } | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
private val _logs: SharedFlow<List<LogMessage>> = | ||
filters.transformLatest { filters -> | ||
val logs = logsController.getLogs(filters).toMutableList() | ||
|
||
emit(logs) | ||
|
||
getIncomingLogs(filters).collect { | ||
logs.add(it) | ||
emit(logs) | ||
} | ||
}.shareIn(viewModelScope + Dispatchers.IO, SharingStarted.Eagerly, 1) | ||
|
||
override val logs: StateFlow<List<LogMessage>> = object : | ||
StateFlow<List<LogMessage>>, | ||
SharedFlow<List<LogMessage>> by _logs { | ||
override val value: List<LogMessage> get() = replayCache[0] | ||
} | ||
|
||
override fun setFilters(filters: LogsFilters) { | ||
this.filters.value = filters | ||
} | ||
} | ||
|
||
private fun LogsController.getLogs(filters: LogsFilters) = | ||
getLogs( | ||
levels = filters.levels, | ||
messageContains = filters.messageContains, | ||
newerThan = filters.timestampNewerThan?.toEpochMilli(), | ||
olderThan = filters.timestampOlderThan?.toEpochMilli() | ||
) |
78 changes: 0 additions & 78 deletions
78
app/src/main/java/de/westnordost/streetcomplete/screens/about/LogsViewModelImpl.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.