From 548dce0bcff06ee7a1795defe8e7789b9d7aad94 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期五, 25 四月 2025 16:52:50 +0800 Subject: [PATCH] refactor(nfc): 重构 NFC 读写助手类并优化相关功能 --- generallibrary/src/main/java/com/dayu/general/activity/MainActivity.kt | 73 ++++++++++++++++++++++++++++++++++-- 1 files changed, 69 insertions(+), 4 deletions(-) diff --git a/generallibrary/src/main/java/com/dayu/general/activity/MainActivity.kt b/generallibrary/src/main/java/com/dayu/general/activity/MainActivity.kt index 0f8a324..87af8d5 100644 --- a/generallibrary/src/main/java/com/dayu/general/activity/MainActivity.kt +++ b/generallibrary/src/main/java/com/dayu/general/activity/MainActivity.kt @@ -1,17 +1,28 @@ 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)) @@ -19,15 +30,54 @@ setupFragments() initView() initTab() + getUserInfo() + } + + override fun onNfcBack(intent: Intent) { + intent.let { nfcIntent -> + // 鑾峰彇褰撳墠鏄剧ず鐨凢ragment + val currentFragment = fragments[binding?.viewPager?.currentItem ?: 0] + + // 濡傛灉褰撳墠鏄剧ず鐨勬槸鍏呭�糉ragment锛屽垯灏哊FC淇℃伅浼犻�掔粰瀹冨鐞� + if (currentFragment is RechargeFragment) { + currentFragment.handleNfcIntent(nfcIntent) + } + } + } + private fun getUserInfo() { + // 浣跨敤姝g‘鐨勭被鍨嬪弬鏁� + 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) } } @@ -115,4 +165,19 @@ } } } + + //鐐瑰嚮涓ゆ閫�鍑虹▼搴� 鏈夋椂闂撮棿闅� 闂撮殧鍐呯偣鍑诲垯閫�鍑虹▼搴� 鍚﹀垯 鍒欐彁绀� + 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) + } } \ No newline at end of file -- Gitblit v1.8.0