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

import android.content.Context
import android.databinding.BaseObservable
import android.databinding.ObservableInt
import android.view.View
import android.widget.Toast
import jackall.moncalc.App
import jackall.moncalc.common.PreferenceKeys
import jackall.moncalc.common.PreferenceNames
import jackall.moncalc.contract.MainServiceContract
import jackall.moncalc.db.MonstDataRealmHelper
import jackall.moncalc.db.QuestRecordRealmHelper
import jackall.moncalc.utils.MySharedPref
import jackall.moncalc.vo.Fruit
import jackall.moncalc.vo.Grade
import jackall.moncalc.vo.QuestRecord
import jackall.moncalc.vo.Temple
import java.util.*

/**
 * 記録画面オーバーレイのViewModel
 *
 * Created by matsumoto_k on 2017/11/01.
 */
class OverlayRegistViewModel(val mainServiceContract: MainServiceContract, val context: Context) : BaseObservable() {
    private val monstDataRealmHelper = MonstDataRealmHelper()
    private val questRecordRealmHelper = QuestRecordRealmHelper()
    private val mySharedPref by lazy { MySharedPref(context, PreferenceNames.CONFIG) }
    val templeRank = ObservableInt(mySharedPref.getValue(PreferenceKeys.TEMPLEPOS, Int::class.java, 0) as Int)
    val fruitRank = ObservableInt(mySharedPref.getValue(PreferenceKeys.FRUITPOS, Int::class.java, 0) as Int)
    val gradeRank = ObservableInt(mySharedPref.getValue(PreferenceKeys.GRADEPOS, Int::class.java, 0) as Int)

    fun onClickRegist(view: View) {
        questRecordRealmHelper.insert(
                QuestRecord(
                        templeId = monstDataRealmHelper.findIdByRank(Temple::class.java, templeRank.get()),
                        fruitId = monstDataRealmHelper.findIdByRank(Fruit::class.java, fruitRank.get()),
                        gradeId = monstDataRealmHelper.findIdByRank(Grade::class.java, gradeRank.get()),
                        createAt = Date()
                )
        )
        recordSpinnerPosition(templeRank.get(), fruitRank.get(), gradeRank.get())
        Toast.makeText(App.instance, "記録しました", Toast.LENGTH_LONG).show()
        mainServiceContract.showMoveView()
    }

    fun realmInstanceClose() {
        monstDataRealmHelper.close()
        questRecordRealmHelper.close()
    }

    fun recordSpinnerPosition(templePos: Int, fruitPos: Int, gradePos: Int) {
        mySharedPref.putValue(PreferenceKeys.TEMPLEPOS, Int::class.java, templePos)
        mySharedPref.putValue(PreferenceKeys.FRUITPOS, Int::class.java, fruitPos)
        mySharedPref.putValue(PreferenceKeys.GRADEPOS, Int::class.java, gradePos)
    }
}