Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW - Ilya Lisitsa Flow #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.32"
ext.kotlin_version = '1.6.20'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
7 changes: 4 additions & 3 deletions flowcats/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ plugins {
}

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "otus.homework.flowcats"
minSdkVersion 23
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0"

Expand All @@ -37,7 +37,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
Expand All @@ -46,4 +46,5 @@ dependencies {
implementation 'androidx.activity:activity-ktx:1.2.3'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.3'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.0-beta01"
}
6 changes: 4 additions & 2 deletions flowcats/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Flow" >
<activity android:name=".MainActivity">
android:theme="@style/Theme.Flow">
<activity
android:name=".MainActivity"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
16 changes: 13 additions & 3 deletions flowcats/src/main/java/otus/homework/flowcats/CatsRepository.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package otus.homework.flowcats

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

class CatsRepository(
private val catsService: CatsService,
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.IO,
private val refreshIntervalMs: Long = 5000
) {

fun listenForCatFacts() = flow {
fun listenForCatFacts(): Flow<Result<Fact>> = flow<Result<Fact>> {
while (true) {
val latestNews = catsService.getCatFact()
emit(latestNews)
emit(Success(latestNews))
delay(refreshIntervalMs)
}
}
}.catch {
emit(Error)
emitAll(listenForCatFacts())
}.flowOn(backgroundDispatcher)
}
37 changes: 34 additions & 3 deletions flowcats/src/main/java/otus/homework/flowcats/CatsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package otus.homework.flowcats
import android.content.Context
import android.util.AttributeSet
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout

class CatsView @JvmOverloads constructor(
Expand All @@ -11,12 +12,42 @@ class CatsView @JvmOverloads constructor(
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView {

override fun populate(fact: Fact) {
findViewById<TextView>(R.id.fact_textView).text = fact.text
private val tvFact by lazy {
findViewById<TextView>(R.id.fact_textView)
}

override fun populate(result: Result<Fact>) {
when (result) {
is Loading -> {
doOnLoading()
}
is Success -> {
doOnSuccess(result.data)
}
is Error -> {
doOnError()
}
}

}

private fun doOnLoading() {
tvFact.text = context.getString(R.string.loading_state_text)
}

private fun doOnSuccess(fact: Fact) {
tvFact.text = fact.text
}

private fun doOnError() {
val errorMessage = context.getString(R.string.error_state_text)
val toast = Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT)

toast.show()
}
}

interface ICatsView {

fun populate(fact: Fact)
fun populate(result: Result<Fact>)
}
30 changes: 13 additions & 17 deletions flowcats/src/main/java/otus/homework/flowcats/CatsViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package otus.homework.flowcats

import androidx.lifecycle.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn

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

private val _catsLiveData = MutableLiveData<Fact>()
val catsLiveData: LiveData<Fact> = _catsLiveData

init {
viewModelScope.launch {
withContext(Dispatchers.IO) {
catsRepository.listenForCatFacts().collect {
_catsLiveData.value = it
}
}
}
}
val catsStateFlow: StateFlow<Result<Fact>> = catsRepository
.listenForCatFacts()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = Loading
)
}

class CatsViewModelFactory(private val catsRepository: CatsRepository) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DiContainer {
.build()
}

val service by lazy { retrofit.create(CatsService::class.java) }
private val service: CatsService by lazy { retrofit.create(CatsService::class.java) }

val repository by lazy { CatsRepository(service) }
}
22 changes: 18 additions & 4 deletions flowcats/src/main/java/otus/homework/flowcats/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package otus.homework.flowcats

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

private val diContainer = DiContainer()
private val catsViewModel by viewModels<CatsViewModel> { CatsViewModelFactory(diContainer.repository) }
private val catsViewModel by viewModels<CatsViewModel> {
CatsViewModelFactory(diContainer.repository)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
setContentView(view)

catsViewModel.catsLiveData.observe(this){
view.populate(it)
initCollectingViewItems(view)
}

private fun initCollectingViewItems(view: CatsView) {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
catsViewModel.catsStateFlow.collect { result ->
view.populate(result)
}
}
}
}
}
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<out T>
class Success<out T>(val data: T) : Result<T>()
object Loading : Result<Nothing>()
object Error : Result<Nothing>()
2 changes: 2 additions & 0 deletions flowcats/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Flow cats</string>
<string name="loading_state_text">Loading…</string>
<string name="error_state_text">Default error</string>
</resources>
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed May 05 23:18:49 MSK 2021
#Sat Apr 23 10:36:49 MSK 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
zipStoreBase=GRADLE_USER_HOME
62 changes: 56 additions & 6 deletions operators/src/main/java/otus/homework/flow/SampleInteractor.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package otus.homework.flow

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.transform
import kotlinx.coroutines.flow.zip

@ExperimentalCoroutinesApi
class SampleInteractor(
Expand All @@ -10,15 +17,20 @@ class SampleInteractor(

/**
* Реализуйте функцию task1 которая последовательно:
* 1) возводит числа в 5ую степень
* 1) возводит числа в 5ую степень // У вас тут ошибка если делать как тут написано то тест никогда не пройдется) я так понимаю тут перемножение на 5 имелось ввиду, а не степень
* 2) убирает чила <= 20
* 3) убирает четные числа
* 4) добавляет постфикс "won"
* 5) берет 3 первых числа
* 6) возвращает результат
*/
fun task1(): Flow<String> {
return flowOf()
return sampleRepository.produceNumbers()
.map { it * 5 }
.filter { it > 20 }
.filter { it % 2 != 0 }
.map { "$it won" }
.take(3)
}

/**
Expand All @@ -29,7 +41,8 @@ class SampleInteractor(
* Если число не делится на 3,5,15 - эмитим само число
*/
fun task2(): Flow<String> {
return flowOf()
return sampleRepository.produceNumbers()
.fizzBuzzOperator()
}

/**
Expand All @@ -38,7 +51,12 @@ class SampleInteractor(
* Если айтемы в одно из флоу кончились то результирующий флоу также должен закончится
*/
fun task3(): Flow<Pair<String, String>> {
return flowOf()
val colorsFlow = sampleRepository.produceColors()
val formsFlow = sampleRepository.produceForms()

return colorsFlow.zip(formsFlow) { firstData, secondData ->
firstData to secondData
}
}

/**
Expand All @@ -48,6 +66,38 @@ class SampleInteractor(
* При любом исходе, будь то выброс исключения или успешная отработка функции вызовите метод dotsRepository.completed()
*/
fun task4(): Flow<Int> {
return flowOf()
val defaultValue = -1

return sampleRepository.produceNumbers()
.catch { exception ->
if (exception is IllegalArgumentException) {
emit(defaultValue)
} else {
throw exception
}
}
.onCompletion {
sampleRepository.completed()
}
}

private fun Flow<Int>.fizzBuzzOperator(): Flow<String> = transform { value ->
val fizz = "Fizz"
val buzz = "Buzz"
val fizzBuzz = "$fizz$buzz"

emit(value.toString())

when {
value % 15 == 0 -> {
emit(fizzBuzz)
}
value % 3 == 0 -> {
emit(fizz)
}
value % 5 == 0 -> {
emit(buzz)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class SampleInteractorTest {
runBlockingTest {
dotsInteractor.task4().toList()
}

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