diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..67ea887c 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.4.0" } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatViewViewModel.kt b/app/src/main/java/otus/homework/coroutines/CatViewViewModel.kt new file mode 100644 index 00000000..b412e4b5 --- /dev/null +++ b/app/src/main/java/otus/homework/coroutines/CatViewViewModel.kt @@ -0,0 +1,55 @@ +package otus.homework.coroutines + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.* +import java.net.SocketTimeoutException + +class CatViewViewModel( + private val catsService: CatsService, + private val photoService: PhotoService, +) : ViewModel() { + + private val _catsLiveData = MutableLiveData() + val catsLiveData: LiveData = _catsLiveData + private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> + CrashMonitor.trackWarning() + _catsLiveData.value = + Error( + if (throwable is SocketTimeoutException) TIME_OUT + else throwable.message ?: throwable.toString() + ) + } + + init { + onInitComplete() + } + + fun onInitComplete() { + viewModelScope.launch(exceptionHandler) { + coroutineScope { + val fact = async(Dispatchers.IO) { + catsService.getCatFact() + } + val photo = async(Dispatchers.IO) { + photoService.getPhoto() + } + val factWithPhoto = FactWithPhoto( + fact = fact.await(), + photo = photo.await().first().url + ) + _catsLiveData.value = Success(factWithPhoto) + } + } + } + + companion object { + const val TIME_OUT = "Не удалось получить ответ от сервера" + } +} + +sealed class Result +data class Success(val fact: FactWithPhoto) : Result() +data class Error(val message: String) : Result() \ 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 deleted file mode 100644 index e4b05120..00000000 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ /dev/null @@ -1,35 +0,0 @@ -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() - } - }) - } - - fun attachView(catsView: ICatsView) { - _catsView = catsView - } - - fun detachView() { - _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 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..9e50d05c 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -2,9 +2,10 @@ 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 class CatsView @JvmOverloads constructor( context: Context, @@ -12,21 +13,16 @@ class CatsView @JvmOverloads constructor( defStyleAttr: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { - var presenter :CatsPresenter? = null - - override fun onFinishInflate() { - super.onFinishInflate() - findViewById