diff --git a/app/build.gradle b/app/build.gradle index a414e0e8..d05c1e91 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-android:2.9.2' } \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/Cat.kt b/app/src/main/java/otus/homework/coroutines/Cat.kt new file mode 100644 index 00000000..125caf3a --- /dev/null +++ b/app/src/main/java/otus/homework/coroutines/Cat.kt @@ -0,0 +1,8 @@ +package otus.homework.coroutines + +import android.media.Image + +data class Cat( + val fact: String, + val imageUrl: String +) \ No newline at end of file diff --git a/app/src/main/java/otus/homework/coroutines/CatImage.kt b/app/src/main/java/otus/homework/coroutines/CatImage.kt new file mode 100644 index 00000000..cac27f78 --- /dev/null +++ b/app/src/main/java/otus/homework/coroutines/CatImage.kt @@ -0,0 +1,14 @@ +package otus.homework.coroutines + +import com.google.gson.annotations.SerializedName + +data class CatImage( + @field:SerializedName("id") + val id: String, + @field:SerializedName("url") + val url: String, + @field:SerializedName("width") + val width: Int, + @field:SerializedName("height") + val height: Int +) \ 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..b478fdc9 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsPresenter.kt @@ -1,35 +1,58 @@ -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 +//package otus.homework.coroutines +// +//import kotlinx.coroutines.CoroutineName +//import kotlinx.coroutines.CoroutineScope +//import kotlinx.coroutines.Dispatchers +//import kotlinx.coroutines.Job +//import kotlinx.coroutines.async +//import kotlinx.coroutines.launch +//import java.net.SocketTimeoutException +//import kotlin.coroutines.cancellation.CancellationException +// +// +//class CatsPresenter( +// private val catsService: CatsService, +// private val catsImagesService: CatsImageService, +// private val presenterScope: CoroutineScope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine")) +//) { +// +// private var _catsView: ICatsView? = null +// private var job: Job? = null +// +// fun onInitComplete() { +// job = presenterScope.launch { +// try { +// val fact = async { catsService.getCatFact() }.await() +// val catImage:CatImage = async { catsImagesService.getCatImage()[0] }.await() +// _catsView?.populate(Cat(fact.toString(), catImage.url)) +// +// } catch (ex: Exception) { +// +// if (ex is CancellationException) throw ex +// when (ex) { +// +// is SocketTimeoutException -> { +// _catsView?.onError("Unable to get response from server") +// } +// else -> { +// CrashMonitor.trackWarning(ex.message.toString()) +// _catsView?.onError(ex.message.toString()) +// } +// } +// } +// } +// } +// +// fun onStop() { +// job?.cancel() +// } +// +// +// fun attachView(catsView: ICatsView) { +// _catsView = catsView +// } +// +// fun detachView() { +// _catsView = null +// } +//} diff --git a/app/src/main/java/otus/homework/coroutines/CatsService.kt b/app/src/main/java/otus/homework/coroutines/CatsService.kt index 479b2cfb..6e322a0c 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsService.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsService.kt @@ -1,10 +1,17 @@ package otus.homework.coroutines -import retrofit2.Call + import retrofit2.http.GET interface CatsService { @GET("fact") - fun getCatFact() : Call -} \ No newline at end of file + suspend fun getCatFact() : Fact +} + +interface CatsImageService { + + @GET("v1/images/search") + suspend fun getCatImage() : List + +} diff --git a/app/src/main/java/otus/homework/coroutines/CatsView.kt b/app/src/main/java/otus/homework/coroutines/CatsView.kt index be04b2a8..5266fe54 100644 --- a/app/src/main/java/otus/homework/coroutines/CatsView.kt +++ b/app/src/main/java/otus/homework/coroutines/CatsView.kt @@ -2,31 +2,49 @@ package otus.homework.coroutines import android.content.Context import android.util.AttributeSet +import android.util.Log import android.widget.Button +import android.widget.ImageView import android.widget.TextView +import android.widget.Toast +import com.squareup.picasso.Picasso import androidx.constraintlayout.widget.ConstraintLayout + class CatsView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView { - var presenter :CatsPresenter? = null +// var presenter: CatsPresenter? = null + + override fun populate(cat: Cat) { + findViewById(R.id.fact_textView).text = cat.fact + Picasso.get().load(cat.imageUrl).into(findViewById(R.id.catImageView)); + } + + override fun onError(text: String) { + Toast.makeText(context, text, Toast.LENGTH_SHORT).show() + } - override fun onFinishInflate() { - super.onFinishInflate() + fun showErrorToast(errorMsg: String) { + Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show() + } + + fun setOnButtonClick(onButtonClick: () -> Unit) { findViewById