From e2168e13b82918602db8cd3ba73f538d6553ea4c Mon Sep 17 00:00:00 2001 From: Petr Date: Sun, 19 Jan 2025 11:54:25 +0400 Subject: [PATCH 1/8] =?UTF-8?q?###=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B9=D1=82?= =?UTF-8?q?=D0=B8=20=D1=81=20=D0=BA=D0=BE=D0=BB=D0=BB=D0=B1=D0=B5=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BD=D0=B0=20=D1=81=D0=B0=D1=81=D0=BF=D0=B5?= =?UTF-8?q?=D0=BD=D0=B4=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=BE=D1=80=D1=83=D1=82=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 3 +- app/src/main/AndroidManifest.xml | 7 +++- .../otus/homework/coroutines/CatsPresenter.kt | 41 +++++++++++++------ .../otus/homework/coroutines/CatsService.kt | 3 +- .../java/otus/homework/coroutines/CatsView.kt | 21 +++++++++- .../otus/homework/coroutines/CrashMonitor.kt | 11 ++++- .../otus/homework/coroutines/DiContainer.kt | 6 +++ .../coroutines/IUserMessageHandler.kt | 10 +++++ .../otus/homework/coroutines/MainActivity2.kt | 34 +++++++++++++++ .../homework/coroutines/PresenterScope.kt | 21 ++++++++++ app/src/main/res/layout/activity_main.xml | 9 ++++ app/src/main/res/layout/activity_main2.xml | 10 +++++ app/src/main/res/values/strings.xml | 4 ++ .../main/res/xml/network_security_config.xml | 9 ++++ 14 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/otus/homework/coroutines/IUserMessageHandler.kt create mode 100644 app/src/main/java/otus/homework/coroutines/MainActivity2.kt create mode 100644 app/src/main/java/otus/homework/coroutines/PresenterScope.kt create mode 100644 app/src/main/res/layout/activity_main2.xml create mode 100644 app/src/main/res/xml/network_security_config.xml diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..8e029959 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { compileSdkVersion 34 defaultConfig { applicationId "otus.homework.coroutines" - minSdkVersion 23 + minSdkVersion 24 targetSdkVersion 34 versionCode 1 versionName "1.0" @@ -33,6 +33,7 @@ android { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0' implementation 'androidx.core:core-ktx:1.12.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fe34985b..8ffb859f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,10 +7,15 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" + android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Coroutines"> - + diff --git a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt index e4b05120..e828121c 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,28 +1,41 @@ package otus.homework.coroutines -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.net.SocketTimeoutException +import kotlin.coroutines.cancellation.CancellationException class CatsPresenter( private val catsService: CatsService ) { private var _catsView: ICatsView? = null + private val presenterScope = PresenterScope(CoroutineName("CatsCoroutine")) + private var workJob: 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()!!) + if (workJob?.isActive == true) { + _catsView?.handle(R.string.cats_wait_next_fact) + return + } + workJob = presenterScope.launch { + try { + val fact = withContext(Dispatchers.IO) { + catsService.getCatFact() } + _catsView?.populate(fact) + } catch (timeOutException: SocketTimeoutException) { + _catsView?.handle(R.string.app_request_timeout) + } catch (_: CancellationException) { + } catch (t: Throwable) { + CrashMonitor.trackError(t) + _catsView?.handle(t.message.toString()) } - - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() - } - }) + } } fun attachView(catsView: ICatsView) { @@ -31,5 +44,7 @@ class CatsPresenter( fun detachView() { _catsView = null + CrashMonitor.trackWarning("Stop detachView") + 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..829b930c 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 diff --git a/app/src/main/java/otus/homework/coroutines/CatsView.kt b/app/src/main/java/otus/homework/coroutines/CatsView.kt index be04b2a8..09babe4d 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -1,10 +1,16 @@ package otus.homework.coroutines import android.content.Context +import android.content.Intent import android.util.AttributeSet import android.widget.Button import android.widget.TextView +import android.widget.Toast +import androidx.annotation.StringRes import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.content.ContextCompat.startActivity +import androidx.core.os.bundleOf + class CatsView @JvmOverloads constructor( context: Context, @@ -19,14 +25,27 @@ class CatsView @JvmOverloads constructor( findViewById