Jonny
10/13/2019, 1:48 PMclass AddJobViewModel() : ViewModel() {
val minPrice = 20
val maxPrice = 1000
val step = 10
var description: MutableLiveData<String> = MutableLiveData("")
var displayPrice: MutableLiveData<String> = MutableLiveData(String.format("Price: %d Kr", minPrice))
val seekBarMax: MutableLiveData<Int> = MutableLiveData((maxPrice - minPrice) / step)
var currentPrice: MutableLiveData<Int>
var seekBarProgress: MutableLiveData<Int>
init {
currentPrice = object: MutableLiveData<Int>(minPrice) {
override fun setValue(value: Int?) {
if (this.value == value) {
return
}
super.setValue(value)
displayPrice.value = String.format("Price: %d Kr", value)
seekBarProgress.value = value!! / step - minPrice
}
override fun postValue(value: Int?) {
if (this.value == value) {
return
}
super.postValue(value)
displayPrice.postValue(String.format("Price: %d Kr", value))
seekBarProgress.postValue(value!! / step - minPrice)
}
}
seekBarProgress = object: MutableLiveData<Int>(0) {
override fun setValue(value: Int?) {
if (this.value == value) {
return
}
super.setValue(value)
currentPrice.value = minPrice + value!! * step
}
override fun postValue(value: Int?) {
if (this.value == value) {
return
}
super.postValue(value)
currentPrice.postValue(minPrice + value!! * step)
}
}
currentPrice.value = 200
}
}
Thomas Nordmeyer
10/14/2019, 7:22 AMval currentPrice = MutableLiveData<Int>()
val displayPrice = Transformations.map(currentPrice) {
String.format("Price: %d Kr", it)
}
val currentPrice = MutableLiveData<Int>()
val displayPrice = MutableLiveData<String>()
init {
currentPrice.observeForever() {
displayPrice.value = String.format("Price: %d Kr", it)
}
}
Jonny
10/14/2019, 8:48 PMval seekBarMax: MutableLiveData<Int> = MutableLiveData((maxPrice - minPrice) / step)
val currentPrice = object: MutableLiveData<Int>(minPrice) {
override fun setValue(value: Int?) {
if (this.value != value) {
super.setValue(value)
}
}
override fun postValue(value: Int?) {
if (this.value != value) {
super.postValue(value)
}
}
}
val seekBarProgress = object: MutableLiveData<Int>(0) {
override fun setValue(value: Int?) {
if (this.value != value) {
super.setValue(value)
}
}
override fun postValue(value: Int?) {
if (this.value != value) {
super.postValue(value)
}
}
}
val displayPrice = Transformations.map(currentPrice) {
"Price: $it Kr"
}
init {
seekBarProgress.observeForever {
currentPrice.value = minPrice + it * step
}
currentPrice.observeForever {
seekBarProgress.value = (it - minPrice) / step
}
}
Thomas Nordmeyer
10/15/2019, 6:05 AM