Skip to content
Open
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
30 changes: 26 additions & 4 deletions flowcats/src/main/java/otus/homework/flowcats/CatsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,44 @@ package otus.homework.flowcats

import androidx.lifecycle.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import otus.homework.flowcats.Result

class CatsViewModel(
private val catsRepository: CatsRepository
) : ViewModel() {

private val _catsLiveData = MutableLiveData<Fact>()
val catsLiveData: LiveData<Fact> = _catsLiveData
private val _cats = MutableStateFlow<Result>(
Result.Success(
Fact(
"",
false,
"",
"",
"",
false,
"",
"",
""
)
)
)
val cats = _cats.asStateFlow()

init {
viewModelScope.launch {
withContext(Dispatchers.IO) {
catsRepository.listenForCatFacts().collect {
_catsLiveData.value = it
catsRepository.listenForCatFacts()
.catch {
_cats.value = Result.Error
}
.collect {
_cats.value = Result.Success(it)
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions flowcats/src/main/java/otus/homework/flowcats/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package otus.homework.flowcats

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

Expand All @@ -14,8 +18,19 @@ class MainActivity : AppCompatActivity() {
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
setContentView(view)

catsViewModel.catsLiveData.observe(this){
view.populate(it)
lifecycleScope.launch {
catsViewModel.cats.collectLatest {
when (it) {
Result.Error -> {
Toast.makeText(this@MainActivity, "Error", Toast.LENGTH_SHORT).show()
}
is Result.Success<*> -> {
if (it.res is Fact) {
view.populate(it.res)
}
}
}
}
}
}
}
6 changes: 6 additions & 0 deletions flowcats/src/main/java/otus/homework/flowcats/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package otus.homework.flowcats

sealed class Result {
data class Success<T>(val res: T): Result()
data object Error: Result()
}
101 changes: 97 additions & 4 deletions operators/src/main/java/otus/homework/flow/SampleInteractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package otus.homework.flow

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlin.random.Random
import kotlin.random.nextInt

@ExperimentalCoroutinesApi
class SampleInteractor(
Expand All @@ -18,7 +20,14 @@ class SampleInteractor(
* 6) возвращает результат
*/
fun task1(): Flow<String> {
return flowOf()
val values = flowOf(7, 12, 4, 8, 11, 5, 7, 16, 99, 1)
return values.transform {
emit(it * 5)
}.filter {
it > 20 && it % 2 > 0
}.transform {
emit("${it} won")
}.take(3)
}

/**
Expand All @@ -29,7 +38,26 @@ class SampleInteractor(
* Если число не делится на 3,5,15 - эмитим само число
*/
fun task2(): Flow<String> {
return flowOf()
val values = (1..21).asFlow()
return values.transform<Int, String> {
when {
(it % 15) == 0 -> {
emit("$it")
emit("FizzBuzz")
}
(it % 5) == 0 -> {
emit("$it")
emit("Buzz")
}
(it % 3) == 0 -> {
emit("$it")
emit("Fizz")
}
else -> {
emit("$it")
}
}
}
}

/**
Expand All @@ -38,7 +66,17 @@ class SampleInteractor(
* Если айтемы в одно из флоу кончились то результирующий флоу также должен закончится
*/
fun task3(): Flow<Pair<String, String>> {
return flowOf()
val values = flowOf(
"Red",
"Green",
"Blue",
"Black",
"White"
)
val values1 = flowOf("Circle", "Square", "Triangle")
return values.zip(values1) { v, v1 ->
v to v1
}
}

/**
Expand All @@ -48,6 +86,61 @@ class SampleInteractor(
* При любом исходе, будь то выброс исключения или успешная отработка функции вызовите метод dotsRepository.completed()
*/
fun task4(): Flow<Int> {
return flowOf()
val values = flow {
(1..10).forEach {
emit(it)
}
}
return values.catch {
if (it is IllegalArgumentException) {
throw it
} else {
emit(-1)
}
}.onCompletion {
sampleRepository.completed()
}
}

fun task4IllegalArgumentException(): Flow<Int> {
val values = flow {
(1..10).forEach {
if (it == 5) {
throw IllegalArgumentException("Failed")
} else {
emit(it)
}
}
}
return values.catch {
if (it !is IllegalArgumentException) {
throw it
} else {
emit(-1)
}
}.onCompletion {
sampleRepository.completed()
}
}

fun task4NotIllegalArgumentException(): Flow<Int> {
val values = flow {
(1..10).forEach {
if (it == 5) {
throw SecurityException("Security breach")
} else {
emit(it)
}
}
}
return values.catch {
if (it !is IllegalArgumentException) {
throw it
} else {
emit(-1)
}
}.onCompletion {
sampleRepository.completed()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class SampleInteractorTest {
}

val expected = listOf(1, 2, 3, 4, -1)
val actual = dotsInteractor.task4().toList()
val actual = dotsInteractor.task4IllegalArgumentException().toList()

assertEquals(expected, actual)

Expand All @@ -139,9 +139,8 @@ class SampleInteractorTest {

assertThrows(SecurityException::class.java){
runBlockingTest {
dotsInteractor.task4().toList()
dotsInteractor.task4NotIllegalArgumentException().toList()
}

}
verify(exactly = 1) { dotsRepository.completed() }
}
Expand Down