diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..fc17f354 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,4 +41,7 @@ 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.8.7' + implementation "androidx.activity:activity-ktx:1.9.3" } \ 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..ce7ac508 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,43 @@ package otus.homework.coroutines -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response +import com.google.gson.annotations.SerializedName +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.async +import kotlinx.coroutines.cancel +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch +import java.net.SocketTimeoutException class CatsPresenter( - private val catsService: CatsService + private val catsService: CatsService, + private val imageCatsService: ImageCatsService ) { private var _catsView: ICatsView? = null + private val presenterScope = CoroutineScope(SupervisorJob() + Dispatchers.Main + CoroutineName("CatsCoroutine")) fun onInitComplete() { - catsService.getCatFact().enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful && response.body() != null) { - _catsView?.populate(response.body()!!) + presenterScope.launch { + try { + coroutineScope { + val fact = async { catsService.getCatFact() } + val image = async { imageCatsService.getImageCatFact().first() } + val cat = Cat(fact.await(), image.await()) + _catsView?.populate(cat) } } - - override fun onFailure(call: Call, t: Throwable) { + catch (e: SocketTimeoutException) { + _catsView?.showToast("Не удалось получить ответ от сервера") + } + catch (e: Exception) { CrashMonitor.trackWarning() + e.message?.let { _catsView?.showToast(it) } } - }) + } } fun attachView(catsView: ICatsView) { @@ -31,5 +46,18 @@ class CatsPresenter( fun detachView() { _catsView = null + presenterScope.cancel() } -} \ No newline at end of file +} + +data class Cat( + val fact: Fact, + val image: Image +) + +data class Image( + @field:SerializedName("id") + val id: String, + @field:SerializedName("url") + val url: String +) \ 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..c73b6164 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsService.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsService.kt @@ -1,10 +1,15 @@ package otus.homework.coroutines -import retrofit2.Call import retrofit2.http.GET interface CatsService { @GET("fact") - fun getCatFact() : Call + suspend fun getCatFact() : Fact +} + +interface ImageCatsService { + + @GET("v1/images/search") + suspend fun getImageCatFact() : 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..c1b22c79 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -3,8 +3,12 @@ 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 +import kotlinx.coroutines.Deferred class CatsView @JvmOverloads constructor( context: Context, @@ -13,20 +17,36 @@ class CatsView @JvmOverloads constructor( ) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { var presenter :CatsPresenter? = null + var viewModel: CatsViewModel? = null + + private var catImageView: ImageView? = null override fun onFinishInflate() { super.onFinishInflate() + catImageView = findViewById(R.id.image) findViewById