结算页面、设置页面
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package com.sw.dualscreen.activity
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.widget.addTextChangedListener
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.sw.dualscreen.R
|
||||
import com.sw.dualscreen.adapter.CollectedFoodNewAdapter
|
||||
import com.sw.dualscreen.databinding.ActivityCollectedFoodBinding
|
||||
import com.sw.dualscreen.databinding.LayoutEmptySearchBinding
|
||||
import com.sw.dualscreen.dialog.WarnDialog
|
||||
import com.sw.dualscreen.ext.addOnActionSearchListener
|
||||
import com.sw.dualscreen.ext.hideKeyboard
|
||||
import com.sw.dualscreen.objbox.CollectedFoodBean
|
||||
import com.sw.dualscreen.objbox.Food
|
||||
import com.sw.dualscreen.objbox.ObjectBox
|
||||
import com.sw.dualscreen.viewmodel.BaseViewModel
|
||||
import com.sw.dualscreen.viewmodel.UserViewModel
|
||||
import com.sw.plate.utils.ToastUtils
|
||||
import io.objectbox.Box
|
||||
import io.objectbox.kotlin.boxFor
|
||||
|
||||
class CollectedFoodActivity : BaseActivity<ActivityCollectedFoodBinding>() {
|
||||
private val viewModel by viewModels<UserViewModel>()
|
||||
override fun getViewModel(): BaseViewModel {
|
||||
return viewModel
|
||||
}
|
||||
|
||||
override fun inflateViewBinding(): ActivityCollectedFoodBinding {
|
||||
return ActivityCollectedFoodBinding.inflate(layoutInflater)
|
||||
}
|
||||
|
||||
private val list: MutableList<CollectedFoodBean> = mutableListOf()
|
||||
private val adapter by lazy {
|
||||
CollectedFoodNewAdapter(list).apply {
|
||||
isStateViewEnable = true
|
||||
addOnItemChildClickListener(R.id.ivDeleteFood) { _, _, position ->
|
||||
deleteGoods(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun initialize() {
|
||||
super.initialize()
|
||||
binding.rvFoodList.let {
|
||||
it.layoutManager = LinearLayoutManager(this)
|
||||
it.adapter = adapter
|
||||
}
|
||||
binding.include.let {
|
||||
it.tvPageTitle.text = "已采集餐品"
|
||||
it.ivPageBack.setOnClickListener { finish() }
|
||||
}
|
||||
binding.ivFoodSearch.setOnClickListener {
|
||||
val searchName = binding.etInputFood.text.toString().trim()
|
||||
if (searchName.isBlank()) {
|
||||
ToastUtils.showToast("请输入物品名称")
|
||||
return@setOnClickListener
|
||||
}
|
||||
getCollectGoods(searchName)
|
||||
}
|
||||
binding.etInputFood.let { v ->
|
||||
v.addOnActionSearchListener {
|
||||
val searchName = binding.etInputFood.text.toString().trim()
|
||||
if (searchName.isBlank()) {
|
||||
ToastUtils.showToast("请输入物品名称")
|
||||
return@addOnActionSearchListener
|
||||
}
|
||||
getCollectGoods(searchName)
|
||||
}
|
||||
v.addTextChangedListener {
|
||||
if (it.isNullOrBlank()) {
|
||||
getCollectGoods()
|
||||
}
|
||||
}
|
||||
}
|
||||
getCollectGoods()
|
||||
}
|
||||
|
||||
|
||||
private var box: Box<Food>? = null
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun getCollectGoods(searchName: String? = null) {
|
||||
if (box == null) {
|
||||
box = ObjectBox.boxStore.boxFor(Food::class)
|
||||
}
|
||||
list.clear()
|
||||
var queryList = box?.all?.distinctBy { it.name }
|
||||
if (searchName.isNullOrBlank().not()) {
|
||||
queryList = queryList?.filter { it.name?.contains(searchName) == true }
|
||||
}
|
||||
queryList?.forEach {
|
||||
list.add(CollectedFoodBean(foodName = it.name))
|
||||
}
|
||||
adapter.notifyDataSetChanged()
|
||||
if (list.isEmpty()) {
|
||||
loadEmptyView()
|
||||
}
|
||||
binding.root.hideKeyboard()
|
||||
}
|
||||
|
||||
private var emptyBinding: LayoutEmptySearchBinding? = null
|
||||
private fun loadEmptyView() {
|
||||
if (emptyBinding == null) {
|
||||
emptyBinding =
|
||||
LayoutEmptySearchBinding.inflate(layoutInflater, binding.rvFoodList, false)
|
||||
}
|
||||
emptyBinding?.root?.let { layout ->
|
||||
layout.setOnClickListener { layout.hideKeyboard() }
|
||||
adapter.stateView = layout
|
||||
}
|
||||
}
|
||||
|
||||
private fun deleteGoods(position: Int) {
|
||||
WarnDialog(
|
||||
context = context,
|
||||
content = "请确认是否删除菜品:${list[position].foodName}?",
|
||||
confirmBlock = {
|
||||
Thread {
|
||||
runOnUiThread {
|
||||
showWaitingDialog("加载中……")
|
||||
}
|
||||
val name = list[position].foodName
|
||||
val filterIdList = box?.all?.filter { it.name == name }?.map { it.id }
|
||||
box?.removeByIds(filterIdList)
|
||||
runOnUiThread {
|
||||
hideWaitingDialog()
|
||||
adapter.removeAt(position)
|
||||
ToastUtils.showToast("删除成功")
|
||||
if (list.isEmpty()) {
|
||||
loadEmptyView()
|
||||
}
|
||||
}
|
||||
}.start()
|
||||
}).show()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user