From bb237e3bbb54c69a390ffe5ecfc2e799ff6371b0 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 8 Nov 2019 17:21:46 +0100 Subject: [PATCH] Fix ViewModel for Signout --- .../riotx/core/platform/VectorBaseActivity.kt | 2 +- .../riotx/features/home/HomeDetailFragment.kt | 2 - .../SignOutBottomSheetDialogFragment.kt | 29 +++--------- .../workers/signout/SignOutViewModel.kt | 44 +++++-------------- 4 files changed, 18 insertions(+), 59 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt index 0cbc4e9b1d..f0f6eea91d 100644 --- a/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/riotx/core/platform/VectorBaseActivity.kt @@ -138,7 +138,7 @@ abstract class VectorBaseActivity : BaseMvRxActivity(), HasScreenInjector { supportFragmentManager.fragmentFactory = screenComponent.fragmentFactory() super.onCreate(savedInstanceState) viewModelFactory = screenComponent.viewModelFactory() - configurationViewModel = ViewModelProviders.of(this, viewModelFactory).get(ConfigurationViewModel::class.java) + configurationViewModel = viewModelProvider.get(ConfigurationViewModel::class.java) bugReporter = screenComponent.bugReporter() // Shake detector rageShake = screenComponent.rageShake() diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeDetailFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeDetailFragment.kt index 77d606b14e..5b56a19648 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeDetailFragment.kt @@ -92,8 +92,6 @@ class HomeDetailFragment @Inject constructor( // Use the SignOutViewModel, it observe the keys backup state and this is what we need here val model = fragmentViewModelProvider.get(SignOutViewModel::class.java) - model.init(session) - model.keysBackupState.observe(viewLifecycleOwner, Observer { keysBackupState -> when (keysBackupState) { null -> diff --git a/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutBottomSheetDialogFragment.kt b/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutBottomSheetDialogFragment.kt index 32d67b87ca..94c718466f 100644 --- a/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutBottomSheetDialogFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutBottomSheetDialogFragment.kt @@ -18,7 +18,6 @@ package im.vector.riotx.features.workers.signout import android.app.Activity import android.app.Dialog -import android.content.Context import android.content.Intent import android.os.Bundle import android.view.LayoutInflater @@ -31,27 +30,19 @@ import android.widget.TextView import androidx.appcompat.app.AlertDialog import androidx.core.view.isVisible import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.ViewModelProviders import androidx.transition.TransitionManager import butterknife.BindView import butterknife.ButterKnife import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog -import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.crypto.keysbackup.KeysBackupState import im.vector.riotx.R -import im.vector.riotx.core.di.DaggerScreenComponent -import im.vector.riotx.core.platform.VectorBaseActivity +import im.vector.riotx.core.platform.VectorBaseBottomSheetDialogFragment import im.vector.riotx.core.utils.toast import im.vector.riotx.features.crypto.keysbackup.settings.KeysBackupManageActivity import im.vector.riotx.features.crypto.keysbackup.setup.KeysBackupSetupActivity -class SignOutBottomSheetDialogFragment : BottomSheetDialogFragment() { - - lateinit var session: Session - lateinit var viewModelFactory: ViewModelProvider.Factory +class SignOutBottomSheetDialogFragment : VectorBaseBottomSheetDialogFragment() { @BindView(R.id.bottom_sheet_signout_warning_text) lateinit var sheetTitle: TextView @@ -97,20 +88,10 @@ class SignOutBottomSheetDialogFragment : BottomSheetDialogFragment() { private lateinit var viewModel: SignOutViewModel - override fun onAttach(context: Context) { - super.onAttach(context) - val vectorBaseActivity = activity as VectorBaseActivity - val screenComponent = DaggerScreenComponent.factory().create(vectorBaseActivity.getVectorComponent(), vectorBaseActivity) - viewModelFactory = screenComponent.viewModelFactory() - session = screenComponent.activeSessionHolder().getActiveSession() - } - override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - viewModel = ViewModelProviders.of(this, viewModelFactory).get(SignOutViewModel::class.java) - - viewModel.init(session) + viewModel = fragmentViewModelProvider.get(SignOutViewModel::class.java) setupClickableView.setOnClickListener { context?.let { context -> @@ -162,7 +143,7 @@ class SignOutBottomSheetDialogFragment : BottomSheetDialogFragment() { } } - viewModel.keysExportedToFile.observe(this, Observer { + viewModel.keysExportedToFile.observe(viewLifecycleOwner, Observer { val hasExportedToFile = it ?: false if (hasExportedToFile) { // We can allow to sign out @@ -177,7 +158,7 @@ class SignOutBottomSheetDialogFragment : BottomSheetDialogFragment() { } }) - viewModel.keysBackupState.observe(this, Observer { + viewModel.keysBackupState.observe(viewLifecycleOwner, Observer { if (viewModel.keysExportedToFile.value == true) { // ignore this return@Observer diff --git a/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutViewModel.kt b/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutViewModel.kt index c32d6f6631..8964b29952 100644 --- a/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/workers/signout/SignOutViewModel.kt @@ -23,59 +23,43 @@ import im.vector.matrix.android.api.session.crypto.keysbackup.KeysBackupState import im.vector.matrix.android.api.session.crypto.keysbackup.KeysBackupStateListener import javax.inject.Inject -class SignOutViewModel @Inject constructor() : ViewModel(), KeysBackupStateListener { +class SignOutViewModel @Inject constructor(private val session: Session) : ViewModel(), KeysBackupStateListener { // Keys exported manually var keysExportedToFile = MutableLiveData() var keysBackupState = MutableLiveData() - private var mxSession: Session? = null + init { + session.getKeysBackupService().addListener(this) - fun init(session: Session) { - if (mxSession == null) { - mxSession = session - - mxSession?.getKeysBackupService() - ?.addListener(this) - } - - keysBackupState.value = mxSession?.getKeysBackupService() - ?.state + keysBackupState.value = session.getKeysBackupService().state } /** * Safe way to get the current KeysBackup version */ fun getCurrentBackupVersion(): String { - return mxSession - ?.getKeysBackupService() - ?.currentBackupVersion - ?: "" + return session.getKeysBackupService().currentBackupVersion ?: "" } /** * Safe way to get the number of keys to backup */ fun getNumberOfKeysToBackup(): Int { - return mxSession - ?.inboundGroupSessionsCount(false) - ?: 0 + return session.inboundGroupSessionsCount(false) } /** * Safe way to tell if there are more keys on the server */ fun canRestoreKeys(): Boolean { - return mxSession - ?.getKeysBackupService() - ?.canRestoreKeys() == true + return session.getKeysBackupService().canRestoreKeys() } override fun onCleared() { super.onCleared() - mxSession?.getKeysBackupService() - ?.removeListener(this) + session.getKeysBackupService().removeListener(this) } override fun onStateChange(newState: KeysBackupState) { @@ -84,7 +68,7 @@ class SignOutViewModel @Inject constructor() : ViewModel(), KeysBackupStateListe fun refreshRemoteStateIfNeeded() { if (keysBackupState.value == KeysBackupState.Disabled) { - mxSession?.getKeysBackupService()?.checkAndStartKeysBackup() + session.getKeysBackupService().checkAndStartKeysBackup() } } @@ -92,13 +76,9 @@ class SignOutViewModel @Inject constructor() : ViewModel(), KeysBackupStateListe /** * The backup check on logout flow has to be displayed if there are keys in the store, and the keys backup state is not Ready */ - fun doYouNeedToBeDisplayed(session: Session?): Boolean { - return session - ?.inboundGroupSessionsCount(false) - ?: 0 > 0 - && session - ?.getKeysBackupService() - ?.state != KeysBackupState.ReadyToBackUp + fun doYouNeedToBeDisplayed(session: Session): Boolean { + return session.inboundGroupSessionsCount(false) > 0 + && session.getKeysBackupService().state != KeysBackupState.ReadyToBackUp } } }