Ensure we use the correct viewModelProvider

This commit is contained in:
Benoit Marty 2019-11-08 17:08:29 +01:00
parent a6df63f6d9
commit ec4d7e29ec
35 changed files with 75 additions and 40 deletions

View file

@ -69,11 +69,19 @@ abstract class VectorBaseActivity : BaseMvRxActivity(), HasScreenInjector {
@BindView(R.id.vector_coordinator_layout)
var coordinatorLayout: CoordinatorLayout? = null
/* ==========================================================================================
* View model
* ========================================================================================== */
private lateinit var viewModelFactory: ViewModelProvider.Factory
protected val viewModelProvider
get() = ViewModelProviders.of(this, viewModelFactory)
/* ==========================================================================================
* DATA
* ========================================================================================== */
protected lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var configurationViewModel: ConfigurationViewModel
private lateinit var sessionListener: SessionListener
protected lateinit var bugReporter: BugReporter

View file

@ -22,6 +22,7 @@ import android.os.Parcelable
import android.widget.FrameLayout
import androidx.annotation.CallSuper
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.MvRxView
import com.airbnb.mvrx.MvRxViewModelStore
@ -41,9 +42,24 @@ abstract class VectorBaseBottomSheetDialogFragment : BottomSheetDialogFragment()
override val mvrxViewModelStore by lazy { MvRxViewModelStore(viewModelStore) }
private lateinit var mvrxPersistedViewId: String
private lateinit var screenComponent: ScreenComponent
protected lateinit var viewModelFactory: ViewModelProvider.Factory
final override val mvrxViewId: String by lazy { mvrxPersistedViewId }
/* ==========================================================================================
* View model
* ========================================================================================== */
private lateinit var viewModelFactory: ViewModelProvider.Factory
protected val activityViewModelProvider
get() = ViewModelProviders.of(requireActivity(), viewModelFactory)
protected val fragmentViewModelProvider
get() = ViewModelProviders.of(this, viewModelFactory)
/* ==========================================================================================
* BottomSheetBehavior
* ========================================================================================== */
private var bottomSheetBehavior: BottomSheetBehavior<FrameLayout>? = null
val vectorBaseActivity: VectorBaseActivity by lazy {

View file

@ -25,6 +25,7 @@ import androidx.annotation.LayoutRes
import androidx.annotation.MainThread
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import butterknife.ButterKnife
import butterknife.Unbinder
import com.airbnb.mvrx.BaseMvRxFragment
@ -51,10 +52,21 @@ abstract class VectorBaseFragment : BaseMvRxFragment(), HasScreenInjector {
* Navigator
* ========================================================================================== */
protected lateinit var viewModelFactory: ViewModelProvider.Factory
protected lateinit var navigator: Navigator
private lateinit var screenComponent: ScreenComponent
/* ==========================================================================================
* View model
* ========================================================================================== */
private lateinit var viewModelFactory: ViewModelProvider.Factory
protected val activityViewModelProvider
get() = ViewModelProviders.of(requireActivity(), viewModelFactory)
protected val fragmentViewModelProvider
get() = ViewModelProviders.of(this, viewModelFactory)
/* ==========================================================================================
* Life cycle
* ========================================================================================== */

View file

@ -43,7 +43,7 @@ class KeysBackupRestoreActivity : SimpleFragmentActivity() {
override fun initUiAndData() {
super.initUiAndData()
viewModel = ViewModelProviders.of(this, viewModelFactory).get(KeysBackupRestoreSharedViewModel::class.java)
viewModel = viewModelProvider.get(KeysBackupRestoreSharedViewModel::class.java)
viewModel.initSession(session)
viewModel.keyVersionResult.observe(this, Observer { keyVersion ->

View file

@ -53,8 +53,8 @@ class KeysBackupRestoreFromKeyFragment @Inject constructor()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(KeysBackupRestoreFromKeyViewModel::class.java)
sharedViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(KeysBackupRestoreSharedViewModel::class.java)
viewModel = fragmentViewModelProvider.get(KeysBackupRestoreFromKeyViewModel::class.java)
sharedViewModel = activityViewModelProvider.get(KeysBackupRestoreSharedViewModel::class.java)
mKeyTextEdit.setText(viewModel.recoveryCode.value)
mKeyTextEdit.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {

View file

@ -64,8 +64,8 @@ class KeysBackupRestoreFromPassphraseFragment @Inject constructor(): VectorBaseF
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(KeysBackupRestoreFromPassphraseViewModel::class.java)
sharedViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(KeysBackupRestoreSharedViewModel::class.java)
viewModel = fragmentViewModelProvider.get(KeysBackupRestoreFromPassphraseViewModel::class.java)
sharedViewModel = activityViewModelProvider.get(KeysBackupRestoreSharedViewModel::class.java)
viewModel.passphraseErrorText.observe(viewLifecycleOwner, Observer { newValue ->
mPassphraseInputLayout.error = newValue

View file

@ -38,7 +38,7 @@ class KeysBackupRestoreSuccessFragment @Inject constructor() : VectorBaseFragmen
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(KeysBackupRestoreSharedViewModel::class.java)
sharedViewModel = activityViewModelProvider.get(KeysBackupRestoreSharedViewModel::class.java)
sharedViewModel.importKeyResult?.let {
val part1 = resources.getQuantityString(R.plurals.keys_backup_restore_success_description_part1,

View file

@ -43,7 +43,7 @@ class KeysBackupSetupActivity : SimpleFragmentActivity() {
replaceFragment(R.id.container, KeysBackupSetupStep1Fragment::class.java)
}
viewModel = ViewModelProviders.of(this, viewModelFactory).get(KeysBackupSetupSharedViewModel::class.java)
viewModel = viewModelProvider.get(KeysBackupSetupSharedViewModel::class.java)
viewModel.showManualExport.value = intent.getBooleanExtra(EXTRA_SHOW_MANUAL_EXPORT, false)
viewModel.initSession(session)

View file

@ -44,7 +44,7 @@ class KeysBackupSetupStep1Fragment @Inject constructor() : VectorBaseFragment()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(KeysBackupSetupSharedViewModel::class.java)
viewModel = activityViewModelProvider.get(KeysBackupSetupSharedViewModel::class.java)
viewModel.showManualExport.observe(viewLifecycleOwner, Observer {
val showOption = it ?: false

View file

@ -79,7 +79,7 @@ class KeysBackupSetupStep2Fragment @Inject constructor() : VectorBaseFragment()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(KeysBackupSetupSharedViewModel::class.java)
viewModel = activityViewModelProvider.get(KeysBackupSetupSharedViewModel::class.java)
viewModel.shouldPromptOnBack = true
bindViewToViewModel()

View file

@ -58,7 +58,7 @@ class KeysBackupSetupStep3Fragment @Inject constructor() : VectorBaseFragment()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(KeysBackupSetupSharedViewModel::class.java)
viewModel = activityViewModelProvider.get(KeysBackupSetupSharedViewModel::class.java)
viewModel.shouldPromptOnBack = false

View file

@ -83,7 +83,7 @@ class SASVerificationActivity : SimpleFragmentActivity() {
override fun initUiAndData() {
super.initUiAndData()
viewModel = ViewModelProviders.of(this, viewModelFactory).get(SasVerificationViewModel::class.java)
viewModel = viewModelProvider.get(SasVerificationViewModel::class.java)
val transactionID: String? = intent.getStringExtra(EXTRA_TRANSACTION_ID)
if (isFirstCreation()) {

View file

@ -51,7 +51,7 @@ class SASVerificationIncomingFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(SasVerificationViewModel::class.java)
viewModel = activityViewModelProvider.get(SasVerificationViewModel::class.java)
otherUserDisplayNameTextView.text = viewModel.otherUser?.displayName ?: viewModel.otherUserId
otherUserIdTextView.text = viewModel.otherUserId

View file

@ -62,7 +62,7 @@ class SASVerificationShortCodeFragment @Inject constructor(): VectorBaseFragment
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(SasVerificationViewModel::class.java)
viewModel = activityViewModelProvider.get(SasVerificationViewModel::class.java)
viewModel.transaction?.let {
if (it.supportsEmoji()) {

View file

@ -53,7 +53,7 @@ class SASVerificationStartFragment @Inject constructor(): VectorBaseFragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(vectorBaseActivity, viewModelFactory).get(SasVerificationViewModel::class.java)
viewModel = activityViewModelProvider.get(SasVerificationViewModel::class.java)
viewModel.transactionState.observe(viewLifecycleOwner, Observer {
val uxState = (viewModel.transaction as? OutgoingSasVerificationRequest)?.uxState
when (uxState) {

View file

@ -31,7 +31,7 @@ class SASVerificationVerifiedFragment @Inject constructor() : VectorBaseFragment
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(SasVerificationViewModel::class.java)
viewModel = activityViewModelProvider.get(SasVerificationViewModel::class.java)
}
@OnClick(R.id.sas_verification_verified_done_button)

View file

@ -69,7 +69,7 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FcmHelper.ensureFcmTokenIsRetrieved(this, pushManager)
sharedActionViewModel = ViewModelProviders.of(this, viewModelFactory).get(HomeSharedActionViewModel::class.java)
sharedActionViewModel = viewModelProvider.get(HomeSharedActionViewModel::class.java)
drawerLayout.addDrawerListener(drawerListener)
if (isFirstCreation()) {
replaceFragment(R.id.homeDetailFragmentContainer, LoadingFragment::class.java)
@ -147,7 +147,7 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
}
// Force remote backup state update to update the banner if needed
ViewModelProviders.of(this, viewModelFactory).get(SignOutViewModel::class.java).refreshRemoteStateIfNeeded()
viewModelProvider.get(SignOutViewModel::class.java).refreshRemoteStateIfNeeded()
}
override fun configure(toolbar: Toolbar) {

View file

@ -20,7 +20,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.view.forEachIndexed
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import com.google.android.material.bottomnavigation.BottomNavigationItemView
@ -63,7 +62,7 @@ class HomeDetailFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(HomeSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(HomeSharedActionViewModel::class.java)
setupBottomNavigationView()
setupToolbar()
@ -91,7 +90,7 @@ class HomeDetailFragment @Inject constructor(
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, viewModelFactory).get(SignOutViewModel::class.java)
val model = fragmentViewModelProvider.get(SignOutViewModel::class.java)
model.init(session)

View file

@ -51,7 +51,7 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
toolbar.visibility = View.GONE
sharedActionViewModel = ViewModelProviders.of(this, viewModelFactory).get(CreateDirectRoomSharedActionViewModel::class.java)
sharedActionViewModel = viewModelProvider.get(CreateDirectRoomSharedActionViewModel::class.java)
sharedActionViewModel
.observe()
.subscribe { sharedAction ->

View file

@ -43,7 +43,7 @@ class CreateDirectRoomDirectoryUsersFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(CreateDirectRoomSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(CreateDirectRoomSharedActionViewModel::class.java)
setupRecyclerView()
setupSearchByMatrixIdView()
setupCloseView()

View file

@ -53,7 +53,7 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(CreateDirectRoomSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(CreateDirectRoomSharedActionViewModel::class.java)
vectorBaseActivity.setSupportActionBar(createDirectRoomToolbar)
setupRecyclerView()
setupFilterView()

View file

@ -43,7 +43,7 @@ class GroupListFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(HomeSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(HomeSharedActionViewModel::class.java)
groupController.callback = this
stateView.contentView = groupListEpoxyRecyclerView
groupListEpoxyRecyclerView.setController(groupController)

View file

@ -206,7 +206,7 @@ class RoomDetailFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(MessageSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(MessageSharedActionViewModel::class.java)
attachmentsHelper = AttachmentsHelper.create(this, this).register()
keyboardStateUtils = KeyboardStateUtils(requireActivity())
setupToolbar(roomToolbar)

View file

@ -61,7 +61,7 @@ class MessageActionsBottomSheet : VectorBaseBottomSheetDialogFragment(), Message
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(MessageSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(MessageSharedActionViewModel::class.java)
recyclerView.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
recyclerView.adapter = messageActionsEpoxyController.adapter
// Disable item animation

View file

@ -100,7 +100,7 @@ class RoomListFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(RoomListQuickActionsSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(RoomListQuickActionsSharedActionViewModel::class.java)
setupCreateRoomButton()
setupRecyclerView()
roomListViewModel.subscribe { renderState(it) }

View file

@ -69,7 +69,7 @@ class RoomListQuickActionsBottomSheet : VectorBaseBottomSheetDialogFragment(), R
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(RoomListQuickActionsSharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(RoomListQuickActionsSharedActionViewModel::class.java)
recyclerView.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
recyclerView.adapter = roomListActionsEpoxyController.adapter
// Disable item animation

View file

@ -30,7 +30,7 @@ class EmojiChooserFragment @Inject constructor() : VectorBaseFragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(EmojiChooserViewModel::class.java)
viewModel = activityViewModelProvider.get(EmojiChooserViewModel::class.java)
viewModel.initWithContext(context!!)
(view as? RecyclerView)?.let {
it.adapter = viewModel.adapter

View file

@ -90,7 +90,7 @@ class EmojiReactionPickerActivity : VectorBaseActivity(),
tabLayout = findViewById(R.id.tabs)
viewModel = ViewModelProviders.of(this, viewModelFactory).get(EmojiChooserViewModel::class.java)
viewModel = viewModelProvider.get(EmojiChooserViewModel::class.java)
viewModel.eventId = intent.getStringExtra(EXTRA_EVENT_ID)

View file

@ -41,7 +41,7 @@ class EmojiSearchResultFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(EmojiChooserViewModel::class.java)
sharedViewModel = activityViewModelProvider.get(EmojiChooserViewModel::class.java)
epoxyController.listener = object : ReactionClickListener {
override fun onReactionSelected(reaction: String) {

View file

@ -93,7 +93,7 @@ class PublicRoomsFragment @Inject constructor(
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(RoomDirectorySharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(RoomDirectorySharedActionViewModel::class.java)
setupRecyclerView()
}

View file

@ -48,7 +48,7 @@ class RoomDirectoryActivity : VectorBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(this, viewModelFactory).get(RoomDirectorySharedActionViewModel::class.java)
sharedActionViewModel = viewModelProvider.get(RoomDirectorySharedActionViewModel::class.java)
if (isFirstCreation()) {
roomDirectoryViewModel.handle(RoomDirectoryAction.FilterWith(intent?.getStringExtra(INITIAL_FILTER) ?: ""))

View file

@ -55,7 +55,7 @@ class CreateRoomActivity : VectorBaseActivity(), ToolbarConfigurable {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(this, viewModelFactory).get(RoomDirectorySharedActionViewModel::class.java)
sharedActionViewModel = viewModelProvider.get(RoomDirectorySharedActionViewModel::class.java)
sharedActionViewModel
.observe()
.subscribe { sharedAction ->

View file

@ -43,7 +43,7 @@ class CreateRoomFragment @Inject constructor(private val createRoomController: C
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
vectorBaseActivity.setSupportActionBar(createRoomToolbar)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(RoomDirectorySharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(RoomDirectorySharedActionViewModel::class.java)
setupRecyclerView()
createRoomClose.setOnClickListener {
sharedActionViewModel.post(RoomDirectorySharedAction.Back)

View file

@ -46,7 +46,7 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr
return when (activity) {
is CreateRoomActivity -> activity.createRoomViewModelFactory.create(state)
is RoomDirectoryActivity -> activity.createRoomViewModelFactory.create(state)
else -> throw IllegalStateException("Wrong activity")
else -> error("Wrong activity")
}
}
}

View file

@ -72,7 +72,7 @@ class RoomDirectoryPickerFragment @Inject constructor(val roomDirectoryPickerVie
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
sharedActionViewModel = ViewModelProviders.of(requireActivity(), viewModelFactory).get(RoomDirectorySharedActionViewModel::class.java)
sharedActionViewModel = activityViewModelProvider.get(RoomDirectorySharedActionViewModel::class.java)
setupRecyclerView()
}