Browse Source

product model

dola 1 year ago
parent
commit
7d60292243

+ 48 - 0
app/src/main/java/com/sambath/kunkhmer/adapter/ProductAdapter.kt

@@ -0,0 +1,48 @@
+package com.sambath.kunkhmer.adapter
+
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.ImageView
+import android.widget.TextView
+import androidx.recyclerview.widget.RecyclerView
+import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.config.Config
+import com.sambath.kunkhmer.remote.ProductInfo
+import com.squareup.picasso.Picasso
+
+class ProductAdapter(var cardList: MutableList<ProductInfo>, private val listener: ProductAdapter.OnItemClickListener) : RecyclerView.Adapter<ProductAdapter.ViewHolder>() {
+    interface OnItemClickListener {
+        fun onItemClick(position: Int)
+    }
+
+    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductAdapter.ViewHolder {
+        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_product_item_card, parent, false)
+        return ViewHolder(itemView)
+    }
+
+    override fun onBindViewHolder(holder: ProductAdapter.ViewHolder, position: Int) {
+        val currentItem = cardList[position]
+
+        Picasso.get().load(Config.BASE_URL + "/v1/product/image/" + currentItem.filename).into(holder.imageView)
+        holder.subTitleTextView.text = currentItem.desc
+        holder.titleTextView.text = currentItem.name
+    }
+
+    override fun getItemCount() = cardList.size
+
+    inner class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
+        val imageView: ImageView = itemView.findViewById(R.id.product_image)
+        val subTitleTextView: TextView = itemView.findViewById(R.id.product_desc)
+        val titleTextView: TextView = itemView.findViewById(R.id.product_title)
+
+        init {
+            itemView.setOnClickListener {
+                val position = adapterPosition
+                if (position != RecyclerView.NO_POSITION) {
+                    listener.onItemClick(position)
+                }
+            }
+        }
+    }
+}

+ 3 - 0
app/src/main/java/com/sambath/kunkhmer/adapter/ProductCardItem.kt

@@ -0,0 +1,3 @@
+package com.sambath.kunkhmer.adapter
+
+data class ProductCardItem(val id: String, val imageResource: String, val title: String, val subTitle: String, val price: Double)

+ 36 - 0
app/src/main/java/com/sambath/kunkhmer/remote/ProductDataModel.kt

@@ -0,0 +1,36 @@
+package com.sambath.kunkhmer.remote
+
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class ProductResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: ProductData?
+)
+
+@JsonClass(generateAdapter = true)
+data class ProductData(
+    @Json(name = "object")
+    val obj: MutableList<ProductInfo>,
+)
+
+@JsonClass(generateAdapter = true)
+data class ProductInfo(
+    @Json(name = "_id")
+    val productId: String,
+    @Json(name = "price")
+    val price: Int,
+    @Json(name = "name")
+    val name: String,
+    @Json(name = "desc")
+    val desc: String,
+    @Json(name = "createdAt")
+    val createdAt: String,
+    @Json(name = "filename")
+    val filename: String,
+)

+ 3 - 0
app/src/main/java/com/sambath/kunkhmer/remote/service/ApiService.kt

