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
val response = withTimeoutOrNull(expectedTime) { | |
var response: ...? = null | |
do { | |
if (coroutineContext[Job]?.isActive == true) { | |
response = | |
repository.data | |
if (response.inAnalysis()) // check if response stay on analysis, if yes, the code call a delay | |
delay(checkoutAttemptDelay) | |
} |
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
private fun observeViewModel() { | |
viewLifecycleOwner.lifecycleScope.launch { | |
exampleViewModel.testFeatureEvents.value().filter { | |
it is ExampleViewModel.ExampleEvents.OpenDialog | |
}.collect { _ -> | |
findNavController().navigate(R.id.actionppenGenericDialog) | |
} | |
} | |
} |
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
@OptIn(InternalCoroutinesApi::class) | |
suspend fun <T> Flow<T>.collectWithTimeOut() = flow{ | |
withTimeoutOrNull(10) { | |
collect { | |
emit(it) | |
} | |
} | |
} |
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
@OptIn(InternalCoroutinesApi::class) | |
suspend fun <T> Flow<T>.collectOrNull() :T? { | |
var data:T? = null | |
withTimeoutOrNull(10) { | |
collect { | |
data = it | |
} | |
} | |
return data | |
} |
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
@Test | |
fun `when start viewmodel should send open second fragment only time`() = runBlocking { | |
val expectedEvent = ExampleEvents.OpenSecondFragment | |
coEvery { | |
exampleUseCase.invoke() | |
} retruns String() | |
val viewModel = ExampleViewModel(exampleUseCase) | |
assertEquals(expectedEvent, viewModel.testFeatureEvents.take(1).firstOrNull()) | |
assertNull(viewModel.testFeatureEvents.collectWithTimeOut()) | |
} |
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
fun <T> SingleEvent<T>.flowWithLifeCycle( | |
lifeCycle: Lifecycle, | |
lifecycleState: Lifecycle.State = Lifecycle.State.CREATED | |
) = value().flowWithLifecycle(lifeCycle, lifecycleState) |
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
private fun observeViewModel() { | |
viewLifecycleOwner.lifecycleScope.launch { | |
exampleViewModel.testFeatureEvents.value().collect { event -> | |
when (event) { | |
is ExampleViewModel.ExampleEvents.OpenDialog -> { | |
findNavController().navigate(R.id.actionppenGenericDialog) | |
} | |
is ExampleViewModel.ExampleEvents.OpenSecondFragment -> { | |
findNavController().navigate(R.id.actionOpenSecondFragment) | |
} |
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
private fun observeViewModel() { | |
viewLifecycleOwner.lifecycleScope.launch { | |
exampleViewModel.testFeatureEvents.flowWithLifeCycle(lifecycle).collect { event -> | |
when (event) { | |
is ExampleViewModel.ExampleEvents.OpenDialog -> { | |
findNavController().navigate(R.id.actionppenGenericDialog) | |
} | |
is ExampleViewModel.ExampleEvents.OpenSecondFragment -> { | |
findNavController().navigate(R.id.actionOpenSecondFragment) | |
} |
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
class ExampleViewModel( | |
private val exampleUseCase: UseCase | |
) : ViewModel() { | |
sealed class ExampleEvents { | |
object OpenDialog : ExampleEvents() | |
object OpenSecondFragment : ExampleEvents() | |
} | |
private val _testFeatureSingleEvents = MutableSingleEvent<ExampleEvents>() | |
val testFeatureEvents: SingleEvent<ExampleEvents> = _testFeatureSingleEvents |
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
class ExampleViewModel( | |
private val exampleUseCase: UseCase | |
) : ViewModel() { | |
sealed class ExampleEvents { | |
object OpenDialog : ExampleEvents() | |
object OpenSecondFragment : ExampleEvents() | |
} | |
private val _testFeatureSingleEvents = MutableSingleEvent<ExampleEvents>() | |
val testFeatureEvents: SingleEvent<ExampleEvents> = _testFeatureSingleEvents |