From daef586461e8c0c4f03d8251641bec51392e8a27 Mon Sep 17 00:00:00 2001 From: terrakok Date: Fri, 9 Apr 2021 17:09:21 +0300 Subject: [PATCH] Move fragment transaction type to FragmentScreen property. --- .../com/github/terrakok/cicerone/Router.kt | 18 ++++------- .../cicerone/androidx/AppNavigator.kt | 31 ++++++++----------- .../terrakok/cicerone/androidx/AppScreen.kt | 1 + .../cicerone/androidx/TransactionInfo.kt | 21 ------------- .../com/github/terrakok/cicerone/commands.kt | 5 +-- 5 files changed, 21 insertions(+), 55 deletions(-) delete mode 100644 library/src/main/kotlin/com/github/terrakok/cicerone/androidx/TransactionInfo.kt diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/Router.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/Router.kt index cf6dd46..40a2298 100644 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/Router.kt +++ b/library/src/main/kotlin/com/github/terrakok/cicerone/Router.kt @@ -12,11 +12,9 @@ open class Router : BaseRouter() { * Open new screen and add it to the screens chain. * * @param screen screen - * @param clearContainer if FALSE then new screen shows over previous */ - @JvmOverloads - fun navigateTo(screen: Screen, clearContainer: Boolean = true) { - executeCommands(Forward(screen, clearContainer)) + fun navigateTo(screen: Screen) { + executeCommands(Forward(screen)) } /** @@ -57,11 +55,9 @@ open class Router : BaseRouter() { * Opens several screens inside single transaction. * * @param screens - * @param showOnlyTopScreenView if FALSE then all screen views show together */ - @JvmOverloads - fun newChain(vararg screens: Screen, showOnlyTopScreenView: Boolean = true) { - val commands = screens.map { Forward(it, showOnlyTopScreenView) } + fun newChain(vararg screens: Screen) { + val commands = screens.map { Forward(it) } executeCommands(*commands.toTypedArray()) } @@ -69,15 +65,13 @@ open class Router : BaseRouter() { * Clear current stack and open several screens inside single transaction. * * @param screens - * @param showOnlyTopScreenView if FALSE then all screen views show together */ - @JvmOverloads - fun newRootChain(vararg screens: Screen, showOnlyTopScreenView: Boolean = true) { + fun newRootChain(vararg screens: Screen) { val commands = screens.mapIndexed { index, screen -> if (index == 0) Replace(screen) else - Forward(screen, showOnlyTopScreenView) + Forward(screen) } executeCommands(BackTo(null), *commands.toTypedArray()) } diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppNavigator.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppNavigator.kt index 52b26d5..57f8ab8 100644 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppNavigator.kt +++ b/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppNavigator.kt @@ -6,8 +6,6 @@ import android.os.Handler import android.os.Looper import androidx.fragment.app.* import com.github.terrakok.cicerone.* -import com.github.terrakok.cicerone.androidx.TransactionInfo.Type.ADD -import com.github.terrakok.cicerone.androidx.TransactionInfo.Type.REPLACE /** * Navigator implementation for launch fragments and activities. @@ -23,7 +21,7 @@ open class AppNavigator @JvmOverloads constructor( protected val fragmentFactory: FragmentFactory = fragmentManager.fragmentFactory ) : Navigator { - protected val localStackCopy = mutableListOf() + protected val localStackCopy = mutableListOf() private val mainHandler = Handler(Looper.getMainLooper()) override fun applyCommands(commands: Array) { @@ -50,8 +48,7 @@ open class AppNavigator @JvmOverloads constructor( private fun copyStackToLocal() { localStackCopy.clear() for (i in 0 until fragmentManager.backStackEntryCount) { - val str = fragmentManager.getBackStackEntryAt(i).name - localStackCopy.add(TransactionInfo.fromString(str)) + localStackCopy.add(fragmentManager.getBackStackEntryAt(i).name) } } @@ -75,8 +72,7 @@ open class AppNavigator @JvmOverloads constructor( checkAndStartActivity(screen) } is FragmentScreen -> { - val type = if (command.clearContainer) REPLACE else ADD - commitNewFragmentScreen(screen, type, true) + commitNewFragmentScreen(screen, true) } } } @@ -90,10 +86,10 @@ open class AppNavigator @JvmOverloads constructor( is FragmentScreen -> { if (localStackCopy.isNotEmpty()) { fragmentManager.popBackStack() - val removed = localStackCopy.removeAt(localStackCopy.lastIndex) - commitNewFragmentScreen(screen, removed.type, true) + localStackCopy.removeAt(localStackCopy.lastIndex) + commitNewFragmentScreen(screen, true) } else { - commitNewFragmentScreen(screen, REPLACE, false) + commitNewFragmentScreen(screen, false) } } } @@ -114,7 +110,6 @@ open class AppNavigator @JvmOverloads constructor( protected open fun commitNewFragmentScreen( screen: FragmentScreen, - type: TransactionInfo.Type, addToBackStack: Boolean ) { val fragment = screen.createFragment(fragmentFactory) @@ -125,14 +120,14 @@ open class AppNavigator @JvmOverloads constructor( fragmentManager.findFragmentById(containerId), fragment ) - when (type) { - ADD -> transaction.add(containerId, fragment, screen.screenKey) - REPLACE -> transaction.replace(containerId, fragment, screen.screenKey) + if (screen.clearContainer) { + transaction.replace(containerId, fragment, screen.screenKey) + } else { + transaction.add(containerId, fragment, screen.screenKey) } if (addToBackStack) { - val transactionInfo = TransactionInfo(screen.screenKey, type) - transaction.addToBackStack(transactionInfo.toString()) - localStackCopy.add(transactionInfo) + transaction.addToBackStack(screen.screenKey) + localStackCopy.add(screen.screenKey) } transaction.commit() } @@ -145,7 +140,7 @@ open class AppNavigator @JvmOverloads constructor( backToRoot() } else { val screenKey = command.screen.screenKey - val index = localStackCopy.indexOfFirst { it.screenKey == screenKey } + val index = localStackCopy.indexOfFirst { it == screenKey } if (index != -1) { val forRemove = localStackCopy.subList(index, localStackCopy.size) fragmentManager.popBackStack(forRemove.first().toString(), 0) diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppScreen.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppScreen.kt index 6241db8..84d34df 100644 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppScreen.kt +++ b/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/AppScreen.kt @@ -15,6 +15,7 @@ fun interface Creator { open class FragmentScreen @JvmOverloads constructor( private val key: String? = null, + val clearContainer: Boolean = true, private val fragmentCreator: Creator ) : AppScreen() { override val screenKey: String get() = key ?: super.screenKey diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/TransactionInfo.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/TransactionInfo.kt deleted file mode 100644 index b0020ef..0000000 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/androidx/TransactionInfo.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.github.terrakok.cicerone.androidx - -data class TransactionInfo( - val screenKey: String, - val type: Type -) { - enum class Type(val symbol: Char) { - ADD('+'), - REPLACE('-'); - } - - override fun toString() = screenKey + type.symbol - - companion object { - @JvmStatic - fun fromString(str: String) = TransactionInfo( - str.dropLast(1), - if (str.last() == Type.ADD.symbol) Type.ADD else Type.REPLACE - ) - } -} \ No newline at end of file diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/commands.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/commands.kt index 67c275a..5eddb83 100644 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/commands.kt +++ b/library/src/main/kotlin/com/github/terrakok/cicerone/commands.kt @@ -10,10 +10,7 @@ interface Command /** * Opens new screen. */ -data class Forward( - val screen: Screen, - val clearContainer: Boolean -) : Command +data class Forward(val screen: Screen) : Command /** * Replaces the current screen.