From b696e4a6deac35278957b4b0497fc0f3793e12a4 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 28 Dec 2018 12:35:40 +0100 Subject: [PATCH] Navigation : add simple fragment backstack management. --- .../core/platform/OnBackPressed.kt | 10 ++++++++ .../core/platform/RiotFragment.kt | 5 +++- .../riotredesign/features/MainActivity.kt | 2 -- .../features/RootFlowCoordinator.kt | 4 --- .../features/home/HomeActivity.kt | 25 +++++++++++++++++-- .../features/home/HomeFlowCoordinator.kt | 4 --- .../features/login/LoginFlowCoordinator.kt | 2 -- 7 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/im/vector/riotredesign/core/platform/OnBackPressed.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt diff --git a/app/src/main/java/im/vector/riotredesign/core/platform/OnBackPressed.kt b/app/src/main/java/im/vector/riotredesign/core/platform/OnBackPressed.kt new file mode 100644 index 0000000000..00faa0c76e --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/core/platform/OnBackPressed.kt @@ -0,0 +1,10 @@ +package im.vector.riotredesign.core.platform + +interface OnBackPressed { + + /** + * Returns true, if the on back pressed event has been handled by this Fragment. + * Otherwise return false + */ + fun onBackPressed(): Boolean +} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt b/app/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt index 88300a4ee1..79d39066ac 100644 --- a/app/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt @@ -2,12 +2,15 @@ package im.vector.riotredesign.core.platform import com.airbnb.mvrx.BaseMvRxFragment -abstract class RiotFragment : BaseMvRxFragment() { +abstract class RiotFragment : BaseMvRxFragment(), OnBackPressed { val riotActivity: RiotActivity by lazy { activity as RiotActivity } + override fun onBackPressed(): Boolean { + return false + } override fun invalidate() { //no-ops by default diff --git a/app/src/main/java/im/vector/riotredesign/features/MainActivity.kt b/app/src/main/java/im/vector/riotredesign/features/MainActivity.kt index 32258059c9..c4363a861e 100644 --- a/app/src/main/java/im/vector/riotredesign/features/MainActivity.kt +++ b/app/src/main/java/im/vector/riotredesign/features/MainActivity.kt @@ -5,14 +5,12 @@ import im.vector.matrix.android.api.Matrix import im.vector.riotredesign.core.platform.RiotActivity import im.vector.riotredesign.features.home.HomeActivity import im.vector.riotredesign.features.login.LoginActivity -import org.koin.android.ext.android.inject class MainActivity : RiotActivity() { private val authenticator = Matrix.getInstance().authenticator() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val intent = if (authenticator.hasActiveSessions()) { diff --git a/app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt b/app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt deleted file mode 100644 index 2c5fc2062c..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt +++ /dev/null @@ -1,4 +0,0 @@ -package im.vector.riotredesign.features - -class RootFlowCoordinator { -} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt index 7499694ac6..b930574249 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt @@ -3,6 +3,7 @@ package im.vector.riotredesign.features.home import android.content.Context import android.content.Intent import android.os.Bundle +import android.support.v4.app.FragmentManager import android.support.v4.view.GravityCompat import android.support.v4.widget.DrawerLayout import android.support.v7.app.ActionBarDrawerToggle @@ -12,6 +13,7 @@ import android.view.MenuItem import android.view.View import im.vector.riotredesign.R import im.vector.riotredesign.core.extensions.replaceFragment +import im.vector.riotredesign.core.platform.OnBackPressed import im.vector.riotredesign.core.platform.RiotActivity import im.vector.riotredesign.core.platform.ToolbarConfigurable import im.vector.riotredesign.features.home.room.detail.LoadingRoomDetailFragment @@ -46,7 +48,6 @@ class HomeActivity : RiotActivity(), HomeNavigator, ToolbarConfigurable { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - // Android home android.R.id.home -> { drawerLayout.openDrawer(GravityCompat.START) return true @@ -60,10 +61,30 @@ class HomeActivity : RiotActivity(), HomeNavigator, ToolbarConfigurable { if (drawerLayout.isDrawerOpen(Gravity.LEFT)) { drawerLayout.closeDrawer(Gravity.LEFT) } else { - super.onBackPressed() + val handled = recursivelyDispatchOnBackPressed(supportFragmentManager) + if (!handled) { + super.onBackPressed() + } } } + private fun recursivelyDispatchOnBackPressed(fm: FragmentManager): Boolean { + if (fm.backStackEntryCount == 0) + return false + val reverseOrder = fm.fragments.filter { it is OnBackPressed }.reversed() + for (f in reverseOrder) { + val handledByChildFragments = recursivelyDispatchOnBackPressed(f.childFragmentManager) + if (handledByChildFragments) { + return true + } + val backPressable = f as OnBackPressed + if (backPressable.onBackPressed()) { + return true + } + } + return false + } + // HomeNavigator ******************************************************************************* override fun openRoomDetail(roomId: String, eventId: String?) { diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt deleted file mode 100644 index 331b22fb0c..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt +++ /dev/null @@ -1,4 +0,0 @@ -package im.vector.riotredesign.features.home - -class HomeFlowCoordinator { -} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt b/app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt deleted file mode 100644 index 147879d124..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt +++ /dev/null @@ -1,2 +0,0 @@ -package im.vector.riotredesign.features.login -