diff --git a/app/build.gradle b/app/build.gradle index 679dbba4..a92a987c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,4 +42,12 @@ dependencies { implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.squareup.picasso:picasso:2.71828' + +//coroutine retrofit + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines" + implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" + + implementation "com.squareup.okhttp3:logging-interceptor:4.2.2" } \ 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..dc4d4cf4 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 android.util.Log +import kotlinx.coroutines.* import retrofit2.Response +import java.net.SocketTimeoutException +import kotlin.coroutines.CoroutineContext class CatsPresenter( - private val catsService: CatsService + private val catsServiceFact: CatsService, + private val catsServiceImage: CatsService ) { private var _catsView: ICatsView? = null + private val presenterScope = + PresenterScope(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 factResponse = withContext(Dispatchers.IO) { catsServiceFact.getCatFact() } + val imageResponse = withContext(Dispatchers.IO) { catsServiceImage.getCatImage() } + val factImage = FactImage(factResponse, imageResponse) + _catsView?.populate(factImage) + } catch (e: Exception) { + when (e) { + is SocketTimeoutException -> { + _catsView?.showToast("Не удалось получить ответ от сервером") + } + else -> { + _catsView?.showToast(e.message.toString()) + CrashMonitor.trackWarning() + e.printStackTrace() + } } } - - override fun onFailure(call: Call, t: Throwable) { - CrashMonitor.trackWarning() - } - }) + } } fun attachView(catsView: ICatsView) { @@ -31,5 +43,14 @@ class CatsPresenter( fun detachView() { _catsView = null + presenterScope.cancel() } +} + +class PresenterScope( + private val dispatchers: CoroutineDispatcher, + private val coroutineName: CoroutineName +) : CoroutineScope { + override val coroutineContext: CoroutineContext + get() = dispatchers + coroutineName } \ 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..dcfe7e56 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 kotlinx.coroutines.Deferred import retrofit2.Call +import retrofit2.Response import retrofit2.http.GET interface CatsService { @GET("random?animal_type=cat") - fun getCatFact() : Call + suspend fun getCatFact() : Fact + + @GET("meow") + suspend fun getCatImage() : Image } \ 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..9511036c 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 android.widget.Toast +import android.widget.Toast.LENGTH_LONG import androidx.constraintlayout.widget.ConstraintLayout +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import com.squareup.picasso.Picasso class CatsView @JvmOverloads constructor( context: Context, @@ -12,21 +17,34 @@ class CatsView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { - var presenter :CatsPresenter? = null + var presenter: CatsPresenter? = null + private var refreshLayout: SwipeRefreshLayout? = null + override fun onFinishInflate() { super.onFinishInflate() findViewById