diff --git a/app/src/main/kotlin/jackall/moncalc/activity/MainActivity.kt b/app/src/main/kotlin/jackall/moncalc/activity/MainActivity.kt index 2a0d9d8..6cd637c 100644 --- a/app/src/main/kotlin/jackall/moncalc/activity/MainActivity.kt +++ b/app/src/main/kotlin/jackall/moncalc/activity/MainActivity.kt @@ -4,11 +4,20 @@ import android.databinding.DataBindingUtil import android.os.Bundle import android.support.v7.app.AppCompatActivity +import com.google.gson.Gson +import com.google.gson.stream.JsonReader +import jackall.moncalc.Const import jackall.moncalc.R import jackall.moncalc.adapter.MainFragmentPagerAdapter +import jackall.moncalc.common.PreferenceKeys +import jackall.moncalc.common.PreferenceNames import jackall.moncalc.databinding.ActivityMainBinding +import jackall.moncalc.db.MonstDataRealmHelper +import jackall.moncalc.utils.MySharedPref import jackall.moncalc.viewmodel.MainViewModel +import jackall.moncalc.vo.MonstData import kotlinx.android.synthetic.main.activity_main.view.* +import java.io.InputStreamReader /** * Created by matsumoto_k on 2017/11/03. @@ -16,9 +25,26 @@ class MainActivity : AppCompatActivity() { val mainBinding by lazy { DataBindingUtil.setContentView(this, R.layout.activity_main) } val mainViewModel by lazy { ViewModelProviders.of(this, MainViewModel.Factory(this)).get(MainViewModel::class.java) } + val mySharedPref by lazy { MySharedPref(this, PreferenceNames.CONFIG) } + val monstDataRealmHelper = MonstDataRealmHelper() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + if ((mySharedPref.getValue(PreferenceKeys.QUESTDATAVERSION, Int::class.java, -1) as Int) < Const.questDataVersion) { + val inputStream = assets.open("data.json") + val jsonReader = JsonReader(InputStreamReader(inputStream)) + val monstData = Gson().fromJson(jsonReader, MonstData::class.java) + + monstDataRealmHelper.update(monstData.fruit) + monstDataRealmHelper.update(monstData.temple) + monstDataRealmHelper.update(monstData.grade) + + mySharedPref.putValue(PreferenceKeys.QUESTDATAVERSION, Int::class.java, Const.questDataVersion) + + println("db update") + } + mainBinding.mainViewModel = mainViewModel lifecycle.addObserver(mainViewModel) mainBinding.root.main_view_pager.adapter = MainFragmentPagerAdapter(supportFragmentManager, this)