Skip to content

Commit

Permalink
Merge pull request #50 from Trendyol/ObserveDestinationChangesClearGr…
Browse files Browse the repository at this point in the history
…oupBug

Observe destination changes clear group bug
  • Loading branch information
MertNYuksel authored Aug 16, 2023
2 parents df747da + e50a9d2 commit 5f2f1ab
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 6 deletions.
16 changes: 12 additions & 4 deletions app/src/main/java/com/trendyol/medusa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,27 @@ class MainActivity : AppCompatActivity(), Navigator.NavigatorListener {

multipleStackNavigator.initialize(savedInstanceState)
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.resetCurrentTab).setOnClickListener {
multipleStackNavigator.resetCurrentTab(restartRootFragmentCheckBox.isChecked)
}
findViewById<Button>(R.id.resetXTab).setOnClickListener {
multipleStackNavigator.reset(1, restartRootFragmentCheckBox.isChecked)
}

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
findViewById<Button>(R.id.reset).setOnClickListener { multipleStackNavigator.reset() }
findViewById<Button>(R.id.resetWithNewSet).setOnClickListener {
multipleStackNavigator.resetWithFragmentProvider(newListOfFragments)
}

findViewById<Button>(R.id.startWithGroup).setOnClickListener {
multipleStackNavigator.start(FragmentGenerator.generateNewFragment(), "group1")
}
findViewById<Button>(R.id.resetGroup).setOnClickListener {
multipleStackNavigator.clearGroup("group1")
}
multipleStackNavigator.observeDestinationChanges(this) {
Log.d("Destination Changed", "${it.javaClass.name} - ${it.tag}")
}

}

override fun onBackPressed() {
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@
android:singleLine="true"
android:text="Reset With New Fragments" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/startWithGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="Start With Group" />

<Button
android:id="@+id/resetGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="Reset Group" />
</LinearLayout>

</LinearLayout>
</RelativeLayout>

Expand Down
81 changes: 81 additions & 0 deletions app/src/test/java/com/trendyol/medusa/DestinationListenerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,87 @@ class DestinationListenerTest {
}
}
}

@Test
fun `given four fragment, when clearGroup is called to remove middle fragments, then observeDestinationChanges should not be triggered`() {
val testObserver = TestObserver<Fragment>()
launchActivity<MainActivity>().use { scenario ->
// Given
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onActivity { activity ->
activity.multipleStackNavigator.observeDestinationChanges(activity, testObserver)
activity.doAndExecuteFragmentTransactions {
start(FragmentGenerator.generateNewFragment())
start(FragmentGenerator.generateNewFragment(), "group-name")
start(FragmentGenerator.generateNewFragment(), "group-name")
start(FragmentGenerator.generateNewFragment())
clearGroup("group-name")
}
activity.supportFragmentManager.executePendingTransactions()

Assert.assertEquals(
"fragment 2",
testObserver.getLastFragmentName(indexFromLast = 3)
)
Assert.assertEquals(
"fragment 3",
testObserver.getLastFragmentName(indexFromLast = 2)
)
Assert.assertEquals(
"fragment 4",
testObserver.getLastFragmentName(indexFromLast = 1)
)
Assert.assertEquals(
"fragment 5",
testObserver.getLastFragmentName(indexFromLast = 0)
)
}
}
}

@Test
fun `given four fragment, when clearGroup is called to last two fragments, then observeDestinationChanges should be triggered`() {
val testObserver = TestObserver<Fragment>()
launchActivity<MainActivity>().use { scenario ->
// Given
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.onActivity { activity ->
activity.multipleStackNavigator.observeDestinationChanges(activity, testObserver)
activity.doAndExecuteFragmentTransactions {
start(FragmentGenerator.generateNewFragment())
start(FragmentGenerator.generateNewFragment())
start(FragmentGenerator.generateNewFragment(), "group-name")
start(FragmentGenerator.generateNewFragment(), "group-name")
}
activity.supportFragmentManager.executePendingTransactions()
activity.doAndExecuteFragmentTransactions {
clearGroup("group-name")
}
activity.supportFragmentManager.executePendingTransactions()
Assert.assertEquals(
"fragment 2",
testObserver.getLastFragmentName(indexFromLast = 4)
)
Assert.assertEquals(
"fragment 3",
testObserver.getLastFragmentName(indexFromLast = 3)
)
Assert.assertEquals(
"fragment 4",
testObserver.getLastFragmentName(indexFromLast = 2)
)
Assert.assertEquals(
"fragment 5",
testObserver.getLastFragmentName(indexFromLast = 1)
)
Assert.assertEquals(
"fragment 3",
testObserver.getLastFragmentName(indexFromLast = 0)
)
}
}
}

private fun MainActivity.doAndExecuteFragmentTransactions(run: MultipleStackNavigator.() -> Unit) =
run.invoke(this.multipleStackNavigator).also { supportFragmentManager.executePendingTransactions() }

Expand Down
2 changes: 1 addition & 1 deletion medusalib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {

ext {
PUBLISH_GROUP_ID = 'com.trendyol'
PUBLISH_VERSION = '0.10.3'
PUBLISH_VERSION = '0.10.4'
PUBLISH_ARTIFACT_ID = 'medusa'
PUBLISH_DESCRIPTION = "Android Fragment Stack Controller"
PUBLISH_URL = "https://github.com/Trendyol/medusa"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ open class MultipleStackNavigator(
if (fragmentGroupName == DEFAULT_GROUP_NAME) {
throw IllegalArgumentException("Fragment group name can not be empty.")
}
val upperFragmentTag = fragmentStackState.peekItemFromSelectedTab()?.fragmentTag

val poppedFragmentTags = fragmentStackState
.popItems(fragmentGroupName)
.map { it.fragmentTag }

if (poppedFragmentTags.isNotEmpty()) {
fragmentManagerController.removeFragments(poppedFragmentTags)
showUpperFragment()
val poppedUpperFragment = poppedFragmentTags.contains(upperFragmentTag)
if (poppedUpperFragment) {
showUpperFragment()
}
}
}

Expand Down

0 comments on commit 5f2f1ab

Please sign in to comment.