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

import android.databinding.BaseObservable
import android.databinding.ObservableField
import android.databinding.ObservableInt
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 titleVisible = ObservableInt(View.VISIBLE)
    val editIconVisible = ObservableInt(View.VISIBLE)
    val checkEditIconVisible = ObservableInt(View.INVISIBLE)
    val editTextVisible = ObservableInt(View.INVISIBLE)
    val editText = ObservableField<String>()
    val realmChangeListener = RealmChangeListener<Counter> {
        if (it.isValid) {
            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)
    }

    fun onClickEdit(view: View) {
        titleVisible.set(View.INVISIBLE)
        editTextVisible.set(View.VISIBLE)
        editIconVisible.set(View.INVISIBLE)
        checkEditIconVisible.set(View.VISIBLE)
        editText.set(title.get())
    }

    fun onClickCheckEdit(view: View) {
        counterContract.editTitle(id, editText.get())
        titleVisible.set(View.VISIBLE)
        editTextVisible.set(View.INVISIBLE)
        editIconVisible.set(View.VISIBLE)
        checkEditIconVisible.set(View.INVISIBLE)
    }
}