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.R import jackall.moncalc.adapter.AnalyzeTempleAdapter import jackall.moncalc.db.MonstDataRealmHelper import jackall.moncalc.db.QuestRecordRealmHelper import jackall.moncalc.model.PieChartModel import jackall.moncalc.vo.Temple /** * 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 var items: ArrayList<TempleAnalyzeItemViewModel> 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 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: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 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) } } 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 } } }