221 lines
9.4 KiB
Kotlin
221 lines
9.4 KiB
Kotlin
package com.sw.inbound.adapter
|
||
|
||
import android.content.Context
|
||
import android.text.InputType
|
||
import android.view.LayoutInflater
|
||
import android.view.ViewGroup
|
||
import android.widget.EditText
|
||
import android.widget.LinearLayout
|
||
import android.widget.TextView
|
||
import androidx.core.graphics.toColorInt
|
||
import androidx.core.view.updateLayoutParams
|
||
import androidx.core.widget.addTextChangedListener
|
||
import androidx.recyclerview.widget.RecyclerView
|
||
import com.chad.library.adapter4.BaseQuickAdapter
|
||
import com.chad.library.adapter4.viewholder.QuickViewHolder
|
||
import com.sw.inbound.R
|
||
import com.sw.inbound.databinding.ListItemReceiptGoodsBinding
|
||
import com.sw.inbound.dialog.WarnDialog
|
||
import com.sw.inbound.ext.toFormattedString
|
||
import com.sw.inbound.ext.toSafeDouble
|
||
import com.sw.inbound.model.response.GoodsInfo
|
||
import com.sw.inbound.utils.ext.dp
|
||
import com.sw.inbound.utils.ext.gone
|
||
import com.sw.inbound.utils.ext.hideKeyboard
|
||
import com.sw.inbound.utils.ext.ifNullOrBlank
|
||
import com.sw.inbound.utils.ext.ifNullOrZero
|
||
import com.sw.inbound.utils.ext.roundedDecimalPlace
|
||
import com.sw.inbound.utils.ext.setShapeDrawable
|
||
import com.sw.inbound.utils.ext.toast
|
||
import com.sw.inbound.utils.ext.visible
|
||
import com.sw.inbound.utils.removeTrailingZeros
|
||
|
||
class ReceiptGoodsAdapter(var list: MutableList<GoodsInfo>) :
|
||
BaseQuickAdapter<GoodsInfo, ReceiptGoodsAdapter.VH>(list) {
|
||
|
||
inner class VH(var binding: ListItemReceiptGoodsBinding) : QuickViewHolder(binding.root)
|
||
|
||
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
|
||
val inflater = LayoutInflater.from(context)
|
||
val binding = ListItemReceiptGoodsBinding.inflate(inflater, parent, false)
|
||
return VH(binding)
|
||
}
|
||
|
||
override fun onBindViewHolder(holder: VH, position: Int, item: GoodsInfo?) {
|
||
holder.binding.run {
|
||
root.updateLayoutParams<RecyclerView.LayoutParams> {
|
||
height = 100.dp
|
||
topMargin = 2.dp
|
||
bottomMargin = 3.dp
|
||
}
|
||
root.setShapeDrawable(
|
||
solidColor = if (item!!.isSelected) "#FFD5E1FF" else "#FFFFFFFF",
|
||
radius = 6.dp
|
||
)
|
||
root.setOnClickListener {
|
||
it.hideKeyboard()
|
||
}
|
||
updateTextView(
|
||
tvGoodsName, tvProcurementUnit,
|
||
tvProcurementCount, tvGoodsWeight,
|
||
etReceiptCount, etReceiptAmount
|
||
//tvReceiptCount, tvProcurementPrice, tvProcurementAmount,
|
||
)
|
||
tvGoodsName.text = item.goodName.ifNullOrBlank("-")
|
||
tvProcurementUnit.text = item.unitName.ifNullOrBlank("-")
|
||
val realCount = item.receiveCount?:0.toDouble()
|
||
tvProcurementCount.text = if (realCount == 0.toDouble()) "-" else realCount.removeTrailingZeros()
|
||
//tvProcurementPrice.text = item.recUnitPriceTaxInBak.toSafeDouble(2).ifNullOrZero("-")
|
||
//tvProcurementAmount.text = item.recPriceExItem.toSafeDouble(2).ifNullOrZero("-")
|
||
//tvReceiptCount.text = item.receivedNum.toSafeDouble(2).ifNullOrZero("-")
|
||
|
||
tvGoodsWeight.text = doubleFormat(item.goodsWeight?.toDouble())
|
||
|
||
tvReceiptPrice.text = doubleFormat(item.recUnitPriceTaxIn)
|
||
|
||
if (item.unitHint.isNullOrBlank().not()) {
|
||
unitDivider.visible()
|
||
tvUnitHint.run {
|
||
visible()
|
||
text = item.unitHint?.replace(",", "\n")
|
||
}
|
||
}
|
||
|
||
tvState.gone()
|
||
ivState.run {
|
||
visible()
|
||
setImageResource(
|
||
if (item.isSelected.not()) R.mipmap.ic_receipt_no
|
||
else if (item.isLocalGoods) R.mipmap.ic_receipt_local
|
||
else if ((item.receivedNum?:0.toDouble()) < (item.receiveCount?:0.toDouble())) R.mipmap.ic_receipt_part
|
||
else R.mipmap.ic_receipt_all
|
||
)
|
||
}
|
||
etReceiptCount.run {
|
||
isEnabled = item.receivedNum != null && item.receivedNum != 0.toDouble()
|
||
updateLayoutParams<LinearLayout.LayoutParams> {
|
||
topMargin = if (isEnabled) 20.dp else 0
|
||
bottomMargin = if (isEnabled) 20.dp else 0
|
||
leftMargin = if (isEnabled) 20.dp else 0
|
||
rightMargin = if (isEnabled) 20.dp else 0
|
||
}
|
||
if (isEnabled) {
|
||
setShapeDrawable(strokeColor = "#FFDCDCF0", strokeWidth = 2.dp, radius = 8.dp)
|
||
val unit = item.unitName
|
||
inputType = if (unit == "千克" || unit == "公斤" || unit == "斤") {
|
||
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
|
||
} else {
|
||
InputType.TYPE_CLASS_NUMBER
|
||
}
|
||
setText(item.receivedNum?.removeTrailingZeros())
|
||
setSelection(length())
|
||
} else {
|
||
setText("-")
|
||
}
|
||
}
|
||
etReceiptAmount.run {
|
||
isEnabled = item.recPriceInItem != null && item.recPriceInItem != 0.toDouble()
|
||
updateLayoutParams<LinearLayout.LayoutParams> {
|
||
topMargin = if (isEnabled) 20.dp else 0
|
||
bottomMargin = if (isEnabled) 20.dp else 0
|
||
leftMargin = if (isEnabled) 20.dp else 0
|
||
rightMargin = if (isEnabled) 20.dp else 0
|
||
}
|
||
if (isEnabled) {
|
||
setShapeDrawable(strokeColor = "#FFDCDCF0", strokeWidth = 2.dp, radius = 6.dp)
|
||
setText(item.recPriceInItem?.removeTrailingZeros())
|
||
setSelection(length())
|
||
} else {
|
||
setText("-")
|
||
val colorString = if (item.isSelected) "#FFD5E1FF" else "#FFFFFFFF"
|
||
setBackgroundColor(colorString.toColorInt())
|
||
}
|
||
}
|
||
addEditListener(etReceiptCount) {
|
||
if (it == 0.toDouble()) {
|
||
context.toast("数量不能为0")
|
||
etReceiptCount.setText(item.receivedNum?.removeTrailingZeros())
|
||
return@addEditListener
|
||
}
|
||
item.receivedNum = it
|
||
item.recUnitPriceTaxIn = item.recPriceInItem!! / it
|
||
tvReceiptPrice.text = "${item.recUnitPriceTaxIn?.roundedDecimalPlace(2)}"
|
||
}
|
||
addEditListener(etReceiptAmount) {
|
||
if (it == 0.toDouble()) {
|
||
context.toast("金额不能为0")
|
||
etReceiptAmount.setText(item.recPriceInItem?.removeTrailingZeros())
|
||
return@addEditListener
|
||
}
|
||
item.recPriceInItem = it
|
||
item.recUnitPriceTaxIn = it / item.receivedNum!!
|
||
tvReceiptPrice.text = "${item.recUnitPriceTaxIn?.roundedDecimalPlace(2)}"
|
||
}
|
||
ivClear.run {
|
||
visible()
|
||
setOnClickListener {
|
||
if (item.isLocalGoods) {
|
||
//删除行数据弹窗
|
||
showWarnDialog(content = "请确认是否删除该条采购数据,删除后不可恢复?") {
|
||
list.removeAt(position)
|
||
notifyDataSetChanged()
|
||
}
|
||
return@setOnClickListener
|
||
}
|
||
//清除数据,恢复默认
|
||
showWarnDialog(content = "请确认是否清除添加的收货信息,清除后不可恢复?") {
|
||
item.receivedNum = item.receivedNumBak
|
||
item.recPriceInItem = 0.0
|
||
item.recUnitPriceTaxIn = 0.0
|
||
item.unitName = item.unitNameBak
|
||
notifyItemChanged(position)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private fun showWarnDialog(content:String, callback:()-> Unit) {
|
||
WarnDialog(context = context, content = content, confirmBlock = callback).show()
|
||
}
|
||
|
||
private fun updateTextView(vararg views: TextView) {
|
||
views.forEach {
|
||
it.setTextColor("#FF1E2832".toColorInt())
|
||
it.paint.isFakeBoldText = true
|
||
}
|
||
}
|
||
|
||
private fun addEditListener(editText: EditText, action: (Double) -> Unit) {
|
||
editText.run {
|
||
addTextChangedListener(onTextChanged = { it, _, _, _ ->
|
||
try {
|
||
if (editText.isFocused.not()) {
|
||
return@addTextChangedListener
|
||
}
|
||
val input = it.toString()
|
||
if (input.startsWith(".")) {
|
||
setText("0.")
|
||
setSelection(length())
|
||
} else if (input.contains(".")) {
|
||
val index = input.indexOf(".")
|
||
if (input.length - index > 3) {
|
||
setText(input.substring(0, index + 3))
|
||
setSelection(length())
|
||
}
|
||
}
|
||
val value = if (input == "0.") 0.0 else input.toDouble()
|
||
action(value)
|
||
} catch (e: Exception) {
|
||
e.printStackTrace()
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
private fun doubleFormat(num:Double?): String{
|
||
val realNum = num?:0.toDouble()
|
||
return if (realNum == 0.toDouble()) "-" else realNum.toFormattedString(2)
|
||
}
|
||
|
||
} |