Commit 9aa1d19b authored by Eugene Shapovalov's avatar Eugene Shapovalov 💬

Refactored bottom bar fragment transactions.

parent 353a192d
......@@ -46,6 +46,19 @@ class MainFragment : BaseFragment(), MainView {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
AHBottomNavigationAdapter(activity, R.menu.main_bottom_menu).apply {
setupWithBottomNavigation(bottomBar)
}
with(bottomBar) {
accentColor = context.color(R.color.colorPrimary)
inactiveColor = context.color(R.color.silver)
setOnTabSelectedListener { position, wasSelected ->
if (!wasSelected) showTab(position, currentItem)
true
}
}
if (savedInstanceState == null) {
tabs = createNewFragments()
childFragmentManager.beginTransaction()
......@@ -53,25 +66,21 @@ class MainFragment : BaseFragment(), MainView {
.add(R.id.mainScreenContainer, tabs[tabKeys[1]], tabKeys[1])
.add(R.id.mainScreenContainer, tabs[tabKeys[2]], tabKeys[2])
.add(R.id.mainScreenContainer, tabs[tabKeys[3]], tabKeys[3])
.commit()
showTab(0)
.hide(tabs[tabKeys[1]])
.hide(tabs[tabKeys[2]])
.hide(tabs[tabKeys[3]])
.commitNow()
bottomBar.setCurrentItem(0, false)
} else {
tabs = findFragments()
}
}
AHBottomNavigationAdapter(activity, R.menu.main_bottom_menu).apply {
setupWithBottomNavigation(bottomBar)
}
with(bottomBar) {
accentColor = context.color(R.color.colorPrimary)
inactiveColor = context.color(R.color.silver)
setOnTabSelectedListener { position, wasSelected ->
if (!wasSelected) postDelayed({ showTab(position) }, 150)
true
}
}
private fun showTab(newItem: Int, oldItem: Int) {
childFragmentManager.beginTransaction()
.hide(tabs[tabKeys[oldItem]])
.show(tabs[tabKeys[newItem]])
.commit()
}
private fun createNewFragments(): HashMap<String, BaseFragment> = hashMapOf(
......@@ -88,15 +97,5 @@ class MainFragment : BaseFragment(), MainView {
tabKeys[3] to childFragmentManager.findFragmentByTag(tabKeys[3]) as BaseFragment
)
private fun showTab(position: Int) {
childFragmentManager.beginTransaction()
.detach(tabs[tabKeys[0]])
.detach(tabs[tabKeys[1]])
.detach(tabs[tabKeys[2]])
.detach(tabs[tabKeys[3]])
.attach(tabs[tabKeys[position]])
.commit()
}
override fun onBackPressed() = presenter.onBackPressed()
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment