Ganfra's review: use NavigationViewModel to open the drawer

This commit is contained in:
Benoit Marty 2019-06-10 16:53:21 +02:00
parent b48c920292
commit fa297a7b6a
3 changed files with 44 additions and 6 deletions

View File

@ -27,10 +27,12 @@ import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.airbnb.mvrx.viewModel
import im.vector.matrix.android.api.Matrix
import im.vector.riotredesign.R
import im.vector.riotredesign.core.extensions.hideKeyboard
import im.vector.riotredesign.core.extensions.observeEvent
import im.vector.riotredesign.core.extensions.replaceFragment
import im.vector.riotredesign.core.platform.OnBackPressed
import im.vector.riotredesign.core.platform.ToolbarConfigurable
@ -46,7 +48,13 @@ import org.koin.android.scope.ext.android.getOrCreateScope

class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {

// Supported navigation actions for this Activity
sealed class Navigation {
object OpenDrawer : Navigation()
}

private val homeActivityViewModel: HomeActivityViewModel by viewModel()
private lateinit var navigationViewModel: HomeNavigationViewModel
private val homeNavigator by inject<HomeNavigator>()

private var progress: ProgressDialog? = null
@ -63,6 +71,9 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
super.onCreate(savedInstanceState)
bindScope(getOrCreateScope(HomeModule.HOME_SCOPE))
homeNavigator.activity = this

navigationViewModel = ViewModelProviders.of(this).get(HomeNavigationViewModel::class.java)

drawerLayout.addDrawerListener(drawerListener)
if (isFirstCreation()) {
val homeDrawerFragment = HomeDrawerFragment.newInstance()
@ -82,6 +93,12 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
progress?.dismiss()
}
})

navigationViewModel.navigateTo.observeEvent(this) { navigation ->
when (navigation) {
is Navigation.OpenDrawer -> drawerLayout.openDrawer(GravityCompat.START)
}
}
}

override fun onDestroy() {
@ -122,11 +139,6 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
return true
}

// Called by HomeDetailFragment
fun openDrawer() {
drawerLayout.openDrawer(GravityCompat.START)
}

override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)

View File

@ -20,6 +20,7 @@ import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import androidx.core.view.forEachIndexed
import androidx.lifecycle.ViewModelProviders
import com.airbnb.mvrx.args
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
@ -56,6 +57,7 @@ class HomeDetailFragment : VectorBaseFragment() {
private lateinit var currentDisplayMode: RoomListFragment.DisplayMode

private val viewModel: HomeDetailViewModel by fragmentViewModel()
private lateinit var navigationViewModel: HomeNavigationViewModel

override fun getLayoutResId(): Int {
return R.layout.fragment_home_detail
@ -65,6 +67,9 @@ class HomeDetailFragment : VectorBaseFragment() {
super.onActivityCreated(savedInstanceState)
currentDisplayMode = savedInstanceState?.getSerializable(CURRENT_DISPLAY_MODE) as? RoomListFragment.DisplayMode
?: RoomListFragment.DisplayMode.HOME

navigationViewModel = ViewModelProviders.of(requireActivity()).get(HomeNavigationViewModel::class.java)

switchDisplayMode(currentDisplayMode)
setupBottomNavigationView()
setupToolbar()
@ -89,7 +94,7 @@ class HomeDetailFragment : VectorBaseFragment() {
groupToolbarAvatarImageView
)
groupToolbarAvatarImageView.setOnClickListener {
(vectorBaseActivity as? HomeActivity)?.openDrawer()
navigationViewModel.goTo(HomeActivity.Navigation.OpenDrawer)
}
}


View File

@ -0,0 +1,21 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.riotredesign.features.home

import im.vector.riotredesign.core.mvrx.NavigationViewModel

class HomeNavigationViewModel : NavigationViewModel<HomeActivity.Navigation>()