Remove listener use fragmentCallback

This commit is contained in:
Valere 2021-09-29 14:40:18 +02:00
parent d24f448c70
commit cdc6b7e7d5
6 changed files with 114 additions and 51 deletions

View file

@ -27,6 +27,8 @@ import android.view.MenuItem
import androidx.core.view.GravityCompat
import androidx.core.view.isVisible
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.viewModel
import com.google.android.material.appbar.MaterialToolbar
@ -93,7 +95,8 @@ class HomeActivity :
UnreadMessagesSharedViewModel.Factory,
PromoteRestrictedViewModel.Factory,
NavigationInterceptor,
SpaceInviteBottomSheet.InteractionListener {
SpaceInviteBottomSheet.InteractionListener,
MatrixToBottomSheet.InteractionListener {
private lateinit var sharedActionViewModel: HomeSharedActionViewModel
@ -142,6 +145,22 @@ class HomeActivity :
}
}
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = this@HomeActivity
}
super.onFragmentResumed(fm, f)
}
override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = null
}
super.onFragmentPaused(fm, f)
}
}
private val drawerListener = object : DrawerLayout.SimpleDrawerListener() {
override fun onDrawerStateChanged(newState: Int) {
hideKeyboard()
@ -170,6 +189,7 @@ class HomeActivity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false)
FcmHelper.ensureFcmTokenIsRetrieved(this, pushManager, vectorPreferences.areNotificationEnabledForDevice())
sharedActionViewModel = viewModelProvider.get(HomeSharedActionViewModel::class.java)
views.drawerLayout.addDrawerListener(drawerListener)
@ -445,6 +465,7 @@ class HomeActivity :
override fun onDestroy() {
views.drawerLayout.removeDrawerListener(drawerListener)
supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
super.onDestroy()
}
@ -526,30 +547,15 @@ class HomeActivity :
}
override fun navToMemberProfile(userId: String, deepLink: Uri): Boolean {
val listener = object : MatrixToBottomSheet.InteractionListener {
override fun navigateToRoom(roomId: String) {
navigator.openRoom(this@HomeActivity, roomId)
}
}
// TODO check if there is already one??
MatrixToBottomSheet.withLink(deepLink.toString(), listener)
MatrixToBottomSheet.withLink(deepLink.toString())
.show(supportFragmentManager, "HA#MatrixToBottomSheet")
return true
}
override fun navToRoom(roomId: String?, eventId: String?, deepLink: Uri?): Boolean {
if (roomId == null) return false
val listener = object : MatrixToBottomSheet.InteractionListener {
override fun navigateToRoom(roomId: String) {
navigator.openRoom(this@HomeActivity, roomId)
}
override fun switchToSpace(spaceId: String) {
navigator.switchToSpace(this@HomeActivity, spaceId, Navigator.PostSwitchSpaceAction.None)
}
}
MatrixToBottomSheet.withLink(deepLink.toString(), listener)
MatrixToBottomSheet.withLink(deepLink.toString())
.show(supportFragmentManager, "HA#MatrixToBottomSheet")
return true
}
@ -586,4 +592,12 @@ class HomeActivity :
}
override fun create(initialState: ActiveSpaceViewState) = promoteRestrictedViewModelFactory.create(initialState)
override fun mxToBottomSheetNavigateToRoom(roomId: String) {
navigator.openRoom(this, roomId)
}
override fun mxToBottomSheetSwitchToSpace(spaceId: String) {
navigator.switchToSpace(this, spaceId, Navigator.PostSwitchSpaceAction.None)
}
}

View file

