Created
April 3, 2020 10:04
-
-
Save firdaus1453/6e88614da39ecd9de7b00a140684164b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package id.gits.shober.cart.ui.setorders | |
| import android.content.Context | |
| import android.text.Editable | |
| import android.text.TextWatcher | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.EditText | |
| import android.widget.ImageView | |
| import android.widget.TextView | |
| import androidx.recyclerview.widget.RecyclerView | |
| import id.gits.core.data.local.model.product.Product | |
| import id.gits.core.helper.extensions.currencyFormatToRupiah | |
| import id.gits.core.helper.extensions.loadImageRounded | |
| import id.gits.core.helper.extensions.safe | |
| import id.gits.core.widget.QuantityView | |
| import id.gits.core.widget.TextWatcherEditText | |
| import id.gits.shober.cart.R | |
| import kotlinx.android.synthetic.main.item_set_orders.view.* | |
| /** | |
| * Created by Muhammad Firdaus on 02/04/2020. | |
| */ | |
| class SetOrdersAdapter(val context: Context, private val mListener: SetOrdersUserActionListener, val mViewModel: SetOrdersViewModel) : RecyclerView.Adapter<SetOrdersAdapter.ViewHolder>() { | |
| private val items: MutableList<Product> = mutableListOf() | |
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
| return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_set_orders, parent, false)) | |
| } | |
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
| holder.bind(items[position], mListener, position, mViewModel) | |
| } | |
| override fun getItemCount(): Int { | |
| return items.size | |
| } | |
| fun replaceData(data: List<Product>) { | |
| items.clear() | |
| items.addAll(data) | |
| notifyDataSetChanged() | |
| } | |
| fun updateItem(model: Product, position: Int) { | |
| items[position] = model | |
| notifyItemChanged(position) | |
| } | |
| class ViewHolder(val mView: View) : RecyclerView.ViewHolder(mView) { | |
| lateinit var editLastPrice: TextWatcherEditText | |
| lateinit var quantityView: QuantityView | |
| lateinit var textNameStore: TextView | |
| lateinit var textPriceOriginal: TextView | |
| lateinit var textUntung: TextView | |
| lateinit var textSize: TextView | |
| lateinit var textColor: TextView | |
| lateinit var ivImageProduct: ImageView | |
| lateinit var mData: Product | |
| lateinit var mViewModel: SetOrdersViewModel | |
| var mPosition = 0 | |
| fun bind(data: Product, listener: SetOrdersUserActionListener, position: Int, mViewModel: SetOrdersViewModel) { | |
| // Initiate data | |
| mData = data | |
| mPosition = position | |
| this.mViewModel = mViewModel | |
| // Initiate view | |
| mView.apply { | |
| editLastPrice = et_last_price | |
| quantityView = qv_jumlah_set_orders | |
| textNameStore = tv_name_product_set_orders | |
| textPriceOriginal = tv_price_set_orders | |
| textUntung = tv_untung_didapat | |
| textSize = tv_size_set_orders | |
| textColor = tv_color_set_orders | |
| ivImageProduct = iv_product_set_orders | |
| } | |
| // Clear textwatcher | |
| editLastPrice.clearTextChangedListeners() | |
| editLastPrice.onTextChanged { value -> | |
| mData.lastPrice = value | |
| mViewModel.lastPriceChange(mData, position) | |
| } | |
| // Set content data | |
| addValueToView() | |
| setImageProduct() | |
| onClickListener() | |
| } | |
| private fun addValueToView() { | |
| mView.apply { | |
| textNameStore.text = mData.name | |
| val mUntung = mData.untung.currencyFormatToRupiah() | |
| textUntung.text = mUntung | |
| textSize.text = "L" | |
| textColor.text = "Merah" | |
| val mQty = mData.qty ?: 0 | |
| quantityView.quantity = mQty | |
| val mSale = mData.totalSale.currencyFormatToRupiah() | |
| textPriceOriginal.text = mSale | |
| } | |
| } | |
| private fun setImageProduct() { | |
| mView.apply { | |
| val mImages = mData.images | |
| if (!mImages.isNullOrEmpty()) { | |
| mImages.forEach { | |
| if (it.url?.isNotEmpty() == true) { | |
| ivImageProduct.loadImageRounded(context, it.url.safe()) | |
| } | |
| } | |
| } else { | |
| ivImageProduct.loadImageRounded(context, "") | |
| } | |
| } | |
| } | |
| private fun onClickListener() { | |
| mView.apply { | |
| mViewModel.apply { | |
| quantityView.onButtonClickListener = object : QuantityView.OnButtonClickListener { | |
| override fun onCLickNegativeButton() { | |
| changeQuantity(quantityView.quantity, mData, mPosition, false) | |
| } | |
| override fun onClickPlusButton() { | |
| changeQuantity(quantityView.quantity, mData, mPosition, true) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| private fun EditText.onTextChanged(valueToPass: (Double) -> Unit) { | |
| this.addTextChangedListener(object : TextWatcher { | |
| override fun afterTextChanged(s: Editable?) { | |
| } | |
| override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |
| } | |
| override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | |
| valueToPass(s.toString().toDoubleOrNull().safe(0.0)) | |
| } | |
| }) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment