feat(activity): 新增已采集食材列表页,支持搜索、删除及空视图展示
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
package com.shuwei.dish.match.ui
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.graphics.Color
|
||||
import android.graphics.Typeface
|
||||
import android.os.Bundle
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.text.style.StyleSpan
|
||||
import android.view.LayoutInflater
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.core.widget.addTextChangedListener
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.shuwei.dish.match.R
|
||||
import com.shuwei.dish.match.adapter.CollectedFoodAdapter
|
||||
import com.shuwei.dish.match.base.BaseActivity
|
||||
import com.shuwei.dish.match.databinding.ActivityCollectedFoodBinding
|
||||
import com.shuwei.dish.match.databinding.LayoutEmptyViewBinding
|
||||
import com.shuwei.dish.match.dialog.CommonDialog
|
||||
import com.shuwei.dish.match.entity.CollectedFoodItem
|
||||
import com.shuwei.dish.match.utils.KeyboardUtil
|
||||
import com.shuwei.dish.match.utils.ext.appendText
|
||||
import com.shuwei.dish.match.utils.ext.buildSpannableString
|
||||
import com.shuwei.dish.match.utils.ext.gone
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
import com.shuwei.dish.match.utils.ext.visible
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* 已采集食材列表页面
|
||||
* 展示所有已采集的食材及其采集数量,支持搜索和删除
|
||||
*/
|
||||
class CollectedFoodActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: ActivityCollectedFoodBinding
|
||||
|
||||
private val foodList = mutableListOf<CollectedFoodItem>()
|
||||
|
||||
private var emptyViewBinding: LayoutEmptyViewBinding? = null
|
||||
|
||||
private val adapter by lazy {
|
||||
CollectedFoodAdapter(foodList).apply {
|
||||
isStateViewEnable = true
|
||||
// 点击删除按钮,移除对应食材
|
||||
addOnItemChildClickListener(R.id.ivDelete) { _, _, position ->
|
||||
showDeleteDialog(foodList[position].foodName)
|
||||
}
|
||||
setOnItemClickListener { _, view, _ ->
|
||||
KeyboardUtil.hideKeyboard(view)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除弹窗
|
||||
*/
|
||||
private fun showDeleteDialog(foodName: String) {
|
||||
val showFoodName = foodName.split("WP").first()
|
||||
val separator = if (showFoodName.length <= 10) "\n" else ""
|
||||
val content = buildSpannableString {
|
||||
appendText("食材:")
|
||||
appendText(showFoodName, StyleSpan(Typeface.BOLD), ForegroundColorSpan(Color.BLACK))
|
||||
appendText(" 删除后无法恢复,${separator}确认要删除吗?")
|
||||
}
|
||||
CommonDialog(this)
|
||||
.setTitle("温馨提示")
|
||||
.setContent(content)
|
||||
.setNegativeButton("取消")
|
||||
.setPositiveButton("确认") {
|
||||
appViewModel.removeCollectedFood(
|
||||
foodName = foodName,
|
||||
nameFilter = binding.editSearch.text.toString()
|
||||
)
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
statusBarDarkFont(enable = true)
|
||||
binding = ActivityCollectedFoodBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
setHeaderBackground()
|
||||
binding.root.setOnClickListener { v ->
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
setTitleBar(titleBarAction = {
|
||||
it.visible()
|
||||
it.setOnClickListener { v ->
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
}, titleAction = {
|
||||
it.text = "已采集食材"
|
||||
}, rightIconActon = {
|
||||
it.gone()
|
||||
}, backAction = {
|
||||
it.visible()
|
||||
it.setOnClickListener {
|
||||
finish()
|
||||
}
|
||||
})
|
||||
initRecyclerView()
|
||||
initSearch()
|
||||
initObserver()
|
||||
loadCollectedFoodList()
|
||||
}
|
||||
|
||||
private fun loadCollectedFoodList(name: String = "") {
|
||||
appViewModel.loadCollectedFoodList(nameFilter = name)
|
||||
}
|
||||
|
||||
/**
|
||||
* 收集 searchFoodState,统一处理 Loading / Success / Error 状态
|
||||
*/
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun initObserver() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
appViewModel.collectedFoodListState.collect { items ->
|
||||
foodList.clear()
|
||||
foodList.addAll(items)
|
||||
adapter.notifyDataSetChanged()
|
||||
if (items.isEmpty()) {
|
||||
loadEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 展示空视图 */
|
||||
private fun loadEmptyView() {
|
||||
if (emptyViewBinding == null) {
|
||||
emptyViewBinding = LayoutEmptyViewBinding.inflate(
|
||||
LayoutInflater.from(this), binding.rvCollectedFood, false
|
||||
)
|
||||
}
|
||||
emptyViewBinding!!.tvContent.text = "暂无数据"
|
||||
emptyViewBinding!!.tvSubContent.text = "未查询到食材信息"
|
||||
adapter.stateView = emptyViewBinding!!.root
|
||||
}
|
||||
|
||||
/** 初始化列表 */
|
||||
private fun initRecyclerView() {
|
||||
binding.rvCollectedFood.run {
|
||||
layoutManager = LinearLayoutManager(this@CollectedFoodActivity)
|
||||
adapter = this@CollectedFoodActivity.adapter
|
||||
}
|
||||
}
|
||||
|
||||
/** 初始化搜索栏交互 */
|
||||
private fun initSearch() {
|
||||
binding.btnSearch.setOnClickListener { v ->
|
||||
performSearch {
|
||||
toast(binding.editSearch.hint.toString())
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
binding.editSearch.run {
|
||||
addTextChangedListener {
|
||||
performSearch {
|
||||
loadCollectedFoodList()
|
||||
}
|
||||
}
|
||||
setOnEditorActionListener { v, actionId, _ ->
|
||||
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
||||
performSearch {
|
||||
toast(binding.editSearch.hint.toString())
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 根据关键词过滤食材列表 */
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun performSearch(action: () -> Unit) {
|
||||
val keyword = binding.editSearch.text.toString().trim()
|
||||
if (keyword.isEmpty()) {
|
||||
action()
|
||||
return
|
||||
}
|
||||
// 根据关键词从数据库查询并刷新列表
|
||||
loadCollectedFoodList(keyword)
|
||||
}
|
||||
}
|
||||
@@ -256,7 +256,7 @@ class DishSamplingActivity : BaseActivity() {
|
||||
setSelection(text.length)
|
||||
}
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
|
||||
private fun searchDishType() {
|
||||
|
||||
@@ -119,7 +119,7 @@ class FoodSearchActivity : BaseActivity() {
|
||||
binding.ivDishSearch.setOnClickListener { v ->
|
||||
pageNo = 1
|
||||
getInputAndSearch()
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
binding.refreshLayout.setEnableRefresh(true)
|
||||
binding.refreshLayout.setEnableLoadMore(false)
|
||||
@@ -136,13 +136,13 @@ class FoodSearchActivity : BaseActivity() {
|
||||
queryListInfo(foodName!!)
|
||||
}
|
||||
binding.root.setOnClickListener { v ->
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
binding.etInputDish.let { v ->
|
||||
v.addOnActionSearchListener {
|
||||
pageNo = 1
|
||||
getInputAndSearch()
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
KeyboardUtil.hideKeyboard( v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -551,7 +551,7 @@ class PrepareCookActivity : BaseActivity() {
|
||||
setSelection(text.length)
|
||||
}
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(v.context, v)
|
||||
KeyboardUtil.hideKeyboard(v)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -120,7 +120,7 @@ class SelectDishActivity : BaseActivity() {
|
||||
putExtra(FoodSearchActivity.DINNER_TYPE, dinnerType)
|
||||
putExtra(FoodSearchActivity.FOOD_NAME, searchText)
|
||||
}
|
||||
KeyboardUtil.hideKeyboard(this, binding.etInputDish)
|
||||
KeyboardUtil.hideKeyboard(binding.etInputDish)
|
||||
}
|
||||
|
||||
private fun loadFragment() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.shuwei.dish.match.ui.fragment
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.net.Uri
|
||||
@@ -22,6 +23,7 @@ import com.shuwei.dish.match.databinding.LayoutCameraPreviewBinding
|
||||
import com.shuwei.dish.match.dialog.Loading
|
||||
import com.shuwei.dish.match.objbox.FoodCollectionBean
|
||||
import com.shuwei.dish.match.objbox.FoodModule
|
||||
import com.shuwei.dish.match.ui.CollectedFoodActivity
|
||||
import com.shuwei.dish.match.ui.SingleFragmentActivity
|
||||
import com.shuwei.dish.match.utils.BitmapSaver
|
||||
import com.shuwei.dish.match.utils.CameraUtils
|
||||
@@ -243,7 +245,7 @@ class CollectFragment : BaseFragment<FragmentCollectBinding>(){
|
||||
}
|
||||
|
||||
binding.btnCollectedFood.setOnClickListener {
|
||||
// startActivity(Intent(requireActivity(), CollectedFoodActivity::class.java))
|
||||
startActivity(Intent(requireActivity(), CollectedFoodActivity::class.java))
|
||||
}
|
||||
binding.btnTakePhoto.clickWithDebounce {
|
||||
val count = foodCollectionList.count { it.bitmap != null }
|
||||
|
||||
Reference in New Issue
Block a user