diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..557b196a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,4 +41,5 @@ dependencies { implementation 'com.google.android.material:material:1.11.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.squareup.picasso:picasso:2.71828' + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.0" } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatFact.kt b/app/src/main/java/otus/homework/coroutines/CatFact.kt new file mode 100644 index 00000000..5cad3f65 --- /dev/null +++ b/app/src/main/java/otus/homework/coroutines/CatFact.kt @@ -0,0 +1,6 @@ +package otus.homework.coroutines + +data class CatFact( + val fact: Fact, + val imageUrl: String = "", +) \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt index e4b05120..fd1e69d2 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,68 @@ package otus.homework.coroutines +import android.content.res.Resources +import kotlinx.coroutines.CancellationException +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 { + 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, response: Response) { - if (response.isSuccessful && response.body() != null) { - _catsView?.populate(response.body()!!) - } + CatFact( + fact = facts, + imageUrl = imageUrl, + ) + }.onFailure { error -> + if (error is CancellationException) throw error + errorHandler(error) + }.onSuccess { + _catsView?.populate(it) } + } + } - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() + fun onInitComplete() { + job = presenterScope.launch { + try { + val facts = catsService.getCatFact() + _catsView?.populate(CatFact(facts)) + } catch (e: Exception) { + errorHandler(e) } - }) + } + } + + 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 +71,6 @@ class CatsPresenter( fun detachView() { _catsView = null + job.cancel() } } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatsService.kt b/app/src/main/java/otus/homework/coroutines/CatsService.kt index 479b2cfb..14e468c6 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsService.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsService.kt @@ -1,10 +1,12 @@ package otus.homework.coroutines -import retrofit2.Call import retrofit2.http.GET interface CatsService { @GET("fact") - fun getCatFact() : Call + suspend fun getCatFact() : Fact + + @GET("https://api.thecatapi.com/v1/images/search") + suspend fun loadImage() : List } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatsView.kt b/app/src/main/java/otus/homework/coroutines/CatsView.kt index be04b2a8..d3e9d4ef 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -3,8 +3,11 @@ package otus.homework.coroutines import android.content.Context import android.util.AttributeSet import android.widget.Button +import android.widget.ImageView import android.widget.TextView +import android.widget.Toast import androidx.constraintlayout.widget.ConstraintLayout +import com.squareup.picasso.Picasso class CatsView @JvmOverloads constructor( context: Context, @@ -17,16 +20,26 @@ class CatsView @JvmOverloads constructor( override fun onFinishInflate() { super.onFinishInflate() findViewById