moved most of the bottom_navigation_viewcontroller logic to bottom_navigation_fragment
This commit is contained in:
parent
263ac4d659
commit
b5f0ab736c
|
|
@ -0,0 +1,2 @@
|
|||
<manifest
|
||||
package="ru.touchin.roboswag.bottom_navigation_fragment"/>
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation_fragment
|
||||
package ru.touchin.roboswag.bottom_navigation_fragment
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.IdRes
|
||||
|
|
@ -6,13 +6,9 @@ import androidx.fragment.app.FragmentManager
|
|||
import ru.touchin.roboswag.navigation_base.FragmentNavigation
|
||||
import ru.touchin.roboswag.navigation_base.activities.NavigationActivity
|
||||
|
||||
/**
|
||||
* Created by Daniil Borisovskii on 15/08/2019.
|
||||
* Activity to manage tab container navigation.
|
||||
*/
|
||||
abstract class BottomNavigationActivity : NavigationActivity() {
|
||||
|
||||
val innerNavigation: FragmentNavigation
|
||||
open val innerNavigation: FragmentNavigation
|
||||
get() = getNavigationContainer(supportFragmentManager)?.navigation ?: navigation
|
||||
|
||||
/**
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation_fragment
|
||||
package ru.touchin.roboswag.bottom_navigation_fragment
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation_fragment
|
||||
package ru.touchin.roboswag.bottom_navigation_fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation_fragment
|
||||
package ru.touchin.roboswag.bottom_navigation_fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
|
|
@ -16,6 +16,7 @@ android {
|
|||
|
||||
dependencies {
|
||||
api project(":navigation-viewcontroller")
|
||||
api project(":bottom-navigation-fragment")
|
||||
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<manifest
|
||||
package="ru.touchin.roboswag.bottom_navigation_viewcontroller"/>
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import ru.touchin.roboswag.navigation.activities.NavigationActivity
|
||||
import ru.touchin.roboswag.navigation.viewcontrollers.ViewControllerNavigation
|
||||
import ru.touchin.roboswag.bottom_navigation_fragment.BottomNavigationActivity as FragmentBottomNavigationActivity
|
||||
|
||||
abstract class BottomNavigationActivity : FragmentBottomNavigationActivity() {
|
||||
|
||||
final override val innerNavigation: ViewControllerNavigation<BottomNavigationActivity>
|
||||
get() = getNavigationContainer(supportFragmentManager)?.navigation
|
||||
?: navigation as ViewControllerNavigation<BottomNavigationActivity>
|
||||
|
||||
}
|
||||
|
|
@ -95,7 +95,7 @@ open class FragmentNavigation(
|
|||
addToStack: Boolean,
|
||||
args: Bundle?,
|
||||
backStackName: String?,
|
||||
tag: String?,
|
||||
tag: String? = null,
|
||||
transactionSetup: ((FragmentTransaction) -> Unit)?
|
||||
) {
|
||||
if (fragmentManager.isDestroyed) {
|
||||
|
|
|
|||
|
|
@ -1,2 +0,0 @@
|
|||
<manifest
|
||||
package="ru.touchin.roboswag.components.tabbarnavigation_fragment"/>
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
<manifest
|
||||
package="ru.touchin.roboswag.components.tabbarnavigation_viewcontroller"/>
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import ru.touchin.roboswag.components.navigation.activities.NavigationActivity
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewControllerNavigation
|
||||
|
||||
/**
|
||||
* Created by Daniil Borisovskii on 15/08/2019.
|
||||
* Activity to manage tab container navigation.
|
||||
*/
|
||||
abstract class BottomNavigationActivity : NavigationActivity() {
|
||||
|
||||
val innerNavigation: ViewControllerNavigation<BottomNavigationActivity>
|
||||
get() = getNavigationContainer(supportFragmentManager)?.navigation ?: navigation as ViewControllerNavigation<BottomNavigationActivity>
|
||||
|
||||
/**
|
||||
* Navigates to the given navigation tab.
|
||||
* Can be called from any node of navigation graph so all back stack will be cleared.
|
||||
*
|
||||
* @param navigationTabId Id of navigation tab.
|
||||
* @param state State of the given tab. If not null tab's fragment will be recreated, otherwise only in case it has not been created before.
|
||||
*/
|
||||
fun navigateTo(@IdRes navigationTabId: Int, state: Parcelable? = null) {
|
||||
supportFragmentManager.run {
|
||||
// Clear all navigation stack unto the main bottom navigation (tagged as top)
|
||||
popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
|
||||
|
||||
(primaryNavigationFragment as? BottomNavigationFragment)?.navigateTo(navigationTabId, state)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNavigationContainer(fragmentManager: FragmentManager?): NavigationContainerFragment? =
|
||||
fragmentManager
|
||||
?.primaryNavigationFragment
|
||||
?.let { navigationFragment ->
|
||||
navigationFragment as? NavigationContainerFragment
|
||||
?: getNavigationContainer(navigationFragment.childFragmentManager)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,127 +0,0 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import android.util.SparseArray
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.annotation.LayoutRes
|
||||
import androidx.core.util.forEach
|
||||
import androidx.core.view.children
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import ru.touchin.roboswag.components.navigation.fragments.ViewControllerFragment
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewController
|
||||
import ru.touchin.roboswag.core.utils.ShouldNotHappenException
|
||||
|
||||
class BottomNavigationController(
|
||||
private val context: Context,
|
||||
private val fragmentManager: FragmentManager,
|
||||
private val viewControllers: SparseArray<BottomNavigationFragment.TabData>,
|
||||
@IdRes private val contentContainerViewId: Int,
|
||||
@LayoutRes private val contentContainerLayoutId: Int,
|
||||
private val wrapWithNavigationContainer: Boolean = false,
|
||||
@IdRes private val topLevelViewControllerId: Int = 0, // If it zero back press with empty fragment back stack would close the app
|
||||
private val onReselectListener: (() -> Unit)? = null
|
||||
) {
|
||||
|
||||
private var callback: FragmentManager.FragmentLifecycleCallbacks? = null
|
||||
|
||||
private var currentViewControllerId = -1
|
||||
|
||||
fun attach(navigationTabsContainer: ViewGroup) {
|
||||
detach()
|
||||
|
||||
//This is provides to set pressed tab status to isActivated providing an opportunity to specify custom style
|
||||
callback = object : FragmentManager.FragmentLifecycleCallbacks() {
|
||||
override fun onFragmentViewCreated(fragmentManager: FragmentManager, fragment: Fragment, view: View, savedInstanceState: Bundle?) {
|
||||
viewControllers.forEach { itemId, (viewControllerClass, _) ->
|
||||
if (isViewControllerFragment(fragment, viewControllerClass)) {
|
||||
navigationTabsContainer.children.forEach { itemView -> itemView.isActivated = itemView.id == itemId }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fragmentManager.registerFragmentLifecycleCallbacks(callback!!, false)
|
||||
|
||||
navigationTabsContainer.children.forEach { itemView ->
|
||||
viewControllers[itemView.id]?.let { (viewControllerClass, _) ->
|
||||
itemView.setOnClickListener {
|
||||
if (!isViewControllerFragment(fragmentManager.primaryNavigationFragment, viewControllerClass)) {
|
||||
navigateTo(itemView.id)
|
||||
} else {
|
||||
onReselectListener?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun detach() = callback?.let(fragmentManager::unregisterFragmentLifecycleCallbacks)
|
||||
|
||||
@Suppress("detekt.ComplexMethod")
|
||||
fun navigateTo(@IdRes itemId: Int, state: Parcelable? = null) {
|
||||
// Find view controller class that needs to open
|
||||
val (viewControllerClass, defaultViewControllerState, saveStateOnSwitching) = viewControllers[itemId] ?: return
|
||||
if (state != null && state::class != defaultViewControllerState::class) {
|
||||
throw ShouldNotHappenException(
|
||||
"Incorrect state type for navigation tab root ViewController. Should be ${defaultViewControllerState::class}"
|
||||
)
|
||||
}
|
||||
val viewControllerState = state ?: defaultViewControllerState
|
||||
val transaction = fragmentManager.beginTransaction()
|
||||
// Detach current primary fragment
|
||||
fragmentManager.primaryNavigationFragment?.let(transaction::detach)
|
||||
val viewControllerName = viewControllerClass.canonicalName
|
||||
var fragment = fragmentManager.findFragmentByTag(viewControllerName)
|
||||
|
||||
if (saveStateOnSwitching && state == null && fragment != null) {
|
||||
transaction.attach(fragment)
|
||||
} else {
|
||||
// If fragment already exist remove it first
|
||||
if (fragment != null) transaction.remove(fragment)
|
||||
|
||||
fragment = if (wrapWithNavigationContainer) {
|
||||
Fragment.instantiate(
|
||||
context,
|
||||
NavigationContainerFragment::class.java.name,
|
||||
NavigationContainerFragment.args(viewControllerClass, viewControllerState, contentContainerViewId, contentContainerLayoutId)
|
||||
)
|
||||
} else {
|
||||
Fragment.instantiate(
|
||||
context,
|
||||
ViewControllerFragment::class.java.name,
|
||||
ViewControllerFragment.args(viewControllerClass, viewControllerState)
|
||||
)
|
||||
}
|
||||
transaction.add(contentContainerViewId, fragment, viewControllerName)
|
||||
}
|
||||
|
||||
transaction
|
||||
.setPrimaryNavigationFragment(fragment)
|
||||
.setReorderingAllowed(true)
|
||||
.commit()
|
||||
|
||||
currentViewControllerId = itemId
|
||||
}
|
||||
|
||||
// When you are in any tab instead of main you firstly navigate to main tab before exit application
|
||||
fun onBackPressed() =
|
||||
if (fragmentManager.primaryNavigationFragment?.childFragmentManager?.backStackEntryCount == 0
|
||||
&& topLevelViewControllerId != 0
|
||||
&& currentViewControllerId != topLevelViewControllerId) {
|
||||
navigateTo(topLevelViewControllerId)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
||||
private fun isViewControllerFragment(fragment: Fragment?, viewControllerClass: Class<out ViewController<*, *>>) =
|
||||
if (wrapWithNavigationContainer) {
|
||||
(fragment as NavigationContainerFragment).getViewControllerClass()
|
||||
} else {
|
||||
(fragment as ViewControllerFragment<*, *>).viewControllerClass
|
||||
} === viewControllerClass
|
||||
}
|
||||
|
|
@ -1,118 +0,0 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import android.util.SparseArray
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.fragment.app.Fragment
|
||||
import ru.touchin.roboswag.components.navigation.activities.OnBackPressedListener
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.EmptyState
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewController
|
||||
|
||||
abstract class BottomNavigationFragment : Fragment() {
|
||||
|
||||
private lateinit var bottomNavigationController: BottomNavigationController
|
||||
|
||||
private val backPressedListener = OnBackPressedListener { bottomNavigationController.onBackPressed() }
|
||||
|
||||
protected abstract val rootLayoutId: Int
|
||||
|
||||
protected abstract val navigationContainerViewId: Int
|
||||
|
||||
protected abstract val contentContainerViewId: Int
|
||||
|
||||
protected abstract val contentContainerLayoutId: Int
|
||||
|
||||
protected abstract val topLevelViewControllerId: Int
|
||||
|
||||
protected abstract val wrapWithNavigationContainer: Boolean
|
||||
|
||||
protected abstract val navigationViewControllers: SparseArray<TabData>
|
||||
|
||||
protected open val reselectListener: (() -> Unit) = { getNavigationActivity().innerNavigation.up(inclusive = true) }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
bottomNavigationController = BottomNavigationController(
|
||||
context = requireContext(),
|
||||
fragmentManager = childFragmentManager,
|
||||
viewControllers = navigationViewControllers,
|
||||
contentContainerViewId = contentContainerViewId,
|
||||
contentContainerLayoutId = contentContainerLayoutId,
|
||||
topLevelViewControllerId = topLevelViewControllerId,
|
||||
wrapWithNavigationContainer = wrapWithNavigationContainer,
|
||||
onReselectListener = reselectListener
|
||||
)
|
||||
if (savedInstanceState == null) {
|
||||
bottomNavigationController.navigateTo(topLevelViewControllerId)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
val fragmentView = inflater.inflate(rootLayoutId, container, false)
|
||||
|
||||
bottomNavigationController.attach(fragmentView.findViewById(navigationContainerViewId))
|
||||
|
||||
(activity as BottomNavigationActivity).addOnBackPressedListener(backPressedListener)
|
||||
|
||||
return fragmentView
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
(activity as BottomNavigationActivity).removeOnBackPressedListener(backPressedListener)
|
||||
bottomNavigationController.detach()
|
||||
}
|
||||
|
||||
fun navigateTo(@IdRes navigationTabId: Int, state: Parcelable? = null) {
|
||||
bottomNavigationController.navigateTo(navigationTabId, state)
|
||||
}
|
||||
|
||||
private fun getNavigationActivity() = requireActivity() as BottomNavigationActivity
|
||||
|
||||
class TabData(
|
||||
val viewControllerClass: Class<out ViewController<*, *>>,
|
||||
viewControllerState: Parcelable,
|
||||
/**
|
||||
* It can be useful in some cases when it is necessary to create ViewController
|
||||
* with initial state every time when tab opens.
|
||||
*/
|
||||
val saveStateOnSwitching: Boolean = true
|
||||
) {
|
||||
|
||||
/**
|
||||
* It is value as class body property instead of value as constructor parameter to specify
|
||||
* custom getter of this field which returns copy of Parcelable every time it be called.
|
||||
* This is necessary to avoid modifying this value if it would be a value as constructor parameter
|
||||
* and every getting of this value would return the same instance.
|
||||
*/
|
||||
val viewControllerState = viewControllerState
|
||||
get() = field.copy()
|
||||
|
||||
operator fun component1() = viewControllerClass
|
||||
|
||||
operator fun component2() = viewControllerState
|
||||
|
||||
operator fun component3() = saveStateOnSwitching
|
||||
|
||||
private fun Parcelable.copy(): Parcelable =
|
||||
if (this is EmptyState) {
|
||||
EmptyState
|
||||
} else {
|
||||
val parcel = Parcel.obtain()
|
||||
parcel.writeParcelable(this, 0)
|
||||
parcel.setDataPosition(0)
|
||||
val result = parcel.readParcelable<Parcelable>(
|
||||
javaClass.classLoader ?: Thread.currentThread().contextClassLoader
|
||||
) ?: throw IllegalStateException("Failed to copy tab state")
|
||||
parcel.recycle()
|
||||
result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
package ru.touchin.roboswag.components.tabbarnavigation
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.IdRes
|
||||
import androidx.annotation.LayoutRes
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewController
|
||||
import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewControllerNavigation
|
||||
import ru.touchin.roboswag.core.utils.ShouldNotHappenException
|
||||
|
||||
class NavigationContainerFragment : Fragment() {
|
||||
|
||||
companion object {
|
||||
private const val VIEW_CONTROLLER_CLASS_ARG = "VIEW_CONTROLLER_CLASS_ARG"
|
||||
private const val VIEW_CONTROLLER_STATE_ARG = "VIEW_CONTROLLER_STATE_ARG"
|
||||
private const val CONTAINER_VIEW_ID_ARG = "CONTAINER_VIEW_ID_ARG"
|
||||
private const val CONTAINER_LAYOUT_ID_ARG = "CONTAINER_LAYOUT_ID_ARG"
|
||||
private const val TRANSITION_ARG = "TRANSITION_ARG"
|
||||
|
||||
fun args(
|
||||
cls: Class<out ViewController<*, *>>,
|
||||
state: Parcelable,
|
||||
@IdRes containerViewId: Int,
|
||||
@LayoutRes containerLayoutId: Int,
|
||||
transition: Int = FragmentTransaction.TRANSIT_NONE
|
||||
) = Bundle().apply {
|
||||
putSerializable(VIEW_CONTROLLER_CLASS_ARG, cls)
|
||||
putParcelable(VIEW_CONTROLLER_STATE_ARG, state)
|
||||
putInt(CONTAINER_VIEW_ID_ARG, containerViewId)
|
||||
putInt(CONTAINER_LAYOUT_ID_ARG, containerLayoutId)
|
||||
putInt(TRANSITION_ARG, transition)
|
||||
}
|
||||
}
|
||||
|
||||
val navigation by lazy {
|
||||
ViewControllerNavigation<BottomNavigationActivity>(
|
||||
requireContext(),
|
||||
childFragmentManager,
|
||||
containerViewId,
|
||||
transition
|
||||
)
|
||||
}
|
||||
|
||||
@IdRes
|
||||
private var containerViewId = 0
|
||||
|
||||
@LayoutRes
|
||||
private var containerLayoutId = 0
|
||||
|
||||
private var transition = 0
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun getViewControllerClass(): Class<out ViewController<out BottomNavigationActivity, Parcelable>> =
|
||||
arguments?.getSerializable(VIEW_CONTROLLER_CLASS_ARG) as Class<out ViewController<out BottomNavigationActivity, Parcelable>>
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
arguments?.let { args ->
|
||||
transition = args.getInt(TRANSITION_ARG)
|
||||
containerViewId = args.getInt(CONTAINER_VIEW_ID_ARG)
|
||||
containerLayoutId = args.getInt(CONTAINER_LAYOUT_ID_ARG)
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
navigation.setInitialViewController(getViewControllerClass(), args.getParcelable(VIEW_CONTROLLER_STATE_ARG)
|
||||
?: throw ShouldNotHappenException("Fragment state must not be null"))
|
||||
}
|
||||
} ?: throw ShouldNotHappenException("Fragment is not instantiable without arguments")
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
|
||||
inflater.inflate(containerLayoutId, container, false)
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue