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) {
|
private fun renderState(state: StartAppViewState) {
|
||||||
if (state.duration > 0) {
|
if (state.mayBeLongToProcess) {
|
||||||
views.status.setText(R.string.updating_your_data)
|
views.status.setText(R.string.updating_your_data)
|
||||||
}
|
}
|
||||||
views.status.isVisible = state.duration > 0
|
views.status.isVisible = state.mayBeLongToProcess
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleViewEvents(event: StartAppViewEvent) {
|
private fun handleViewEvents(event: StartAppViewEvent) {
|
||||||
when (event) {
|
when (event) {
|
||||||
|
StartAppViewEvent.StartForegroundService -> handleStartForegroundService()
|
||||||
StartAppViewEvent.AppStarted -> handleAppStarted()
|
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() {
|
private fun handleAppStarted() {
|
||||||
if (intent.hasExtra(EXTRA_NEXT_INTENT)) {
|
if (intent.hasExtra(EXTRA_NEXT_INTENT)) {
|
||||||
// Start the next Activity
|
// Start the next Activity
|
||||||
|
@ -19,5 +19,13 @@ package im.vector.app.features.start
|
|||||||
import im.vector.app.core.platform.VectorViewEvents
|
import im.vector.app.core.platform.VectorViewEvents
|
||||||
|
|
||||||
sealed interface StartAppViewEvent : 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
|
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.MavericksAssistedViewModelFactory
|
||||||
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||||
import im.vector.app.core.platform.VectorViewModel
|
import im.vector.app.core.platform.VectorViewModel
|
||||||
import im.vector.lib.core.utils.flow.tickerFlow
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
@ -54,7 +52,7 @@ class StartAppViewModel @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun handleStartApp() {
|
private fun handleStartApp() {
|
||||||
startTimer()
|
handleLongProcessing()
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
// This can take time because of DB migration(s), so do it in a background task.
|
// This can take time because of DB migration(s), so do it in a background task.
|
||||||
activeSessionSetter.tryToSetActiveSession(startSync = true)
|
activeSessionSetter.tryToSetActiveSession(startSync = true)
|
||||||
@ -62,12 +60,11 @@ class StartAppViewModel @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startTimer() {
|
private fun handleLongProcessing() {
|
||||||
setState { copy(duration = 0) }
|
viewModelScope.launch(Dispatchers.Default) {
|
||||||
tickerFlow(viewModelScope, 1.seconds.inWholeMilliseconds)
|
delay(1.seconds.inWholeMilliseconds)
|
||||||
.onEach {
|
setState { copy(mayBeLongToProcess = true) }
|
||||||
setState { copy(duration = duration + 1) }
|
_viewEvents.post(StartAppViewEvent.StartForegroundService)
|
||||||
}
|
}
|
||||||
.launchIn(viewModelScope)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,5 +19,5 @@ package im.vector.app.features.start
|
|||||||
import com.airbnb.mvrx.MavericksState
|
import com.airbnb.mvrx.MavericksState
|
||||||
|
|
||||||
data class StartAppViewState(
|
data class StartAppViewState(
|
||||||
val duration: Long = 0
|
val mayBeLongToProcess: Boolean = false
|
||||||
) : MavericksState
|
) : MavericksState
|
||||||
|
Loading…
Reference in New Issue
Block a user