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

Add new func for returns the given fragmentStackIndex #39

Open
wants to merge 3 commits into
base: develop
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/java/com/trendyol/medusa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class MainActivity : AppCompatActivity(), Navigator.NavigatorListener {
val restartRootFragmentCheckBox = findViewById<View>(R.id.restartSwitch) as SwitchCompat
findViewById<Button>(R.id.resetCurrentTab).setOnClickListener { multipleStackNavigator.resetCurrentTab(restartRootFragmentCheckBox.isChecked) }
findViewById<Button>(R.id.resetXTab).setOnClickListener { multipleStackNavigator.reset(1, restartRootFragmentCheckBox.isChecked) }
findViewById<Button>(R.id.resetXTabXFragment).setOnClickListener { multipleStackNavigator.reset(1, 2) }

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
findViewById<Button>(R.id.reset).setOnClickListener { multipleStackNavigator.reset() }
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@

</LinearLayout>

<Button
android:id="@+id/resetXTabXFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset X Tab X Fragment" />

</LinearLayout>
</RelativeLayout>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ open class MultipleStackNavigator(
initializeStackState()
}

override fun reset(tabIndex: Int, targetStackIndex: Int) {
val fragmentTagStack = fragmentStackState.fragmentTagStack[tabIndex]
val stackSize = fragmentTagStack.size - 1
if (stackSize > targetStackIndex) {
(stackSize downTo targetStackIndex + 1).forEach { position ->
fragmentManagerController.findFragmentByTagAndRemove(fragmentTagStack[position].fragmentTag)
fragmentTagStack.pop()
}
fragmentManagerController.commitAllowingStateLoss()
fragmentManagerController.enableFragment(getCurrentFragmentTag())
} else {
reset(tabIndex)
}
Copy link
Member

@MertNYuksel MertNYuksel Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should handle else case too. We can call reset(tabIndex: Int, resetRootFragment: Boolean = true) to reset the tab or throw an exception. Which one do you think would be appropriate for your usecase?

}

override fun clearGroup(fragmentGroupName: String) {
if (fragmentGroupName == DEFAULT_GROUP_NAME) {
throw IllegalArgumentException("Fragment group name can not be empty.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ interface Navigator {
*/
fun initialize(savedState: Bundle?)

/**
* Returns to the given fragmentStackIndex in the tabIndex
* @param tabIndex
* @param targetStackIndex return targetStackIndex
*/
fun reset(tabIndex: Int, targetStackIndex: Int)

/**
* Listeners
*/
Expand Down