| | |
| | | package com.dayu.general.activity |
| | | |
| | | import android.content.Intent |
| | | import android.os.Bundle |
| | | import android.view.KeyEvent |
| | | import android.view.LayoutInflater |
| | | import android.widget.Toast |
| | | import androidx.core.content.ContextCompat |
| | | import androidx.fragment.app.Fragment |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.general.BaseApplication |
| | | import com.dayu.general.R |
| | | import com.dayu.general.adapter.TabAdapter |
| | | import com.dayu.general.bean.net.UserInfoResult |
| | | import com.dayu.general.databinding.ActivityMainBinding |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | | |
| | | class MainActivity : BaseActivity() { |
| | | class MainActivity : BaseNfcActivity() { |
| | | |
| | | var binding: ActivityMainBinding? = null |
| | | private val fragments: ArrayList<Fragment> = ArrayList() |
| | | var mExitTime: Long = 0 |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | | super.onCreate(savedInstanceState) |
| | | binding = ActivityMainBinding.inflate(LayoutInflater.from(this)) |
| | |
| | | setupFragments() |
| | | initView() |
| | | initTab() |
| | | getUserInfo() |
| | | } |
| | | |
| | | override fun onNfcBack(intent: Intent) { |
| | | intent.let { nfcIntent -> |
| | | // 获取当前显示的Fragment |
| | | val currentFragment = fragments[binding?.viewPager?.currentItem ?: 0] |
| | | |
| | | // 如果当前显示的是充值Fragment,则将NFC信息传递给它处理 |
| | | if (currentFragment is RechargeFragment) { |
| | | currentFragment.handleNfcIntent(nfcIntent) |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | private fun getUserInfo() { |
| | | // 使用正确的类型参数 |
| | | ApiManager.getInstance().requestGetLoading( |
| | | this, |
| | | "base/user/getUserInfos/" + BaseApplication.userId, |
| | | UserInfoResult::class.java, |
| | | null, |
| | | object : SubscriberListener<BaseResponse<UserInfoResult>>() { |
| | | override fun onNext(t: BaseResponse<UserInfoResult>) { |
| | | if (t.success) { |
| | | BaseApplication.userName = t.content?.userName ?: "" |
| | | BaseApplication.userPhone = t.content?.phone ?: "" |
| | | BaseApplication.blockName = t.content?.blockName ?: "" |
| | | } else { |
| | | // 处理搜索失败的情况 |
| | | ToastUtil.show(t.msg) |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("搜索失败: ${e?.message ?: "未知错误"}") |
| | | } |
| | | } |
| | | ) |
| | | } |
| | | |
| | | |
| | | private fun initView() { |
| | | binding!!.BSCardLL.setOnClickListener { v -> changeBottomState(Tab.BSC) } |
| | | binding!!.rechargeLL.setOnClickListener { v -> changeBottomState(Tab.RECHARGE) } |
| | | binding!!.myLL.setOnClickListener { v -> changeBottomState(Tab.MY) } |
| | | binding!!.BSCardLL.setOnClickListener { changeBottomState(Tab.BSC) } |
| | | binding!!.myLL.setOnClickListener { changeBottomState(Tab.MY) } |
| | | binding!!.rechargeLL.setOnClickListener { changeBottomState(Tab.RECHARGE) } |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | //点击两次退出程序 有时间间隔 间隔内点击则退出程序 否则 则提示 |
| | | override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { |
| | | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| | | if ((System.currentTimeMillis() - mExitTime) > 2000) { |
| | | Toast.makeText(this@MainActivity, "再按一次退出程序", Toast.LENGTH_SHORT) |
| | | .show() |
| | | mExitTime = System.currentTimeMillis() |
| | | } else { |
| | | this@MainActivity.finish() |
| | | } |
| | | return true |
| | | } |
| | | return super.onKeyDown(keyCode, event) |
| | | } |
| | | } |