diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..6539edcf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,4 +41,6 @@ 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.10.0" + implementation "androidx.activity:activity-ktx:1.8.0" } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatImage.kt b/app/src/main/java/otus/homework/coroutines/CatImage.kt new file mode 100644 index 00000000..2f7f4eaf --- /dev/null +++ b/app/src/main/java/otus/homework/coroutines/CatImage.kt @@ -0,0 +1,12 @@ +package otus.homework.coroutines + +import com.google.gson.annotations.SerializedName + +data class CatImage( + @field:SerializedName("url") + val url: String, + @field:SerializedName("width") + val width: Int, + @field:SerializedName("height") + val height: Int, +) \ 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..6e73ea86 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,54 @@ package otus.homework.coroutines -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response +import android.content.Context +import android.widget.Toast +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.async +import kotlinx.coroutines.launch +import java.net.SocketTimeoutException class CatsPresenter( + val context: Context, private val catsService: CatsService ) { private var _catsView: ICatsView? = null + private val presenterScope = CoroutineScope(Dispatchers.Main + SupervisorJob() + CoroutineName("CatsCoroutine")) + private var job: Job? = null fun onInitComplete() { - catsService.getCatFact().enqueue(object : Callback { + job = presenterScope.launch { + try { + val getCatFactDiffered = async { catsService.getCatFact() } + val getCatImageDiffered = async { catsService.getCatImage() } - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful && response.body() != null) { - _catsView?.populate(response.body()!!) - } - } + val getCatFactResponse = getCatFactDiffered.await() + val getCatImageResponse = getCatImageDiffered.await().firstOrNull() - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() + val catModelsMapper = CatModels( + fact = getCatFactResponse.fact, + url = getCatImageResponse?.url.orEmpty(), + width = getCatImageResponse?.width ?: 0,//значение не использую, но пусть будет + height = getCatImageResponse?.height ?: 0,//значение не использую, но пусть будет + ) + _catsView?.populate(catModelsMapper) + } catch (e: Exception) { + when (e) { + is SocketTimeoutException -> { + Toast.makeText(context, R.string.timeout_error_text, Toast.LENGTH_SHORT) + .show() + } + else -> { + CrashMonitor.trackWarning() + Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show() + } + } } - }) + } } fun attachView(catsView: ICatsView) { @@ -32,4 +58,8 @@ class CatsPresenter( fun detachView() { _catsView = null } + + fun onStop() { + 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..d2d3544b 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 +import retrofit2.http.Url interface CatsService { @GET("fact") - fun getCatFact() : Call + suspend fun getCatFact() : Fact + + @GET + suspend fun getCatImage( + @Url url: String = "https://api.thecatapi.com/v1/images/search" + ): 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..19fd230c 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -3,30 +3,36 @@ 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 androidx.constraintlayout.widget.ConstraintLayout +import com.squareup.picasso.Picasso class CatsView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, - defStyleAttr: Int = 0 + defStyleAttr: Int = 0, + var onButtonClick: (() -> Unit)? = null ) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { - var presenter :CatsPresenter? = null override fun onFinishInflate() { super.onFinishInflate() findViewById