feat(activity): 新增 BaseActivity 沉浸式基类和 HomeActivity 首页,统一 Activity 继承关系
- 新增 BaseActivity:继承 ComponentActivity,隐藏状态栏和导航栏,底部上滑可临时唤出导航栏 - 新增 HomeActivity(XML 版本)及对应布局 activity_home.xml,作为应用启动入口 - GoodsListActivity、FoodCollectionActivity、LocalImagePreviewActivity、PurchaseOrderActivity 统一改为继承 BaseActivity - 更新 AndroidManifest.xml:HomeActivity 注册为启动入口 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package com.sw.inbound.activity
|
||||
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.activity.viewModels
|
||||
import com.sw.inbound.base.BaseActivity
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import com.sw.inbound.databinding.ActivityHomeBinding
|
||||
import com.sw.inbound.model.response.User
|
||||
import com.sw.inbound.objbox.FoodModule
|
||||
import com.sw.inbound.sdk.SensorScaleUtils
|
||||
import com.sw.inbound.utils.DateTimeUtils
|
||||
import com.sw.inbound.utils.ThreadUtils
|
||||
import com.sw.inbound.utils.ext.gone
|
||||
import com.sw.inbound.utils.ext.startActivity
|
||||
import com.sw.inbound.utils.ext.visible
|
||||
import com.sw.inbound.viewmodel.UserViewModel
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* 首页 Activity(XML 版本)
|
||||
* 对应原 HomeScreen.kt Compose 版本,原文件保留不动
|
||||
* 功能:展示标题栏(含实时时间或用户信息)和两个功能入口(采购单入库、自采入库)
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class HomeActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: ActivityHomeBinding
|
||||
|
||||
/** 用于获取用户信息的 ViewModel */
|
||||
private val viewModel: UserViewModel by viewModels()
|
||||
|
||||
/** 每秒刷新当前时间的 Handler */
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
/** 时间刷新任务,每 1 秒更新一次标题栏右侧时间文字 */
|
||||
private val timeRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
binding.tvTime.text = DateTimeUtils.getChineseDateString()
|
||||
handler.postDelayed(this, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityHomeBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
initViews()
|
||||
observeUser()
|
||||
SensorScaleUtils.startScale(autoScale = true)
|
||||
lifecycleScope.launch {
|
||||
FoodModule.init(this@HomeActivity)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
SensorScaleUtils.closeScale()
|
||||
ThreadUtils.release()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化各控件的点击事件
|
||||
*/
|
||||
private fun initViews() {
|
||||
// 标题点击:跳转菜品采集页面
|
||||
binding.tvTitle.setOnClickListener {
|
||||
startActivity<FoodCollectionActivity>()
|
||||
}
|
||||
|
||||
// 采购单入库入口
|
||||
binding.ivOrderPurchase.setOnClickListener {
|
||||
startActivity<PurchaseOrderActivity>()
|
||||
}
|
||||
|
||||
// 自采入库入口,传入非采购单标志
|
||||
binding.ivOrderSelfProcurement.setOnClickListener {
|
||||
startActivity<SelfProcurementActivity> {
|
||||
putExtra(GoodsListActivity.IS_RECEIPT_PAGE, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 观察用户登录状态,动态切换标题栏右侧显示内容:
|
||||
* - 已登录:显示用户名 + 退出图标
|
||||
* - 未登录:显示实时时间
|
||||
*/
|
||||
private fun observeUser() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.user.collect { user ->
|
||||
updateUserView(user)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据登录状态切换标题栏右侧 UI
|
||||
* @param user 当前用户信息,为 null 表示未登录
|
||||
*/
|
||||
private fun updateUserView(user: User?) {
|
||||
if (user != null) {
|
||||
// 已登录:显示用户名和退出图标,隐藏时间
|
||||
binding.tvTime.gone()
|
||||
binding.llUserInfo.visible()
|
||||
binding.tvUserName.text = user.name ?: "用户"
|
||||
} else {
|
||||
// 未登录:显示实时时间,隐藏用户信息
|
||||
binding.llUserInfo.gone()
|
||||
binding.tvTime.visible()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// 页面可见时启动时间刷新
|
||||
handler.post(timeRunnable)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
// 页面不可见时停止刷新,避免内存泄漏
|
||||
handler.removeCallbacks(timeRunnable)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user