package jackall.moncalc.viewmodel import android.arch.lifecycle.ViewModel import android.arch.lifecycle.ViewModelProvider import android.databinding.ObservableField import jackall.moncalc.db.MonstDataRealmHelper import jackall.moncalc.db.QuestRecordRealmHelper /** * Created by matsumoto_k on 2017/11/03. */ class HistoryViewModel() : LifecycleViewModel() { val questRealmHelper = QuestRecordRealmHelper() val monstDataRealmHelper = MonstDataRealmHelper() 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 { 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)}%") } // // 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 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() monstDataRealmHelper.close() questRealmHelper.close() } class Factory() : ViewModelProvider.NewInstanceFactory() { override fun <T : ViewModel?> create(modelClass: Class<T>): T { return jackall.moncalc.viewmodel.HistoryViewModel() as T } } }