Skip to content

Commit

Permalink
Final Commit for basic working of E-Commerce
Browse files Browse the repository at this point in the history
  • Loading branch information
debz-g committed Aug 3, 2022
1 parent 89255cc commit 00ddbf1
Show file tree
Hide file tree
Showing 25 changed files with 317 additions and 69 deletions.
7 changes: 7 additions & 0 deletions .idea/misc.xml

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

4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ android {
dependencies {

implementation 'com.google.firebase:firebase-firestore:24.2.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
def nav_version = '2.5.0'

implementation 'androidx.core:core-ktx:1.8.0'
Expand Down Expand Up @@ -87,4 +88,7 @@ dependencies {
//Coroutines
implementation('org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.3')
implementation 'com.razorpay:checkout:1.6.19'

//lottie
implementation 'com.airbnb.android:lottie:3.4.0'
}
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="devs.redfox.local_e_commerce">
package="devs.redfox.local_e_commerce" >

<application
android:allowBackup="true"
Expand All @@ -12,7 +12,10 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LocalECommerce"
tools:targetApi="31">
tools:targetApi="31" >
<activity
android:name=".activity.ConfirmationActivity"
android:exported="false" />
<activity
android:name=".activity.CheckoutActivity"
android:exported="false" />
Expand All @@ -36,7 +39,7 @@
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ class AddressActivity : AppCompatActivity() {

private lateinit var binding: ActivityAddressBinding

private lateinit var preferences:SharedPreferences
private lateinit var preferences: SharedPreferences

private lateinit var totalCost: String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAddressBinding.inflate(layoutInflater)
setContentView(binding.root)
preferences = this.getSharedPreferences("user", MODE_PRIVATE)

totalCost = intent.getStringExtra("totalCost")!!

loadUserInfo()

binding.btnProceed.setOnClickListener {
Expand All @@ -44,10 +48,10 @@ class AddressActivity : AppCompatActivity() {
state: String,
pinCode: String
) {
if(number.isEmpty() || name.isEmpty() || address.isEmpty() || city.isEmpty() || state.isEmpty() || pinCode.isEmpty()){
Toast.makeText(this,"Please fill all fields",Toast.LENGTH_SHORT).show()
}else{
storeData(address,city,state,pinCode)
if (number.isEmpty() || name.isEmpty() || address.isEmpty() || city.isEmpty() || state.isEmpty() || pinCode.isEmpty()) {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show()
} else {
storeData(address, city, state, pinCode)
}
}

Expand All @@ -58,30 +62,33 @@ class AddressActivity : AppCompatActivity() {
pinCode: String
) {

val map = hashMapOf<String,Any>()
val map = hashMapOf<String, Any>()
map["address"] = address
map["city"] = city
map["state"] = state
map["pinCode"] = pinCode

Firebase.firestore.collection("users")
.document(preferences.getString("number","")!!)
.document(preferences.getString("number", "")!!)
.update(map).addOnSuccessListener {

val b = Bundle()
b.putStringArrayList("productIds", intent.getStringArrayListExtra("productIds"))
b.putString("totalCost", totalCost)
val intent = Intent(this, CheckoutActivity::class.java)
intent.putExtra("productIds", intent.getStringArrayExtra("productIds"))
intent.putExtras(b)
startActivity(intent)
}
.addOnFailureListener {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show()
}
}

private fun loadUserInfo(){

private fun loadUserInfo() {


Firebase.firestore.collection("users").document(preferences.getString("number","")!!)
Firebase.firestore.collection("users").document(preferences.getString("number", "")!!)
.get().addOnSuccessListener {
binding.userName.setText(it.getString("userName"))
binding.userNumber.setText(it.getString("userPhoneNumber"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package devs.redfox.local_e_commerce.activity


import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.razorpay.Checkout
import com.razorpay.PaymentResultListener
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.ProductModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject


Expand All @@ -18,13 +27,15 @@ class CheckoutActivity : AppCompatActivity(), PaymentResultListener {
val checkout = Checkout()
checkout.setKeyID("rzp_test_CsJK0mxcTnqyKp")

val price = intent.getStringExtra("totalCost")

try {
val options = JSONObject()
options.put("name", "PassKiDukaan")
options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png")
options.put("theme.color", "#3399cc")
options.put("currency", "INR")
options.put("amount", "50000") //pass amount in currency subunits
options.put("amount", (price!!.toInt()*100)) //pass amount in currency subunits
options.put("prefill.email", "[email protected]")
options.put("prefill.contact", "7098910064")
checkout.open(this, options)
Expand All @@ -35,6 +46,52 @@ class CheckoutActivity : AppCompatActivity(), PaymentResultListener {

override fun onPaymentSuccess(p0: String?) {
Toast.makeText(this, "Payment Success", Toast.LENGTH_SHORT).show()
uploadData()
}

private fun uploadData() {
val id = intent.getStringArrayListExtra("productIds")
for(currentId in id!!){
fetchData(currentId)
}
}

private fun fetchData(productId: String?) {

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

Firebase.firestore.collection("products")
.document(productId!!).get().addOnSuccessListener {

lifecycleScope.launch(Dispatchers.IO) {
dao.deleteProduct(ProductModel(productId))
}

saveData(it.getString("productName"), it.getString("productSp"), productId)
}
}

private fun saveData(name: String?, price: String?, productId: String) {

val preferences = this.getSharedPreferences("user", MODE_PRIVATE)
val data = hashMapOf<String, Any>()
data["name"] = name!!
data["price"] = price!!
data["productId"] = productId
data["status"] = "Ordered"
data["userId"] = preferences.getString("number", "")!!

val firestore = Firebase.firestore.collection("allOrders")
val key = firestore.document().id
data["orderId"] = key

firestore.document(key).set(data).addOnSuccessListener {
Toast.makeText(this, "Order Placed", Toast.LENGTH_SHORT).show()
startActivity(Intent(this, ConfirmationActivity::class.java))
}.addOnFailureListener {
Toast.makeText(this, "Something went wrong, order not placed", Toast.LENGTH_SHORT).show()
}

}

override fun onPaymentError(p0: Int, p1: String?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package devs.redfox.local_e_commerce.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import devs.redfox.local_e_commerce.R

class ConfirmationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_confirmation)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package devs.redfox.local_e_commerce.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View.GONE
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import devs.redfox.local_e_commerce.databinding.AllOrderItemLayoutBinding
import devs.redfox.local_e_commerce.model.AllOrderModel


class AllOrderAdapter(val list: ArrayList<AllOrderModel>, val context: Context) :
RecyclerView.Adapter<AllOrderAdapter.AllOrderViewHolder>() {

inner class AllOrderViewHolder(val binding: AllOrderItemLayoutBinding) :
RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AllOrderViewHolder {
return AllOrderViewHolder(
AllOrderItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}

override fun onBindViewHolder(holder: AllOrderViewHolder, position: Int) {
holder.binding.productTitle.text = list[position].name
holder.binding.productPrice.text = list[position].price



when (list[position].status) {
"Ordered" -> {
holder.binding.productStatus.text = "Order Status: Ordered"

}
"Dispatched" -> {
holder.binding.productStatus.text = "Order Status: Dispatched"

}
"Delivered" -> {
holder.binding.productStatus.text = "Order Status: Delivered"

}
"Canceled" -> {
holder.binding.productStatus.text = "Order Status: Cancelled"

}

}

}


override fun getItemCount(): Int {
return list.size
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ class CartFragment : Fragment() {

binding.checkout.setOnClickListener {
val intent = Intent(context, AddressActivity::class.java)
intent.putExtra("totalCost", total)
intent.putExtra("productIds", list)

val b = Bundle()
b.putStringArrayList("productIds", list)
b.putString("totalCost", total.toString())
intent.putExtras(b)

startActivity(intent)
}
}
Expand Down
Loading

0 comments on commit 00ddbf1

Please sign in to comment.