Avoid updating the UI every seconds
This commit is contained in:
parent
9dda647c52
commit
3063c0da49
@ -142,23 +142,27 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
|
||||
}
|
||||
|
||||
private fun renderState(state: StartAppViewState) {
|
||||
if (state.duration > 0) {
|
||||
if (state.mayBeLongToProcess) {
|
||||
views.status.setText(R.string.updating_your_data)
|
||||
}
|
||||
views.status.isVisible = state.duration > 0
|
||||
if (state.duration == 1L && startAppViewModel.shouldStartApp()) {
|
||||
// Start foreground service, because the operation may take a while
|
||||
val intent = Intent(this, StartAppAndroidService::class.java)
|
||||
ContextCompat.startForegroundService(this, intent)
|
||||
}
|
||||
views.status.isVisible = state.mayBeLongToProcess
|
||||
}
|
||||
|
||||
private fun handleViewEvents(event: StartAppViewEvent) {
|
||||
when (event) {
|
||||
StartAppViewEvent.StartForegroundService -> handleStartForegroundService()
|
||||
StartAppViewEvent.AppStarted -> handleAppStarted()
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleStartForegroundService() {
|
||||
if (startAppViewModel.shouldStartApp()) {
|
||||
// Start foreground service, because the operation may take a while
|
||||
val intent = Intent(this, StartAppAndroidService::class.java)
|
||||
ContextCompat.startForegroundService(this, intent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleAppStarted() {
|
||||
if (intent.hasExtra(EXTRA_NEXT_INTENT)) {
|
||||
// Start the next Activity
|
||||
|
@ -19,5 +19,13 @@ package im.vector.app.features.start
|
||||
import im.vector.app.core.platform.VectorViewEvents
|
||||
|
||||
sealed interface StartAppViewEvent : VectorViewEvents {
|
||||
/**
|
||||
* Will be sent if the process is taking more than 1 second.
|
||||
*/
|
||||
object StartForegroundService : StartAppViewEvent
|
||||
|
||||
/**
|
||||
* Will be sent when the current Session has been set.
|
||||
*/
|
||||
object AppStarted : StartAppViewEvent
|
||||
}
|
||||
|
@ -24,10 +24,8 @@ import im.vector.app.core.di.ActiveSessionSetter
|
||||
import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
||||
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||
import im.vector.app.core.platform.VectorViewModel
|
||||
import im.vector.lib.core.utils.flow.tickerFlow
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@ -54,7 +52,7 @@ class StartAppViewModel @AssistedInject constructor(
|
||||
}
|
||||
|
||||
private fun handleStartApp() {
|
||||
startTimer()
|
||||
handleLongProcessing()
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
// This can take time because of DB migration(s), so do it in a background task.
|
||||
activeSessionSetter.tryToSetActiveSession(startSync = true)
|
||||
@ -62,12 +60,11 @@ class StartAppViewModel @AssistedInject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTimer() {
|
||||
setState { copy(duration = 0) }
|
||||
tickerFlow(viewModelScope, 1.seconds.inWholeMilliseconds)
|
||||
.onEach {
|
||||
setState { copy(duration = duration + 1) }
|
||||
}
|
||||
.launchIn(viewModelScope)
|
||||
private fun handleLongProcessing() {
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
delay(1.seconds.inWholeMilliseconds)
|
||||
setState { copy(mayBeLongToProcess = true) }
|
||||
_viewEvents.post(StartAppViewEvent.StartForegroundService)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,5 +19,5 @@ package im.vector.app.features.start
|
||||
import com.airbnb.mvrx.MavericksState
|
||||
|
||||
data class StartAppViewState(
|
||||
val duration: Long = 0
|
||||
val mayBeLongToProcess: Boolean = false
|
||||
) : MavericksState
|
||||
|
Loading…
Reference in New Issue
Block a user