Skip to content

Commit

Permalink
Added cart feature
Browse files Browse the repository at this point in the history
  • Loading branch information
madhurmehta007 committed Jul 10, 2022
1 parent 989f8e7 commit fa08862
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ android {
}

dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

implementation 'com.google.firebase:firebase-firestore:24.2.0'
def nav_version = "2.4.2"
def nav_version = '2.5.0'

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
Expand Down Expand Up @@ -78,12 +78,12 @@ dependencies {
implementation 'com.github.denzcoskun:ImageSlideshow:0.1.0'

//Room
def roomVersion = "2.4.2"
def roomVersion = "2.5.0-alpha02"

implementation("androidx.room:room-runtime:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")
implementation("androidx.room:room-ktx:$roomVersion")

//Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1")
implementation('org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.3')
}
43 changes: 30 additions & 13 deletions app/src/main/java/devs/redfox/local_e_commerce/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import devs.redfox.local_e_commerce.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
var i = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
Expand All @@ -22,20 +23,36 @@ class MainActivity : AppCompatActivity() {
popupMenu.inflate(R.menu.bottom_nav)
binding.bottomBar.setupWithNavController(popupMenu.menu,navController)

navController.addOnDestinationChangedListener(object: NavController.OnDestinationChangedListener{
override fun onDestinationChanged(
controller: NavController,
destination: NavDestination,
arguments: Bundle?
) {
title = when(destination.id){
R.id.cartFragment -> "My Cart"
R.id.moreFragment -> "My Dashboard"
R.id.homeFragment -> "Home"
else -> "LocalECommerce"
// navController.addOnDestinationChangedListener(object: NavController.OnDestinationChangedListener{
// override fun onDestinationChanged(
// controller: NavController,
// destination: NavDestination,
// arguments: Bundle?
// ) {
// title = when(destination.id){
// R.id.cartFragment -> "My Cart"
// R.id.moreFragment -> "My Dashboard"
// R.id.homeFragment -> "Home"
// else -> "LocalECommerce"
// }
// }
//
// })
binding.bottomBar.onItemSelected = {
when(it){
0 -> {
i=0
navController.navigate(R.id.homeFragment)
}
1 -> i=1
2 -> i=2
}

})
}
}
override fun onBackPressed(){
super.onBackPressed()
if(i==0){
finish()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package devs.redfox.local_e_commerce.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.denzcoskun.imageslider.constants.ScaleTypes
import com.denzcoskun.imageslider.models.SlideModel
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import devs.redfox.local_e_commerce.MainActivity
import devs.redfox.local_e_commerce.R
import devs.redfox.local_e_commerce.database.AppDatabase
import devs.redfox.local_e_commerce.database.ProductDao
import devs.redfox.local_e_commerce.database.ProductModel
import devs.redfox.local_e_commerce.databinding.ActivityCategoryBinding
import devs.redfox.local_e_commerce.databinding.ActivityProductDetailBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ProductDetailActivity : AppCompatActivity() {
private lateinit var binding: ActivityProductDetailBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityProductDetailBinding.inflate(layoutInflater)
setContentView(binding.root)


getProductDetails(intent.getStringExtra("id"))
setContentView(binding.root)


}

Expand All @@ -27,19 +37,72 @@ class ProductDetailActivity : AppCompatActivity() {
Firebase.firestore.collection("products")
.document(proId!!).get().addOnSuccessListener {
val list = it.get("productImages") as ArrayList<String>
binding.textView7.text = it.getString("productName")
binding.textView8.text = it.getString("productSp")
binding.textView9.text = it.getString("productDescription")
val name = it.getString("productName")
val productSp = it.getString("productSp")
val productDesc = it.getString("productDescription")

binding.textView7.text = name
binding.textView8.text = productSp
binding.textView9.text = productDesc

val slideList = ArrayList<SlideModel>()
for(data in list) {
slideList.add(SlideModel(data,ScaleTypes.CENTER_CROP))
}

cartAction(proId,name,productSp,it.getString("productCoverImg"))

binding.imageSlider.setImageList(slideList)

}.addOnFailureListener {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show()
}
}

private fun cartAction(proId: String, name: String?, productSp: String?, coverImg: String?) {

val productDao = AppDatabase.getInstance(this).productDao()

if(productDao.isExit(proId)!=null){
binding.textView10.text = "Go to Cart"
}else{
binding.textView10.text = "Add to Cart"
}

binding.textView10.setOnClickListener {
if(productDao.isExit(proId)!=null){
openCart()
}else{
if (coverImg != null) {
addToCart(productDao,proId,name,productSp,coverImg)
}
}
}
}

private fun addToCart(
productDao: ProductDao,
proId: String,
name: String?,
productSp: String?,
coverImg: String?
) {

val data = ProductModel(proId,name,coverImg,productSp)
lifecycleScope.launch(Dispatchers.IO){
productDao.insertProduct(data)
binding.textView10.text = "Go to Cart"
}
}

private fun openCart() {

val preference = this.getSharedPreferences("info", MODE_PRIVATE)
val editor = preference.edit()
editor.putBoolean("isCart",true)
editor.apply()

startActivity(Intent(this,MainActivity::class.java))
finish()
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package devs.redfox.local_e_commerce.database

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.lifecycle.LiveData
import androidx.room.*

@Dao
interface ProductDao {

@Insert
suspend fun insertProduct(product:ProductModel)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertProduct(vararg product:ProductModel)

@Delete
suspend fun deleteProduct(product:ProductModel)
suspend fun deleteProduct(vararg product:ProductModel)

@Query("SELECT * FROM products")
fun getAllProducts():List<ProductModel>
fun getAllProducts(): LiveData<List<ProductModel>>

@Query("SELECT * FROM products WHERE productId = :id")
fun isExit(id:String):ProductModel
fun isExit(id : String) : ProductModel
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ data class ProductModel (
val productId : String,
@ColumnInfo(name = "productName")
val productName : String? = "",
@ColumnInfo(name = "")
val productImage : String? = "productImage",
@ColumnInfo(name = "productImage")
val productImage : String? = "",
@ColumnInfo(name = "productSp")
val productSp : String? = "",

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,28 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import devs.redfox.local_e_commerce.R
import devs.redfox.local_e_commerce.databinding.FragmentHomeBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
* A simple [Fragment] subclass.
* Use the [CartFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class CartFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}

private var _binding: FragmentHomeBinding? = null
private val binding
get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_cart, container, false)
}
_binding = FragmentHomeBinding.inflate(inflater, container, false)

val preference = requireContext().getSharedPreferences("info", AppCompatActivity.MODE_PRIVATE)
val editor = preference.edit()
editor.putBoolean("isCart",false)
editor.apply()

companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment CartFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
CartFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
return binding.root
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package devs.redfox.local_e_commerce.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.firestore.ktx.toObject
import com.google.firebase.ktx.Firebase
import devs.redfox.local_e_commerce.R
import devs.redfox.local_e_commerce.activity.ProductDetailActivity
import devs.redfox.local_e_commerce.adapter.CategoryAdapter
import devs.redfox.local_e_commerce.adapter.ProductAdapter
import devs.redfox.local_e_commerce.databinding.FragmentHomeBinding
Expand All @@ -28,6 +33,16 @@ class HomeFragment : Fragment() {
): View? {
_binding = FragmentHomeBinding.inflate(inflater, container, false)

binding.button2.setOnClickListener {
val intent = Intent(requireContext(),ProductDetailActivity::class.java)
startActivity(intent)
}

val preference = requireContext().getSharedPreferences("info", AppCompatActivity.MODE_PRIVATE)

if(preference.getBoolean("isCart",false))
findNavController().navigate(R.id.action_homeFragment_to_cartFragment)

getCategories()
getSliderImage()
getProducts()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
android:orientation="vertical">

<com.google.android.material.card.MaterialCardView
android:id="@+id/button2"
android:layout_height="190dp"
android:layout_width="match_parent"
app:cardCornerRadius="20dp">
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
android:id="@+id/homeFragment"
android:name="devs.redfox.local_e_commerce.fragment.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_cartFragment"
app:destination="@id/cartFragment" />
</fragment>
<fragment
android:id="@+id/moreFragment"
android:name="devs.redfox.local_e_commerce.fragment.MoreFragment"
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.enableJetifier=true

0 comments on commit fa08862

Please sign in to comment.