Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: New fragments navigation #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.jetradar.navigation.fragments

import androidx.annotation.AnimRes

class AnimationHolder(
@AnimRes val enter: Int,
@AnimRes val exit: Int,
@AnimRes val popEnter: Int,
@AnimRes val popExit: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.jetradar.navigation.fragments
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.transaction
import com.jakewharton.rxrelay2.PublishRelay
import com.jetradar.navigation.Navigation
Expand All @@ -29,7 +30,8 @@ import io.reactivex.Observable

class FragmentsNavigation(
private val fragmentManager: FragmentManager,
@IdRes private val containerViewId: Int
@IdRes private val containerViewId: Int,
private val defaultAnimations: AnimationHolder? = null
) : Navigation {

private val navigationEvents = PublishRelay.create<NavigationEvent>()
Expand All @@ -38,40 +40,59 @@ class FragmentsNavigation(

override fun navigate(command: NavigationCommand): Boolean = with(command) {
when (this) {
is OpenScreenCommand -> openScreen(fragment, addToBackStack)
is OpenAsRootScreenCommand -> openAsRootScreenCommand(fragment)
else -> return false
is ForwardCommand -> forward(fragment, tag, animations)
is ReplaceCommand -> replace(fragment, tag, animations)
is BackToCommand -> backTo(tag)
else -> return false
}
return true
}

private fun openScreen(fragment: Fragment, addToBackStack: Boolean) {
private fun forward(fragment: Fragment, tag: String, animations: AnimationHolder?) {
fragmentManager.transaction(allowStateLoss = true) {
add(containerViewId, fragment)
if (addToBackStack) addToBackStack(null)
replace(containerViewId, fragment, tag)
addToBackStack(tag)
applyAnimation(animations)
}
navigationEvents.accept(OpenScreenEvent(fragment.javaClass.name))
navigationEvents.accept(OpenScreenEvent(tag))
}

private fun openAsRootScreenCommand(fragment: Fragment) {
clear()
fragmentManager.transaction(allowStateLoss = true) { replace(containerViewId, fragment) }
fragmentManager.executePendingTransactions()
private fun replace(fragment: Fragment, tag: String, animations: AnimationHolder?) {
fragmentManager.transaction(allowStateLoss = true) {
replace(containerViewId, fragment, tag)
applyAnimation(animations)
}
navigationEvents.accept(OpenScreenEvent(tag))
}

private fun FragmentTransaction.applyAnimation(animations: AnimationHolder?) {
val customAnimation = animations ?: defaultAnimations
customAnimation?.let { setCustomAnimations(it.enter, it.exit, it.popEnter, it.popExit) }
}

private fun backTo(tag: String) {
do {
if (currentScreen()?.tag == tag) {
fragmentManager.executePendingTransactions()
return
}
} while (back())
throw IllegalStateException("Screen with tag=$tag not found in stack")
}

override fun back(): Boolean {
val currentScreen = currentScreen() ?: return false
val popped = fragmentManager.popBackStackImmediate() // TODO: remove fragments by container id
if (popped) navigationEvents.accept(CloseScreenEvent(currentScreen.javaClass.name))
return popped
if (fragmentManager.backStackEntryCount == 0) return false
fragmentManager.popBackStack(currentScreen.tag ?: currentScreen.javaClass.name, 0)
navigationEvents.accept(CloseScreenEvent(currentScreen.tag ?: currentScreen.javaClass.name))
return true
}

override fun clear() {
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) // TODO: remove fragments by container id
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
}

private fun currentScreen(): Fragment? =
fragmentManager.findFragmentById(containerViewId)
private fun currentScreen(): Fragment? = fragmentManager.findFragmentById(containerViewId)

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ package com.jetradar.navigation.fragments
import androidx.fragment.app.Fragment
import com.jetradar.navigation.NavigationCommand

class OpenScreenCommand(
class ForwardCommand(
val fragment: Fragment,
val addToBackStack: Boolean
val tag: String = fragment.javaClass.name,
val animations: AnimationHolder? = null
) : NavigationCommand

class OpenAsRootScreenCommand(
val fragment: Fragment
class ReplaceCommand(
val fragment: Fragment,
val tag: String = fragment.javaClass.name,
val animations: AnimationHolder? = null
) : NavigationCommand

class BackToCommand(val tag: String) : NavigationCommand
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package com.jetradar.navigation.fragments
import com.jetradar.navigation.NavigationEvent

class OpenScreenEvent(
val fragment: String
val tag: String
) : NavigationEvent {
override val message = "→ $fragment"
override val message = "→ $tag"
}

class CloseScreenEvent(
val fragment: String
val tag: String
) : NavigationEvent {
override val message = "← $fragment"
override val message = "← $tag"
}