Skip to content

Commit f5c697a

Browse files
Add message functionality, placing order, and add to cart in firebase database
1 parent 0a32043 commit f5c697a

17 files changed

+618
-86
lines changed

lib/consts/firebase_consts.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ User? currentUser = auth.currentUser;
99
const usersCollection = "users";
1010
const productsCollection = "products";
1111
const cartCollection = "cart";
12+
const chatsCollection = "chats";
13+
const messagesCollection = "messages";
14+
const ordersCollection = "orders";

lib/consts/list.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ const itemDetailsButtonsList = [
4545

4646
const profileButtonList = [wishlist, orders, messages];
4747
const profileButtonIconsList = [icOrder, icOrder, icMessages];
48+
49+
const paymentMethodsImgList = [imgCod, imgStripe, imgPaypal];
50+
const paymentMethhodsList = [cod, stripe, paypal];

lib/consts/strings.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const signup = "Sign up";
1717
const loggedin = "Logged in successfully";
1818
const loggedout = "Logged ou successfully";
1919
const updated = "Updated";
20+
const typeMessage = "Type a message...";
2021

2122
const createNewAccount = "or, create new account";
2223
const loginWith = "Log in with";
@@ -74,3 +75,13 @@ const wishlist = "Wish List",
7475
oldPass = "Old Password",
7576
newPass = "New Password",
7677
wrongOldPass = "Wrong Old Password";
78+
79+
// shipping screen string
80+
const address = "Address",
81+
city = "City",
82+
state = "State",
83+
postalCode = "Postal Code",
84+
phone = "Phone";
85+
86+
// payment methods string
87+
const paypal = "Paypal", stripe = "Stripe", cod = "Cash of Delivery";

lib/controllers/cart_controller.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:emart/consts/consts.dart';
3+
import 'package:emart/controllers/home_controller.dart';
14
import 'package:get/get.dart';
25

36
class CartCotroller extends GetxController {
47
var totalPrice = 0.obs;
58

9+
// text editing controller
10+
TextEditingController addressController = TextEditingController();
11+
TextEditingController cityController = TextEditingController();
12+
TextEditingController stateController = TextEditingController();
13+
TextEditingController postalCodeController = TextEditingController();
14+
TextEditingController phoneController = TextEditingController();
15+
16+
// payment index
17+
var paymentIndex = 0.obs;
18+
19+
late dynamic productSnapshot;
20+
var products = [];
21+
622
// calculate total price of item present in cart
723
calculateTotalPrice(data) {
824
totalPrice.value = 0;
@@ -11,4 +27,48 @@ class CartCotroller extends GetxController {
1127
totalPrice.value + int.parse(data[price]['total_price'].toString());
1228
}
1329
}
30+
31+
// change payment index
32+
changePaymentIndex(index) {
33+
paymentIndex.value = index;
34+
}
35+
36+
// order placing
37+
placeMyOrder({required orderPaymentMethod, required totalAmount}) async {
38+
await getProductDetails();
39+
40+
await firestore.collection(ordersCollection).doc().set({
41+
'order_code': "223322",
42+
'order_date': FieldValue.serverTimestamp(),
43+
'order_by': currentUser!.uid,
44+
'order_by_name': Get.find<HomeController>().userName,
45+
'order_by_email': currentUser!.email,
46+
'order_by_address': addressController.text,
47+
'order_by_city': cityController.text,
48+
'order_by_state': stateController.text,
49+
'order_by_phone': phoneController.text,
50+
'order_by_postal_code': postalCodeController.text,
51+
'shipping_method': "Home Delivery",
52+
'payment_method': orderPaymentMethod,
53+
'order_placed': true,
54+
'order_confirmed': false,
55+
'order_delivered': false,
56+
'order_on_delivery': false,
57+
'total_amount': totalAmount,
58+
'orders': FieldValue.arrayUnion(products),
59+
});
60+
}
61+
62+
// product details
63+
getProductDetails() {
64+
products.clear();
65+
for (var i = 0; i < productSnapshot.length; i++) {
66+
products.add({
67+
'color': productSnapshot[i]['color'],
68+
'image': productSnapshot[i]['image'],
69+
'quantity': productSnapshot[i]['quantity'],
70+
'title': productSnapshot[i]['title'],
71+
});
72+
}
73+
}
1474
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ignore_for_file: curly_braces_in_flow_control_structures
2+
3+
import 'package:cloud_firestore/cloud_firestore.dart';
4+
import 'package:emart/controllers/home_controller.dart';
5+
import 'package:get/get.dart';
6+
import 'package:emart/consts/consts.dart';
7+
8+
class ChatController extends GetxController {
9+
@override
10+
void onInit() {
11+
getChatId();
12+
super.onInit();
13+
}
14+
15+
var chats = firestore.collection(chatsCollection);
16+
17+
var friendName = Get.arguments[0];
18+
var friendId = Get.arguments[1];
19+
20+
var senderName = Get.find<HomeController>().userName;
21+
var currentId = currentUser!.uid;
22+
23+
TextEditingController msgController = TextEditingController();
24+
25+
dynamic chatDocId;
26+
27+
var isLoading = false.obs;
28+
29+
getChatId() async {
30+
isLoading(true);
31+
await chats
32+
.where('users', isEqualTo: {friendId: null, currentId: null})
33+
.limit(1)
34+
.get()
35+
.then((QuerySnapshot snapshot) {
36+
if (snapshot.docs.isNotEmpty)
37+
chatDocId = snapshot.docs.single.id;
38+
else
39+
chats.add({
40+
'created_on': null,
41+
'last_message': '',
42+
'users': {friendId: null, currentId: null},
43+
'toId': '',
44+
'fromId': '',
45+
'friend_name': friendName,
46+
'sender_name': senderName,
47+
}).then((value) {
48+
chatDocId = value.id;
49+
});
50+
});
51+
isLoading(false);
52+
}
53+
54+
sendMessage(String msg) async {
55+
if (msg.trim().isNotEmpty) {
56+
chats.doc(chatDocId).update({
57+
'created_on': FieldValue.serverTimestamp(),
58+
'last_message': msg,
59+
'toId': friendId,
60+
'fromId': currentId,
61+
});
62+
63+
chats.doc(chatDocId).collection(messagesCollection).doc().set({
64+
'created_on': FieldValue.serverTimestamp(),
65+
'message': msg,
66+
'uid': currentId,
67+
});
68+
}
69+
}
70+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
import 'package:emart/consts/consts.dart';
12
import 'package:get/get.dart';
23

34
class HomeController extends GetxController {
45
var currentNavIndex = 0.obs;
6+
7+
var userName = '';
8+
9+
@override
10+
void onInit() {
11+
getUserName();
12+
super.onInit();
13+
}
14+
15+
getUserName() async {
16+
var n = await firestore
17+
.collection(usersCollection)
18+
.where('id', isEqualTo: currentUser!.uid)
19+
.get()
20+
.then((value) {
21+
if (value.docs.isNotEmpty) return value.docs.single['name'];
22+
});
23+
24+
userName = n;
25+
}
526
}

lib/controllers/product_controller.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// ignore_for_file: curly_braces_in_flow_control_structures
2+
3+
import 'package:cloud_firestore/cloud_firestore.dart';
14
import 'package:emart/consts/consts.dart';
25
import 'package:emart/models/category_model.dart';
36
import 'package:flutter/services.dart';
@@ -8,6 +11,7 @@ class ProductController extends GetxController {
811
var quantity = 0.obs;
912
var colorIndex = 0.obs;
1013
var totalPrice = 0.obs;
14+
var isFav = false.obs;
1115

1216
getSubCategories(title) async {
1317
subCat.clear();
@@ -61,4 +65,27 @@ class ProductController extends GetxController {
6165
totalPrice.value = 0;
6266
colorIndex.value = 0;
6367
}
68+
69+
addToWishList(docId, context) async {
70+
firestore.collection(productsCollection).doc(docId).set({
71+
'p_wishlist': FieldValue.arrayUnion([currentUser!.uid])
72+
}, SetOptions(merge: true));
73+
isFav(true);
74+
VxToast.show(context, msg: "Added to Wishlist");
75+
}
76+
77+
removeFromWishList(docId, context) async {
78+
firestore.collection(productsCollection).doc(docId).set({
79+
'p_wishlist': FieldValue.arrayRemove([currentUser!.uid])
80+
}, SetOptions(merge: true));
81+
isFav(false);
82+
VxToast.show(context, msg: "Remove from Wishlist");
83+
}
84+
85+
checkIsFav(data) async {
86+
if (data['p_wishlist'].contains(currentUser!.uid))
87+
isFav(true);
88+
else
89+
isFav(false);
90+
}
6491
}

lib/services/firestore_service.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ class FirestoreService {
2222
// delete cart document from databse
2323
static deleteCartDocument(docId) =>
2424
firestore.collection(cartCollection).doc(docId).delete();
25+
26+
// gett all chat messages
27+
static getChatMessages(docId) => firestore
28+
.collection(chatsCollection)
29+
.doc(docId)
30+
.collection(messagesCollection)
31+
.orderBy('created_on', descending: false)
32+
.snapshots();
2533
}

0 commit comments

Comments
 (0)