Skip to content

Commit

Permalink
Move fragment transaction type to FragmentScreen property.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Apr 9, 2021
1 parent 7a862cb commit daef586
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 55 deletions.
18 changes: 6 additions & 12 deletions library/src/main/kotlin/com/github/terrakok/cicerone/Router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

/**
Expand Down Expand Up @@ -57,27 +55,23 @@ 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())
}

/**
* 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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,7 +21,7 @@ open class AppNavigator @JvmOverloads constructor(
protected val fragmentFactory: FragmentFactory = fragmentManager.fragmentFactory
) : Navigator {

protected val localStackCopy = mutableListOf<TransactionInfo>()
protected val localStackCopy = mutableListOf<String>()
private val mainHandler = Handler(Looper.getMainLooper())

override fun applyCommands(commands: Array<out Command>) {
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
Expand All @@ -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()
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fun interface Creator<A, R> {

open class FragmentScreen @JvmOverloads constructor(
private val key: String? = null,
val clearContainer: Boolean = true,
private val fragmentCreator: Creator<FragmentFactory, Fragment>
) : AppScreen() {
override val screenKey: String get() = key ?: super.screenKey
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit daef586

Please sign in to comment.