Newer
Older
OverlayMultiCounter / app / src / main / kotlin / jackall / overlaymulticounter / activity / MainActivity.kt
MatsumotoKeiju on 10 Nov 2017 4 KB [add] Main contract
package jackall.overlaymulticounter.activity

import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.databinding.DataBindingUtil
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.support.v7.app.AppCompatActivity
import com.getkeepsafe.taptargetview.TapTarget
import com.getkeepsafe.taptargetview.TapTargetView
import com.google.android.gms.ads.AdSize
import jackall.overlaymulticounter.Const
import jackall.overlaymulticounter.R
import jackall.overlaymulticounter.common.PreferenceKeys
import jackall.overlaymulticounter.common.PreferenceNames
import jackall.overlaymulticounter.contract.MainContract
import jackall.overlaymulticounter.databinding.ActivityMainBinding
import jackall.overlaymulticounter.db.CounterRealmHelper
import jackall.overlaymulticounter.service.MainService
import jackall.overlaymulticounter.utils.AdUtil
import jackall.overlaymulticounter.utils.MySharedPref
import jackall.overlaymulticounter.viewmodel.MainViewModel
import jackall.overlaymulticounter.vo.Counter
import kotlinx.android.synthetic.main.activity_main.view.*


class MainActivity : AppCompatActivity(), MainContract {

    lateinit var binding: ActivityMainBinding
    lateinit var mainViewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        mainViewModel = ViewModelProviders.of(this, MainViewModel.Factory(this as MainContract)).get(MainViewModel::class.java)
        binding.mainViewModel = mainViewModel
        lifecycle.addObserver(mainViewModel)

        if (CounterRealmHelper().getNextId() == 0) {
            CounterRealmHelper().insert(
                    Counter(CounterRealmHelper().getNextId(),
                            "Counter0",
                            0)
            )
        }
    }

    override fun onStart() {
        super.onStart()
        AdUtil.startBannerAd(this, binding.root.main_banner_frame, AdSize.BANNER, Const.mainBannerUnitId)
        AdUtil.startInterstitialAd(this)
        if (MySharedPref(this, PreferenceNames.CONFIG).getValue(PreferenceKeys.initialOverlay, Boolean::class.java, true) as Boolean) {
            TapTargetView.showFor(this, // `this` is an Activity
                    TapTarget.forView(findViewById(R.id.main_service_button),
                            resources.getString(R.string.target_title),
                            resources.getString(R.string.target_text))
                            // All options below are optional
                            .outerCircleColor(R.color.outer_circle_color)
                            .outerCircleAlpha(0.96f)
                            .targetCircleColor(R.color.target_circle_color)
                            .titleTextSize(24)
                            .descriptionTextSize(20)
                            .descriptionTextColor(R.color.target_description_color)
                            .textColor(R.color.target_text_color)
                            .textTypeface(Typeface.SANS_SERIF)
                            .dimColor(R.color.target_dim_color)
                            .drawShadow(true)
                            .cancelable(false)
                            .tintTarget(true)
                            .transparentTarget(true)
                            .targetRadius(50),
                    object : TapTargetView.Listener() {
                        override fun onTargetClick(view: TapTargetView) {
                            super.onTargetClick(view)
                            binding.root.main_service_button.callOnClick()
                        }
                    })
        }
    }

    override fun startService() {
        startService(Intent(this, MainService::class.java))
    }

    override fun stopService() {
        stopService(Intent(this, MainService::class.java))
    }

    override fun openSetting() {
        val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:jackall.overlaymulticounter"))
        startActivityForResult(intent, 0)
    }
}