From 4827b76b80c72f12685c72b4f4a6217417a3d8fa Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 11 Jun 2019 15:45:40 +0200 Subject: [PATCH] Integrate keysbackup banner --- .../features/home/HomeDetailFragment.kt | 56 ++++++++++++++++++- .../features/navigation/DefaultNavigator.kt | 10 ++++ .../features/navigation/Navigator.kt | 3 + .../main/res/layout/fragment_home_detail.xml | 13 ++++- .../res/layout/view_keys_backup_banner.xml | 7 ++- 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt index cd47df2d38..7cd1e0b8fc 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt @@ -20,20 +20,26 @@ import android.os.Bundle import android.os.Parcelable import android.view.LayoutInflater import androidx.core.view.forEachIndexed +import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import com.airbnb.mvrx.args import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import com.google.android.material.bottomnavigation.BottomNavigationItemView import com.google.android.material.bottomnavigation.BottomNavigationMenuView +import im.vector.matrix.android.api.session.Session +import im.vector.matrix.android.api.session.crypto.keysbackup.KeysBackupState import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.ToolbarConfigurable import im.vector.riotredesign.core.platform.VectorBaseFragment +import im.vector.riotredesign.core.ui.views.KeysBackupBanner import im.vector.riotredesign.features.home.room.list.RoomListFragment import im.vector.riotredesign.features.home.room.list.RoomListParams import im.vector.riotredesign.features.home.room.list.UnreadCounterBadgeView +import im.vector.riotredesign.features.workers.signout.SignOutViewModel import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_home_detail.* +import org.koin.android.ext.android.inject @Parcelize @@ -50,7 +56,7 @@ private const val INDEX_CATCHUP = 0 private const val INDEX_PEOPLE = 1 private const val INDEX_ROOMS = 2 -class HomeDetailFragment : VectorBaseFragment() { +class HomeDetailFragment : VectorBaseFragment(), KeysBackupBanner.Delegate { private val params: HomeDetailParams by args() private val unreadCounterBadgeViews = arrayListOf() @@ -59,6 +65,8 @@ class HomeDetailFragment : VectorBaseFragment() { private val viewModel: HomeDetailViewModel by fragmentViewModel() private lateinit var navigationViewModel: HomeNavigationViewModel + private val session by inject() + override fun getLayoutResId(): Int { return R.layout.fragment_home_detail } @@ -73,6 +81,41 @@ class HomeDetailFragment : VectorBaseFragment() { switchDisplayMode(currentDisplayMode) setupBottomNavigationView() setupToolbar() + setupKeysBackupBanner() + } + + private fun setupKeysBackupBanner() { + // Keys backup banner + // Use the SignOutViewModel, it observe the keys backup state and this is what we need here + val model = ViewModelProviders.of(this).get(SignOutViewModel::class.java) + + model.init(session) + + model.keysBackupState.observe(this, Observer { keysBackupState -> + when (keysBackupState) { + null -> + homeKeysBackupBanner.render(KeysBackupBanner.State.Hidden, false) + KeysBackupState.Disabled -> + homeKeysBackupBanner.render(KeysBackupBanner.State.Setup(model.getNumberOfKeysToBackup()), false) + KeysBackupState.NotTrusted, + KeysBackupState.WrongBackUpVersion -> + // In this case, getCurrentBackupVersion() should not return "" + homeKeysBackupBanner.render(KeysBackupBanner.State.Recover(model.getCurrentBackupVersion()), false) + KeysBackupState.WillBackUp, + KeysBackupState.BackingUp -> + homeKeysBackupBanner.render(KeysBackupBanner.State.BackingUp, false) + KeysBackupState.ReadyToBackUp -> + if (model.canRestoreKeys()) { + homeKeysBackupBanner.render(KeysBackupBanner.State.Update(model.getCurrentBackupVersion()), false) + } else { + homeKeysBackupBanner.render(KeysBackupBanner.State.Hidden, false) + } + else -> + homeKeysBackupBanner.render(KeysBackupBanner.State.Hidden, false) + } + }) + + homeKeysBackupBanner.delegate = this } @@ -140,6 +183,17 @@ class HomeDetailFragment : VectorBaseFragment() { .commit() } + /* ========================================================================================== + * KeysBackupBanner Listener + * ========================================================================================== */ + + override fun setupKeysBackup() { + navigator.openKeysBackupSetup(requireActivity(), false) + } + + override fun recoverKeysBackup() { + navigator.openKeysBackupManager(requireActivity()) + } override fun invalidate() = withState(viewModel) { unreadCounterBadgeViews[INDEX_CATCHUP].render(UnreadCounterBadgeView.State(it.notificationCountCatchup, it.notificationHighlightCatchup)) diff --git a/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt b/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt index e084b79832..3dd5c6d9f5 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt @@ -19,6 +19,8 @@ package im.vector.riotredesign.features.navigation import android.content.Context import android.content.Intent import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom +import im.vector.riotredesign.features.crypto.keysbackup.settings.KeysBackupManageActivity +import im.vector.riotredesign.features.crypto.keysbackup.setup.KeysBackupSetupActivity import im.vector.riotredesign.features.debug.DebugMenuActivity import im.vector.riotredesign.features.home.room.detail.RoomDetailActivity import im.vector.riotredesign.features.home.room.detail.RoomDetailArgs @@ -53,4 +55,12 @@ class DefaultNavigator : Navigator { override fun openDebug(context: Context) { context.startActivity(Intent(context, DebugMenuActivity::class.java)) } + + override fun openKeysBackupSetup(context: Context, showManualExport: Boolean) { + context.startActivity(KeysBackupSetupActivity.intent(context, showManualExport)) + } + + override fun openKeysBackupManager(context: Context) { + context.startActivity(KeysBackupManageActivity.intent(context)) + } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt b/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt index 8873227b00..3754529ffb 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt @@ -31,4 +31,7 @@ interface Navigator { fun openDebug(context: Context) + fun openKeysBackupSetup(context: Context, showManualExport: Boolean) + + fun openKeysBackupManager(context: Context) } \ No newline at end of file diff --git a/vector/src/main/res/layout/fragment_home_detail.xml b/vector/src/main/res/layout/fragment_home_detail.xml index a54fa61e0a..7bf625389b 100644 --- a/vector/src/main/res/layout/fragment_home_detail.xml +++ b/vector/src/main/res/layout/fragment_home_detail.xml @@ -44,13 +44,24 @@ + + + app:layout_constraintTop_toBottomOf="@+id/homeKeysBackupBanner" /> - + android:minHeight="67dp" + tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"> - \ No newline at end of file + \ No newline at end of file