-
Notifications
You must be signed in to change notification settings - Fork 251
HomeWork - Домашнее задание по корутинам #273
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
base: development
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| data class CatFact( | ||
| val fact: Fact, | ||
| val imageUrl: String = "", | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,66 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import android.content.res.Resources | ||
| import kotlinx.coroutines.CoroutineName | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.launch | ||
| import retrofit2.Call | ||
| import retrofit2.Callback | ||
| import retrofit2.Response | ||
| import java.net.SocketTimeoutException | ||
|
|
||
| class CatsPresenter( | ||
| private val catsService: CatsService | ||
| private val catsService: CatsService, | ||
| private val resources: Resources, | ||
| ) { | ||
|
|
||
| private var _catsView: ICatsView? = null | ||
|
|
||
| fun onInitComplete() { | ||
| catsService.getCatFact().enqueue(object : Callback<Fact> { | ||
| private val presenterScope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine")) | ||
| private var job: Job = Job() | ||
|
|
||
| fun load() { | ||
| job = presenterScope.launch { | ||
| runCatching { | ||
| val facts = catsService.getCatFact() | ||
| val imageUrl = catsService.loadImage().first().url | ||
|
|
||
| override fun onResponse(call: Call<Fact>, response: Response<Fact>) { | ||
| if (response.isSuccessful && response.body() != null) { | ||
| _catsView?.populate(response.body()!!) | ||
| } | ||
| CatFact( | ||
| fact = facts, | ||
| imageUrl = imageUrl, | ||
| ) | ||
| }.onFailure { | ||
| errorHandler(it) | ||
| }.onSuccess { | ||
| _catsView?.populate(it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onFailure(call: Call<Fact>, t: Throwable) { | ||
| CrashMonitor.trackWarning() | ||
| fun onInitComplete() { | ||
| job = presenterScope.launch { | ||
| try { | ||
| val facts = catsService.getCatFact() | ||
| _catsView?.populate(CatFact(facts)) | ||
| } catch (e: Exception) { | ||
| errorHandler(e) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вот тут еще остался перехват |
||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| private fun errorHandler(e: Throwable) { | ||
| if (e is SocketTimeoutException) { | ||
| _catsView?.showToast( | ||
| resources.getString( | ||
| R.string.socet_timeout_error, | ||
| ), | ||
| ) | ||
| } else { | ||
| CrashMonitor.trackWarning() | ||
| _catsView?.showToast(e.message.orEmpty()) | ||
| } | ||
| } | ||
|
|
||
| fun attachView(catsView: ICatsView) { | ||
|
|
@@ -31,5 +69,6 @@ class CatsPresenter( | |
|
|
||
| fun detachView() { | ||
| _catsView = null | ||
| job.cancel() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import retrofit2.Call | ||
| import retrofit2.http.GET | ||
|
|
||
| interface CatsService { | ||
|
|
||
| @GET("fact") | ||
| fun getCatFact() : Call<Fact> | ||
| suspend fun getCatFact() : Fact | ||
|
|
||
| @GET("https://api.thecatapi.com/v1/images/search") | ||
| suspend fun loadImage() : List<FactImage> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import android.content.res.Resources | ||
| import android.util.Log | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.CoroutineExceptionHandler | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
| import java.net.SocketTimeoutException | ||
|
|
||
| class CatsViewModel( | ||
| private val catsService: CatsService, | ||
| private val resources: Resources, | ||
| ) : ViewModel() { | ||
|
|
||
| private val _catsFact = MutableStateFlow<Result>(Result.Success(Unit)) | ||
| val catsFact = _catsFact.asStateFlow() | ||
|
|
||
| private val exceptionHandler = CoroutineExceptionHandler { _, _ -> | ||
| CrashMonitor.trackWarning() | ||
| } | ||
|
|
||
| fun load() { | ||
| viewModelScope.launch(exceptionHandler) { | ||
| runCatching { | ||
| val facts = catsService.getCatFact() | ||
| val imageUrl = catsService.loadImage().first().url | ||
|
|
||
| CatFact( | ||
| fact = facts, | ||
| imageUrl = imageUrl, | ||
| ) | ||
| }.onFailure { error -> | ||
| _catsFact.update { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. также перехват
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. исправил |
||
| Result.Error(errorHandler(error)) | ||
| } | ||
| }.onSuccess { fact -> | ||
| _catsFact.update { | ||
| Result.Success(fact) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun errorHandler(e: Throwable): String { | ||
| Log.d("Fact", e.message.toString()) | ||
| return if (e is SocketTimeoutException) { | ||
| resources.getString( | ||
| R.string.socet_timeout_error, | ||
| ) | ||
| } else { | ||
| e.message.orEmpty() | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| sealed interface Result { | ||
| data class Success<T>(val result: T) : Result | ||
| data class Error(val message: String) : Result | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package otus.homework.coroutines | ||
|
|
||
| import com.google.gson.annotations.SerializedName | ||
|
|
||
| data class FactImage( | ||
| @field:SerializedName("url") | ||
| val url: String, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| <resources> | ||
| <string name="app_name">Cat Facts </string> | ||
| <string name="more_facts">More Facts</string> | ||
| <string name="refresh_screen">Refresh Screen</string> | ||
| <string name="socet_timeout_error">Не удалось получить ответ от сервером</string> | ||
| </resources> |
Uh oh!
There was an error while loading. Please reload this page.