Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
PaperChase
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
h703249754
PaperChase
Commits
b9f08f1f
Commit
b9f08f1f
authored
Apr 22, 2025
by
Adam Bruck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
seperate chats
parent
1f7c3937
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
37 deletions
+34
-37
book_detail_page.dart
lib/book_detail_page.dart
+34
-37
No files found.
lib/book_detail_page.dart
View file @
b9f08f1f
...
...
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import
'package:cloud_firestore/cloud_firestore.dart'
;
import
'package:firebase_auth/firebase_auth.dart'
;
import
'package:flutter/foundation.dart'
;
import
'package:paperchase_app/chat_page.dart'
;
import
'colors.dart'
;
import
'NavBar.dart'
;
...
...
@@ -97,7 +98,7 @@ class BookDetailsPage extends StatelessWidget {
width:
double
.
infinity
,
child:
ElevatedButton
(
onPressed:
()
=>
_contactSeller
(
context
,
book
[
'userId'
],
title
),
_contactSeller
(
context
,
book
[
'userId'
],
title
,
bookId
),
style:
ElevatedButton
.
styleFrom
(
backgroundColor:
kPrimaryColor
,
padding:
const
EdgeInsets
.
symmetric
(
vertical:
16
),
...
...
@@ -155,12 +156,8 @@ class BookDetailsPage extends StatelessWidget {
Future
<
void
>
_contactSeller
(
BuildContext
context
,
String
sellerId
,
String
bookTitle
)
async
{
Future
<
void
>
_contactSeller
(
BuildContext
context
,
String
sellerId
,
String
bookTitle
,
String
bookId
)
async
{
final
currentUser
=
FirebaseAuth
.
instance
.
currentUser
;
if
(
currentUser
==
null
)
{
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'Please log in to contact the seller'
)),
...
...
@@ -170,18 +167,25 @@ class BookDetailsPage extends StatelessWidget {
try
{
final
users
=
[
currentUser
.
uid
,
sellerId
]..
sort
();
final
chatRoomId
=
users
.
join
(
'_'
);
final
chatRoomId
=
"
${bookId}
_
${users.join('_')}
"
;
// Ensures uniqueness per book
final
existingChat
=
await
FirebaseFirestore
.
instance
.
collection
(
'
chat
s'
)
.
doc
(
chatRoom
Id
)
final
sellerDoc
=
await
FirebaseFirestore
.
instance
.
collection
(
'
user
s'
)
.
doc
(
seller
Id
)
.
get
();
final
sellerName
=
sellerDoc
.
exists
?
"
${sellerDoc['first_name']}
${sellerDoc['last_name']}
"
:
"Unknown Seller"
;
final
chatRef
=
FirebaseFirestore
.
instance
.
collection
(
'chats'
).
doc
(
chatRoomId
);
final
chatData
=
{
'users'
:
users
,
'bookId'
:
bookId
,
'bookTitle'
:
bookTitle
,
'lastMessage'
:
'Hi! Is this book still available?'
,
'lastMessageTime'
:
FieldValue
.
serverTimestamp
(),
'bookTitle'
:
bookTitle
,
'createdAt'
:
FieldValue
.
serverTimestamp
(),
'participants'
:
{
currentUser
.
uid
:
true
,
...
...
@@ -189,47 +193,40 @@ class BookDetailsPage extends StatelessWidget {
},
};
final
existingChat
=
await
chatRef
.
get
();
if
(
existingChat
.
exists
)
{
await
FirebaseFirestore
.
instance
.
collection
(
'chats'
)
.
doc
(
chatRoomId
)
.
update
({
await
chatRef
.
update
({
'lastMessage'
:
chatData
[
'lastMessage'
],
'lastMessageTime'
:
chatData
[
'lastMessageTime'
],
});
}
else
{
await
FirebaseFirestore
.
instance
.
collection
(
'chats'
)
.
doc
(
chatRoomId
)
.
set
(
chatData
);
await
chatRef
.
set
(
chatData
);
}
await
FirebaseFirestore
.
instance
.
collection
(
'chats'
)
.
doc
(
chatRoomId
)
.
collection
(
'messages'
)
.
add
({
await
chatRef
.
collection
(
'messages'
).
add
({
'senderId'
:
currentUser
.
uid
,
'message'
:
'Hi! Is this book still available?'
,
'timestamp'
:
FieldValue
.
serverTimestamp
(),
'read'
:
false
,
});
if
(
context
.
mounted
)
{
Navigator
.
pushReplacementNamed
(
context
,
'/inbox'
);
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'Chat started with the seller'
)),
Navigator
.
push
(
context
,
MaterialPageRoute
(
builder:
(
context
)
=>
StrictChatPage
(
chatId:
chatRoomId
,
otherUserName:
sellerName
,
),
),
);
}
}
catch
(
e
)
{
debugPrint
(
"Error contacting seller:
$e
"
);
if
(
context
.
mounted
)
{
debugPrint
(
'Error starting chat:
$e
'
);
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'Failed to contact seller. Please try again.'
)),
const
SnackBar
(
content:
Text
(
'Failed to contact seller. Please try again.'
)),
);
}
}
}
void
_confirmAndDeleteBook
(
BuildContext
context
,
String
bookId
)
async
{
final
shouldDelete
=
await
showDialog
<
bool
>(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment