Add a message when migrating the data (after 1 seconds)

This commit is contained in:
Benoit Marty 2022-07-15 17:32:49 +02:00 committed by Benoit Marty
parent b7826c02a3
commit 0bedfc8087
4 changed files with 44 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.airbnb.mvrx.viewModel import com.airbnb.mvrx.viewModel
@ -49,6 +50,7 @@ import im.vector.app.features.signout.hard.SignedOutActivity
import im.vector.app.features.start.StartAppAction import im.vector.app.features.start.StartAppAction
import im.vector.app.features.start.StartAppViewEvent import im.vector.app.features.start.StartAppViewEvent
import im.vector.app.features.start.StartAppViewModel import im.vector.app.features.start.StartAppViewModel
import im.vector.app.features.start.StartAppViewState
import im.vector.app.features.themes.ActivityOtherThemes import im.vector.app.features.themes.ActivityOtherThemes
import im.vector.app.features.ui.UiStateRepository import im.vector.app.features.ui.UiStateRepository
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -127,6 +129,9 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
startAppViewModel.onEach {
renderState(it)
}
startAppViewModel.viewEvents.stream() startAppViewModel.viewEvents.stream()
.onEach(::handleViewEvents) .onEach(::handleViewEvents)
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
@ -134,6 +139,13 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
startAppViewModel.handle(StartAppAction.StartApp) startAppViewModel.handle(StartAppAction.StartApp)
} }
private fun renderState(state: StartAppViewState) {
if (state.duration > 0) {
views.status.setText(R.string.updating_your_data)
}
views.status.isVisible = state.duration > 0
}
private fun handleViewEvents(event: StartAppViewEvent) { private fun handleViewEvents(event: StartAppViewEvent) {
when (event) { when (event) {
StartAppViewEvent.AppStarted -> handleAppStarted() StartAppViewEvent.AppStarted -> handleAppStarted()

View File

@ -24,8 +24,12 @@ 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.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds
class StartAppViewModel @AssistedInject constructor( class StartAppViewModel @AssistedInject constructor(
@Assisted val initialState: StartAppViewState, @Assisted val initialState: StartAppViewState,
@ -50,10 +54,20 @@ class StartAppViewModel @AssistedInject constructor(
} }
private fun handleStartApp() { private fun handleStartApp() {
startTimer()
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)
_viewEvents.post(StartAppViewEvent.AppStarted) _viewEvents.post(StartAppViewEvent.AppStarted)
} }
} }
private fun startTimer() {
setState { copy(duration = 0) }
tickerFlow(viewModelScope, 1.seconds.inWholeMilliseconds)
.onEach {
setState { copy(duration = duration + 1) }
}
.launchIn(viewModelScope)
}
} }

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?><!-- The background of this Activity is in the theme (so just added as tools here, for preview),
<!-- The background of this Activity is in the theme (so just added as tools here, for preview),
so we just add a ProgressBar here --> so we just add a ProgressBar here -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
@ -22,4 +21,20 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/layout_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_marginEnd="@dimen/layout_horizontal_margin"
android:gravity="center"
android:textColor="@color/palette_white"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/progressBar"
tools:text="@string/updating_your_data"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1625,6 +1625,7 @@
<string name="error_no_network">No network. Please check your Internet connection.</string> <string name="error_no_network">No network. Please check your Internet connection.</string>
<string name="change_room_directory_network">"Change network"</string> <string name="change_room_directory_network">"Change network"</string>
<string name="please_wait">"Please wait…"</string> <string name="please_wait">"Please wait…"</string>
<string name="updating_your_data">Updating your data…</string>
<!--TODO: delete--> <!--TODO: delete-->
<string name="group_all_communities" tools:ignore="UnusedResources">"All Communities"</string> <string name="group_all_communities" tools:ignore="UnusedResources">"All Communities"</string>