@ -20,10 +20,12 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.google.android.material.appbar.MaterialToolbar
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.airbnb.mvrx.viewModel
import com.google.android.material.appbar.MaterialToolbar
import im.vector.app.R
import im.vector.app.core.di.ScreenComponent
import im.vector.app.core.extensions.hideKeyboard
@ -32,25 +34,44 @@ import im.vector.app.core.platform.ToolbarConfigurable
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivityRoomDetailBinding
import im.vector.app.features.home.room.breadcrumbs.BreadcrumbsFragment
import im.vector.app.features.matrixto.MatrixToBottomSheet
import im.vector.app.features.navigation.Navigator
import im.vector.app.features.room.RequireActiveMembershipAction
import im.vector.app.features.room.RequireActiveMembershipViewEvents
import im.vector.app.features.room.RequireActiveMembershipViewModel
import im.vector.app.features.room.RequireActiveMembershipViewState
import im.vector.app.features.widgets.permissions.RoomWidgetPermissionViewModel
import im.vector.app.features.widgets.permissions.RoomWidgetPermissionViewState
import javax.inject.Inject
class RoomDetailActivity :
VectorBaseActivity<ActivityRoomDetailBinding>(),
ToolbarConfigurable,
RequireActiveMembershipViewModel.Factory,
RoomWidgetPermissionViewModel.Factory {
RoomWidgetPermissionViewModel.Factory,
MatrixToBottomSheet.InteractionListener {
override fun getBinding(): ActivityRoomDetailBinding {
return ActivityRoomDetailBinding.inflate(layoutInflater)
}
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = this@RoomDetailActivity
}
super.onFragmentResumed(fm, f)
}
override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = null
}
super.onFragmentPaused(fm, f)
}
}
override fun getCoordinatorLayout() = views.coordinatorLayout
private lateinit var sharedActionViewModel: RoomDetailSharedActionViewModel
@ -79,6 +100,7 @@ class RoomDetailActivity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false)
waitingView = views.waitingView.waitingView
val roomDetailArgs: RoomDetailArgs? = if (intent?.action == ACTION_ROOM_DETAILS_FROM_SHORTCUT) {
RoomDetailArgs(roomId = intent?.extras?.getString(EXTRA_ROOM_ID)!!)
@ -130,6 +152,7 @@ class RoomDetailActivity :
}
override fun onDestroy() {
supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
views.drawerLayout.removeDrawerListener(drawerListener)
super.onDestroy()
}
@ -182,4 +205,12 @@ class RoomDetailActivity :
}
}
}
override fun mxToBottomSheetNavigateToRoom(roomId: String) {
navigator.openRoom(this, roomId)
}
override fun mxToBottomSheetSwitchToSpace(spaceId: String) {
navigator.switchToSpace(this, spaceId, Navigator.PostSwitchSpaceAction.None)
}
}

View file

@ -65,13 +65,8 @@ class MatrixToBottomSheet :
private val viewModel by fragmentViewModel(MatrixToBottomSheetViewModel::class)
interface InteractionListener {
fun navigateToRoom(roomId: String)
fun switchToSpace(spaceId: String) {}
}
override fun onDestroyView() {
interactionListener = null
super.onDestroyView()
fun mxToBottomSheetNavigateToRoom(roomId: String)
fun mxToBottomSheetSwitchToSpace(spaceId: String)
}
override fun invalidate() = withState(viewModel) { state ->
@ -110,12 +105,12 @@ class MatrixToBottomSheet :
viewModel.observeViewEvents {
when (it) {
is MatrixToViewEvents.NavigateToRoom -> {
interactionListener?.navigateToRoom(it.roomId)
interactionListener?.mxToBottomSheetNavigateToRoom(it.roomId)
dismiss()
}
MatrixToViewEvents.Dismiss -> dismiss()
is MatrixToViewEvents.NavigateToSpace -> {
interactionListener?.switchToSpace(it.spaceId)
interactionListener?.mxToBottomSheetSwitchToSpace(it.spaceId)
dismiss()
}
is MatrixToViewEvents.ShowModalError -> {
@ -129,14 +124,13 @@ class MatrixToBottomSheet :
}
companion object {
fun withLink(matrixToLink: String, listener: InteractionListener?): MatrixToBottomSheet {
fun withLink(matrixToLink: String): MatrixToBottomSheet {
return MatrixToBottomSheet().apply {
arguments = Bundle().apply {
putParcelable(MvRx.KEY_ARG, MatrixToArgs(
matrixToLink = matrixToLink
))
}
interactionListener = listener
}
}
}

View file

@ -257,17 +257,11 @@ class DefaultNavigator @Inject constructor(
override fun openMatrixToBottomSheet(context: Context, link: String) {
if (context is AppCompatActivity) {
val listener = object : MatrixToBottomSheet.InteractionListener {
override fun navigateToRoom(roomId: String) {
openRoom(context, roomId)
}
override fun switchToSpace(spaceId: String) {
this@DefaultNavigator.switchToSpace(context, spaceId, Navigator.PostSwitchSpaceAction.None)
}
if (context !is MatrixToBottomSheet.InteractionListener) {
fatalError("Caller context should implement MatrixToBottomSheet.InteractionListener", vectorPreferences.failFast())
}
// TODO check if there is already one??
MatrixToBottomSheet.withLink(link, listener)
MatrixToBottomSheet.withLink(link)
.show(context.supportFragmentManager, "HA#MatrixToBottomSheet")
}
}

View file

@ -29,6 +29,7 @@ import im.vector.app.core.extensions.commitTransaction
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivitySimpleBinding
import im.vector.app.features.matrixto.MatrixToBottomSheet
import im.vector.app.features.navigation.Navigator
import im.vector.app.features.spaces.explore.SpaceDirectoryArgs
import im.vector.app.features.spaces.explore.SpaceDirectoryFragment
import im.vector.app.features.spaces.explore.SpaceDirectoryState
@ -51,18 +52,18 @@ class SpaceExploreActivity : VectorBaseActivity<ActivitySimpleBinding>(), SpaceD
val sharedViewModel: SpaceDirectoryViewModel by viewModel()
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentAttached(fm: FragmentManager, f: Fragment, context: Context) {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = this@SpaceExploreActivity
}
super.onFragmentAttached(fm, f, context)
super.onFragmentResumed(fm, f)
}
override fun onFragmentDetached(fm: FragmentManager, f: Fragment) {
override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = null
}
super.onFragmentDetached(fm, f)
super.onFragmentPaused(fm, f)
}
}
@ -93,7 +94,7 @@ class SpaceExploreActivity : VectorBaseActivity<ActivitySimpleBinding>(), SpaceD
navigator.openRoom(this, it.roomId)
}
is SpaceDirectoryViewEvents.NavigateToMxToBottomSheet -> {
MatrixToBottomSheet.withLink(it.link, this).show(supportFragmentManager, "ShowChild")
MatrixToBottomSheet.withLink(it.link).show(supportFragmentManager, "ShowChild")
}
}
}
@ -115,7 +116,11 @@ class SpaceExploreActivity : VectorBaseActivity<ActivitySimpleBinding>(), SpaceD
override fun create(initialState: SpaceDirectoryState): SpaceDirectoryViewModel =
spaceDirectoryViewModelFactory.create(initialState)
override fun navigateToRoom(roomId: String) {
override fun mxToBottomSheetNavigateToRoom(roomId: String) {
navigator.openRoom(this, roomId)
}
override fun mxToBottomSheetSwitchToSpace(spaceId: String) {
navigator.switchToSpace(this, spaceId, Navigator.PostSwitchSpaceAction.None)
}
}

