package jackall.moncalc.viewmodel import android.arch.lifecycle.ViewModel import android.arch.lifecycle.ViewModelProvider import android.databinding.BindingAdapter import android.databinding.ObservableField 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 io.reactivex.schedulers.Schedulers import jackall.moncalc.App import jackall.moncalc.adapter.AnalyzeFruitAdapter import jackall.moncalc.common.Notification import jackall.moncalc.db.MonstDataRealmHelper import jackall.moncalc.db.QuestRecordRealmHelper import jackall.moncalc.model.PieChartModel import jackall.moncalc.utils.RxBus import jackall.moncalc.vo.Fruit import jackall.moncalc.vo.Grade import kotlin.concurrent.thread /** * Created by matsumoto_k on 2017/11/04. */ class FruitAnalyzeViewModel : LifecycleViewModel() { val pieData = ObservableField<PieData>() val pieChartModel = ObservableField<PieChartModel>() val items = ObservableField<ArrayList<FruitAnalyzeItemViewModel>>() init { thread { setFruitAnalyzeList() setFruitAnalyzePie() } RxBus.observe<Notification>().observeOn(Schedulers.newThread()).subscribe { when (it.name) { Notification.DATACHANGED.name -> { thread { setFruitAnalyzeList() setFruitAnalyzePie() } } } } } fun setFruitAnalyzeList() { val questRealmHelper = QuestRecordRealmHelper() val monstDataRealmHelper = MonstDataRealmHelper() val items = ArrayList<FruitAnalyzeItemViewModel>() items.add( FruitAnalyzeItemViewModel( "総合", questRealmHelper.countGradeById(5).toString(), questRealmHelper.countGradeById(4).toString(), questRealmHelper.countGradeById(3).toString(), questRealmHelper.countGradeById(2).toString(), questRealmHelper.countGradeById(1).toString(), questRealmHelper.countGradeById(0).toString() ) ) // リストビューに入れる monstDataRealmHelper.findAll(Fruit::class.java).forEach { if (!questRealmHelper.isDrop(it.id)) return@forEach val item = FruitAnalyzeItemViewModel() item.fruitName = monstDataRealmHelper.findNameById(Fruit::class.java, it.id) item.grade0 = questRealmHelper.countGrade(it.id, 5).toString() item.grade1 = questRealmHelper.countGrade(it.id, 4).toString() item.grade2 = questRealmHelper.countGrade(it.id, 3).toString() item.grade3 = questRealmHelper.countGrade(it.id, 2).toString() item.grade4 = questRealmHelper.countGrade(it.id, 1).toString() item.grade5 = questRealmHelper.countGrade(it.id, 0).toString() items.add(item) } this.items.set(items.apply { sortByDescending { it.count() } }) questRealmHelper.close() monstDataRealmHelper.close() } fun setFruitAnalyzePie() { val questRealmHelper = QuestRecordRealmHelper() val monstDataRealmHelper = MonstDataRealmHelper() 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.set(PieChartModel(entries.apply { sortByDescending { it.value } }, "")) this.pieData.set(pieChartModel.get().pieData) questRealmHelper.close() monstDataRealmHelper.close() } object Adapter { @JvmStatic @BindingAdapter("android:fruitPieChart") fun hoge(mChart: PieChart, pieData: PieData?) { if (pieData == null) return 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 mChart.invalidate() } @JvmStatic @BindingAdapter("android:setFruitTempleList") fun setAnalyzeFruitList(view: RecyclerView, items: ArrayList<FruitAnalyzeItemViewModel>?) { if (items == null) return view.layoutManager = LinearLayoutManager(App.instance) view.adapter = AnalyzeFruitAdapter(App.instance, items) view.adapter.notifyDataSetChanged() } } class Factory() : ViewModelProvider.NewInstanceFactory() { override fun <T : ViewModel?> create(modelClass: Class<T>): T { return FruitAnalyzeViewModel() as T } } override fun onDestroy() { super.onDestroy() } }