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

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.databinding.ObservableBoolean
import jackall.moncalc.adapter.HistoryRecyclerAdapter
import jackall.moncalc.db.MonstDataRealmHelper
import jackall.moncalc.db.QuestRecordRealmHelper
import jackall.moncalc.vo.Fruit
import jackall.moncalc.vo.Grade
import jackall.moncalc.vo.HistoryItem
import jackall.moncalc.vo.Temple

/**
 * Created by matsumoto_k on 2017/11/03.
 */
class HistoryViewModel(val adapter: HistoryRecyclerAdapter) : LifecycleViewModel() {

    val questRealmHelper = QuestRecordRealmHelper()
    val monstDataRealmHelper = MonstDataRealmHelper()
    val refreshLayoutLoading = ObservableBoolean(true)
    val hisotoryItems = ArrayList<HistoryItem>()

    init {
        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)
    }

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

    class Factory(val adapter: HistoryRecyclerAdapter) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return jackall.moncalc.viewmodel.HistoryViewModel(adapter) as T
        }
    }
}