Skip to content

Commit 0aec6bb

Browse files
Task Otus-Android#2 completed
1 parent 5b61222 commit 0aec6bb

File tree

8 files changed

+97
-16
lines changed

8 files changed

+97
-16
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package otus.homework.coroutines
2+
3+
import retrofit2.Response
4+
import retrofit2.http.GET
5+
6+
interface CatPictureService {
7+
@GET("meow")
8+
suspend fun getCatPicture() : Response<Picture>
9+
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package otus.homework.coroutines
2+
3+
data class CatViewModel(
4+
val fact: String,
5+
val pictureUrl: String,
6+
)

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import java.net.SocketTimeoutException
66
import kotlin.coroutines.CoroutineContext
77

88
class CatsPresenter(
9-
private val catsService: CatsService
9+
private val catsService: CatsService,
10+
private val catPictureService: CatPictureService,
1011
) {
1112

1213
private var _catsView: ICatsView? = null
@@ -20,14 +21,42 @@ class CatsPresenter(
2021
}
2122
) {
2223
try {
23-
val response = catsService.getCatFact()
24+
//Запускаем запросы одновременно
25+
val deferredFact = async { catsService.getCatFact() }
26+
val deferredPicture = async { catPictureService.getCatPicture() }
2427

25-
if (response.isSuccessful && response.body() != null) {
26-
Log.e("CatsPresenter", "Success!!!")
27-
_catsView?.populate(response.body()!!)
28-
}
29-
else {
30-
throw Exception(if (response.body() == null) "Incorrect data from server" else response.message())
28+
//Ждём выполнение всех запросов
29+
val responseFact = deferredFact.await()
30+
val responsePicture = deferredPicture.await()
31+
32+
if (
33+
responseFact.isSuccessful
34+
&& responseFact.body() != null
35+
36+
&& responsePicture.isSuccessful
37+
&& responsePicture.body() != null
38+
&& (responsePicture.body() as Picture).url.isNotEmpty()
39+
) {
40+
Log.e("CatsPresenter", "requests success!!!")
41+
val fact = responseFact.body() as Fact
42+
val picture = responsePicture.body() as Picture
43+
_catsView?.populate(CatViewModel(fact.text, picture.url))
44+
} else {
45+
throw Exception(
46+
if (responseFact.body() == null || responsePicture.body() == null) {
47+
"Incorrect data from server"
48+
}
49+
else {
50+
if (!responseFact.isSuccessful && responsePicture.isSuccessful) {
51+
responseFact.message()
52+
}
53+
else if (responseFact.isSuccessful && !responsePicture.isSuccessful) {
54+
responsePicture.message()
55+
}
56+
else {
57+
responseFact.message() + " " + responsePicture.message()
58+
}
59+
})
3160
}
3261
}
3362
//Обрабатываем определённые ошибки

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ 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
911

1012
class CatsView @JvmOverloads constructor(
1113
context: Context,
@@ -14,6 +16,7 @@ class CatsView @JvmOverloads constructor(
1416
) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView {
1517

1618
var presenter :CatsPresenter? = null
19+
private val picasso by lazy { Picasso.get() }
1720

1821
override fun onFinishInflate() {
1922
super.onFinishInflate()
@@ -22,8 +25,13 @@ class CatsView @JvmOverloads constructor(
2225
}
2326
}
2427

25-
override fun populate(fact: Fact) {
26-
findViewById<TextView>(R.id.fact_textView).text = fact.text
28+
override fun populate(model: CatViewModel) {
29+
findViewById<TextView>(R.id.fact_textView).text = model.fact
30+
model.pictureUrl.let { url ->
31+
picasso
32+
.load(url)
33+
.into(findViewById<ImageView>(R.id.picture_imageView))
34+
}
2735
}
2836

2937
override fun showToast(text: String) {
@@ -36,7 +44,6 @@ class CatsView @JvmOverloads constructor(
3644
}
3745

3846
interface ICatsView {
39-
40-
fun populate(fact: Fact)
47+
fun populate(model: CatViewModel)
4148
fun showToast(text: String)
4249
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ class DiContainer {
1313
}
1414

1515
val service by lazy { retrofit.create(CatsService::class.java) }
16+
17+
private val retrofitPicture by lazy {
18+
Retrofit.Builder()
19+
.baseUrl("https://aws.random.cat/")
20+
.addConverterFactory(GsonConverterFactory.create())
21+
.build()
22+
}
23+
24+
val servicePicture by lazy { retrofitPicture.create(CatPictureService::class.java) }
25+
1626
}

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

Lines changed: 1 addition & 1 deletion
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.service, diContainer.servicePicture)
1919
view.presenter = catsPresenter
2020
catsPresenter.attachView(view)
2121
catsPresenter.onInitComplete()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package otus.homework.coroutines
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class Picture(
6+
@field:SerializedName("file")
7+
val url: String,
8+
)

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@
77
android:layout_height="match_parent"
88
tools:context=".MainActivity">
99

10+
<ImageView
11+
android:id="@+id/picture_imageView"
12+
android:layout_width="match_parent"
13+
android:layout_height="300dp"
14+
android:scaleType="fitCenter"
15+
app:layout_constraintTop_toTopOf="parent"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
/>
19+
1020
<TextView
1121
android:id="@+id/fact_textView"
1222
android:textColor="@color/black"
1323
android:textSize="24sp"
1424
android:layout_width="wrap_content"
1525
android:layout_height="wrap_content"
16-
app:layout_constraintBottom_toBottomOf="parent"
26+
app:layout_constraintTop_toBottomOf="@+id/picture_imageView"
27+
app:layout_constraintBottom_toTopOf="@+id/button"
1728
app:layout_constraintEnd_toEndOf="parent"
1829
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toTopOf="parent" />
30+
tools:text="Test"/>
2031

2132
<Button
2233
android:id="@+id/button"
@@ -25,6 +36,6 @@
2536
android:text="@string/more_facts"
2637
app:layout_constraintEnd_toEndOf="parent"
2738
app:layout_constraintStart_toStartOf="parent"
28-
app:layout_constraintTop_toBottomOf="@+id/fact_textView" />
39+
app:layout_constraintBottom_toBottomOf="parent"/>
2940

3041
</otus.homework.coroutines.CatsView>

0 commit comments

Comments
 (0)