@@ -28,6 +28,9 @@ interface ApiService {
 
 
     @GET("/v1/event/getAll")
     @GET("/v1/event/getAll")
     fun getEvents(): Single<EventResponse>
     fun getEvents(): Single<EventResponse>
+
+    @GET("/v1/product/getAll")
+    fun getProducts(): Single<ProductResponse>
     ///---------------------------------------------------------
     ///---------------------------------------------------------
 
 
     @POST("/v1/api/user/changepassword")
     @POST("/v1/api/user/changepassword")

+ 39 - 1
app/src/main/java/com/sambath/kunkhmer/screen/shop/ShopFragment.kt

@@ -5,13 +5,23 @@ 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 androidx.recyclerview.widget.LinearLayoutManager
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.adapter.ProductAdapter
+import com.sambath.kunkhmer.adapter.ProductCardItem
+import com.sambath.kunkhmer.app.App
+import com.sambath.kunkhmer.screen.news.NewsViewState
+import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNews
+import kotlinx.android.synthetic.main.fragment_shop.view.recyclerViewShop
 
 
 
 
-class ShopFragment : Fragment() {
+class ShopFragment : Fragment(), ProductAdapter.OnItemClickListener {
 
 
     private var _root: View? = null
     private var _root: View? = null
     private val binding get() = _root!!
     private val binding get() = _root!!
+    private var cardList: List<ProductCardItem>? = null
+    private lateinit var productAdapter: ProductAdapter
+    private lateinit var shopViewModel: ShopViewModel
 
 
     override fun onCreate(savedInstanceState: Bundle?) {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         super.onCreate(savedInstanceState)
@@ -22,6 +32,34 @@ class ShopFragment : Fragment() {
         savedInstanceState: Bundle?
         savedInstanceState: Bundle?
     ): View? {
     ): View? {
         _root = inflater.inflate(R.layout.fragment_shop, container, false)
         _root = inflater.inflate(R.layout.fragment_shop, container, false)
+
+        shopViewModel = ShopViewModel(App.injectApiService(), App.injectPrefHelper())
+        productAdapter = ProductAdapter(mutableListOf(), this)
+        shopViewModel.getProducts()
+
+        binding.recyclerViewShop.apply {
+            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+            adapter = productAdapter
+        }
+
+        shopViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
         return binding
         return binding
     }
     }
+
+    private fun render(state: ShopViewModel.ProductViewState) {
+        if (state.isLoginSuccess) {
+            if(state.productData != null) {
+                productAdapter.cardList = state.productData
+                productAdapter.notifyDataSetChanged()
+            }
+        }
+        // Other state handling...
+    }
+
+    override fun onItemClick(position: Int) {
+        TODO("Not yet implemented")
+    }
 }
 }

+ 50 - 0
app/src/main/java/com/sambath/kunkhmer/screen/shop/ShopViewModel.kt

@@ -0,0 +1,50 @@
+package com.sambath.kunkhmer.screen.shop
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import com.sambath.kunkhmer.app.getErrorCode
+import com.sambath.kunkhmer.base.BaseViewModel
+import com.sambath.kunkhmer.remote.LivesInfo
+import com.sambath.kunkhmer.remote.ProductInfo
+import com.sambath.kunkhmer.remote.service.ApiService
+import com.sambath.kunkhmer.util.PrefHelper
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
+import java.util.concurrent.TimeUnit
+
+class ShopViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
+    private val _state = MutableLiveData(ProductViewState())
+    val state: LiveData<ProductViewState> = _state
+    private fun prev() = _state.value!!
+
+    data class ProductViewState(
+        val initial: Boolean = false,
+        val isProgress: Boolean = false,
+        val isLoginSuccess: Boolean = false,
+        val error: String? = null,
+        val productData: MutableList<ProductInfo>? = null,
+    )
+
+    fun getProducts() {
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        disposables.add(
+            apiService.getProducts()
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == 1) {
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, productData = it.data!!.obj)
+                    } else {
+                        _state.value =
+                            prev().copy(isProgress = false, error = "[${it.message.description}]")
+                    }
+                }, {
+                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
+        )
+    }
+}

+ 8 - 15
app/src/main/res/layout/fragment_shop.xml

@@ -1,24 +1,17 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.appcompat.widget.LinearLayoutCompat 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"
     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"
-    android:background="@drawable/ads_popout"
+    android:orientation="vertical"
+    android:background="@color/color_black"
     tools:context=".screen.shop.ShopFragment">
     tools:context=".screen.shop.ShopFragment">
 
 
-    <View
+    <androidx.recyclerview.widget.RecyclerView
+        android:id="@+id/recyclerViewShop"
         android:layout_width="match_parent"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_height="match_parent"
-        android:alpha="0.7"
-        android:background="@color/color_black" />
+        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
 
 
-    <!-- TODO: Update blank fragment layout -->
-    <TextView
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:gravity="center"
-        android:text="@string/hello_blank_fragment"
-        android:textColor="@color/color_white"
-        android:textSize="25sp" />
-
-</FrameLayout>
+</androidx.appcompat.widget.LinearLayoutCompat>

+ 38 - 0
app/src/main/res/layout/layout_product_item_card.xml

@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical">
+
+    <androidx.appcompat.widget.LinearLayoutCompat
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical">
+
+        <ImageView
+            android:id="@+id/product_image"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:padding="10dp"
+            android:src="@drawable/ic_logo_header" />
+
+        <TextView
+            android:id="@+id/product_title"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:padding="5dp"
+            android:text="Product title"
+            android:textSize="14sp"
+            android:textStyle="bold" />
+
+        <TextView
+            android:id="@+id/product_desc"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:text="Product title"
+            android:textSize="12sp" />
+    </androidx.appcompat.widget.LinearLayoutCompat>
+
+</androidx.cardview.widget.CardView>