-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternCutFlowViewModel.kt
69 lines (55 loc) · 2.06 KB
/
PatternCutFlowViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package sk.drevari.optitimb.ui.patternCutFlow
import android.app.Application
import android.graphics.Bitmap
import android.util.Log
import androidx.lifecycle.*
import kotlinx.coroutines.launch
import sk.drevari.optitimb.OTApp
import sk.drevari.optitimb.repo.OTRepository
import sk.drevari.optitimb.repo.db.data.PatternWithLogAndSpecie
import sk.drevari.optitimb.repo.db.entity.Pattern
import sk.drevari.optitimb.utils.Visualization.getTotalCuts
import sk.drevari.optitimb.utils.Visualization.visualize
import javax.inject.Inject
/**
* Created by Sergii Volchenko on 15.06.2020.
* made in Ukraine
*/
class PatternCutFlowViewModel(val app: Application, val patternId: Int) : AndroidViewModel(app) {
@Inject
lateinit var repo: OTRepository
private val _image = MutableLiveData<Bitmap>()
val image: LiveData<Bitmap> = _image
private val _totalSteps = MutableLiveData<Int>()
val totalSteps: LiveData<Int> = _totalSteps
private val _currentStep = MutableLiveData<Int>()
val currentStep: LiveData<Int> = _currentStep
lateinit var pattern: Pattern
init {
(app as OTApp).appComponents.inject(this)
viewModelScope.launch {
pattern = repo.getPatternById(patternId)!!
_image.postValue(pattern.visualize(app, 0))
_totalSteps.postValue(pattern.getTotalCuts())
_currentStep.postValue(0)
}
}
fun nextCut(): Boolean {
if (currentStep.value!! < totalSteps.value!!) {
_currentStep.value = if (_currentStep.value == -1) _currentStep.value!! + 2 else _currentStep.value!! + 1
_image.postValue(pattern.visualize(app, _currentStep.value))
}
return currentStep.value!! < totalSteps.value!!
}
fun previousCut() =
if (currentStep.value!! > 0) {
_currentStep.value = _currentStep.value!! - 1
_image.postValue(pattern.visualize(app, _currentStep.value))
true
} else false
override fun onCleared() {
super.onCleared()
_image.value?.recycle()
}
}