Skip to content

Instantly share code, notes, and snippets.

View jaozinfs's full-sized avatar

João Victor Chaves de Oliveira jaozinfs

View GitHub Profile
@jaozinfs
jaozinfs / teste.kt
Created September 23, 2021 15:24
teste
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)
}
private fun observeViewModel() {
viewLifecycleOwner.lifecycleScope.launch {
exampleViewModel.testFeatureEvents.value().filter {
it is ExampleViewModel.ExampleEvents.OpenDialog
}.collect { _ ->
findNavController().navigate(R.id.actionppenGenericDialog)
}
}
}
@OptIn(InternalCoroutinesApi::class)
suspend fun <T> Flow<T>.collectWithTimeOut() = flow{
withTimeoutOrNull(10) {
collect {
emit(it)
}
}
}
@OptIn(InternalCoroutinesApi::class)
suspend fun <T> Flow<T>.collectOrNull() :T? {
var data:T? = null
withTimeoutOrNull(10) {
collect {
data = it
}
}
return data
}
@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())
}
fun <T> SingleEvent<T>.flowWithLifeCycle(
lifeCycle: Lifecycle,
lifecycleState: Lifecycle.State = Lifecycle.State.CREATED
) = value().flowWithLifecycle(lifeCycle, lifecycleState)
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)
}
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)
}
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
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