From c2cf9acb07a176412c85616b9614ce86cc0c314b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=BE=D0=BB=D0=B5=D0=B2=20=D0=A1=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=81=D0=BB=D0=B0=D0=B2?= Date: Mon, 19 Apr 2021 11:46:18 +0300 Subject: [PATCH 1/2] homework_2: done --- app/build.gradle | 9 +++ .../homework/coroutines/CatImageResponse.kt | 8 +++ .../otus/homework/coroutines/CatsPresenter.kt | 51 +++++++++++++--- .../otus/homework/coroutines/CatsService.kt | 3 +- .../java/otus/homework/coroutines/CatsView.kt | 29 ++++++++- .../otus/homework/coroutines/DiContainer.kt | 7 +++ .../java/otus/homework/coroutines/Fact.kt | 3 +- .../otus/homework/coroutines/ImageService.kt | 8 +++ .../otus/homework/coroutines/MainActivity.kt | 2 +- .../otus/homework/coroutines/MainViewModel.kt | 58 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 61 +++++++++++++------ 11 files changed, 204 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/otus/homework/coroutines/CatImageResponse.kt create mode 100644 app/src/main/java/otus/homework/coroutines/ImageService.kt create mode 100644 app/src/main/java/otus/homework/coroutines/MainViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index 679dbba4..d278c7a3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,4 +42,13 @@ dependencies { implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.squareup.picasso:picasso:2.71828' + + //coroutines + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1' + + //SwipeRefreshLayout + implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" + + //ViewModelScope + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatImageResponse.kt b/app/src/main/java/otus/homework/coroutines/CatImageResponse.kt new file mode 100644 index 00000000..f4f4a46c --- /dev/null +++ b/app/src/main/java/otus/homework/coroutines/CatImageResponse.kt @@ -0,0 +1,8 @@ +package otus.homework.coroutines + +import com.google.gson.annotations.SerializedName + +data class CatImageResponse( + @field:SerializedName("file") + val fileName: String = "" +) diff --git a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt index e4b05120..4a2d2723 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,60 @@ package otus.homework.coroutines +import kotlinx.coroutines.* import retrofit2.Call import retrofit2.Callback import retrofit2.Response +import java.lang.Exception +import java.net.SocketTimeoutException class CatsPresenter( - private val catsService: CatsService + private val catsService: CatsService, + private val imageService: ImageService ) { private var _catsView: ICatsView? = null + private val presenterScope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine")) + private var factJob: Job? = null + private var refreshJob: Job? = 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()!!) + presenterScope.launch { + factJob?.cancelAndJoin() + factJob = launch { + try { + val result = withContext(Dispatchers.IO) { + catsService.getCatFact() + } + _catsView?.populate(result) + } catch (ex: SocketTimeoutException) { + _catsView?.showErrorDialog(ex.localizedMessage ?: "error") + } + catch (ex : Exception) { + CrashMonitor.trackWarning() } } + } + } - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() + fun loadFactAndImage() { + presenterScope.launch { + refreshJob?.cancelAndJoin() + refreshJob = launch { + val factResult = async(Dispatchers.IO) { catsService.getCatFact() } + val imageResult = async(Dispatchers.IO) { imageService.getCatImage() } + try { + val factWithImage = factResult.await().copy(image = imageResult.await().fileName) + _catsView?.populate(factWithImage) + } catch (ex: SocketTimeoutException) { + _catsView?.showErrorDialog(ex.localizedMessage ?: "error") + } + catch (ex : Exception) { + CrashMonitor.trackWarning() + } finally { + _catsView?.stopRefreshing() + } } - }) + } } fun attachView(catsView: ICatsView) { @@ -30,6 +62,7 @@ class CatsPresenter( } fun detachView() { + presenterScope.cancel() _catsView = null } } \ 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 1e11c382..837452c0 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("random?animal_type=cat") - 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 30ac2531..df1ff07c 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -2,31 +2,54 @@ package otus.homework.coroutines import android.content.Context import android.util.AttributeSet -import android.widget.Button -import android.widget.TextView +import android.widget.* import androidx.constraintlayout.widget.ConstraintLayout +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import com.squareup.picasso.Picasso class CatsView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { +) : FrameLayout(context, attrs, defStyleAttr), ICatsView { var presenter :CatsPresenter? = null + private var swipeRefresh: SwipeRefreshLayout? = null override fun onFinishInflate() { super.onFinishInflate() findViewById