update display category list

This commit is contained in:
shaulascr
2025-06-18 00:31:52 +07:00
parent 95d5439050
commit 420208ba19
13 changed files with 254 additions and 51 deletions

View File

@ -29,6 +29,9 @@
android:theme="@style/Theme.Ecommerce_serang"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".ui.product.listproduct.ListCategoryActivity"
android:exported="false" />
<activity
android:name=".ui.product.listproduct.ListProductActivity"
android:exported="false" />

View File

@ -26,6 +26,7 @@ import com.alya.ecommerce_serang.ui.cart.CartActivity
import com.alya.ecommerce_serang.ui.notif.NotificationActivity
import com.alya.ecommerce_serang.ui.product.DetailProductActivity
import com.alya.ecommerce_serang.ui.product.category.CategoryProductsActivity
import com.alya.ecommerce_serang.ui.product.listproduct.ListCategoryActivity
import com.alya.ecommerce_serang.ui.product.listproduct.ListProductActivity
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
import com.alya.ecommerce_serang.utils.SessionManager
@ -100,6 +101,11 @@ class HomeFragment : Fragment() {
val intent = Intent(requireContext(), ListProductActivity::class.java)
startActivity(intent)
}
binding.categoryShowAll.setOnClickListener {
val intent = Intent(requireContext(), ListCategoryActivity::class.java)
startActivity(intent)
}
}
private fun setupSearchView() {

View File

@ -326,10 +326,10 @@ class CheckoutActivity : AppCompatActivity() {
}
}
// Voucher section (if implemented)
binding.layoutVoucher?.setOnClickListener {
Toast.makeText(this, "Voucher feature not implemented", Toast.LENGTH_SHORT).show()
}
// // Voucher section (if implemented)
// binding.layoutVoucher?.setOnClickListener {
// Toast.makeText(this, "Voucher feature not implemented", Toast.LENGTH_SHORT).show()
// }
}
private val addressSelectionLauncher = registerForActivityResult(

View File

@ -360,6 +360,8 @@ class DetailProductActivity : AppCompatActivity() {
val btnClose = view.findViewById<ImageButton>(R.id.btnCloseDialog)
val switchWholesale = view.findViewById<SwitchCompat>(R.id.switch_price)
val titleWholesale = view.findViewById<TextView>(R.id.tv_active_wholesale)
// val descWholesale = view.findViewById<TextView>(R.id.tv_desc_wholesale)
if (!isBuyNow) {
btnBuyNow.setText(R.string.add_to_cart)
@ -368,10 +370,18 @@ class DetailProductActivity : AppCompatActivity() {
switchWholesale.isEnabled = isWholesaleAvailable
switchWholesale.isChecked = isWholesaleSelected
// Set initial quantity based on current selection
currentQuantity = if (isWholesaleSelected) minOrder else 1
tvQuantity.text = currentQuantity.toString()
if (isWholesaleAvailable){
switchWholesale.visibility = View.VISIBLE
Toast.makeText(this, "Minimal pembelian grosir $currentQuantity produk", Toast.LENGTH_SHORT).show()
} else {
switchWholesale.visibility = View.GONE
}
// Set initial quantity based on current selection
switchWholesale.setOnCheckedChangeListener { _, isChecked ->
isWholesaleSelected = isChecked

View File

@ -0,0 +1,112 @@
package com.alya.ecommerce_serang.ui.product.listproduct
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alya.ecommerce_serang.data.api.dto.CategoryItem
import com.alya.ecommerce_serang.data.api.retrofit.ApiConfig
import com.alya.ecommerce_serang.data.repository.ProductRepository
import com.alya.ecommerce_serang.databinding.ActivityListCategoryBinding
import com.alya.ecommerce_serang.ui.product.category.CategoryProductsActivity
import com.alya.ecommerce_serang.utils.BaseViewModelFactory
import com.alya.ecommerce_serang.utils.SessionManager
import com.alya.ecommerce_serang.utils.viewmodel.HomeViewModel
class ListCategoryActivity : AppCompatActivity() {
companion object {
private val TAG = "ListCategoryActivity"
}
private lateinit var binding: ActivityListCategoryBinding
private lateinit var sessionManager: SessionManager
private var categoryAdapter: ListCategoryAdapter? = null
private val viewModel: HomeViewModel by viewModels {
BaseViewModelFactory {
val apiService = ApiConfig.getApiService(sessionManager)
val repository = ProductRepository(apiService)
HomeViewModel(repository)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityListCategoryBinding.inflate(layoutInflater)
setContentView(binding.root)
sessionManager = SessionManager(this)
WindowCompat.setDecorFitsSystemWindows(window, false)
enableEdgeToEdge()
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
val systemBars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.setPadding(
systemBars.left,
systemBars.top,
systemBars.right,
systemBars.bottom
)
windowInsets
}
setupToolbar()
setupObserver()
viewModel.loadCategory()
setupRecyclerView()
}
private fun setupToolbar(){
binding.header.headerLeftIcon.setOnClickListener{
finish()
}
binding.header.headerTitle.text = "Kategori Produk"
}
private fun setupRecyclerView(){
categoryAdapter = ListCategoryAdapter(
categories = emptyList(),
onClick = { category -> handleClickOnCategory(category) }
)
binding.rvListCategories.apply {
adapter = categoryAdapter
layoutManager = GridLayoutManager(
context,
3, // 3 columns
RecyclerView.VERTICAL,
false
)
}
}
private fun setupObserver(){
viewModel.category.observe(this) { category ->
Log.d("ListCategoryActivity", "Received categories: ${category.size}")
updateCategories(category)
}
}
private fun updateCategories(category: List<CategoryItem>){
if (category.isEmpty()) {
binding.rvListCategories.visibility = View.GONE
} else {
binding.rvListCategories.visibility = View.VISIBLE
categoryAdapter?.updateData(category)
}
}
private fun handleClickOnCategory(category: CategoryItem){
val intent = Intent(this, CategoryProductsActivity::class.java)
intent.putExtra(CategoryProductsActivity.EXTRA_CATEGORY, category)
startActivity(intent)
}
}

View File

@ -0,0 +1,55 @@
package com.alya.ecommerce_serang.ui.product.listproduct
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.alya.ecommerce_serang.BuildConfig.BASE_URL
import com.alya.ecommerce_serang.R
import com.alya.ecommerce_serang.data.api.dto.CategoryItem
import com.alya.ecommerce_serang.databinding.ItemCategoryHomeBinding
import com.bumptech.glide.Glide
class ListCategoryAdapter(
private var categories:List<CategoryItem>,
private val onClick:(category: CategoryItem) -> Unit
): RecyclerView.Adapter<ListCategoryAdapter.ViewHolder>() {
inner class ViewHolder(private val binding: ItemCategoryHomeBinding): RecyclerView.ViewHolder(binding.root){
fun bind(category: CategoryItem) = with(binding) {
Log.d("CategoriesAdapter", "Binding category: ${category.name}, Image: ${category.image}")
val fullImageUrl = if (category.image.startsWith("/")) {
BASE_URL + category.image.removePrefix("/") // Append base URL if the path starts with "/"
} else {
category.image // Use as is if it's already a full URL
}
Log.d("CategoriesAdapter", "Loading image: $fullImageUrl")
Glide.with(itemView.context)
.load(fullImageUrl) // Ensure full URL
.placeholder(R.drawable.placeholder_image)
.into(imageCategory)
name.text = category.name
root.setOnClickListener { onClick(category) }
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder (
ItemCategoryHomeBinding.inflate(LayoutInflater.from(parent.context),parent,false)
)
override fun getItemCount() = categories.size
override fun onBindViewHolder(holder: ListCategoryAdapter.ViewHolder, position: Int) {
holder.bind(categories[position])
}
fun updateData(newCategories: List<CategoryItem>) {
categories = newCategories.toList()
notifyDataSetChanged()
}
}

View File

@ -69,7 +69,7 @@ class ListProductActivity : AppCompatActivity() {
private fun setupRecyclerView() {
binding.rvProducts.apply {
binding.rvProductsList.apply {
adapter = productAdapter
layoutManager = GridLayoutManager(
context,
@ -112,14 +112,14 @@ class ListProductActivity : AppCompatActivity() {
private fun updateProducts(products: List<ProductsItem>, storeMap: Map<Int, StoreItem>) {
if (products.isEmpty()) {
Log.d(TAG, "Product list is empty, hiding RecyclerView")
binding.rvProducts.visibility = View.VISIBLE
binding.rvProductsList.visibility = View.VISIBLE
} else {
Log.d(TAG, "Displaying product list in RecyclerView")
binding.rvProducts.visibility = View.VISIBLE // <-- Fix here
binding.rvProductsList.visibility = View.VISIBLE // <-- Fix here
productAdapter = ListProductAdapter(products, onClick = { product ->
handleProductClick(product)
}, storeMap = storeMap)
binding.rvProducts.adapter = productAdapter
binding.rvProductsList.adapter = productAdapter
productAdapter?.updateProducts(products)
}
}

View File

@ -1,6 +1,8 @@
package com.alya.ecommerce_serang.utils.viewmodel
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.alya.ecommerce_serang.data.api.dto.CategoryItem
@ -25,6 +27,9 @@ class HomeViewModel (
private val _storeMap = MutableStateFlow<Map<Int, StoreItem>>(emptyMap())
val storeMap: StateFlow<Map<Int, StoreItem>> = _storeMap.asStateFlow()
private val _category = MutableLiveData<List<CategoryItem>>()
val category: LiveData<List<CategoryItem>> get() = _category
init {
loadProducts()
loadCategories()
@ -73,6 +78,16 @@ class HomeViewModel (
}
}
fun loadCategory() {
viewModelScope.launch {
when (val result = productRepository.getAllCategories()) {
is Result.Success -> _category.value = result.data
is Result.Error -> Log.e("HomeViewModel", "Failed to fetch categories", result.exception)
is Result.Loading -> {}
}
}
}
fun retry() {
loadProducts()

View File

@ -142,35 +142,6 @@
android:layout_height="8dp"
android:background="#F5F5F5" />
<!-- Voucher Section -->
<LinearLayout
android:id="@+id/layout_voucher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/white"
android:padding="16dp"
android:gravity="center_vertical">
<!-- <ImageView-->
<!-- android:layout_width="24dp"-->
<!-- android:layout_height="24dp"-->
<!-- android:src="@drawable/ic_voucher_24" />-->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gunakan Voucher"
android:textSize="14sp"
android:layout_marginStart="8dp" />
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_arrow_right" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="8dp"

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.product.listproduct.ListCategoryActivity">
<include
android:id="@+id/header"
layout="@layout/header" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvListCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:clipChildren="false"
android:clipToPadding="false"
app:layout_constraintTop_toBottomOf="@id/header"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:listitem="@layout/item_category_home" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -8,12 +8,14 @@
tools:context=".ui.product.listproduct.ListProductActivity">
<include
android:id="@+id/searchContainer"
android:id="@+id/searchContainerList"
layout="@layout/view_search_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/rvProductsList"/>
<!-- <com.google.android.material.divider.MaterialDivider-->
<!-- android:id="@+id/divider_product"-->
@ -23,11 +25,11 @@
<!-- app:layout_constraintTop_toBottomOf="@id/searchContainer"/>-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvProducts"
android:id="@+id/rvProductsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="23dp"
app:layout_constraintTop_toBottomOf="@id/searchContainer"
app:layout_constraintTop_toBottomOf="@id/searchContainerList"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="4dp"
app:spanCount="2"

View File

@ -39,14 +39,15 @@
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Minimal pembelian 10 buah untuk harga grosir"
android:fontFamily="@font/dmsans_mediumitalic"
android:textSize="12sp"
android:textColor="@color/black_300"
android:layout_marginBottom="8dp"/>
<!-- <TextView-->
<!-- android:id="@+id/tv_desc_wholesale"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Minimal pembelian 10 buah untuk harga grosir"-->
<!-- android:fontFamily="@font/dmsans_mediumitalic"-->
<!-- android:textSize="12sp"-->
<!-- android:textColor="@color/black_300"-->
<!-- android:layout_marginBottom="8dp"/>-->
<androidx.constraintlayout.widget.ConstraintLayout

View File

@ -43,7 +43,7 @@
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/showAll"
android:id="@+id/categoryShowAll"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"