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

cleared tab stack from different tab #173

Merged
merged 8 commits into from
Dec 26, 2018
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
9 changes: 9 additions & 0 deletions frag-nav/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ dependencies {
testImplementation "org.spekframework.spek2:spek-dsl-jvm:$spek_version"
testImplementation "org.spekframework.spek2:spek-runner-junit5:$spek_version"
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation('org.jetbrains.spek:spek-api:1.1.5') {
exclude group: 'org.jetbrains.kotlin'
}
testImplementation('org.jetbrains.spek:spek-junit-platform-engine:1.1.5') {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
}


}

group = publishedGroupId // Maven Group ID for the artifact
Expand Down
39 changes: 28 additions & 11 deletions frag-nav/src/main/java/com/ncapdevi/fragnav/FragNavController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,29 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
*/
@JvmOverloads
fun clearStack(transactionOptions: FragNavTransactionOptions? = defaultTransactionOptions) {
if (currentStackIndex == NO_TAB) {
clearStack(currentStackIndex,transactionOptions)
}

/**
* Clears the passed tab's stack to get to just the bottom Fragment. This will reveal the root fragment
*
* @param tabIndex Index of tab that needs to be cleared
* @param transactionOptions Transaction options to be displayed
*/
@JvmOverloads
fun clearStack(tabIndex: Int, transactionOptions: FragNavTransactionOptions? = defaultTransactionOptions) {
if (tabIndex == NO_TAB) {
return
}

//Grab Current stack
val fragmentStack = fragmentStacksTags[currentStackIndex]
val fragmentStack = fragmentStacksTags[tabIndex]

// Only need to start popping and reattach if the stack is greater than 1
if (fragmentStack.size > 1) {
val ft = createTransactionWithOptions(transactionOptions, true)
//Only animate if we're clearing the current stack
val shouldAnimate = tabIndex == currentStackIndex
val ft = createTransactionWithOptions(transactionOptions,true, shouldAnimate)

//Pop all of the fragments on the stack and remove them from the FragmentManager
while (fragmentStack.size > 1) {
Expand Down Expand Up @@ -657,10 +670,14 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
setTransition(options.transition)

options.sharedElements.forEach { sharedElement ->
addSharedElement(
sharedElement.first,
sharedElement.second
)
sharedElement.first?.let {
sharedElement.second?.let { it1 ->
addSharedElement(
it,
it1
)
}
}
}

when {
Expand Down Expand Up @@ -729,7 +746,7 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,

fun getFragmentManagerForDialog(): FragmentManager {
val currentFrag = this.currentFrag
return if(currentFrag?.isAdded == true) {
return if (currentFrag?.isAdded == true) {
currentFrag.childFragmentManager
} else {
this.fragmentManger
Expand Down Expand Up @@ -809,9 +826,9 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
val stackArray = stackArrays.getJSONArray(x)
val stack = Stack<String>()
(0 until stackArray.length())
.map { stackArray.getString(it) }
.filter { !it.isNullOrEmpty() && !"null".equals(it, ignoreCase = true) }
.mapNotNullTo(stack) { it }
.map { stackArray.getString(it) }
.filter { !it.isNullOrEmpty() && !"null".equals(it, ignoreCase = true) }
.mapNotNullTo(stack) { it }

fragmentStacksTags.add(stack)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,35 @@ class FragNavControllerTest : FragNavController.TransactionListener {
Assert.assertTrue(mFragNavController.isRootFragment)
}

@Test
fun testTabStackClear() {
val rootFragments = ArrayList<Fragment>()
rootFragments.add(Fragment())
rootFragments.add(Fragment())

val mFragNavController = FragNavController(fragmentManager, frameLayout.id).apply {
this.rootFragments = rootFragments
}
mFragNavController.initialize()

Assert.assertEquals(FragNavController.TAB1.toLong(), mFragNavController.currentStackIndex.toLong())
Assert.assertNotNull(mFragNavController.currentStack)

var size = mFragNavController.currentStack?.size ?: 1

mFragNavController.pushFragment(Fragment())
Assert.assertTrue(mFragNavController.currentStack?.size == ++size)

mFragNavController.pushFragment(Fragment())
Assert.assertTrue(mFragNavController.currentStack?.size == ++size)

mFragNavController.switchTab(FragNavController.TAB2)

mFragNavController.clearStack(FragNavController.TAB1)

mFragNavController.switchTab(FragNavController.TAB1)
Assert.assertTrue(mFragNavController.currentStack?.size == 1)
}

override fun onTabTransaction(fragment: Fragment?, index: Int) {
Assert.assertNotNull(fragment)
Expand Down