Newer
Older
MonCalc / app / src / main / kotlin / jackall / moncalc / viewmodel / HistoryViewModel.kt
package jackall.moncalc.viewmodel

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.databinding.ObservableField
import io.reactivex.schedulers.Schedulers
import jackall.moncalc.common.Notification
import jackall.moncalc.db.QuestRecordRealmHelper
import jackall.moncalc.utils.RxBus
import kotlin.concurrent.thread

/**
 * Created by matsumoto_k on 2017/11/03.
 */
class HistoryViewModel() : LifecycleViewModel() {

    val todayTempleCount = ObservableField<String>()
    val todayDrop = ObservableField<String>()
    val todaySpecialDrop = ObservableField<String>()
    val todaySpecialPercent = ObservableField<String>()
    val weekTempleCount = ObservableField<String>()
    val weekDrop = ObservableField<String>()
    val weekSpecialDrop = ObservableField<String>()
    val weekSpecialPercent = ObservableField<String>()

//    var lineData: LineData
//    var labels: ArrayList<String>
//    val refreshLayoutLoading = ObservableBoolean(true)
//    val hisotoryItems = ArrayList<HistoryItem>()

    init {
        thread {
            setHistoryData()
        }
        RxBus.observe<Notification>().observeOn(Schedulers.newThread()).subscribe {
            when (it.name) {
                Notification.DATACHANGED.name -> {
                    thread {
                        setHistoryData()
                    }
                }
            }
        }
//        val entries = ArrayList<Entry>()
//        val labels = ArrayList<String>()
//        questRealmHelper.findByPeriod(30).forEachIndexed { index, value ->
//            entries.add(
//                    Entry(index.toFloat(), value.toFloat())
//            )
//        }
//
//        val lineChartModel = LineChartModel(entries, "神殿周回数")
//        lineData = lineChartModel.lineData
//        this.labels = labels
//        onRefresh()
    }

    fun setHistoryData() {
        val questRealmHelper = QuestRecordRealmHelper()
        todayTempleCount.set(questRealmHelper.getTodayCount().toString())
        todayDrop.set(questRealmHelper.getTodayDropCount().toString())
        todaySpecialDrop.set(questRealmHelper.getTodaySpecialCount().toString())
        val todaySpecialPercent = (questRealmHelper.getTodaySpecialCount().toFloat() / questRealmHelper.getTodayCount().toFloat()) * 100
        if (todaySpecialPercent.isNaN()) {
            this.todaySpecialPercent.set("0%")
        } else {
            this.todaySpecialPercent.set("${String.format("%.1f", todaySpecialPercent)}%")
        }
        weekTempleCount.set(questRealmHelper.getWeekCount().toString())
        weekDrop.set(questRealmHelper.getWeekDropCount().toString())
        weekSpecialDrop.set(questRealmHelper.getWeekSpecialCount().toString())
        val weekSpecialPercent = (questRealmHelper.getWeekSpecialCount().toFloat() / questRealmHelper.getWeekCount().toFloat()) * 100
        if (weekSpecialPercent.isNaN()) {
            this.weekSpecialPercent.set("0%")
        } else {
            this.weekSpecialPercent.set("${String.format("%.1f", weekSpecialPercent)}%")
        }
        questRealmHelper.close()
    }

//    fun onRefresh() {
//        refreshLayoutLoading.set(true)
//        hisotoryItems.clear()
//        questRealmHelper.findAll().sortedByDescending { it.createAt }.forEach {
//            hisotoryItems.add(
//                    HistoryItem(templeName = monstDataRealmHelper.findNameById(Temple::class.java, it.templeId),
//                            fruitName = monstDataRealmHelper.findNameById(Fruit::class.java, it.fruitId),
//                            gradeName = monstDataRealmHelper.findNameById(Grade::class.java, it.gradeId),
//                            createAt = it.createAt)
//            )
//        }
//        adapter.setItemAndRefresh(hisotoryItems)
//        refreshLayoutLoading.set(false)
//    }

//    object Adapter {
//        @JvmStatic
//        @BindingAdapter("android:lineChart", "android:lineLabels")
//        fun hoge(mChart: LineChart, lineData: LineData, labels: ArrayList<String>) {
//            mChart.data = lineData
//            val hoge = mChart.xAxis
//            hoge.setValueFormatter { value, axis ->
//                val tmp = value
//                if (value.toInt().toFloat() == tmp) {
//                    labels.get(value.toInt())
//                } else {
//                    ""
//                }
//            }
//            mChart.setDrawGridBackground(false)
//        }
//    }

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

    class Factory() : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return jackall.moncalc.viewmodel.HistoryViewModel() as T
        }
    }
}