Newer
Older
OverlayMultiCounter / app / src / main / kotlin / jackall / overlaymulticounter / viewmodel / CounterItemViewModel.kt
MatsumotoKeiju on 9 Nov 2017 1 KB [add] Overlay Counter
package jackall.overlaymulticounter.viewmodel

import android.databinding.BaseObservable
import android.databinding.ObservableField
import android.view.View
import io.realm.RealmChangeListener
import jackall.overlaymulticounter.contract.CounterContract
import jackall.overlaymulticounter.vo.Counter

/**
 * Created by matsumoto_k on 2017/11/08.
 */
class CounterItemViewModel(val counter: Counter, val counterContract: CounterContract) : BaseObservable() {
    val id = counter.id
    val title = ObservableField<String>(counter.title)
    val count = ObservableField<String>(counter.count.toString())
    val realmChangeListener = RealmChangeListener<Counter> {
        title.set(it.title)
        count.set(it.count.toString())
    }

    init {
        counter.addChangeListener(realmChangeListener)
    }

    fun onClickPlus(view: View) {
        counterContract.plus(id)
    }

    fun onClickMinus(view: View) {
        counterContract.minus(id)
    }

    fun onClickReset(view: View) {
        counterContract.reset(id)
    }
}