-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Trendyol/PreloadedFragment
Preloaded fragment
- Loading branch information
Showing
10 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/trendyol/medusa/SamplePreloadFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.trendyol.medusa | ||
|
||
import android.os.Bundle | ||
import android.os.SystemClock | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Chronometer | ||
|
||
class SamplePreloadFragment : BaseFragment() { | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
return inflater.inflate(R.layout.fragment_sample_preload, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
startTimer() | ||
} | ||
|
||
private fun startTimer() { | ||
getChronometer().apply { | ||
base = SystemClock.elapsedRealtime() | ||
start() | ||
} | ||
} | ||
|
||
private fun stopTimer() { | ||
getChronometer().stop() | ||
} | ||
|
||
override fun onDestroyView() { | ||
stopTimer() | ||
super.onDestroyView() | ||
} | ||
|
||
private fun getChronometer(): Chronometer = requireView().findViewById(R.id.chronometer) | ||
|
||
companion object { | ||
const val TAG = "SamplePreloadFragmentTag" | ||
fun newInstance(): SamplePreloadFragment = SamplePreloadFragment() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<Chronometer | ||
android:id="@+id/chronometer" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:textAlignment="center" | ||
android:textSize="16sp" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
medusalib/src/androidTest/java/com/trendyol/medusalib/navigator/PreloadFragmentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.trendyol.medusalib.navigator | ||
|
||
import androidx.fragment.app.testing.launchFragmentInContainer | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth | ||
import com.trendyol.medusalib.TestChildFragment | ||
import com.trendyol.medusalib.TestParentWithNavigatorFragment | ||
import com.trendyol.medusalib.navigator.controller.PreloadedFragmentResult | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class PreloadFragmentTest { | ||
|
||
@Test | ||
fun testPreloadFragment() { | ||
val scenario = launchFragmentInContainer<TestParentWithNavigatorFragment>() | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
|
||
scenario.onFragment { parentFragment -> | ||
val navigator = parentFragment.navigator | ||
|
||
val fragmentTag = "preloaded_test_fragment" | ||
val testChildFragment = TestChildFragment.newInstance("Preload Fragment") | ||
|
||
navigator.preloadFragment(testChildFragment, fragmentTag) | ||
parentFragment.childFragmentManager.executePendingTransactions() | ||
|
||
val preloadedFragment = parentFragment.childFragmentManager.findFragmentByTag(fragmentTag) | ||
Truth.assertThat(preloadedFragment).isNotNull() | ||
Truth.assertThat(preloadedFragment?.isVisible).isFalse() | ||
Truth.assertThat(parentFragment.childFragmentManager.fragments.any { it.isVisible }).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun testStartPreloadedFragment() { | ||
val scenario = launchFragmentInContainer<TestParentWithNavigatorFragment>() | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
scenario.onFragment { parentFragment -> | ||
val fragmentTag = "preloaded_test_fragment" | ||
val navigator = parentFragment.navigator | ||
val testChildFragment = TestChildFragment.newInstance("Preload Fragment") | ||
|
||
navigator.preloadFragment(testChildFragment, fragmentTag) | ||
parentFragment.childFragmentManager.executePendingTransactions() | ||
|
||
val result = navigator.startPreloadedFragment(null, fragmentTag) | ||
parentFragment.childFragmentManager.executePendingTransactions() | ||
|
||
Truth.assertThat(result).isEqualTo(PreloadedFragmentResult.Success) | ||
val startedFragment = parentFragment.childFragmentManager.findFragmentByTag(fragmentTag) | ||
Truth.assertThat(startedFragment).isNotNull() | ||
Truth.assertThat(startedFragment?.isVisible).isTrue() | ||
|
||
val fragmentsWithoutPreloaded = parentFragment.childFragmentManager.fragments - startedFragment | ||
Truth.assertThat(fragmentsWithoutPreloaded.all { it?.isVisible == false }).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun testStartPreloadedFragmentWithFallback() { | ||
val scenario = launchFragmentInContainer<TestParentWithNavigatorFragment>() | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
scenario.onFragment { parentFragment -> | ||
val fragmentTag = "non_existent_fragment" | ||
val navigator = parentFragment.navigator | ||
val fallbackFragment = TestChildFragment.newInstance("Fallback Fragment") | ||
|
||
val result = navigator.startPreloadedFragment(fallbackFragment, fragmentTag) | ||
parentFragment.childFragmentManager.executePendingTransactions() | ||
|
||
val startedFragment = parentFragment.childFragmentManager.findFragmentByTag(fragmentTag) | ||
Truth.assertThat(result).isEqualTo(PreloadedFragmentResult.FallbackSuccess) | ||
Truth.assertThat(startedFragment).isNotNull() | ||
Truth.assertThat(startedFragment?.isVisible).isTrue() | ||
|
||
val fragmentsWithoutPreloaded = parentFragment.childFragmentManager.fragments - startedFragment | ||
Truth.assertThat(fragmentsWithoutPreloaded.all { it?.isVisible == false }).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun testStartPreloadedFragmentNotFound() { | ||
val scenario = launchFragmentInContainer<TestParentWithNavigatorFragment>() | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
scenario.onFragment { parentFragment -> | ||
val fragmentTag = "non_existent_fragment" | ||
val navigator = parentFragment.navigator | ||
|
||
val result = navigator.startPreloadedFragment(null, fragmentTag) | ||
parentFragment.childFragmentManager.executePendingTransactions() | ||
|
||
val fragment = parentFragment.childFragmentManager.findFragmentByTag(fragmentTag) | ||
Truth.assertThat(result).isEqualTo(PreloadedFragmentResult.NotFound) | ||
Truth.assertThat(fragment).isNull() | ||
Truth.assertThat(parentFragment.childFragmentManager.fragments.any { it.isVisible }).isTrue() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...alib/src/main/java/com/trendyol/medusalib/navigator/controller/PreloadedFragmentResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.trendyol.medusalib.navigator.controller | ||
|
||
/** | ||
* Represents the result of attempting to show a preloaded fragment. | ||
*/ | ||
sealed interface PreloadedFragmentResult { | ||
|
||
/** | ||
* Indicates that the preloaded fragment was successfully shown. | ||
*/ | ||
data object Success : PreloadedFragmentResult | ||
|
||
/** | ||
* Indicates that a fallback fragment was used and successfully shown. | ||
*/ | ||
data object FallbackSuccess : PreloadedFragmentResult | ||
|
||
/** | ||
* Indicates that neither the preloaded fragment nor a fallback fragment was found. | ||
*/ | ||
data object NotFound : PreloadedFragmentResult | ||
} |