Skip to content

Commit d0c1ebb

Browse files
vsokolikVera Sokolova
authored and
Vera Sokolova
committed
[1] added suspend function, presenter scope and exception logging
1 parent 962c942 commit d0c1ebb

File tree

9 files changed

+69
-43
lines changed

9 files changed

+69
-43
lines changed

app/build.gradle

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ plugins {
44
}
55

66
android {
7-
compileSdkVersion 30
7+
compileSdkVersion 34
88
buildToolsVersion "30.0.3"
99

1010
defaultConfig {
1111
applicationId "otus.homework.coroutines"
1212
minSdkVersion 23
13-
targetSdkVersion 30
13+
targetSdkVersion 34
1414
versionCode 1
1515
versionName "1.0"
1616

@@ -34,12 +34,13 @@ android {
3434

3535
dependencies {
3636
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
37-
implementation 'androidx.core:core-ktx:1.3.2'
37+
implementation 'androidx.core:core-ktx:1.10.1'
3838
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
3939
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
40-
implementation 'com.google.code.gson:gson:2.8.6'
41-
implementation 'androidx.appcompat:appcompat:1.2.0'
42-
implementation 'com.google.android.material:material:1.3.0'
43-
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
40+
implementation 'com.google.code.gson:gson:2.10.1'
41+
implementation 'androidx.appcompat:appcompat:1.6.1'
42+
implementation 'com.google.android.material:material:1.9.0'
43+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4444
implementation 'com.squareup.picasso:picasso:2.71828'
45+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.2'
4546
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
package otus.homework.coroutines
22

3-
import retrofit2.Call
4-
import retrofit2.Callback
5-
import retrofit2.Response
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.cancel
5+
import kotlinx.coroutines.launch
6+
import kotlinx.coroutines.runBlocking
7+
import java.lang.Exception
8+
import java.net.SocketTimeoutException
69

710
class CatsPresenter(
811
private val catsService: CatsService
912
) {
1013

1114
private var _catsView: ICatsView? = null
15+
private val catsScope = PresenterScope()
1216

1317
fun onInitComplete() {
14-
catsService.getCatFact().enqueue(object : Callback<Fact> {
15-
16-
override fun onResponse(call: Call<Fact>, response: Response<Fact>) {
17-
if (response.isSuccessful && response.body() != null) {
18-
_catsView?.populate(response.body()!!)
18+
runBlocking(Dispatchers.IO) {
19+
catsScope.launch {
20+
try {
21+
val response = catsService.getCatFact()
22+
if (response.isSuccessful && response.body() != null) {
23+
_catsView?.populate(response.body()!!)
24+
}
25+
} catch (e: Exception) {
26+
if (e is SocketTimeoutException) {
27+
_catsView?.showToast("Не удалось получить ответ от сервером")
28+
} else {
29+
CrashMonitor.trackWarning(e.message.toString())
30+
_catsView?.showToast(e.message.toString())
31+
}
1932
}
2033
}
21-
22-
override fun onFailure(call: Call<Fact>, t: Throwable) {
23-
CrashMonitor.trackWarning()
24-
}
25-
})
34+
}
2635
}
2736

2837
fun attachView(catsView: ICatsView) {
@@ -32,4 +41,8 @@ class CatsPresenter(
3241
fun detachView() {
3342
_catsView = null
3443
}
44+
45+
fun onStop(){
46+
catsScope.cancel()
47+
}
3548
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package otus.homework.coroutines
22

3-
import retrofit2.Call
3+
import retrofit2.Response
44
import retrofit2.http.GET
55

66
interface CatsService {
77

88
@GET("fact")
9-
fun getCatFact() : Call<Fact>
9+
suspend fun getCatFact() : Response<Fact>
1010
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.util.AttributeSet
55
import android.widget.Button
66
import android.widget.TextView
7+
import android.widget.Toast
78
import androidx.constraintlayout.widget.ConstraintLayout
89

910
class CatsView @JvmOverloads constructor(
@@ -22,11 +23,16 @@ class CatsView @JvmOverloads constructor(
2223
}
2324

2425
override fun populate(fact: Fact) {
25-
findViewById<TextView>(R.id.fact_textView).text = fact.text
26+
findViewById<TextView>(R.id.fact_textView).text = fact.fact
27+
}
28+
29+
override fun showToast(message: String) {
30+
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
2631
}
2732
}
2833

2934
interface ICatsView {
3035

3136
fun populate(fact: Fact)
37+
fun showToast(message: String)
3238
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package otus.homework.coroutines
22

3+
import android.util.Log
4+
35
object CrashMonitor {
46

57
/**
68
* Pretend this is Crashlytics/AppCenter
79
*/
8-
fun trackWarning() {
10+
private val crashesList = ArrayList<String>()
11+
12+
fun trackWarning(message: String) {
13+
crashesList.add(message)
14+
Log.e("crash_mirror", message)
915
}
1016
}

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

+4-18
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,8 @@ package otus.homework.coroutines
33
import com.google.gson.annotations.SerializedName
44

55
data class Fact(
6-
@field:SerializedName("createdAt")
7-
val createdAt: String,
8-
@field:SerializedName("deleted")
9-
val deleted: Boolean,
10-
@field:SerializedName("_id")
11-
val id: String,
12-
@field:SerializedName("text")
13-
val text: String,
14-
@field:SerializedName("source")
15-
val source: String,
16-
@field:SerializedName("used")
17-
val used: Boolean,
18-
@field:SerializedName("type")
19-
val type: String,
20-
@field:SerializedName("user")
21-
val user: String,
22-
@field:SerializedName("updatedAt")
23-
val updatedAt: String
6+
@field:SerializedName("fact")
7+
val fact: String,
8+
@field:SerializedName("length")
9+
val length: Int
2410
)

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

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class MainActivity : AppCompatActivity() {
2525
if (isFinishing) {
2626
catsPresenter.detachView()
2727
}
28+
catsPresenter.onStop()
2829
super.onStop()
2930
}
3031
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package otus.homework.coroutines
2+
3+
import kotlinx.coroutines.CoroutineName
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.Job
7+
import kotlin.coroutines.CoroutineContext
8+
9+
class PresenterScope : CoroutineScope {
10+
11+
override val coroutineContext: CoroutineContext
12+
get() = Job() + Dispatchers.Main + CoroutineName("CatsCoroutine")
13+
}

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
3-
ext.kotlin_version = "1.6.20"
3+
ext.kotlin_version = "1.8.20"
44
repositories {
55
mavenCentral()
66
google()

0 commit comments

Comments
 (0)