Fix ViewModel for Signout

This commit is contained in:
Benoit Marty 2019-11-08 17:21:46 +01:00
parent ec4d7e29ec
commit bb237e3bbb
4 changed files with 18 additions and 59 deletions

View file

@ -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()

View file

@ -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 ->

View file

@ -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

View file

@ -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<Boolean>()
var keysBackupState = MutableLiveData<KeysBackupState>()
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
}
}
}