Skip to content

Commit f52279b

Browse files
committed
Otus-Android#2 added UI state
1 parent 6a61e84 commit f52279b

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import kotlinx.coroutines.Job
66
import kotlinx.coroutines.launch
77
import java.net.SocketTimeoutException
88

9+
data class CatsUIState(
10+
val fact: Fact,
11+
val image: Image
12+
)
13+
914
class CatsPresenter(
1015
private val catsService: CatsService,
16+
private val imageService: ImageService,
1117
private val presenterScope: CoroutineScope
1218
) {
1319

@@ -17,12 +23,10 @@ class CatsPresenter(
1723
fun onInitComplete() {
1824
_job = presenterScope.launch {
1925
try {
20-
// imageService.getCatImage().also {
21-
// println("image url ${it.url}")
22-
// }
23-
catsService.getCatFact().also { fact ->
24-
_catsView?.populate(fact)
25-
}
26+
val image = imageService.getCatImage().first()
27+
val fact = catsService.getCatFact()
28+
29+
_catsView?.populate(CatsUIState(fact, image))
2630
} catch (e: CancellationException) {
2731
throw e
2832
} catch (e: SocketTimeoutException) {

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

Lines changed: 6 additions & 3 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
private val context: Context,
@@ -22,8 +24,9 @@ class CatsView @JvmOverloads constructor(
2224
}
2325
}
2426

25-
override fun populate(fact: Fact) {
26-
findViewById<TextView>(R.id.fact_textView).text = fact.fact
27+
override fun populate(state: CatsUIState) {
28+
findViewById<TextView>(R.id.fact_textView).text = state.fact.fact
29+
Picasso.get().load(state.image.url).into(findViewById<ImageView>(R.id.catImage))
2730
}
2831

2932
override fun showToast(message: String) {
@@ -33,7 +36,7 @@ class CatsView @JvmOverloads constructor(
3336

3437
interface ICatsView {
3538

36-
fun populate(fact: Fact)
39+
fun populate(state: CatsUIState)
3740

3841
fun showToast(message: String)
3942
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import retrofit2.http.GET
44

55
interface ImageService {
66
@GET("search")
7-
suspend fun getCatImage(): Image
7+
suspend fun getCatImage(): List<Image>
88
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class MainActivity : AppCompatActivity() {
1717

1818
catsPresenter = CatsPresenter(
1919
diContainer.catsService,
20+
diContainer.imageService,
2021
diContainer.presenterScope
2122
)
2223
view.presenter = catsPresenter

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

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

10+
<ImageView
11+
android:id="@+id/catImage"
12+
android:layout_width="300dp"
13+
android:layout_height="300dp"
14+
android:contentDescription="@string/cat_image_description"
15+
app:layout_constraintTop_toTopOf="parent"
16+
app:layout_constraintBottom_toTopOf="@id/fact_textView"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintEnd_toEndOf="parent"
19+
/>
20+
1021
<TextView
1122
android:id="@+id/fact_textView"
1223
android:textColor="@color/black"
1324
android:textSize="24sp"
1425
android:layout_width="wrap_content"
1526
android:layout_height="wrap_content"
16-
app:layout_constraintBottom_toBottomOf="parent"
1727
app:layout_constraintEnd_toEndOf="parent"
1828
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toTopOf="parent" />
29+
app:layout_constraintTop_toBottomOf="@id/catImage" />
2030

2131
<Button
2232
android:id="@+id/button"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">Cat Facts </string>
33
<string name="more_facts">More Facts</string>
4+
<string name="cat_image_description">Random cat image</string>
45
</resources>

0 commit comments

Comments
 (0)