product change stock and price

This commit is contained in:
Gracia Hotmauli
2025-06-20 01:50:39 +07:00
parent 454498d6eb
commit 3c97b3b3de
10 changed files with 286 additions and 59 deletions

View File

@ -5,56 +5,47 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.alya.ecommerce_serang.R import com.alya.ecommerce_serang.R
import com.alya.ecommerce_serang.data.api.dto.ProductsItem
import com.alya.ecommerce_serang.databinding.FragmentChangePriceBottomSheetBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
// TODO: Rename parameter arguments, choose names that match class ChangePriceBottomSheetFragment(
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private val product: ProductsItem,
private const val ARG_PARAM1 = "param1" private val onSave: (productId: Int, newPrice: Int) -> Unit
private const val ARG_PARAM2 = "param2" ) : BottomSheetDialogFragment() {
private var _binding: FragmentChangePriceBottomSheetBinding? = null
/** private val binding get() = _binding!!
* A simple [Fragment] subclass.
* Use the [ChangePriceBottomSheetFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ChangePriceBottomSheetFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View {
// Inflate the layout for this fragment _binding = FragmentChangePriceBottomSheetBinding.inflate(inflater, container, false)
return inflater.inflate(R.layout.fragment_change_price_bottom_sheet, container, false)
binding.header.headerTitle.text = "Atur Harga"
binding.header.headerLeftIcon.setImageResource(R.drawable.ic_close)
binding.header.headerLeftIcon.setOnClickListener { dismiss() }
binding.edtPrice.setText(product.price)
binding.btnSave.setOnClickListener {
val newPrice = binding.edtPrice.text.toString().replace(".", "").toIntOrNull()
if (newPrice != null && newPrice > 0) {
onSave(product.id, newPrice)
dismiss()
} else {
Toast.makeText(requireContext(), "Masukkan harga yang valid", Toast.LENGTH_SHORT).show()
}
}
return binding.root
} }
companion object { override fun onDestroyView() {
/** super.onDestroyView()
* Use this factory method to create a new instance of _binding = null
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ChangePriceBottomSheetFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
ChangePriceBottomSheetFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
} }
} }

View File

@ -0,0 +1,69 @@
package com.alya.ecommerce_serang.ui.profile.mystore.product
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.alya.ecommerce_serang.R
import com.alya.ecommerce_serang.data.api.dto.ProductsItem
import com.alya.ecommerce_serang.databinding.FragmentChangeStockBottomSheetBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
class ChangeStockBottomSheetFragment(
private val product: ProductsItem,
private val onSave: (productId: Int, newStock: Int) -> Unit
): BottomSheetDialogFragment() {
private var _binding: FragmentChangeStockBottomSheetBinding? = null
private val binding get() = _binding!!
private var stock = 0
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentChangeStockBottomSheetBinding.inflate(inflater, container, false)
binding.header.headerTitle.text = "Atur Stok"
binding.header.headerLeftIcon.setImageResource(R.drawable.ic_close)
binding.header.headerLeftIcon.setOnClickListener { dismiss() }
stock = product.stock
updateStock()
binding.btnMinus.setOnClickListener {
if (stock > 0) stock--
updateStock()
}
binding.btnPlus.setOnClickListener {
stock++
updateStock()
}
binding.btnSave.setOnClickListener {
onSave(product.id, stock)
dismiss()
}
return binding.root
}
private fun updateStock() {
binding.edtStock.setText(stock.toString())
if (stock == 0) {
binding.btnMinus.isEnabled = false
binding.btnMinus.setColorFilter(ContextCompat.getColor(requireContext(), R.color.black_100))
} else {
binding.btnMinus.isEnabled = true
binding.btnMinus.setColorFilter(ContextCompat.getColor(requireContext(), R.color.blue_500))
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

View File

@ -20,9 +20,10 @@ class ProductActivity : AppCompatActivity() {
private lateinit var binding: ActivityProductBinding private lateinit var binding: ActivityProductBinding
private lateinit var sessionManager: SessionManager private lateinit var sessionManager: SessionManager
private lateinit var productAdapter: ProductAdapter
private val viewModel: ProductViewModel by viewModels { private val viewModel: ProductViewModel by viewModels {
BaseViewModelFactory { BaseViewModelFactory {
sessionManager = SessionManager(this)
val apiService = ApiConfig.getApiService(sessionManager) val apiService = ApiConfig.getApiService(sessionManager)
val productRepository = ProductRepository(apiService) val productRepository = ProductRepository(apiService)
ProductViewModel(productRepository) ProductViewModel(productRepository)
@ -30,6 +31,8 @@ class ProductActivity : AppCompatActivity() {
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
sessionManager = SessionManager(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = ActivityProductBinding.inflate(layoutInflater) binding = ActivityProductBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
@ -56,9 +59,18 @@ class ProductActivity : AppCompatActivity() {
is Result.Success -> { is Result.Success -> {
binding.progressBar.visibility = View.GONE binding.progressBar.visibility = View.GONE
val products = result.data val products = result.data
binding.rvStoreProduct.adapter = ProductAdapter(products) {
Toast.makeText(this, "Produk: ${it.name}", Toast.LENGTH_SHORT).show() productAdapter = ProductAdapter(
} products,
onItemClick = { products ->
Toast.makeText(this, "Produk ${products.name} diklik", Toast.LENGTH_SHORT).show()
},
onUpdateProduct = { productId, updatedFields ->
viewModel.updateProduct(productId, updatedFields)
}
)
binding.rvStoreProduct.adapter = productAdapter
} }
is Result.Error -> { is Result.Error -> {
binding.progressBar.visibility = View.GONE binding.progressBar.visibility = View.GONE
@ -66,6 +78,19 @@ class ProductActivity : AppCompatActivity() {
} }
} }
} }
viewModel.productUpdateResult.observe(this) { result ->
when (result) {
is Result.Success -> {
Toast.makeText(this, "Produk berhasil diperbarui", Toast.LENGTH_SHORT).show()
viewModel.loadMyStoreProducts()
}
is Result.Error -> {
Toast.makeText(this, "Gagal memperbarui produk", Toast.LENGTH_SHORT).show()
}
else -> {}
}
}
} }
private fun setupHeader() { private fun setupHeader() {

View File

@ -3,6 +3,7 @@ package com.alya.ecommerce_serang.ui.profile.mystore.product
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import android.widget.PopupMenu import android.widget.PopupMenu
import android.widget.TextView import android.widget.TextView
@ -15,10 +16,14 @@ import com.alya.ecommerce_serang.R
import com.alya.ecommerce_serang.data.api.dto.Product import com.alya.ecommerce_serang.data.api.dto.Product
import com.alya.ecommerce_serang.data.api.dto.ProductsItem import com.alya.ecommerce_serang.data.api.dto.ProductsItem
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
class ProductAdapter( class ProductAdapter(
private val products: List<ProductsItem>, private val products: List<ProductsItem>,
private val onItemClick: (ProductsItem) -> Unit private val onItemClick: (ProductsItem) -> Unit,
private val onUpdateProduct: (productId: Int, updatedFields: Map<String, RequestBody>) -> Unit
) : RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() { ) : RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
inner class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
@ -28,6 +33,8 @@ class ProductAdapter(
private val tvProductStock: TextView = itemView.findViewById(R.id.tv_product_stock) private val tvProductStock: TextView = itemView.findViewById(R.id.tv_product_stock)
private val tvProductStatus: TextView = itemView.findViewById(R.id.tv_product_status) private val tvProductStatus: TextView = itemView.findViewById(R.id.tv_product_status)
private val ivMenu: ImageView = itemView.findViewById(R.id.iv_menu) private val ivMenu: ImageView = itemView.findViewById(R.id.iv_menu)
private val btnChangePrice: Button = itemView.findViewById(R.id.btn_change_price)
private val btnChangeStock: Button = itemView.findViewById(R.id.btn_change_stock)
fun bind(product: ProductsItem) { fun bind(product: ProductsItem) {
tvProductName.text = product.name tvProductName.text = product.name
@ -55,7 +62,6 @@ class ProductAdapter(
.into(ivProduct) .into(ivProduct)
ivMenu.setOnClickListener { ivMenu.setOnClickListener {
// Show Bottom Sheet when menu is clicked
val bottomSheetFragment = ProductOptionsBottomSheetFragment(product) val bottomSheetFragment = ProductOptionsBottomSheetFragment(product)
bottomSheetFragment.show( bottomSheetFragment.show(
(itemView.context as FragmentActivity).supportFragmentManager, (itemView.context as FragmentActivity).supportFragmentManager,
@ -63,6 +69,36 @@ class ProductAdapter(
) )
} }
btnChangePrice.setOnClickListener {
val bottomSheetFragment = ChangePriceBottomSheetFragment(product) { id, newPrice ->
val body = mapOf(
"product_id" to id.toString().toRequestBody("text/plain".toMediaTypeOrNull()),
"price" to newPrice.toString().toRequestBody("text/plain".toMediaTypeOrNull())
)
onUpdateProduct(id, body)
Toast.makeText(itemView.context, "Harga berhasil diubah", Toast.LENGTH_SHORT).show()
}
bottomSheetFragment.show(
(itemView.context as FragmentActivity).supportFragmentManager,
bottomSheetFragment.tag
)
}
btnChangeStock.setOnClickListener {
val bottomSheetFragment = ChangeStockBottomSheetFragment(product) { id, newStock ->
val body = mapOf(
"product_id" to id.toString().toRequestBody("text/plain".toMediaTypeOrNull()),
"stock" to newStock.toString().toRequestBody("text/plain".toMediaTypeOrNull())
)
onUpdateProduct(id, body)
Toast.makeText(itemView.context, "Stok berhasil diubah", Toast.LENGTH_SHORT).show()
}
bottomSheetFragment.show(
(itemView.context as FragmentActivity).supportFragmentManager,
bottomSheetFragment.tag
)
}
itemView.setOnClickListener { itemView.setOnClickListener {
onItemClick(product) onItemClick(product)
} }

View File

@ -6,6 +6,7 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.alya.ecommerce_serang.data.api.response.store.sells.Orders import com.alya.ecommerce_serang.data.api.response.store.sells.Orders
import com.alya.ecommerce_serang.data.api.response.store.sells.OrdersItem import com.alya.ecommerce_serang.data.api.response.store.sells.OrdersItem
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
@ -13,6 +14,7 @@ import com.alya.ecommerce_serang.data.repository.AddressRepository
import com.alya.ecommerce_serang.data.repository.SellsRepository import com.alya.ecommerce_serang.data.repository.SellsRepository
import com.alya.ecommerce_serang.databinding.ActivityDetailShipmentBinding import com.alya.ecommerce_serang.databinding.ActivityDetailShipmentBinding
import com.alya.ecommerce_serang.ui.profile.mystore.sells.SellsProductAdapter import com.alya.ecommerce_serang.ui.profile.mystore.sells.SellsProductAdapter
import com.alya.ecommerce_serang.ui.profile.mystore.sells.payment.DetailPaymentActivity
import com.alya.ecommerce_serang.utils.BaseViewModelFactory import com.alya.ecommerce_serang.utils.BaseViewModelFactory
import com.alya.ecommerce_serang.utils.SessionManager import com.alya.ecommerce_serang.utils.SessionManager
import com.alya.ecommerce_serang.utils.viewmodel.AddressViewModel import com.alya.ecommerce_serang.utils.viewmodel.AddressViewModel
@ -51,6 +53,12 @@ class DetailShipmentActivity : AppCompatActivity() {
finish() finish()
} }
productAdapter = SellsProductAdapter()
binding.rvProductItems.apply {
adapter = productAdapter
layoutManager = LinearLayoutManager(this@DetailShipmentActivity)
}
val sellsJson = intent.getStringExtra("sells_data") val sellsJson = intent.getStringExtra("sells_data")
if (sellsJson != null) { if (sellsJson != null) {
try { try {

View File

@ -111,9 +111,9 @@ class ProductViewModel(private val repository: ProductRepository) : ViewModel()
fun updateProduct( fun updateProduct(
productId: Int?, productId: Int?,
data: Map<String, RequestBody>, data: Map<String, RequestBody>,
image: MultipartBody.Part?, image: MultipartBody.Part? = null,
halal: MultipartBody.Part?, halal: MultipartBody.Part? = null,
sppirt: MultipartBody.Part? sppirt: MultipartBody.Part? = null
) { ) {
viewModelScope.launch { viewModelScope.launch {
_productUpdateResult.postValue(Result.Loading) _productUpdateResult.postValue(Result.Loading)

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 B

View File

@ -1,14 +1,50 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.profile.mystore.product.ChangePriceBottomSheetFragment"> tools:context=".ui.profile.mystore.product.ChangePriceBottomSheetFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout --> <include
<TextView android:id="@+id/header"
layout="@layout/header" />
<!-- Input Harga dengan Prefix "Rp" -->
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:text="@string/hello_blank_fragment" /> android:orientation="horizontal"
android:background="@drawable/bg_text_field"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="16dp">
</FrameLayout> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rp"
style="@style/label_medium_prominent"
android:textColor="@color/black_300"
android:padding="8dp" />
<EditText
android:id="@+id/edt_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Isi harga produk di sini"
android:inputType="number"
android:padding="8dp"
style="@style/body_small" />
</LinearLayout>
<Button
android:id="@+id/btn_save"
android:text="Simpan"
style="@style/button.large.active.long"
android:enabled="true"
android:layout_marginVertical="16dp"
android:layout_marginHorizontal="16dp"/>
</LinearLayout>

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.profile.mystore.product.ChangeStockBottomSheetFragment"
android:orientation="vertical">
<include
android:id="@+id/header"
layout="@layout/header" />
<!-- Input Harga dengan Prefix "Rp" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/bg_text_field"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="16dp"
android:gravity="center">
<ImageView
android:id="@+id/btn_minus"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="5dp"
android:src="@drawable/ic_minus"
android:clickable="true"
android:focusable="true"/>
<EditText
android:id="@+id/edt_stock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:hint="Isi stok produk di sini"
android:inputType="number"
android:padding="8dp"
style="@style/body_small" />
<ImageView
android:id="@+id/btn_plus"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="5dp"
android:src="@drawable/ic_add"
android:clickable="true"
android:focusable="true"/>
</LinearLayout>
<Button
android:id="@+id/btn_save"
android:text="Simpan"
style="@style/button.large.active.long"
android:enabled="true"
android:layout_marginVertical="16dp"
android:layout_marginHorizontal="16dp"/>
</LinearLayout>