diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..a00b3aa8 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.2.0" } \ 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..ca63c9f2 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,40 @@ package otus.homework.coroutines -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch +import otus.homework.coroutines.CrashMonitor.trackWarning +import otus.homework.coroutines.model.Model +import java.net.SocketTimeoutException + class CatsPresenter( - private val catsService: CatsService + private val catsService: CatsService, + private val imagesService: ImagesService, + private val onShowToast: (String?) -> Unit ) { private var _catsView: ICatsView? = null - + private val presenterScope = CoroutineScope(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 { + val fact = async { catsService.getCatFact() } + val image = async { imagesService.getImage().firstOrNull() } + + _catsView?.populate(Model(fact.await(), image.await()?.url)) + } catch (e: SocketTimeoutException) { + println("Caught $e") + onShowToast("Не удалось получить ответ от сервера") + + } catch (e: Exception) { + trackWarning(e.message) + onShowToast(e.message) } - - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() - } - }) + } } fun attachView(catsView: ICatsView) { @@ -32,4 +44,8 @@ class CatsPresenter( fun detachView() { _catsView = null } + + fun onStop() { + presenterScope.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..63f35fe1 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsService.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsService.kt @@ -1,10 +1,11 @@ package otus.homework.coroutines +import otus.homework.coroutines.model.Fact import retrofit2.Call import retrofit2.http.GET interface CatsService { @GET("fact") - fun getCatFact() : Call + suspend fun getCatFact(): Fact } \ 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..f309e75b 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -3,8 +3,13 @@ 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 +import otus.homework.coroutines.model.Fact +import otus.homework.coroutines.model.Image +import otus.homework.coroutines.model.Model class CatsView @JvmOverloads constructor( context: Context, @@ -12,21 +17,28 @@ class CatsView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { - var presenter :CatsPresenter? = null + var presenter: CatsPresenter? = null + var viewModel: CatsViewModel? = null override fun onFinishInflate() { super.onFinishInflate() findViewById