Skip to content
This repository has been archived by the owner on Jul 31, 2021. It is now read-only.

Commit

Permalink
allow delete chat
Browse files Browse the repository at this point in the history
  • Loading branch information
mkofdwu committed Feb 23, 2021
1 parent 5edf03d commit dd9d36b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class _ChatPageState extends State<ChatPage> {
@override
void deactivate() {
print('chat page deactivated');
// TODO: dont call this if chat has just been deleted
ChatsService.updateLastReadMessageId(
Provider.of<User>(context, listen: false).profile.uid,
widget.chat.id,
Expand Down
30 changes: 28 additions & 2 deletions lib/pages/chat/widgets/popup_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ class ChatPopupMenu extends StatelessWidget {

ChatPopupMenu({@required this.chat, @required this.onUpdate});

void _deleteChat(context) async {
final confirm = await showQuestionDialog(
context: context,
title: 'Delete chat?',
content: 'You won\'t be able to undo this action',
);
if (confirm) {
await ChatsService.deleteChat(
Provider.of<User>(context, listen: false).profile.uid,
chat.id,
);
Navigator.pop(context);
}
}

void _blockAndDeleteChat(context) async {
final confirm = await showQuestionDialog(
context: context,
Expand Down Expand Up @@ -66,15 +81,19 @@ class ChatPopupMenu extends StatelessWidget {
void _starChat(context) {
final uid = Provider.of<User>(context, listen: false).profile.uid;
ChatsService.updateChatDetails(uid, chat.id, {'type': 2});
chat.type = ChatType.starred;
onUpdate();
}

void _unstarChat(context) {
final uid = Provider.of<User>(context, listen: false).profile.uid;
ChatsService.updateChatDetails(uid, chat.id, {'type': 3});
chat.type = ChatType.normal;
onUpdate();
}

void _exportChat(context) {}

@override
Widget build(BuildContext context) {
return PopupMenu(
Expand All @@ -83,8 +102,6 @@ class ChatPopupMenu extends StatelessWidget {
text: 'Wallpaper',
onPressed: () => _changeWallpaper(context),
),
if (chat.type == ChatType.normal || chat.type == ChatType.starred)
MenuDivider(),
if (chat.type == ChatType.normal)
MenuOption(
text: 'Star chat',
Expand All @@ -96,6 +113,15 @@ class ChatPopupMenu extends StatelessWidget {
onPressed: () => _unstarChat(context),
),
MenuDivider(),
MenuOption(
text: 'Export chat',
onPressed: () => _exportChat(context),
),
MenuDivider(),
MenuOption(
text: 'Delete chat',
onPressed: () => _deleteChat(context),
),
MenuOption(
text: 'Block and delete chat',
onPressed: () => _blockAndDeleteChat(context),
Expand Down
5 changes: 0 additions & 5 deletions lib/services/chats_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ class ChatsService {
await chatsRef.doc(chatId).update({
'participants': FieldValue.arrayRemove([uid])
});
// if ((await chatsRef.doc(chatId).collection('participants').limit(1).get())
// .docs
// .isEmpty) {
// await chatsRef.doc(chatId).delete();
// }
final userChatRef = _userChatRef(uid, chatId);
if (userChatRef.path.isNotEmpty) await userChatRef.delete();
}
Expand Down
5 changes: 3 additions & 2 deletions lib/widgets/popup_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MenuOption extends StatelessWidget {
return Material(
child: InkWell(
child: Container(
height: 50,
height: 40,
padding: const EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.centerRight,
child: Text(text, style: TextStyle(fontSize: 18)),
Expand All @@ -32,7 +32,7 @@ class MenuDivider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
child: Container(
width: 70,
height: 2,
Expand Down Expand Up @@ -60,6 +60,7 @@ class PopupMenu extends StatelessWidget {
),
),
clipBehavior: Clip.antiAlias,
padding: const EdgeInsets.symmetric(vertical: 10),
child: IntrinsicWidth(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
Expand Down

0 comments on commit dd9d36b

Please sign in to comment.