File tree Expand file tree Collapse file tree 4 files changed +19
-17
lines changed
app/src/main/java/otus/homework/coroutines Expand file tree Collapse file tree 4 files changed +19
-17
lines changed Original file line number Diff line number Diff line change 1
1
package otus.homework.coroutines
2
2
3
+ import kotlinx.coroutines.CoroutineScope
4
+ import kotlinx.coroutines.launch
5
+
3
6
class CatsPresenter (
4
- private val catsService : CatsService
7
+ private val catsService : CatsService ,
8
+ private val presenterScope : CoroutineScope
5
9
) {
6
10
7
11
private var _catsView : ICatsView ? = null
8
12
9
- suspend fun onInitComplete () {
10
- catsService.getCatFact().also { fact ->
11
- _catsView ?.populate(fact)
13
+ fun onInitComplete () {
14
+ presenterScope.launch {
15
+ catsService.getCatFact().also { fact ->
16
+ _catsView ?.populate(fact)
17
+ }
12
18
}
13
19
}
14
20
Original file line number Diff line number Diff line change @@ -5,9 +5,6 @@ import android.util.AttributeSet
5
5
import android.widget.Button
6
6
import android.widget.TextView
7
7
import androidx.constraintlayout.widget.ConstraintLayout
8
- import androidx.lifecycle.findViewTreeLifecycleOwner
9
- import androidx.lifecycle.lifecycleScope
10
- import kotlinx.coroutines.launch
11
8
12
9
class CatsView @JvmOverloads constructor(
13
10
context : Context ,
@@ -20,9 +17,7 @@ class CatsView @JvmOverloads constructor(
20
17
override fun onFinishInflate () {
21
18
super .onFinishInflate()
22
19
findViewById<Button >(R .id.button).setOnClickListener {
23
- findViewTreeLifecycleOwner()?.lifecycleScope?.launch {
24
- presenter?.onInitComplete()
25
- }
20
+ presenter?.onInitComplete()
26
21
}
27
22
}
28
23
Original file line number Diff line number Diff line change 1
1
package otus.homework.coroutines
2
2
3
+ import kotlinx.coroutines.CoroutineName
4
+ import kotlinx.coroutines.CoroutineScope
5
+ import kotlinx.coroutines.Dispatchers
3
6
import retrofit2.Retrofit
4
7
import retrofit2.converter.gson.GsonConverterFactory
5
8
@@ -13,4 +16,6 @@ class DiContainer {
13
16
}
14
17
15
18
val service by lazy { retrofit.create(CatsService ::class .java) }
19
+
20
+ val presenterScope get() = CoroutineScope (CoroutineName (" CatsCoroutine" ) + Dispatchers .Main )
16
21
}
Original file line number Diff line number Diff line change 1
1
package otus.homework.coroutines
2
2
3
- import androidx.appcompat.app.AppCompatActivity
4
3
import android.os.Bundle
5
- import androidx.lifecycle.lifecycleScope
6
- import kotlinx.coroutines.launch
4
+ import androidx.appcompat.app.AppCompatActivity
7
5
8
6
class MainActivity : AppCompatActivity () {
9
7
@@ -17,12 +15,10 @@ class MainActivity : AppCompatActivity() {
17
15
val view = layoutInflater.inflate(R .layout.activity_main, null ) as CatsView
18
16
setContentView(view)
19
17
20
- catsPresenter = CatsPresenter (diContainer.service)
18
+ catsPresenter = CatsPresenter (diContainer.service, diContainer.presenterScope )
21
19
view.presenter = catsPresenter
22
20
catsPresenter.attachView(view)
23
- lifecycleScope.launch {
24
- catsPresenter.onInitComplete()
25
- }
21
+ catsPresenter.onInitComplete()
26
22
}
27
23
28
24
override fun onStop () {
You can’t perform that action at this time.
0 commit comments