-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message functionality, placing order, and add to cart in firebase…
… database
- Loading branch information
1 parent
0a32043
commit f5c697a
Showing
17 changed files
with
618 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// ignore_for_file: curly_braces_in_flow_control_structures | ||
|
||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:emart/controllers/home_controller.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:emart/consts/consts.dart'; | ||
|
||
class ChatController extends GetxController { | ||
@override | ||
void onInit() { | ||
getChatId(); | ||
super.onInit(); | ||
} | ||
|
||
var chats = firestore.collection(chatsCollection); | ||
|
||
var friendName = Get.arguments[0]; | ||
var friendId = Get.arguments[1]; | ||
|
||
var senderName = Get.find<HomeController>().userName; | ||
var currentId = currentUser!.uid; | ||
|
||
TextEditingController msgController = TextEditingController(); | ||
|
||
dynamic chatDocId; | ||
|
||
var isLoading = false.obs; | ||
|
||
getChatId() async { | ||
isLoading(true); | ||
await chats | ||
.where('users', isEqualTo: {friendId: null, currentId: null}) | ||
.limit(1) | ||
.get() | ||
.then((QuerySnapshot snapshot) { | ||
if (snapshot.docs.isNotEmpty) | ||
chatDocId = snapshot.docs.single.id; | ||
else | ||
chats.add({ | ||
'created_on': null, | ||
'last_message': '', | ||
'users': {friendId: null, currentId: null}, | ||
'toId': '', | ||
'fromId': '', | ||
'friend_name': friendName, | ||
'sender_name': senderName, | ||
}).then((value) { | ||
chatDocId = value.id; | ||
}); | ||
}); | ||
isLoading(false); | ||
} | ||
|
||
sendMessage(String msg) async { | ||
if (msg.trim().isNotEmpty) { | ||
chats.doc(chatDocId).update({ | ||
'created_on': FieldValue.serverTimestamp(), | ||
'last_message': msg, | ||
'toId': friendId, | ||
'fromId': currentId, | ||
}); | ||
|
||
chats.doc(chatDocId).collection(messagesCollection).doc().set({ | ||
'created_on': FieldValue.serverTimestamp(), | ||
'message': msg, | ||
'uid': currentId, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
import 'package:emart/consts/consts.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
class HomeController extends GetxController { | ||
var currentNavIndex = 0.obs; | ||
|
||
var userName = ''; | ||
|
||
@override | ||
void onInit() { | ||
getUserName(); | ||
super.onInit(); | ||
} | ||
|
||
getUserName() async { | ||
var n = await firestore | ||
.collection(usersCollection) | ||
.where('id', isEqualTo: currentUser!.uid) | ||
.get() | ||
.then((value) { | ||
if (value.docs.isNotEmpty) return value.docs.single['name']; | ||
}); | ||
|
||
userName = n; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.