Skip to content

Commit 7fe6dff

Browse files
Otus-Android#3 Реализовано решение ViewModel
1 parent 1f059ec commit 7fe6dff

File tree

5 files changed

+74
-9
lines changed

5 files changed

+74
-9
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ dependencies {
4141
implementation 'com.google.android.material:material:1.11.0'
4242
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4343
implementation 'com.squareup.picasso:picasso:2.71828'
44+
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0"
4445
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class CatsView @JvmOverloads constructor(
1515
defStyleAttr: Int = 0
1616
) : ConstraintLayout(context, attrs, defStyleAttr), ICatsView {
1717

18-
var presenter :CatsPresenter? = null
18+
var viewModel : CatsViewModel? = null
1919

2020
override fun onFinishInflate() {
2121
super.onFinishInflate()
2222
findViewById<Button>(R.id.button).setOnClickListener {
23-
presenter?.onInitComplete()
23+
viewModel?.onInitComplete()
2424
}
2525
}
2626

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package otus.homework.coroutines
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.Job
7+
import kotlinx.coroutines.cancel
8+
import kotlinx.coroutines.launch
9+
import kotlinx.coroutines.withContext
10+
import java.net.SocketTimeoutException
11+
12+
class CatsViewModel(
13+
private val catsService: CatsService,
14+
private val imageService: ImageService
15+
): ViewModel() {
16+
private var _catsView: ICatsView? = null
17+
private var job: Job? = null
18+
19+
fun onInitComplete() {
20+
job = viewModelScope.launch {
21+
try {
22+
val fact = catsService.getCatFact()
23+
val image = imageService.getRandomImage()
24+
val model = FactPresentationModel(fact, image.first())
25+
withContext(Dispatchers.Main) {
26+
_catsView?.populate(model)
27+
}
28+
} catch (e: Exception) {
29+
when (e) {
30+
is SocketTimeoutException -> {
31+
_catsView?.onError(R.string.connection_error_message)
32+
}
33+
else -> {
34+
CrashMonitor.trackWarning()
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
fun attachView(catsView: ICatsView) {
42+
_catsView = catsView
43+
}
44+
45+
fun detachView() {
46+
job?.cancel()
47+
_catsView = null
48+
}
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package otus.homework.coroutines
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.ViewModelProvider
5+
6+
class CatsViewModelFactory(
7+
private val catsService: CatsService,
8+
private val imageService: ImageService
9+
) : ViewModelProvider.NewInstanceFactory() {
10+
override fun <T : ViewModel> create(modelClass: Class<T>): T {
11+
return CatsViewModel(catsService, imageService) as T
12+
}
13+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package otus.homework.coroutines
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import androidx.lifecycle.ViewModelProvider
56

67
class MainActivity : AppCompatActivity() {
78

8-
lateinit var catsPresenter: CatsPresenter
9+
//lateinit var catsPresenter: CatsPresenter
10+
lateinit var viewModel: CatsViewModel
911

1012
private val diContainer = DiContainer()
1113

@@ -14,16 +16,16 @@ class MainActivity : AppCompatActivity() {
1416

1517
val view = layoutInflater.inflate(R.layout.activity_main, null) as CatsView
1618
setContentView(view)
17-
18-
catsPresenter = CatsPresenter(diContainer.service, diContainer.imageService)
19-
view.presenter = catsPresenter
20-
catsPresenter.attachView(view)
21-
catsPresenter.onInitComplete()
19+
viewModel = CatsViewModelFactory(diContainer.service, diContainer.imageService)
20+
.create(CatsViewModel::class.java)
21+
view.viewModel = viewModel
22+
viewModel.attachView(view)
23+
viewModel.onInitComplete()
2224
}
2325

2426
override fun onStop() {
2527
if (isFinishing) {
26-
catsPresenter.detachView()
28+
viewModel.detachView()
2729
}
2830
super.onStop()
2931
}

0 commit comments

Comments
 (0)