package jackall.moncalc.viewmodel import android.arch.lifecycle.ViewModel import android.arch.lifecycle.ViewModelProvider import android.databinding.BindingAdapter import android.graphics.Color import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieData import com.github.mikephil.charting.data.PieEntry import com.github.mikephil.charting.formatter.IValueFormatter import com.github.mikephil.charting.utils.ViewPortHandler import jackall.moncalc.App import jackall.moncalc.adapter.AnalyzeTempleAdapter import jackall.moncalc.db.MonstDataRealmHelper import jackall.moncalc.db.QuestRecordRealmHelper import jackall.moncalc.model.PieChartModel import jackall.moncalc.vo.Fruit import jackall.moncalc.vo.Grade /** * Created by matsumoto_k on 2017/11/04. */ class FruitAnalyzeViewModel : LifecycleViewModel() { val questRealmHelper = QuestRecordRealmHelper() val monstDataRealmHelper = MonstDataRealmHelper() var pieData: PieData var pieChartModel: PieChartModel // var items: ArrayList<TempleAnalyzeItemViewModel> init { val entries = ArrayList<PieEntry>() for (fruitId in 0..5) { val count = questRealmHelper.countGradeById(fruitId) if (count > 0) { entries.add(PieEntry(count.toFloat(), monstDataRealmHelper.findNameById(Grade::class.java, fruitId))) } } pieChartModel = PieChartModel(entries, "") this.pieData = pieChartModel.pieData // val items = ArrayList<TempleAnalyzeItemViewModel>() // items.add( // TempleAnalyzeItemViewModel( // "総合", // questRealmHelper.findAll().size.toString(), // "${String.format("%.1f", questRealmHelper.getAllSpecialPercent())}%" // ) // ) //リストビューに入れる // monstDataRealmHelper.findAll(Temple::class.java).forEach { // val count = questRealmHelper.countById(it.id) // if (count == 0) // return@forEach // items.add( // TempleAnalyzeItemViewModel( // monstDataRealmHelper.findNameById(Temple::class.java, it.id), // count.toString(), // "${String.format("%.1f", questRealmHelper.getSpecialPercent(it.id))}%" // ) // ) // } // this.items = items } object Adapter { @JvmStatic @BindingAdapter("android:fruitPieChart") 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 pieData.setValueFormatter(object : IValueFormatter { override fun getFormattedValue(value: Float, entry: Entry?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String { return String.format("%.0f", value) } }) mChart.description.text = "わくわくの実ドロップ数" mChart.data = pieData } @JvmStatic @BindingAdapter("android:setAnalyzeTempleList") fun hogehoge(view: RecyclerView, items: ArrayList<TempleAnalyzeItemViewModel>) { view.layoutManager = LinearLayoutManager(App.instance) view.adapter = AnalyzeTempleAdapter(App.instance, items) } } class Factory() : ViewModelProvider.NewInstanceFactory() { override fun <T : ViewModel?> create(modelClass: Class<T>): T { return FruitAnalyzeViewModel() as T } } override fun onDestroy() { super.onDestroy() questRealmHelper.close() monstDataRealmHelper.close() } }