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

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.databinding.BindingAdapter
import android.graphics.Color
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieEntry
import jackall.moncalc.App
import jackall.moncalc.R
import jackall.moncalc.db.MonstDataRealmHelper
import jackall.moncalc.db.QuestRecordRealmHelper
import jackall.moncalc.model.PieChartModel


/**
 * Created by matsumoto_k on 2017/11/04.
 */
class TempleAnalyzeViewModel : LifecycleViewModel() {

    val questRealmHelper = QuestRecordRealmHelper()
    val monstDataRealmHelper = MonstDataRealmHelper()
    val baseTempleName by lazy { App.instance.resources.getStringArray(R.array.base_temple_name) }
    var pieData: PieData
    var pieChartModel: PieChartModel

    init {
        val entries = ArrayList<PieEntry>()
        for (attribute in 0..4) {
            val count = questRealmHelper.countByAttribute(attribute)
            if (count > 0) {
                entries.add(PieEntry(count.toFloat(), baseTempleName.get(attribute)))
            }
        }
        pieChartModel = PieChartModel(entries, "")
        this.pieData = pieChartModel.pieData
    }

    object Adapter {
        @JvmStatic
        @BindingAdapter("android:pieChart")
        fun hoge(mChart: PieChart, pieData: PieData) {
            mChart.setEntryLabelColor(Color.BLACK)
            mChart.isDrawHoleEnabled = false
            mChart.legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
            mChart.legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
            mChart.legend.orientation = Legend.LegendOrientation.VERTICAL
            mChart.isRotationEnabled = false
            mChart.description.text = "神殿周回割合"
            mChart.data = pieData
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        questRealmHelper.close()
        monstDataRealmHelper.close()

    }

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