228 lines
7.5 KiB
Kotlin
228 lines
7.5 KiB
Kotlin
package com.sw.dualscreen.activity
|
||
|
||
import android.annotation.SuppressLint
|
||
import androidx.activity.viewModels
|
||
import androidx.core.widget.addTextChangedListener
|
||
import androidx.lifecycle.lifecycleScope
|
||
import androidx.recyclerview.widget.LinearLayoutManager
|
||
import com.sw.dualscreen.GlobalData
|
||
import com.sw.dualscreen.R
|
||
import com.sw.dualscreen.adapter.CollectedFoodNewAdapter
|
||
import com.sw.dualscreen.databinding.ActivityCollectedFoodBinding
|
||
import com.sw.dualscreen.dialog.RemindDialog
|
||
import com.sw.dualscreen.ext.addOnActionSearchListener
|
||
import com.sw.dualscreen.ext.gone
|
||
import com.sw.dualscreen.ext.hideKeyboard
|
||
import com.sw.dualscreen.ext.visible
|
||
import com.sw.dualscreen.objbox.CollectedFoodInfo
|
||
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 kotlinx.coroutines.launch
|
||
|
||
class CollectedFoodActivity : BaseActivity<ActivityCollectedFoodBinding>() {
|
||
|
||
companion object {
|
||
private const val PAGE_SIZE = 100
|
||
}
|
||
|
||
private val viewModel by viewModels<UserViewModel>()
|
||
override fun getViewModel(): BaseViewModel {
|
||
return viewModel
|
||
}
|
||
|
||
override fun inflateViewBinding(): ActivityCollectedFoodBinding {
|
||
return ActivityCollectedFoodBinding.inflate(layoutInflater)
|
||
}
|
||
|
||
private val list: MutableList<CollectedFoodInfo> = mutableListOf()
|
||
private val adapter by lazy {
|
||
CollectedFoodNewAdapter(list).apply {
|
||
// isStateViewEnable = true
|
||
addOnItemChildClickListener(R.id.ivDeleteFood) { _, _, position ->
|
||
loadDeleteDialog(position)
|
||
}
|
||
}
|
||
}
|
||
|
||
override fun initialize() {
|
||
super.initialize()
|
||
registerKeyEvent()
|
||
binding.root.setOnClickListener { it.hideKeyboard() }
|
||
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
|
||
}
|
||
pageNo = 1
|
||
getCollectGoods(searchName)
|
||
}
|
||
v.addTextChangedListener {
|
||
if (it.isNullOrBlank()) {
|
||
pageNo = 1
|
||
getCollectGoods()
|
||
}
|
||
}
|
||
}
|
||
binding.refreshLayout.let {
|
||
it.setEnableRefresh(true)
|
||
it.setEnableLoadMore(false)
|
||
it.setOnRefreshListener {
|
||
pageNo = 1
|
||
getCollectGoods()
|
||
}
|
||
it.setOnLoadMoreListener {
|
||
getCollectGoods()
|
||
}
|
||
}
|
||
binding.emptyInclude.root.setOnClickListener {
|
||
it.hideKeyboard()
|
||
}
|
||
getCollectGoods()
|
||
}
|
||
|
||
@SuppressLint("NotifyDataSetChanged")
|
||
private fun getCollectGoods(searchName: String? = null) {
|
||
// if (box == null) {
|
||
// box = ObjectBox.boxStore.boxFor(Food::class)
|
||
// }
|
||
// list.clear()
|
||
// val totalList = box?.all
|
||
// ?.filter { it.name!=null }
|
||
// ?.groupBy { it.name!! }
|
||
// ?.map { CollectedFoodInfo(foodName = it.key, foodCount = it.value.size) }
|
||
// //var queryMap:Map<String, List<Food>> ?= null
|
||
// if (searchName.isNullOrBlank().not()) {
|
||
// //queryMap = totalMap?.filter { it.key.contains(searchName) }
|
||
// val temp = totalList?.filter { it.foodName?.contains(searchName) == true}
|
||
// if (temp.isNullOrEmpty().not()) {
|
||
// list.addAll(temp)
|
||
// }
|
||
// } else {
|
||
// if (totalList.isNullOrEmpty().not()) {
|
||
// list.addAll(totalList)
|
||
// }
|
||
// }
|
||
// adapter.notifyDataSetChanged()
|
||
// if (list.isEmpty()) {
|
||
// loadEmptyView()
|
||
// }
|
||
|
||
viewModel.getCollectedFoodList(
|
||
pageNo = pageNo,
|
||
pageSize = PAGE_SIZE,
|
||
foodName = searchName ?: ""
|
||
) { items ->
|
||
runOnUiThread {
|
||
loadFoodList(items)
|
||
}
|
||
}
|
||
}
|
||
|
||
@SuppressLint("NotifyDataSetChanged")
|
||
private fun loadFoodList(items: List<CollectedFoodInfo>) {
|
||
finishRefresh()
|
||
if (pageNo == 1 && items.isEmpty()) {
|
||
list.clear()
|
||
adapter.notifyDataSetChanged()
|
||
loadEmptyView()
|
||
binding.refreshLayout.setEnableLoadMore(false)
|
||
return
|
||
}
|
||
binding.refreshLayout.visible()
|
||
binding.emptyInclude.root.gone()
|
||
if (pageNo == 1) {
|
||
list.clear()
|
||
}
|
||
list.addAll(items)
|
||
val isEnableLoadMore = items.size >= PAGE_SIZE
|
||
if (isEnableLoadMore) {
|
||
pageNo++
|
||
}
|
||
binding.refreshLayout.setEnableLoadMore(isEnableLoadMore)
|
||
adapter.notifyDataSetChanged()
|
||
binding.root.hideKeyboard()
|
||
}
|
||
|
||
private var pageNo = 1
|
||
|
||
// private var emptyBinding: LayoutEmptySearchBinding? = null
|
||
private fun loadEmptyView() {
|
||
binding.refreshLayout.gone()
|
||
binding.emptyInclude.root.visible()
|
||
//if (emptyBinding == null) {
|
||
// emptyBinding =
|
||
// LayoutEmptySearchBinding.inflate(layoutInflater, binding.rvFoodList, false)
|
||
//}
|
||
//emptyBinding?.root?.let { layout ->
|
||
// layout.setOnClickListener { layout.hideKeyboard() }
|
||
// adapter.stateView = layout
|
||
//}
|
||
}
|
||
|
||
private fun loadDeleteDialog(position: Int) {
|
||
RemindDialog(
|
||
context = context,
|
||
content = "请确认是否删除菜品:${list[position].foodName}?",
|
||
confirmBlock = {
|
||
deleteGoods(position)
|
||
}).show()
|
||
}
|
||
|
||
private fun deleteGoods(position: Int) {
|
||
val food = list[position]
|
||
showWaitingDialog("加载中……")
|
||
viewModel.deleteCollectFood(food.foodId, GlobalData.foodModelVersion) { deleteSuccess ->
|
||
if (deleteSuccess) {
|
||
// Thread {}.start()
|
||
lifecycleScope.launch {
|
||
val filterFoodList = ObjectBox.filter(food.foodName)
|
||
filterFoodList.forEach { it.isDel = true }
|
||
if (filterFoodList.isNotEmpty()) {
|
||
ObjectBox.putAll(filterFoodList)
|
||
}
|
||
|
||
runOnUiThread {
|
||
hideWaitingDialog()
|
||
adapter.removeAt(position)
|
||
ToastUtils.showToast("删除成功")
|
||
if (list.isEmpty()) {
|
||
loadEmptyView()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
private fun finishRefresh() {
|
||
binding.refreshLayout.let {
|
||
if (pageNo == 1) {
|
||
it.finishRefresh(500)
|
||
} else {
|
||
it.finishLoadMore(500)
|
||
}
|
||
}
|
||
}
|
||
|
||
} |