Newer
Older
OverlayMultiCounter / app / src / main / kotlin / jackall / overlaymulticounter / viewmodel / MainViewModel.kt
MatsumotoKeiju on 10 Nov 2017 4 KB [add] finish
package jackall.overlaymulticounter.viewmodel

import android.app.AlertDialog
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.DialogInterface
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.R
import jackall.overlaymulticounter.adapter.CounterAdapter
import jackall.overlaymulticounter.contract.CounterContract
import jackall.overlaymulticounter.contract.MainContract
import jackall.overlaymulticounter.db.CounterRealmHelper
import jackall.overlaymulticounter.utils.AdUtil
import jackall.overlaymulticounter.utils.RuntimePermissionUtil
import jackall.overlaymulticounter.utils.ServiceUtil
import jackall.overlaymulticounter.vo.Counter
import java.util.*

/**
 * Created by matsumoto_k on 2017/11/10.
 */
class MainViewModel(val contract: MainContract) : LifecycleViewModel(), LifecycleObserver, CounterContract {
    var counterAdapter: CounterAdapter
    var counterRealmHelper = CounterRealmHelper()
    var realmChangeListener: OrderedRealmCollectionChangeListener<RealmResults<Counter>>
    var results: RealmResults<Counter>

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

    fun onClickStartService(view: View) {
        if (RuntimePermissionUtil.checkOverlayPermission(view.context)) {
            AdUtil.startInterstitialAd(view.context)
            if (ServiceUtil.isMainServiceRunning(view.context)) {
                contract.stopService()
            } else {
                contract.startService()
            }
        } else {
            AlertDialog.Builder(view.context)
                    .setMessage(view.context.getString(R.string.permit_overlay))
                    .setPositiveButton(view.context.getString(R.string.setting), DialogInterface.OnClickListener { dialog, which ->
                        contract.openSetting()
                    })
                    .setNegativeButton(view.context.getString(R.string.cancel), null)
                    .show()
        }
    }

    fun onClickClose(view: View) {
        contract.finish()
    }

    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)
    }

    override fun onDestroy() {
        super.onDestroy()
        counterRealmHelper.close()
    }

    class Factory(val contract: MainContract) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return MainViewModel(contract) as T
        }
    }

    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)
        }
    }
}