package com.pickledpepper.storenolibs.adapter
import android.view.ViewGroup
import com.pickledpepper.storenolibs.common.RecyclerViewAdapterBase
import com.pickledpepper.storenolibs.common.ViewWrapper
import com.pickledpepper.storenolibs.data.FavProducts
class FavListAdapter():RecyclerViewAdapterBase<FavProducts, FavListItemView>(){
var onClickAction :((v : FavListItemView, favProduct:FavProducts) -> Unit)?= null
override fun onBindViewHolder(holder: ViewWrapper<FavListItemView>, position: Int) {
val favItem= items[position]
holder.view.apply {
bindFavListItem(favItem)
}
}
override fun onCreateItemView(parent: ViewGroup, viewType: Int): FavListItemView =
FavListItemView(parent.context)
}
package com.pickledpepper.storenolibs.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.google.firebase.auth.FirebaseAuth
import com.pickledpepper.storenolibs.R
import com.pickledpepper.storenolibs.data.FavProducts
import com.pickledpepper.storenolibs.ui.viewmodels.FavoritesViewModel
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
class FavListItemView constructor(context: Context):RelativeLayout(context) {
private var viewModel: FavoritesViewModel
lateinit var user: FirebaseAuth
init {
LayoutInflater.from(context).inflate(R.layout.favs_list_item_view, this, true)
viewModel = ViewModelProviders.of(context as FragmentActivity).get(FavoritesViewModel::class.java)
}
fun bindFavListItem(favProduct : FavProducts){
val removeFavBtn :ImageButton = findViewById(R.id.btn_remove)
val image : ImageView = findViewById(R.id.fav_listview_image)
val favImageInData = favProduct.image
Glide.with(image.context)
.load(favImageInData)
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(8,5)))
.into(image)
val user = FirebaseAuth.getInstance().currentUser
val txtName:TextView = findViewById(R.id.fav_txtview_name)
val txtPrice:TextView = findViewById(R.id.fav_txtview_price)
txtName.text = favProduct.name
txtPrice.text = favProduct.price.toString()
val btnRemoveListener = View.OnClickListener {
viewModel.deleteFav(favProduct.SKU,user?.uid?:"")
}
removeFavBtn.setOnClickListener(btnRemoveListener)
}
}