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

- Fix tab order bug by removing switchTab call in notifyStackItemAdd. #32

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import com.trendyol.medusalib.common.extensions.insertToBottom
import com.trendyol.medusalib.common.extensions.moveToTop
import com.trendyol.medusalib.navigator.data.StackItem
import java.util.*
import kotlin.collections.ArrayList

data class FragmentStackState constructor(
val fragmentTagStack: MutableList<Stack<StackItem>> = ArrayList(),
val fragmentTagStack: MutableList<Stack<StackItem>> = mutableListOf(),
val tabIndexStack: Stack<Int> = Stack()
) {

Expand All @@ -17,18 +16,19 @@ data class FragmentStackState constructor(

fun isTabEmpty(index: Int) = fragmentTagStack[index].isEmpty()

fun addStackItem(tabIndex: Int, stackItem: StackItem) {
val stack = fragmentTagStack.getOrElse(tabIndex) {
Stack<StackItem>().apply {
fragmentTagStack.add(tabIndex, this)
}
}
fun notifyStackItemAdd(tabIndex: Int, stackItem: StackItem) {
val stack = fragmentTagStack.get(tabIndex)
stack.push(stackItem)
switchTab(tabIndex)
}

fun addStackItemToSelectedTab(stackItem: StackItem) {
addStackItem(getSelectedTabIndex(), stackItem)
fun notifyStackItemAddToCurrentTab(stackItem: StackItem) {
notifyStackItemAdd(getSelectedTabIndex(), stackItem)
}

fun setStackCount(size: Int) {
for (index in 0 until size) {
fragmentTagStack.add(Stack())
}
}

fun clear() {
Expand All @@ -50,11 +50,12 @@ data class FragmentStackState constructor(

fun hasSelectedTabOnlyRoot() = fragmentTagStack[getSelectedTabIndex()].size <= 1

fun peekItemFromSelectedTab() = fragmentTagStack[getSelectedTabIndex()].peek()
fun peekItemFromSelectedTab() =
fragmentTagStack[getSelectedTabIndex()].takeIf { it.isNotEmpty() }?.peek()

fun popItem(tabIndex: Int): StackItem {
val item = fragmentTagStack.get(tabIndex).pop()
if(fragmentTagStack.get(tabIndex).isEmpty()) {
if (isTabEmpty(tabIndex) && tabIndex == getSelectedTabIndex() && tabIndexStack.size > 1 ) {
popSelectedTab()
}
return item
Expand Down Expand Up @@ -102,10 +103,6 @@ data class FragmentStackState constructor(
tabIndexStack.addAll(stackState.tabIndexStack)
}

fun peekRootItems(): List<StackItem> {
return fragmentTagStack.map { it.get(0) }
}

fun peekItem(tabIndex: Int) = fragmentTagStack.get(tabIndex).peek()
fun popItemFromSelectedTab() = popItem(getSelectedTabIndex())
fun popSelectedTab(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ open class MultipleStackNavigator(
} else {
fragmentManagerController.disableAndStartFragment(getCurrentFragmentTag(), fragmentData)
}
fragmentStackState.addStackItemToSelectedTab(
fragmentStackState.notifyStackItemAddToCurrentTab(
StackItem(
fragmentTag = createdTag,
groupName = fragmentGroupName
Expand Down Expand Up @@ -128,12 +128,6 @@ open class MultipleStackNavigator(
}

clearAllFragments(tabIndex, resetRootFragment)

if (resetRootFragment) {
val rootFragment = getRootFragment(tabIndex)
val createdTag = tagCreator.create(rootFragment)
fragmentStackState.addStackItem(tabIndex, StackItem(createdTag))
}
}

override fun resetCurrentTab(resetRootFragment: Boolean) {
Expand All @@ -144,10 +138,11 @@ open class MultipleStackNavigator(
val rootFragment = getRootFragment(currentTabIndex)
val createdTag = tagCreator.create(rootFragment)
val rootFragmentData = FragmentData(rootFragment, createdTag)
fragmentStackState.addStackItem(currentTabIndex, StackItem(fragmentTag = createdTag))
fragmentStackState.switchTab(currentTabIndex)
fragmentStackState.notifyStackItemAdd(currentTabIndex, StackItem(fragmentTag = createdTag))
fragmentManagerController.addFragment(rootFragmentData)
} else {
val upperFragmentTag = fragmentStackState.peekItemFromSelectedTab().fragmentTag
val upperFragmentTag = getCurrentFragmentTag()
fragmentManagerController.enableFragment(upperFragmentTag)
}
}
Expand Down Expand Up @@ -182,7 +177,7 @@ open class MultipleStackNavigator(
}

override fun getCurrentFragment(): Fragment? {
val visibleFragmentTag = fragmentStackState.peekItemFromSelectedTab().fragmentTag
val visibleFragmentTag = getCurrentFragmentTag()
return fragmentManagerController.getFragment(visibleFragmentTag)
}

Expand All @@ -196,28 +191,21 @@ open class MultipleStackNavigator(
}

private fun initializeStackState() {
val providedRootFragments = rootFragmentProvider.map { it.invoke() }
addRootFragmentsToStackState(providedRootFragments)

val initialTabIndex = navigatorConfiguration.initialTabIndex
fragmentStackState.switchTab(initialTabIndex)
val rootFragment = rootFragmentProvider.get(initialTabIndex).invoke()
val createdTag = tagCreator.create(rootFragment)
val stackItem = StackItem(fragmentTag = createdTag)

fragmentStackState.setStackCount(rootFragmentProvider.size)
fragmentStackState.notifyStackItemAdd(tabIndex = initialTabIndex, stackItem = stackItem)
fragmentStackState.switchTab(initialTabIndex)

val rootFragment = getRootFragment(initialTabIndex)
val rootFragmentTag = fragmentStackState.peekItem(initialTabIndex).fragmentTag
val rootFragmentData = FragmentData(rootFragment, rootFragmentTag)
fragmentManagerController.addFragment(rootFragmentData)
navigatorListener?.onTabChanged(navigatorConfiguration.initialTabIndex)
}

private fun addRootFragmentsToStackState(rootFragments: List<Fragment>) {
rootFragments.forEachIndexed { index: Int, fragment: Fragment ->
val createdTag = tagCreator.create(fragment)
val stackItem = StackItem(fragmentTag = createdTag)
fragmentStackState.addStackItem(index, stackItem)
}
}

private fun loadStackStateFromSavedState(savedState: Bundle) {
val stackState = fragmentStackStateMapper.fromBundle(savedState.getBundle(MEDUSA_STACK_STATE_KEY))
fragmentStackState.setStackState(stackState)
Expand All @@ -232,17 +220,19 @@ open class MultipleStackNavigator(
}

private fun showUpperFragment() {
val upperFragmentTag = fragmentStackState.peekItemFromSelectedTab().fragmentTag
if (fragmentManagerController.isFragmentNull(upperFragmentTag)) {
val upperFragmentTag = fragmentStackState.peekItemFromSelectedTab()?.fragmentTag
if (upperFragmentTag == null || fragmentManagerController.isFragmentNull(upperFragmentTag)) {
val rootFragment = getRootFragment(fragmentStackState.getSelectedTabIndex())
val rootFragmentData = FragmentData(rootFragment, upperFragmentTag)
val createdTag = tagCreator.create(rootFragment)
val rootFragmentData = FragmentData(rootFragment, createdTag)
fragmentStackState.notifyStackItemAdd(fragmentStackState.getSelectedTabIndex(), StackItem(createdTag))
fragmentManagerController.addFragment(rootFragmentData)
} else {
fragmentManagerController.enableFragment(upperFragmentTag)
}
}

private fun getCurrentFragmentTag() = fragmentStackState.peekItemFromSelectedTab().fragmentTag
private fun getCurrentFragmentTag() = requireNotNull(fragmentStackState.peekItemFromSelectedTab()).fragmentTag

private fun shouldExit(): Boolean {
return fragmentStackState.hasTabStack() && fragmentStackState.hasSelectedTabOnlyRoot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,4 @@ class FragmentStackStateTest {

Truth.assertThat(stackState.fragmentTagStack).containsExactlyElementsIn(emptyStack)
}

@Test
fun `given stackstate with tab index stack of (2, 1, 0), when addStackItem is called with tabIndex 1, then new tab index must be (2, 0, 1)`() {
val stackState = buildFragmentStackState(tabStack = buildTabStack(2, 1, 0))

stackState.addStackItem(1, buildStackItem())

Truth.assertThat(stackState.tabIndexStack).containsExactly(2, 0, 1).inOrder()
}

@Test
fun `given stackstate with tab index stack of (2, 1, 0), when addStackItem is called with tabIndex 0, then new tab index must be (2, 1, 0)`() {
val stackState = buildFragmentStackState(tabStack = buildTabStack(2, 1, 0))

stackState.addStackItem(0, buildStackItem())

Truth.assertThat(stackState.tabIndexStack).containsExactly(2, 1, 0).inOrder()
}
}