package com.quyunshuo.module.home.activity.main

import android.widget.Toast
import androidx.activity.viewModels
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseActivity
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.module.home.R
import com.quyunshuo.module.home.databinding.HomeActivitySleepBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * 首页
 *
 * @author Qu Yunshuo
 * @since 5/22/21 2:26 PM
 */
@AndroidEntryPoint
class SleepActivity : BaseActivity<HomeActivitySleepBinding, SleepViewModel>() {
    private var backPressedTime:Long = 0
    /**
     * 通过 viewModels() + Hilt 获取 ViewModel 实例
     */
    override val mViewModel by viewModels<SleepViewModel>()

    override fun createVB() = HomeActivitySleepBinding.inflate(layoutInflater)

    override fun HomeActivitySleepBinding.initView() {}

    override fun initObserve() {
        observeLiveData(mViewModel.data, ::processData)
    }

    private fun processData(data: String) {
    }

    override fun initRequestData() {
        // 模拟获取数据
        mViewModel.getData()
    }

    override fun onBackPressed() {

        // 获取当前时间戳
        val currentTime = System.currentTimeMillis()

        // 判断与上一次点击返回键的时间间隔是否小于1秒
        if (currentTime - backPressedTime < 1000) {
            super.onBackPressed() // 执行默认的返回键操作（退出Activity）
        } else {
            Toast.makeText(this, UiUtil.getStringRes(R.string.base_home_quit_app), Toast.LENGTH_SHORT).show()
            backPressedTime = currentTime // 更新上一次点击返回键的时间戳
        }
    }
}