Skip to content

Commit e4243b2

Browse files
committed
Otus-Android#3 Result added
1 parent 80f2b19 commit e4243b2

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

app/src/main/java/otus/homework/coroutines/CatsViewModel.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ import kotlinx.coroutines.flow.filterNotNull
1313
import kotlinx.coroutines.flow.onStart
1414
import kotlinx.coroutines.flow.shareIn
1515
import kotlinx.coroutines.launch
16+
import java.net.SocketTimeoutException
1617

1718
class CatsViewModel(
1819
private val catsService: CatsService,
1920
private val imageService: ImageService
2021
) : ViewModel() {
2122

2223
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
23-
CrashMonitor.trackWarning(throwable)
24+
when(throwable) {
25+
is SocketTimeoutException -> _uiState.tryEmit(Result.Error(Throwable("Не удалось получить ответ от сервера")))
26+
else -> {
27+
CrashMonitor.trackWarning(throwable)
28+
_uiState.tryEmit(Result.Error(throwable))
29+
}
30+
}
2431
}
2532

26-
private val _uiState = MutableStateFlow<CatsUIState?>(null)
33+
private val _uiState = MutableStateFlow<Result?>(null)
2734
val uiState = _uiState.onStart { load() }
2835
.filterNotNull()
2936
.shareIn(
@@ -38,9 +45,11 @@ class CatsViewModel(
3845
val image = async { imageService.getCatImage() }
3946

4047
_uiState.emit(
41-
CatsUIState(
42-
fact = fact.await(),
43-
image = image.await()[0]
48+
Result.Success(
49+
CatsUIState(
50+
fact = fact.await(),
51+
image = image.await()[0]
52+
)
4453
)
4554
)
4655
}

app/src/main/java/otus/homework/coroutines/MainActivity.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import android.os.Bundle
44
import android.widget.Button
55
import android.widget.ImageView
66
import android.widget.TextView
7+
import android.widget.Toast
78
import androidx.appcompat.app.AppCompatActivity
89
import androidx.lifecycle.ViewModelProvider
910
import androidx.lifecycle.lifecycleScope
1011
import com.squareup.picasso.Picasso
1112
import kotlinx.coroutines.launch
13+
import otus.homework.coroutines.Result.*
1214

1315
class MainActivity : AppCompatActivity() {
1416

@@ -32,7 +34,7 @@ class MainActivity : AppCompatActivity() {
3234
image = view.findViewById(R.id.catImage)
3335
loadButton = view.findViewById(R.id.button)
3436

35-
configureFact()
37+
processState()
3638
setListeners()
3739
}
3840

@@ -42,12 +44,23 @@ class MainActivity : AppCompatActivity() {
4244
}
4345
}
4446

45-
private fun configureFact() {
47+
private fun processState() {
4648
lifecycleScope.launch {
4749
viewModel.uiState.collect { state ->
48-
fact.text = state.fact.fact
49-
Picasso.get().load(state.image.url).into(image)
50+
when(state) {
51+
is Success<*> -> {
52+
(state.data as CatsUIState).also {
53+
fact.text = it.fact.fact
54+
Picasso.get().load(it.image.url).into(image)
55+
}
56+
}
57+
is Error -> showToast(state.error)
58+
}
5059
}
5160
}
5261
}
62+
63+
private fun showToast(throwable: Throwable, duration: Int = Toast.LENGTH_LONG) {
64+
Toast.makeText(this, throwable.message, duration).show()
65+
}
5366
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package otus.homework.coroutines
2+
3+
sealed class Result{
4+
data class Success<out T>(val data: T) : Result()
5+
data class Error(val error: Throwable) : Result()
6+
}

0 commit comments

Comments
 (0)