diff --git a/changelog.d/3933.bugfix b/changelog.d/3933.bugfix new file mode 100644 index 0000000000..3396bd75e7 --- /dev/null +++ b/changelog.d/3933.bugfix @@ -0,0 +1 @@ +App doesn't take you to a Space after choosing to Join it \ No newline at end of file diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index 83f087639a..3d264104c3 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -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 @@ -96,7 +98,8 @@ class HomeActivity : UnreadMessagesSharedViewModel.Factory, PromoteRestrictedViewModel.Factory, NavigationInterceptor, - SpaceInviteBottomSheet.InteractionListener { + SpaceInviteBottomSheet.InteractionListener, + MatrixToBottomSheet.InteractionListener { private lateinit var sharedActionViewModel: HomeSharedActionViewModel @@ -145,6 +148,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() @@ -173,6 +192,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) @@ -449,6 +469,7 @@ class HomeActivity : override fun onDestroy() { views.drawerLayout.removeDrawerListener(drawerListener) + supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks) super.onDestroy() } @@ -530,30 +551,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) + } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt index 1bcb41cc35..76c3816ce6 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt @@ -22,6 +22,8 @@ import android.os.Bundle import android.widget.Toast 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 @@ -32,6 +34,8 @@ 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 @@ -44,12 +48,30 @@ class RoomDetailActivity : VectorBaseActivity(), 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 @@ -78,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)!!) @@ -129,6 +152,7 @@ class RoomDetailActivity : } override fun onDestroy() { + supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks) views.drawerLayout.removeDrawerListener(drawerListener) super.onDestroy() } @@ -181,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) + } } diff --git a/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheet.kt b/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheet.kt index 3e75b96c32..bf3e7a5d78 100644 --- a/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheet.kt +++ b/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheet.kt @@ -36,7 +36,6 @@ import im.vector.app.databinding.BottomSheetMatrixToCardBinding import im.vector.app.features.home.AvatarRenderer import kotlinx.parcelize.Parcelize import org.matrix.android.sdk.api.session.permalinks.PermalinkData -import java.lang.ref.WeakReference import javax.inject.Inject import kotlin.reflect.KClass @@ -57,13 +56,7 @@ class MatrixToBottomSheet : injector.inject(this) } - private var weakReference = WeakReference(null) - - var interactionListener: InteractionListener? - set(value) { - weakReference = WeakReference(value) - } - get() = weakReference.get() + var interactionListener: InteractionListener? = null override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): BottomSheetMatrixToCardBinding { return BottomSheetMatrixToCardBinding.inflate(inflater, container, false) @@ -72,8 +65,8 @@ class MatrixToBottomSheet : private val viewModel by fragmentViewModel(MatrixToBottomSheetViewModel::class) interface InteractionListener { - fun navigateToRoom(roomId: String) - fun switchToSpace(spaceId: String) {} + fun mxToBottomSheetNavigateToRoom(roomId: String) + fun mxToBottomSheetSwitchToSpace(spaceId: String) } override fun invalidate() = withState(viewModel) { state -> @@ -112,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 -> { @@ -131,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 } } } diff --git a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt index 6ad547fd42..55b74e9b34 100644 --- a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt +++ b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt @@ -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") } } diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpaceExploreActivity.kt b/vector/src/main/java/im/vector/app/features/spaces/SpaceExploreActivity.kt index dbe92d4d93..cee945e202 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpaceExploreActivity.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpaceExploreActivity.kt @@ -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(), 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) } } @@ -86,14 +87,14 @@ class SpaceExploreActivity : VectorBaseActivity(), SpaceD sharedViewModel.observeViewEvents { when (it) { - SpaceDirectoryViewEvents.Dismiss -> { + SpaceDirectoryViewEvents.Dismiss -> { finish() } - is SpaceDirectoryViewEvents.NavigateToRoom -> { + is SpaceDirectoryViewEvents.NavigateToRoom -> { 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(), 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) + } } diff --git a/vector/src/main/java/im/vector/app/features/usercode/UserCodeActivity.kt b/vector/src/main/java/im/vector/app/features/usercode/UserCodeActivity.kt index 0771a5d238..5e8145168b 100644 --- a/vector/src/main/java/im/vector/app/features/usercode/UserCodeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/usercode/UserCodeActivity.kt @@ -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(), 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(), 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(), } } + override fun onDestroy() { + supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks) + super.onDestroy() + } + private fun showFragment(fragmentClass: KClass, bundle: Bundle) { if (supportFragmentManager.findFragmentByTag(fragmentClass.simpleName) == null) { supportFragmentManager.commitTransaction { @@ -110,10 +133,12 @@ class UserCodeActivity : VectorBaseActivity(), } } - 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()