From 48eb9e37f79e60b25c3946baa212d38a1a26a4be Mon Sep 17 00:00:00 2001 From: Pavel Akashev Date: Fri, 11 Apr 2025 19:18:26 +0300 Subject: [PATCH 01/14] #1 CatsService refactored to suspend --- app/src/main/java/otus/homework/coroutines/CatsService.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/otus/homework/coroutines/CatsService.kt b/app/src/main/java/otus/homework/coroutines/CatsService.kt index 479b2cfb..db865d0c 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsService.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsService.kt @@ -1,10 +1,9 @@ package otus.homework.coroutines -import retrofit2.Call import retrofit2.http.GET interface CatsService { @GET("fact") - fun getCatFact() : Call + suspend fun getCatFact() : Fact } \ No newline at end of file From 1e15458abd8c94a38f6f158edcb580415afe50ad Mon Sep 17 00:00:00 2001 From: Pavel Akashev Date: Fri, 11 Apr 2025 19:27:15 +0300 Subject: [PATCH 02/14] #1 CatsPresenter callback -> coroutine --- .../otus/homework/coroutines/CatsPresenter.kt | 21 ++++--------------- .../java/otus/homework/coroutines/CatsView.kt | 7 ++++++- .../otus/homework/coroutines/MainActivity.kt | 6 +++++- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt index e4b05120..4e624f8a 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,15 @@ package otus.homework.coroutines -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response - class CatsPresenter( private val catsService: CatsService ) { private var _catsView: ICatsView? = null - fun onInitComplete() { - catsService.getCatFact().enqueue(object : Callback { - - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful && response.body() != null) { - _catsView?.populate(response.body()!!) - } - } - - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() - } - }) + suspend fun onInitComplete() { + catsService.getCatFact().also { fact -> + _catsView?.populate(fact) + } } fun attachView(catsView: ICatsView) { diff --git a/app/src/main/java/otus/homework/coroutines/CatsView.kt b/app/src/main/java/otus/homework/coroutines/CatsView.kt index be04b2a8..d8c93d0f 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -5,6 +5,9 @@ import android.util.AttributeSet import android.widget.Button import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout +import androidx.lifecycle.findViewTreeLifecycleOwner +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.launch class CatsView @JvmOverloads constructor( context: Context, @@ -17,7 +20,9 @@ class CatsView @JvmOverloads constructor( override fun onFinishInflate() { super.onFinishInflate() findViewById