Skip to content

Commit

Permalink
Merge pull request #73 from MuindiStephen/agrodealers/hotdeals
Browse files Browse the repository at this point in the history
Agrodealers/hotdeals, Checkout & Pay.
  • Loading branch information
MuindiStephen authored Sep 11, 2024
2 parents 1a8a01e + b00afa4 commit 259860c
Show file tree
Hide file tree
Showing 39 changed files with 1,725 additions and 152 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,7 @@ dependencies {

// Pie and donut shaped-chart lib
implementation("ir.mahozad.android:pie-chart:0.7.0")

// Daraja
implementation 'com.androidstudy.daraja:daraja:2.0.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.gms.maps.model.LatLng
import com.steve_md.smartmkulima.R
import com.steve_md.smartmkulima.model.AgroDealer

/**
* Attach / bind data of verified agro-dealers within Agri-Sasa Platform
*/
class AgrodealerAdapter(
private val agrodealers: List<AgroDealer>,
private val userLocation: LatLng,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.steve_md.smartmkulima.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import com.steve_md.smartmkulima.databinding.ItemAgrodealersDealsRowBinding
import com.steve_md.smartmkulima.model.AgroDealerOffers

class AgroDealersOffersListAdapter(private val onItemClicked : (agrodealerOffers: AgroDealerOffers) -> Unit) : RecyclerView.Adapter<AgroDealersOffersListAdapter.MyViewHolder>() {

private var offers: List<AgroDealerOffers> = ArrayList()

inner class MyViewHolder(private val binding: ItemAgrodealersDealsRowBinding) :
RecyclerView.ViewHolder(binding.root) {

val buttonShopNow : Button = binding.buttonAddToCartShopNow

fun bind(offer: AgroDealerOffers) {
binding.imageViewProductFarmInputName.setImageResource(offer.productImageResId)
binding.textViewProductName.text = offer.productName
binding.textViewDiscountPercentage.text = offer.discountPercentage
binding.textViewOriginalPrice.text = "Kes. "+offer.originalPrice.toString()
binding.textViewProductPriceDiscounted.text = "Kes. "+offer.discountedPrice.toString()
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding =
ItemAgrodealersDealsRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return MyViewHolder(binding)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val offer = offers[position]
holder.bind(offer)

holder.itemView.setOnClickListener {
onItemClicked(offer)
}
holder.buttonShopNow.setOnClickListener {
onItemClicked(offer)
}
}

override fun getItemCount(): Int {
return offers.size
}

@SuppressLint("NotifyDataSetChanged")
fun submitList(offersList: List<AgroDealerOffers>) {
offers = offersList
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.steve_md.smartmkulima.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.steve_md.smartmkulima.databinding.ItemDealAgrodealerAddToCartBinding
import com.steve_md.smartmkulima.model.FarmInputAgroDealerCartItem


/**
* Will Attach to the Ui all the cart items stored temporary in the ViewModel
*/
class AgroDealsCartItemsListAdapter (private val onClickListener: OnClickListener)
: ListAdapter<FarmInputAgroDealerCartItem, AgroDealsCartItemsListAdapter.MyViewHolder>(MyDiffUtil) {
object MyDiffUtil : DiffUtil.ItemCallback<FarmInputAgroDealerCartItem>() {
override fun areItemsTheSame(oldItem: FarmInputAgroDealerCartItem, newItem: FarmInputAgroDealerCartItem): Boolean {
return oldItem.offerProduct.id == newItem.offerProduct.id
}

override fun areContentsTheSame(oldItem: FarmInputAgroDealerCartItem, newItem: FarmInputAgroDealerCartItem): Boolean {
return oldItem == newItem
}

}
inner class MyViewHolder(private val binding: ItemDealAgrodealerAddToCartBinding) :
RecyclerView.ViewHolder(binding.root) {

val btnRemoveItemFromCart = binding.buttonRemoveCartItem
val btnIncreaseQ = binding.imageButtonIncrease
val btnDecreaseQ = binding.imageButtonDecrease

@SuppressLint("SetTextI18n")
fun bind(offerAddedToCart: FarmInputAgroDealerCartItem?) {
binding.textViewProductName.text = offerAddedToCart?.offerProduct?.productName
binding.textViewProductPriceAfterDiscount.text = "Kes. "+offerAddedToCart?.offerProduct?.discountedPrice.toString()
binding.textViewDiscountPercentage.text = offerAddedToCart?.offerProduct?.discountPercentage

Glide.with(binding.imageViewProductFarmInputName)
.load(offerAddedToCart?.offerProduct?.productImageResId)
.centerCrop()
.into(binding.imageViewProductFarmInputName)

binding.textViewCartValue.text = offerAddedToCart?.quantity.toString()
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
ItemDealAgrodealerAddToCartBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val offerAddedToCart = getItem(position)
holder.bind(offerAddedToCart)

holder.btnRemoveItemFromCart.setOnClickListener {
onClickListener.onClick(offerAddedToCart)
}

holder.btnIncreaseQ.setOnClickListener {
onClickListener.onIncreaseQuantity(offerAddedToCart)
}

holder.btnDecreaseQ.setOnClickListener {
onClickListener.onDecreaseQuantity(offerAddedToCart)
}


}

class OnClickListener(
val clickListener: (cart: FarmInputAgroDealerCartItem) -> Unit,
val increaseQuantity: (cartItem: FarmInputAgroDealerCartItem) -> Unit,
val decreaseQuantity: (cartItem: FarmInputAgroDealerCartItem) -> Unit
) {
fun onClick(cart: FarmInputAgroDealerCartItem) = clickListener(cart)
fun onIncreaseQuantity(cart: FarmInputAgroDealerCartItem) = increaseQuantity(cart)
fun onDecreaseQuantity(cart: FarmInputAgroDealerCartItem) = decreaseQuantity(cart)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ class FarmAnalyticsRecordListAdapter( private val onClickListener: OnClickListen
holder.openTV.setOnClickListener {
onClickListener.onClick(summary)
}
// holder.itemView.findViewById<TextView>(com.steve_md.smartmkulima.R.id.textView125).setOnClickListener {
//
// }
}

class OnClickListener(val clickListener: (summary: FarmFinancialDataSummary) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class LocalFarmCycleAdapter(private val onClickListener: OnClickListener) :
override fun areItemsTheSame(oldItem: LocalFarmCycle, newItem: LocalFarmCycle): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: LocalFarmCycle, newItem: LocalFarmCycle): Boolean {
return oldItem.cropName == newItem.cropName
}
Expand All @@ -38,7 +37,6 @@ class LocalFarmCycleAdapter(private val onClickListener: OnClickListener) :

// Attach the crop name to the item view for later use in the notification
itemView.tag = cycle?.cropName

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.steve_md.smartmkulima.model

import android.health.connect.datatypes.units.Percentage
import android.os.Parcelable
import com.google.android.gms.maps.model.LatLng
import kotlinx.parcelize.Parcelize
Expand All @@ -13,9 +14,12 @@ data class AgroDealerData(
val longitude:Double
)


/**
* Agro-Dealer
*/
@Parcelize
data class AgroDealer(
val id: Int,
val name: String,
val phone: String,
val email: String,
Expand All @@ -25,5 +29,19 @@ data class AgroDealer(
val categories: String,
val leasingOptionsAvailable: String,
val leasingDetails: String,
val buildingLocation: String
val buildingLocation: String,
val offers: List<AgroDealerOffers>
) : Parcelable

/**
* Agro-Dealer Offers
*/
@Parcelize
data class AgroDealerOffers(
val id: Int,
var productImageResId: Int,
val productName: String,
val originalPrice: Double,
val discountedPrice: Double,
val discountPercentage: String,
) : Parcelable
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.steve_md.smartmkulima.model

/**
* The Cart item for Agricultural inputs
*/
data class FarmInputAgroDealerCartItem(
val offerProduct: AgroDealerOffers,
var quantity: Int = 1
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.navigation.fragment.findNavController
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DataSnapshot
Expand Down Expand Up @@ -66,7 +68,7 @@ class HomeDashboardFragment : Fragment() {
databaseReference = FirebaseDatabase.getInstance().getReference("users").child(userId!!)

databaseReference.child("email")
.addListenerForSingleValueEvent(object : ValueEventListener{
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val username = snapshot.getValue(String::class.java)

Expand Down Expand Up @@ -131,6 +133,28 @@ class HomeDashboardFragment : Fragment() {
// fetch Good Agricultural practices
getGoodAgriculturalPractices()
setUpRecyclerView()

requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
exitApp()
}
}

private fun exitApp() {
AlertDialog.Builder(requireContext())
.setTitle("Exit App")
.setMessage("Do you want to exit?")
.setPositiveButton("Yes") { dialog, which ->
val fragmentManager = requireActivity().supportFragmentManager
if (fragmentManager.backStackEntryCount > 0) {
val first = fragmentManager.getBackStackEntryAt(0)
fragmentManager.popBackStack(first.id, FragmentManager.POP_BACK_STACK_INCLUSIVE)
}

requireActivity().finishAffinity()

}
.setNegativeButton("No", null)
.show()
}


Expand Down
Loading

0 comments on commit 259860c

Please sign in to comment.