Skip to content

Commit 7ae56ac

Browse files
vsokolikVera Sokolova
authored and
Vera Sokolova
committed
[2] image loading
1 parent d0c1ebb commit 7ae56ac

File tree

11 files changed

+118
-17
lines changed

11 files changed

+118
-17
lines changed

app/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ dependencies {
4343
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4444
implementation 'com.squareup.picasso:picasso:2.71828'
4545
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.2'
46+
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3'
47+
testImplementation 'junit:junit:4.12'
4648
}

app/src/main/java/otus/homework/coroutines/CatsService.kt renamed to app/src/main/java/otus/homework/coroutines/CatsFactService.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package otus.homework.coroutines
22

3+
import otus.homework.coroutines.model.Fact
34
import retrofit2.Response
45
import retrofit2.http.GET
56

6-
interface CatsService {
7+
interface CatsFactService {
78

89
@GET("fact")
910
suspend fun getCatFact() : Response<Fact>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package otus.homework.coroutines
2+
3+
import otus.homework.coroutines.model.RandomCat
4+
import retrofit2.Response
5+
import retrofit2.http.GET
6+
7+
interface CatsImageService {
8+
9+
@GET("cat?json=true")
10+
suspend fun getCatImage() : Response<RandomCat>
11+
12+
companion object {
13+
const val BASE_URL = "https://cataas.com/"
14+
}
15+
}

app/src/main/java/otus/homework/coroutines/CatsPresenter.kt

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package otus.homework.coroutines
22

33
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.async
45
import kotlinx.coroutines.cancel
56
import kotlinx.coroutines.launch
67
import kotlinx.coroutines.runBlocking
8+
import otus.homework.coroutines.model.CatModel
79
import java.lang.Exception
810
import java.net.SocketTimeoutException
911

1012
class CatsPresenter(
11-
private val catsService: CatsService
13+
private val factService: CatsFactService,
14+
private val imageService: CatsImageService
1215
) {
1316

1417
private var _catsView: ICatsView? = null
@@ -18,13 +21,17 @@ class CatsPresenter(
1821
runBlocking(Dispatchers.IO) {
1922
catsScope.launch {
2023
try {
21-
val response = catsService.getCatFact()
22-
if (response.isSuccessful && response.body() != null) {
23-
_catsView?.populate(response.body()!!)
24-
}
24+
val catFactJob = async { factService.getCatFact() }
25+
val catImageJob = async { imageService.getCatImage() }
26+
27+
val catModel = CatModel(
28+
catFactJob.await().body()?.fact,
29+
CatsImageService.BASE_URL + catImageJob.await().body()?.url)
30+
31+
_catsView?.populate(catModel)
2532
} catch (e: Exception) {
2633
if (e is SocketTimeoutException) {
27-
_catsView?.showToast("Не удалось получить ответ от сервером")
34+
_catsView?.showToast("Не удалось получить ответ от сервера")
2835
} else {
2936
CrashMonitor.trackWarning(e.message.toString())
3037
_catsView?.showToast(e.message.toString())

app/src/main/java/otus/homework/coroutines/CatsView.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package otus.homework.coroutines
33
import android.content.Context
44
import android.util.AttributeSet
55
import android.widget.Button
6+
import android.widget.ImageView
67
import android.widget.TextView
78
import android.widget.Toast
89
import androidx.constraintlayout.widget.ConstraintLayout
10+
import com.squareup.picasso.Picasso
11+
import otus.homework.coroutines.model.CatModel
912

1013
class CatsView @JvmOverloads constructor(
1114
context: Context,
@@ -22,8 +25,10 @@ class CatsView @JvmOverloads constructor(
2225
}
2326
}
2427

25-
override fun populate(fact: Fact) {
26-
findViewById<TextView>(R.id.fact_textView).text = fact.fact
28+
override fun populate(catModel: CatModel) {
29+
findViewById<TextView>(R.id.fact_textView).text = catModel.fact
30+
val imageView = findViewById<ImageView>(R.id.random_cat)
31+
Picasso.get().load(catModel.imageUrl).into(imageView)
2732
}
2833

2934
override fun showToast(message: String) {
@@ -33,6 +38,6 @@ class CatsView @JvmOverloads constructor(
3338

3439
interface ICatsView {
3540

36-
fun populate(fact: Fact)
41+
fun populate(catModel: CatModel)
3742
fun showToast(message: String)
3843
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
package otus.homework.coroutines
22

3+
import com.google.gson.GsonBuilder
4+
import okhttp3.OkHttpClient
5+
import okhttp3.logging.HttpLoggingInterceptor
36
import retrofit2.Retrofit
47
import retrofit2.converter.gson.GsonConverterFactory
8+
import java.util.concurrent.TimeUnit
59

610
class DiContainer {
711

8-
private val retrofit by lazy {
12+
val serviceFact: CatsFactService by lazy { retrofitCatFact.create(CatsFactService::class.java) }
13+
val serviceImage: CatsImageService by lazy { retrofitCatImage.create(CatsImageService::class.java) }
14+
15+
private val interceptor = run {
16+
val httpLoggingInterceptor = HttpLoggingInterceptor()
17+
httpLoggingInterceptor.apply {
18+
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
19+
}
20+
}
21+
22+
private val okHttpClient = OkHttpClient.Builder()
23+
.addNetworkInterceptor(interceptor) // same for .addInterceptor(...)
24+
.connectTimeout(30, TimeUnit.SECONDS) //Backend is really slow
25+
.writeTimeout(30, TimeUnit.SECONDS)
26+
.readTimeout(30, TimeUnit.SECONDS)
27+
.build()
28+
29+
private val retrofitCatFact by lazy {
930
Retrofit.Builder()
31+
.client(okHttpClient)
1032
.baseUrl("https://catfact.ninja/")
1133
.addConverterFactory(GsonConverterFactory.create())
1234
.build()
1335
}
1436

15-
val service by lazy { retrofit.create(CatsService::class.java) }
37+
private val gson = GsonBuilder()
38+
.setLenient()
39+
.create()
40+
41+
private val retrofitCatImage by lazy {
42+
Retrofit.Builder()
43+
.client(okHttpClient)
44+
.baseUrl(CatsImageService.BASE_URL)
45+
.addConverterFactory(GsonConverterFactory.create(gson))
46+
.build()
47+
}
1648
}

app/src/main/java/otus/homework/coroutines/MainActivity.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MainActivity : AppCompatActivity() {
1515
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
1616
setContentView(view)
1717

18-
catsPresenter = CatsPresenter(diContainer.service)
18+
catsPresenter = CatsPresenter(diContainer.serviceFact, diContainer.serviceImage)
1919
view.presenter = catsPresenter
2020
catsPresenter.attachView(view)
2121
catsPresenter.onInitComplete()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package otus.homework.coroutines.model
2+
3+
data class CatModel(
4+
val fact: String?,
5+
val imageUrl: String?
6+
)

app/src/main/java/otus/homework/coroutines/Fact.kt renamed to app/src/main/java/otus/homework/coroutines/model/Fact.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package otus.homework.coroutines
1+
package otus.homework.coroutines.model
22

33
import com.google.gson.annotations.SerializedName
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package otus.homework.coroutines.model
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class RandomCat(
6+
@field:SerializedName("_id")
7+
val id: String,
8+
@field:SerializedName("tags")
9+
val tags: List<String>,
10+
@field:SerializedName("owner")
11+
val owner: String,
12+
@field:SerializedName("createdAt")
13+
val createdAt: String,
14+
@field:SerializedName("updatedAt")
15+
val updatedAt: String,
16+
@field:SerializedName("url")
17+
val url: String,
18+
@field:SerializedName("size")
19+
val size: Int,
20+
@field:SerializedName("mimetype")
21+
val mimetype: String,
22+
@field:SerializedName("file")
23+
val file: String,
24+
@field:SerializedName("validated")
25+
val validated: Boolean
26+
)

app/src/main/res/layout/activity_main.xml

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77
android:layout_height="match_parent"
88
tools:context=".MainActivity">
99

10+
<ImageView
11+
android:id="@+id/random_cat"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
app:layout_constraintBottom_toTopOf="@+id/fact_textView"
15+
app:layout_constraintEnd_toEndOf="parent"
16+
app:layout_constraintStart_toStartOf="parent"/>
17+
1018
<TextView
1119
android:id="@+id/fact_textView"
12-
android:textColor="@color/black"
13-
android:textSize="24sp"
1420
android:layout_width="wrap_content"
1521
android:layout_height="wrap_content"
22+
android:textColor="@color/black"
23+
android:textSize="24sp"
1624
app:layout_constraintBottom_toBottomOf="parent"
1725
app:layout_constraintEnd_toEndOf="parent"
1826
app:layout_constraintStart_toStartOf="parent"
@@ -26,5 +34,4 @@
2634
app:layout_constraintEnd_toEndOf="parent"
2735
app:layout_constraintStart_toStartOf="parent"
2836
app:layout_constraintTop_toBottomOf="@+id/fact_textView" />
29-
3037
</otus.homework.coroutines.CatsView>

0 commit comments

Comments
 (0)