Skip to content

Commit 8b06611

Browse files
committed
Otus-Android#1 added try-catch
1 parent d921e20 commit 8b06611

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
package otus.homework.coroutines
22

3+
import kotlinx.coroutines.CancellationException
34
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Job
46
import kotlinx.coroutines.launch
7+
import java.net.SocketTimeoutException
58

69
class CatsPresenter(
710
private val catsService: CatsService,
811
private val presenterScope: CoroutineScope
912
) {
1013

1114
private var _catsView: ICatsView? = null
15+
private var _job: Job? = null
1216

1317
fun onInitComplete() {
14-
presenterScope.launch {
15-
catsService.getCatFact().also { fact ->
16-
_catsView?.populate(fact)
18+
_job = presenterScope.launch {
19+
try {
20+
catsService.getCatFact().also { fact ->
21+
_catsView?.populate(fact)
22+
}
23+
} catch (e: CancellationException) {
24+
throw e
25+
} catch (e: SocketTimeoutException) {
26+
showToast("Не удалось получить ответ от сервера")
27+
} catch (e: Exception) {
28+
CrashMonitor.trackWarning(e)
29+
e.message?.let(::showToast)
1730
}
1831
}
1932
}
2033

34+
private fun showToast(message: String) {
35+
_catsView?.showToast(message)
36+
}
37+
2138
fun attachView(catsView: ICatsView) {
2239
_catsView = catsView
2340
}
2441

2542
fun detachView() {
2643
_catsView = null
2744
}
45+
46+
fun cancelCoroutine() {
47+
_job?.run {
48+
if (isActive) {
49+
cancel()
50+
}
51+
}
52+
}
2853
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ 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(
10-
context: Context,
11+
private val context: Context,
1112
attrs: AttributeSet? = null,
1213
defStyleAttr: Int = 0
1314
) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView {
@@ -24,9 +25,15 @@ class CatsView @JvmOverloads constructor(
2425
override fun populate(fact: Fact) {
2526
findViewById<TextView>(R.id.fact_textView).text = fact.fact
2627
}
28+
29+
override fun showToast(message: String) {
30+
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
31+
}
2732
}
2833

2934
interface ICatsView {
3035

3136
fun populate(fact: Fact)
37+
38+
fun showToast(message: String)
3239
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ object CrashMonitor {
55
/**
66
* Pretend this is Crashlytics/AppCenter
77
*/
8-
fun trackWarning() {
9-
}
8+
fun trackWarning(exception: Exception) {}
109
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class MainActivity : AppCompatActivity() {
2323

2424
override fun onStop() {
2525
if (isFinishing) {
26-
catsPresenter.detachView()
26+
catsPresenter.apply {
27+
detachView()
28+
cancelCoroutine()
29+
}
2730
}
2831
super.onStop()
2932
}

0 commit comments

Comments
 (0)