Skip to content

Commit d921e20

Browse files
committed
Otus-Android#1 added PresenterScope
1 parent 1e15458 commit d921e20

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package otus.homework.coroutines
22

3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.launch
5+
36
class CatsPresenter(
4-
private val catsService: CatsService
7+
private val catsService: CatsService,
8+
private val presenterScope: CoroutineScope
59
) {
610

711
private var _catsView: ICatsView? = null
812

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+
}
1218
}
1319
}
1420

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import android.util.AttributeSet
55
import android.widget.Button
66
import android.widget.TextView
77
import androidx.constraintlayout.widget.ConstraintLayout
8-
import androidx.lifecycle.findViewTreeLifecycleOwner
9-
import androidx.lifecycle.lifecycleScope
10-
import kotlinx.coroutines.launch
118

129
class CatsView @JvmOverloads constructor(
1310
context: Context,
@@ -20,9 +17,7 @@ class CatsView @JvmOverloads constructor(
2017
override fun onFinishInflate() {
2118
super.onFinishInflate()
2219
findViewById<Button>(R.id.button).setOnClickListener {
23-
findViewTreeLifecycleOwner()?.lifecycleScope?.launch {
24-
presenter?.onInitComplete()
25-
}
20+
presenter?.onInitComplete()
2621
}
2722
}
2823

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package otus.homework.coroutines
22

3+
import kotlinx.coroutines.CoroutineName
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
36
import retrofit2.Retrofit
47
import retrofit2.converter.gson.GsonConverterFactory
58

@@ -13,4 +16,6 @@ class DiContainer {
1316
}
1417

1518
val service by lazy { retrofit.create(CatsService::class.java) }
19+
20+
val presenterScope get() = CoroutineScope(CoroutineName("CatsCoroutine") + Dispatchers.Main)
1621
}

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

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

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.os.Bundle
5-
import androidx.lifecycle.lifecycleScope
6-
import kotlinx.coroutines.launch
4+
import androidx.appcompat.app.AppCompatActivity
75

86
class MainActivity : AppCompatActivity() {
97

@@ -17,12 +15,10 @@ class MainActivity : AppCompatActivity() {
1715
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
1816
setContentView(view)
1917

20-
catsPresenter = CatsPresenter(diContainer.service)
18+
catsPresenter = CatsPresenter(diContainer.service, diContainer.presenterScope)
2119
view.presenter = catsPresenter
2220
catsPresenter.attachView(view)
23-
lifecycleScope.launch {
24-
catsPresenter.onInitComplete()
25-
}
21+
catsPresenter.onInitComplete()
2622
}
2723

2824
override fun onStop() {

0 commit comments

Comments
 (0)