package jackall.moncalc.activity import android.arch.lifecycle.ViewModelProviders 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 io.realm.RealmList 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.db.QuestRecordRealmHelper import jackall.moncalc.utils.AdUtil import jackall.moncalc.utils.MySharedPref import jackall.moncalc.viewmodel.MainViewModel import jackall.moncalc.vo.Drop import jackall.moncalc.vo.MonstData import jackall.moncalc.vo.QuestRecord import kotlinx.android.synthetic.main.activity_main.view.* import java.io.InputStreamReader import java.util.* /** * Created by matsumoto_k on 2017/11/03. */ class MainActivity : AppCompatActivity() { val mainBinding by lazy { DataBindingUtil.setContentView<ActivityMainBinding>(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<MonstData>(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") } if (Const.develop) { for (i in 0..1000) { val dateStart = Calendar.getInstance() dateStart.set(2017, 9, 1) // 終了日 val dateEnd = Calendar.getInstance() // 期間日数 val dateDiff = (dateEnd.timeInMillis - dateStart.timeInMillis) / (1000 * 60 * 60 * 24) // ランダムな年月日の生成 val dateRandom = Calendar.getInstance() dateRandom.add(dateEnd.get(Calendar.DATE), -Random().nextInt(dateDiff.toInt())) val drops = RealmList<Drop>() val gradeId = if (Math.random() < 0.1) { Random().nextInt(3) + 3 } else { Random().nextInt(3) } drops.add( Drop( fruitId = Random().nextInt(15), gradeId = gradeId ) ) var specialCount: Int if (gradeId >= 3) { specialCount = 1 } else { specialCount = 0 } QuestRecordRealmHelper().insert( QuestRecord( attribute = Random().nextInt(5), templeId = Random().nextInt(20), specialCount = specialCount, dropCount = drops.count(), drops = drops, createAt = dateRandom.time ) ) } } mainBinding.root.main_view_pager.offscreenPageLimit = 3 mainBinding.mainViewModel = mainViewModel lifecycle.addObserver(mainViewModel) mainBinding.root.main_view_pager.adapter = MainFragmentPagerAdapter(supportFragmentManager, this) mainBinding.root.main_tab_layout.setupWithViewPager(mainBinding.root.main_view_pager) } override fun onStart() { super.onStart() AdUtil.startBannerAd(this, mainBinding.root.main_banner_layout, Const.BannerUnitId) } }