Newer
Older
OverlayMultiCounter / app / src / main / kotlin / jackall / overlaymulticounter / viewmodel / OverlayCounterViewModel.kt
MatsumotoKeiju on 10 Nov 2017 3 KB [clean] Add adapter package
package jackall.overlaymulticounter.viewmodel

import android.databinding.BaseObservable
import android.databinding.BindingAdapter
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import io.realm.OrderedRealmCollectionChangeListener
import io.realm.RealmResults
import jackall.overlaymulticounter.App
import jackall.overlaymulticounter.adapter.CounterAdapter
import jackall.overlaymulticounter.R
import jackall.overlaymulticounter.contract.CounterContract
import jackall.overlaymulticounter.db.CounterRealmHelper
import jackall.overlaymulticounter.vo.Counter
import java.util.*

/**
 * Created by matsumoto_k on 2017/11/08.
 */
class OverlayCounterViewModel : BaseObservable(), CounterContract {

    var counterAdapter: CounterAdapter? = null
    var counterRealmHelper = CounterRealmHelper()
    var realmChangeListener: OrderedRealmCollectionChangeListener<RealmResults<Counter>>
    var results: RealmResults<Counter>? = null

    init {
        val items = ArrayList<CounterItemViewModel>()
        realmChangeListener = OrderedRealmCollectionChangeListener { results, changeSet ->
            changeSet.insertions.forEach {
                counterAdapter?.insert(CounterItemViewModel(results.get(it), this@OverlayCounterViewModel as CounterContract))
            }
            changeSet.deletions.forEach {
                counterAdapter?.remove(it)
            }
        }
        results = counterRealmHelper.findAll()
        results?.forEachIndexed { index, counter ->
            items.add(CounterItemViewModel(counter, this@OverlayCounterViewModel as CounterContract))
        }
        results?.addChangeListener(realmChangeListener)
        counterAdapter = CounterAdapter(App.get(), items)
    }

    fun onClickAdd(view: View) {
        counterRealmHelper.insert(
                Counter(
                        id = counterRealmHelper.getNextId(),
                        title = "Counter${counterRealmHelper.getNextId()}",
                        count = 0
                )
        )
    }

    fun onClickRemove(view: View) {
        counterRealmHelper.removeLast()
    }

    fun onClickResetAll(view: View) {
        counterRealmHelper.resetAll()
    }

    override fun plus(id: Int) {
        counterRealmHelper.plus(id)
    }

    override fun minus(id: Int) {
        counterRealmHelper.minus(id)
    }

    override fun reset(id: Int) {
        counterRealmHelper.reset(id)
    }

    override fun editTitle(id: Int, title: String) {
        counterRealmHelper.updateTitle(id, title)
    }

    fun onDestory() {
        counterRealmHelper.close()
    }

    object Adapter {
        @JvmStatic
        @BindingAdapter("android:setCounterAdapter")
        fun setCounterAdapter(recyclerView: RecyclerView, counterAdapter: CounterAdapter?) {
            if (counterAdapter == null)
                return
            recyclerView.layoutManager = LinearLayoutManager(App.get())
            recyclerView.adapter = counterAdapter
            val dividerItemDecoration = DividerItemDecoration(recyclerView.context, LinearLayoutManager.VERTICAL)
            dividerItemDecoration.setDrawable(recyclerView.context.resources.getDrawable(R.drawable.divider))
            recyclerView.addItemDecoration(dividerItemDecoration)
        }
    }
}