左晓为主开发手持机充值管理机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.dayu.general.activity
 
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
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.bean.net.WaterPriceResult
import com.dayu.general.databinding.ActivityMainBinding
import com.dayu.general.net.ApiManager
import com.dayu.general.net.BaseResponse
 
class MainActivity : BaseNfcActivity() {
 
    var binding: ActivityMainBinding? = null
    private val fragments: ArrayList<Fragment> = ArrayList()
    var mExitTime: Long = 0
    private val handler = Handler(Looper.getMainLooper())
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(binding?.root)
        
        // 注册MainActivity实例到BaseApplication
        BaseApplication.setMainActivity(this)
        
        setupFragments()
        initView()
        initTab()
        getUserInfo()
        
        // 延时20秒后获取水价
        handler.postDelayed({
            getWaterPriceFromActivity()
        }, 20000) // 20秒延时
    }
 
    override fun onDestroy() {
        super.onDestroy()
        // 清理Handler回调,防止内存泄漏
        handler.removeCallbacksAndMessages(null)
        // 清理BaseApplication中的MainActivity引用
        BaseApplication.setMainActivity(null)
    }
 
    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)
            }
        }
    }
 
    /**
     * 获取水价信息 - 公开方法供其他地方调用
     */
    fun getWaterPriceFromActivity() {
        // 如果水价已存在且大于0,则不重复获取
        if (BaseApplication.waterPrice > 0.0) {
            return
        }
        
        ApiManager.getInstance().requestGetHideLoading(
            this,
            "terminal/client/getWaterPrice",
            WaterPriceResult::class.java,
            null,
            object : SubscriberListener<BaseResponse<WaterPriceResult>>() {
                override fun onNext(response: BaseResponse<WaterPriceResult>) {
                    if (response.success && response.code == "0001") {
                        // 获取水价成功,保存到BaseApplication
                        response.content?.let { waterPriceResult ->
                            BaseApplication.waterPrice = waterPriceResult.price
                        }
                    }
                }
 
                override fun onError(e: Throwable?) {
                    super.onError(e)
                    // 网络异常时不显示错误信息,避免影响用户体验
                }
            }
        )
    }
 
    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 {  changeBottomState(Tab.BSC) }
        binding!!.myLL.setOnClickListener { changeBottomState(Tab.MY) }
        binding!!.rechargeLL.setOnClickListener { changeBottomState(Tab.RECHARGE) }
    }
 
 
    private fun setupFragments() {
        fragments.add(BSCardFragment())
        fragments.add(RechargeFragment())
        fragments.add(MyFragment())
    }
 
    private fun initTab() {
        binding?.viewPager?.adapter = TabAdapter(this, fragments)
        binding?.viewPager?.currentItem = (1)
        binding?.viewPager?.offscreenPageLimit = 3
        binding?.viewPager?.isUserInputEnabled = false
    }
 
    private enum class Tab {
        BSC, RECHARGE, MY
    }
 
    /**
     * 修改底部状态
     */
    private fun changeBottomState(tab: Tab) {
        resetTabState()
        when (tab) {
            Tab.BSC -> updateTabUI(0, R.drawable.bottom_card_white, R.color.white)
            Tab.RECHARGE -> updateTabUI(1, R.drawable.bottom_recharge_white, R.color.white)
            Tab.MY -> updateTabUI(2, R.drawable.bottom_my_white, R.color.white)
        }
    }
 
    /**
     * 重置所有 Tab 的默认状态
     */
    private fun resetTabState() {
        binding!!.BSCardImg.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.bottom_card_black
            )
        )
        binding!!.BSCardText.setTextColor(ContextCompat.getColor(this, R.color.black))
 
        binding!!.rechargeImg.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.bottom_recharge_black
            )
        )
        binding!!.rechargeText.setTextColor(ContextCompat.getColor(this, R.color.black))
 
        binding!!.myImg.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.bottom_my_black
            )
        )
        binding!!.myText.setTextColor(ContextCompat.getColor(this, R.color.black))
    }
 
    /**
     * 更新某个 Tab 的 UI 状态
     */
    private fun updateTabUI(position: Int, iconResId: Int, textColorResId: Int) {
        if (position == 1) {
            binding!!.viewPager.setCurrentItem(position, true)
        } else {
            binding!!.viewPager.setCurrentItem(position, false)
        }
        when (position) {
            0 -> {
                binding!!.BSCardImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId))
                binding!!.BSCardText.setTextColor(ContextCompat.getColor(this, textColorResId))
            }
 
            1 -> {
                binding!!.rechargeImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId))
                binding!!.rechargeText.setTextColor(ContextCompat.getColor(this, textColorResId))
            }
 
            2 -> {
                binding!!.myImg.setImageDrawable(ContextCompat.getDrawable(this, iconResId))
                binding!!.myText.setTextColor(ContextCompat.getColor(this, textColorResId))
            }
        }
    }
 
    //点击两次退出程序 有时间间隔 间隔内点击则退出程序 否则 则提示
    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)
    }
}