Skip to content

Home work №2 Shebut Denis #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.8.7")
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Coroutines">
<activity android:name=".MainActivity"
<activity android:name=".presentation.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
35 changes: 0 additions & 35 deletions app/src/main/java/otus/homework/coroutines/CatsPresenter.kt

This file was deleted.

10 changes: 0 additions & 10 deletions app/src/main/java/otus/homework/coroutines/CatsService.kt

This file was deleted.

32 changes: 0 additions & 32 deletions app/src/main/java/otus/homework/coroutines/CatsView.kt

This file was deleted.

16 changes: 0 additions & 16 deletions app/src/main/java/otus/homework/coroutines/DiContainer.kt

This file was deleted.

30 changes: 0 additions & 30 deletions app/src/main/java/otus/homework/coroutines/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package otus.homework.coroutines.data.network

import otus.homework.coroutines.model.CatImageModel
import retrofit2.http.GET

interface CatsImageService {
@GET("images/search")
suspend fun getCatImage(): List<CatImageModel>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package otus.homework.coroutines.data.network

import otus.homework.coroutines.model.Fact
import retrofit2.http.GET

interface CatsService {

@GET("fact")
suspend fun getCatFact() : Fact
}
28 changes: 28 additions & 0 deletions app/src/main/java/otus/homework/coroutines/di/DiContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package otus.homework.coroutines.di

import otus.homework.coroutines.data.network.CatsImageService
import otus.homework.coroutines.data.network.CatsService
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class DiContainer {

private val retrofit by lazy {
Retrofit.Builder()
.baseUrl("https://catfact.ninja/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}

private val imageServiceRetrofit by lazy {
Retrofit.Builder()
.baseUrl("https://api.thecatapi.com/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}

val service: CatsService by lazy { retrofit.create(CatsService::class.java) }
val catsImageService: CatsImageService by lazy {
imageServiceRetrofit.create(CatsImageService::class.java)
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/otus/homework/coroutines/model/CatImageModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package otus.homework.coroutines.model

import com.google.gson.annotations.SerializedName

data class CatImageModel(
@field:SerializedName("id")
val id: String?,
@field:SerializedName("url")
val url: String?,
@field:SerializedName("width")
val width: Int?,
@field:SerializedName("height")
val height: Int?,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package otus.homework.coroutines
package otus.homework.coroutines.model

import com.google.gson.annotations.SerializedName

data class Fact(
@field:SerializedName("fact")
val fact: String,
val fact: String?,
@field:SerializedName("length")
val length: Int
val length: Int?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package otus.homework.coroutines.presentation

data class CatsFactState(
val fact: String,
val factLength: Int,
val imageUrl: String,
) {
companion object {
val INIT = CatsFactState(
fact = "",
factLength = 0,
imageUrl = "",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package otus.homework.coroutines.presentation

import android.content.Context
import android.widget.Toast
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import otus.homework.coroutines.data.network.CatsImageService
import otus.homework.coroutines.data.network.CatsService
import otus.homework.coroutines.R
import otus.homework.coroutines.utils.CrashMonitor
import java.net.SocketTimeoutException

class CatsPresenter(
private val catsImageService: CatsImageService,
private val catsService: CatsService,
private val applicationContext: Context,
) {

private val presenterScope = CoroutineScope(Dispatchers.Main + CoroutineName("CatsCoroutine"))
private var fetchJob: Job? = null

private var _catsView: ICatsView? = null

fun onInitComplete() {
fetchJob = presenterScope.launch {
try {
val fact = catsService.getCatFact()
val image = catsImageService.getCatImage()
// _catsView?.render(
// CatsFactState(
// fact = fact.fact.orEmpty(),
// factLength = fact.length ?: 0,
// imageUrl = image[0].url.orEmpty(),
// isLoading = false
// )
// )
} catch (e: SocketTimeoutException) {
Toast.makeText(
applicationContext,
applicationContext.getString(R.string.connection_timeout_error),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
CrashMonitor.trackWarning()
Toast.makeText(
applicationContext,
e.message,
Toast.LENGTH_LONG
).show()
}
}
}

fun attachView(catsView: ICatsView) {
_catsView = catsView
}

fun detachView() {
_catsView = null
}

fun onStop() {
fetchJob?.cancel()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package otus.homework.coroutines.presentation

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
import otus.homework.coroutines.R
import otus.homework.coroutines.utils.Result

class CatsView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView {

//var presenter: CatsPresenter? = null
private var onRefresh: (() -> Unit)? = null

fun setRefreshListener(l: () -> Unit) {
onRefresh = l
}

override fun onFinishInflate() {
super.onFinishInflate()
findViewById<Button>(R.id.button).setOnClickListener {
onRefresh?.invoke()
}
}

override fun render(state: Result) {
when (state) {
is Result.Loading -> {
findViewById<Button>(R.id.button).isEnabled = false
}

is Result.Error -> {
val errorMessage = findViewById<TextView>(R.id.error_textView)
errorMessage.visibility = VISIBLE
errorMessage.text = state.message
findViewById<Button>(R.id.button).isEnabled = true
}

is Result.Success<*> -> {
val data = (state.data as? CatsFactState)
findViewById<TextView>(R.id.fact_textView).text = data?.fact
if (data?.imageUrl?.isNotEmpty() == true) {
Picasso.get()
.load(data.imageUrl)
.into(findViewById<ImageView>(R.id.cat_imageView))
}
findViewById<Button>(R.id.button).isEnabled = true
findViewById<TextView>(R.id.error_textView).visibility = GONE
}
}
}
}

interface ICatsView {

fun render(state: Result)
}
Loading