View file

@ -24,6 +24,7 @@ import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.viewModel
import com.airbnb.mvrx.withState
@ -60,8 +61,25 @@ class UserCodeActivity : VectorBaseActivity<ActivitySimpleBinding>(),
injector.inject(this)
}
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = this@UserCodeActivity
}
super.onFragmentResumed(fm, f)
}
override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
if (f is MatrixToBottomSheet) {
f.interactionListener = null
}
super.onFragmentPaused(fm, f)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false)
if (isFirstCreation()) {
// should be there early for shared element transition
@ -74,7 +92,7 @@ class UserCodeActivity : VectorBaseActivity<ActivitySimpleBinding>(),
UserCodeState.Mode.SCAN -> showFragment(ScanUserCodeFragment::class, Bundle.EMPTY)
is UserCodeState.Mode.RESULT -> {
showFragment(ShowUserCodeFragment::class, Bundle.EMPTY)
MatrixToBottomSheet.withLink(mode.rawLink, this).show(supportFragmentManager, "MatrixToBottomSheet")
MatrixToBottomSheet.withLink(mode.rawLink).show(supportFragmentManager, "MatrixToBottomSheet")
}
}
}
@ -97,6 +115,11 @@ class UserCodeActivity : VectorBaseActivity<ActivitySimpleBinding>(),
}
}
override fun onDestroy() {
supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
super.onDestroy()
}
private fun showFragment(fragmentClass: KClass<out Fragment>, bundle: Bundle) {
if (supportFragmentManager.findFragmentByTag(fragmentClass.simpleName) == null) {
supportFragmentManager.commitTransaction {
@ -110,10 +133,12 @@ class UserCodeActivity : VectorBaseActivity<ActivitySimpleBinding>(),
}
}
override fun navigateToRoom(roomId: String) {
override fun mxToBottomSheetNavigateToRoom(roomId: String) {
navigator.openRoom(this, roomId)
}
override fun mxToBottomSheetSwitchToSpace(spaceId: String) {}
override fun onBackPressed() = withState(sharedViewModel) {
when (it.mode) {
UserCodeState.Mode.SHOW -> super.onBackPressed()