|
@@ -5,13 +5,12 @@ import androidx.fragment.app.Fragment
|
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
-import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
+import androidx.appcompat.widget.SearchView
|
|
|
+import androidx.recyclerview.widget.GridLayoutManager
|
|
|
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 com.sambath.kunkhmer.remote.ProductInfo
|
|
|
import kotlinx.android.synthetic.main.fragment_shop.view.recyclerViewShop
|
|
|
|
|
|
|
|
@@ -19,7 +18,7 @@ class ShopFragment : Fragment(), ProductAdapter.OnItemClickListener {
|
|
|
|
|
|
private var _root: View? = null
|
|
|
private val binding get() = _root!!
|
|
|
- private var cardList: List<ProductCardItem>? = null
|
|
|
+ private var cardList: List<ProductInfo>? = null
|
|
|
private lateinit var productAdapter: ProductAdapter
|
|
|
private lateinit var shopViewModel: ShopViewModel
|
|
|
|
|
@@ -38,7 +37,7 @@ class ShopFragment : Fragment(), ProductAdapter.OnItemClickListener {
|
|
|
shopViewModel.getProducts()
|
|
|
|
|
|
binding.recyclerViewShop.apply {
|
|
|
- layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
|
|
+ layoutManager = GridLayoutManager(context, 2)
|
|
|
adapter = productAdapter
|
|
|
}
|
|
|
|
|
@@ -46,13 +45,37 @@ class ShopFragment : Fragment(), ProductAdapter.OnItemClickListener {
|
|
|
render(it)
|
|
|
})
|
|
|
|
|
|
+ val searchView = binding.findViewById<SearchView>(R.id.searchView)
|
|
|
+ searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
|
|
+ override fun onQueryTextSubmit(query: String?): Boolean {
|
|
|
+ searchView.clearFocus() // close the keyboard after submitting the query
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onQueryTextChange(newText: String?): Boolean {
|
|
|
+ filter(newText.orEmpty())
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
return binding
|
|
|
}
|
|
|
|
|
|
+ private fun filter(query: String) {
|
|
|
+ val filteredList = cardList?.filter {
|
|
|
+ it.name.contains(query, ignoreCase = true)
|
|
|
+ }
|
|
|
+
|
|
|
+ productAdapter.cardList = filteredList.orEmpty().toMutableList()
|
|
|
+ productAdapter.notifyDataSetChanged()
|
|
|
+ }
|
|
|
+
|
|
|
private fun render(state: ShopViewModel.ProductViewState) {
|
|
|
if (state.isLoginSuccess) {
|
|
|
if(state.productData != null) {
|
|
|
productAdapter.cardList = state.productData
|
|
|
+ cardList = productAdapter.cardList
|
|
|
productAdapter.notifyDataSetChanged()
|
|
|
}
|
|
|
}
|
|
@@ -60,6 +83,5 @@ class ShopFragment : Fragment(), ProductAdapter.OnItemClickListener {
|
|
|
}
|
|
|
|
|
|
override fun onItemClick(position: Int) {
|
|
|
- TODO("Not yet implemented")
|
|
|
}
|
|
|
}
|