package jackall.moncalc.viewmodel import android.content.Context import android.databinding.BaseObservable import android.databinding.BindingAdapter import android.databinding.ObservableBoolean import android.databinding.ObservableInt import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.View import android.widget.Toast import io.realm.RealmList import jackall.moncalc.App import jackall.moncalc.adapter.CustomSpinnerAdapter import jackall.moncalc.adapter.RegistAdapter import jackall.moncalc.common.Notification 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.utils.RxBus import jackall.moncalc.vo.* 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) val fruitSpinnerAdapter = CustomSpinnerAdapter(context, monstDataRealmHelper.findNames(Fruit::class.java)) val gradeSpinnerAdapter = CustomSpinnerAdapter(context, monstDataRealmHelper.findNames(Grade::class.java)) val registAdapter = RegistAdapter(context, arrayListOf(RegistItemViewModel(fruitSpinnerAdapter, gradeSpinnerAdapter))) val checkExtra = ObservableBoolean(false) val wakurinRank = ObservableInt() fun onClickAdd(view: View) { registAdapter.add(RegistItemViewModel(fruitSpinnerAdapter, gradeSpinnerAdapter)) } fun onClickRemove(view: View) { registAdapter.remove() } fun onClickRegist(view: View) { val drops = RealmList<Drop>() var count = 0 registAdapter.items.forEach { if (monstDataRealmHelper.findIdByRank(Grade::class.java, it.gradeRank.get()) >= 3) { count++ } drops.add( Drop( fruitId = monstDataRealmHelper.findIdByRank(Fruit::class.java, it.fruitRank.get()), gradeId = monstDataRealmHelper.findIdByRank(Grade::class.java, it.gradeRank.get()) ) ) } questRecordRealmHelper.insert( QuestRecord( attribute = monstDataRealmHelper.findTempleAttributeByRank(templeRank.get()), templeId = monstDataRealmHelper.findIdByRank(Temple::class.java, templeRank.get()), specialCount = count, extra = checkExtra.get(), wakurin = wakurinRank.get(), dropCount = drops.count(), drops = drops, createAt = Date() ) ) recordSpinnerPosition(templeRank.get(), fruitRank.get(), gradeRank.get()) Toast.makeText(App.instance, "記録しました", Toast.LENGTH_LONG).show() RxBus.send(Notification.DATACHANGED) 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) } object Adapter { @JvmStatic @BindingAdapter("android:setRecyclerAdapter") fun setAdapter(recyclerView: RecyclerView, registAdapter: RegistAdapter?) { recyclerView.layoutManager = LinearLayoutManager(App.instance) recyclerView.adapter = registAdapter } } }