feat: delete chat

This commit is contained in:
Stein Milder 2022-11-09 14:18:17 +01:00
parent 2ebb05bb79
commit 79d3de0b35
13 changed files with 117 additions and 67 deletions

View file

@ -15,6 +15,9 @@ scripts:
get:
run: melos exec -c 1 -- "flutter pub get"
upgrade:
run: melos exec -c 1 -- "flutter pub upgrade"
analyze:
run: |
melos exec -c 1 -- \

View file

@ -134,6 +134,7 @@ class _CommunityChatState extends State<CommunityChat> {
chats: widget.dataProvider.getChatsStream(),
onPressStartChat: () => _onPressStartChat(context),
onPressChat: (chat) => _onPressChat(context, chat),
onDeleteChat: (ChatModel chat) => widget.dataProvider.deleteChat(chat),
options: widget.options,
translations: widget.translations,
);

View file

@ -179,7 +179,7 @@ packages:
description:
path: "packages/flutter_community_chat_interface"
ref: HEAD
resolved-ref: adc7d063039e44e5a76275bed48d336b18271f3d
resolved-ref: f3cae1bf88f7f55c485aeab86a95cdff776373d4
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
source: git
version: "0.0.1"
@ -188,7 +188,7 @@ packages:
description:
path: "packages/flutter_community_chat_view"
ref: HEAD
resolved-ref: d10857d28905114c481753cd6193d685b713f028
resolved-ref: f3cae1bf88f7f55c485aeab86a95cdff776373d4
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
source: git
version: "0.0.1"
@ -305,7 +305,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
logging:
dependency: transitive
description:

View file

@ -74,4 +74,8 @@ class FirebaseCommunityChatDataProvider extends CommunityChatInterface {
@override
Future<void> setChat(ChatModel chat) async =>
await _messageService.setChat(chat);
@override
Future<void> deleteChat(ChatModel chat) async =>
await _chatService.deleteChat(chat);
}

View file

@ -172,15 +172,7 @@ class FirebaseChatService {
.collection('chats')
.snapshots()
.listen((snapshot) {
List<String> chatIds = [];
for (var chatDoc in snapshot.docs) {
var chatData = chatDoc.data();
if (chatData['id'] != null) {
chatIds.add(chatData['id']);
}
}
List<String> chatIds = snapshot.docs.map((chat) => chat.id).toList();
if (chatIds.isNotEmpty) {
chatsSubscription = _getSpecificChatsStream(chatIds).listen(
@ -207,12 +199,10 @@ class FirebaseChatService {
for (var element in chatCollection.docs) {
var data = element.data();
if (data.containsKey('id') &&
data.containsKey('users') &&
data['users'] is List) {
if (data.containsKey('users') && data['users'] is List) {
if (data['users'].contains(user.id)) {
return PersonalChatModel(
id: data['id'],
id: element.id,
user: user,
);
}
@ -221,4 +211,31 @@ class FirebaseChatService {
return null;
}
Future<void> deleteChat(ChatModel chat) async {
var chatCollection = await db
.collection(options.chatsCollectionName)
.doc(chat.id)
.withConverter(
fromFirestore: (snapshot, _) =>
FirebaseChatDocument.fromJson(snapshot.data()!, snapshot.id),
toFirestore: (chat, _) => chat.toJson(),
)
.get();
var chatData = chatCollection.data();
if (chatData != null) {
for (var userId in chatData.users) {
db
.collection(options.usersCollectionName)
.doc(userId)
.collection('chats')
.doc(chat.id)
.delete();
}
await db.collection(options.chatsCollectionName).doc(chat.id).delete();
}
}
}

View file

@ -197,7 +197,8 @@ class FirebaseMessageService {
.collection(options.usersCollectionName)
.doc(userId)
.collection('chats')
.add({'id': reference.id, 'users': userIds});
.doc(reference.id)
.set({'users': userIds});
}
chat.id = reference.id;

View file

@ -84,7 +84,7 @@ packages:
name: cloud_firestore
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.0"
version: "3.5.1"
cloud_firestore_platform_interface:
dependency: transitive
description:
@ -182,7 +182,7 @@ packages:
name: firebase_core_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "4.5.1"
version: "4.5.2"
firebase_core_web:
dependency: transitive
description:
@ -203,7 +203,7 @@ packages:
name: firebase_storage_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.18"
version: "4.1.19"
firebase_storage_web:
dependency: transitive
description:
@ -228,7 +228,7 @@ packages:
description:
path: "packages/flutter_community_chat_interface"
ref: HEAD
resolved-ref: adc7d063039e44e5a76275bed48d336b18271f3d
resolved-ref: f3cae1bf88f7f55c485aeab86a95cdff776373d4
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
source: git
version: "0.0.1"
@ -278,7 +278,7 @@ packages:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.1"
version: "4.0.2"
intl:
dependency: transitive
description:
@ -299,7 +299,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
logging:
dependency: transitive
description:

View file

@ -18,4 +18,5 @@ abstract class CommunityChatInterface extends DataInterface {
Stream<List<ChatMessageModel>> getMessagesStream();
Stream<List<ChatModel>> getChatsStream();
Future<List<ChatUserModel>> getChatUsers();
Future<void> deleteChat(ChatModel chat);
}

View file

@ -7,14 +7,14 @@ packages:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "49.0.0"
version: "50.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.0"
version: "5.2.0"
args:
dependency: transitive
description:
@ -28,14 +28,14 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.9.0"
version: "2.10.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
build:
dependency: transitive
description:
@ -56,7 +56,7 @@ packages:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "8.4.1"
version: "8.4.2"
characters:
dependency: transitive
description:
@ -84,7 +84,7 @@ packages:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
version: "3.1.1"
crypto:
dependency: transitive
description:
@ -123,7 +123,7 @@ packages:
description:
path: "."
ref: master
resolved-ref: e348c921d5e621975e4f06d3eeff8e8a89dda109
resolved-ref: "500ed1d08095b33387ae3aa4ed1a2ad4d2fb2ac3"
url: "https://github.com/Iconica-Development/flutter_data_interface.git"
source: git
version: "1.0.0"
@ -147,7 +147,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
logging:
dependency: transitive
description:
@ -161,7 +161,7 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.12"
version: "0.12.13"
material_color_utilities:
dependency: transitive
description:
@ -203,7 +203,7 @@ packages:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
version: "2.1.2"
sky_engine:
dependency: transitive
description: flutter
@ -215,7 +215,7 @@ packages:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.5"
version: "1.2.6"
source_span:
dependency: transitive
description:
@ -229,7 +229,7 @@ packages:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0"
version: "1.11.0"
stream_channel:
dependency: transitive
description:
@ -243,7 +243,7 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
version: "1.2.0"
term_glyph:
dependency: transitive
description:
@ -257,7 +257,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.14"
version: "0.4.16"
typed_data:
dependency: transitive
description:
@ -278,7 +278,7 @@ packages:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
version: "1.0.2"
yaml:
dependency: transitive
description:

View file

@ -186,7 +186,7 @@ packages:
description:
path: "packages/flutter_community_chat_interface"
ref: HEAD
resolved-ref: adc7d063039e44e5a76275bed48d336b18271f3d
resolved-ref: f3cae1bf88f7f55c485aeab86a95cdff776373d4
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
source: git
version: "0.0.1"
@ -252,7 +252,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
logging:
dependency: transitive
description:

View file

@ -12,11 +12,13 @@ class ChatTranslations {
this.cancelImagePickerBtn = 'Cancel',
this.messagePlaceholder = 'Write your message here...',
this.imageUploading = 'Image is uploading...',
this.deleteChatButton = 'Delete',
});
final String chatsTitle;
final String newChatButton;
final String newChatTitle;
final String deleteChatButton;
final String image;
final String searchPlaceholder;
final String cancelImagePickerBtn;

View file

@ -13,6 +13,7 @@ class ChatScreen extends StatefulWidget {
required this.chats,
required this.onPressStartChat,
required this.onPressChat,
required this.onDeleteChat,
this.translations = const ChatTranslations(),
super.key,
});
@ -21,6 +22,7 @@ class ChatScreen extends StatefulWidget {
final ChatTranslations translations;
final Stream<List<ChatModel>> chats;
final VoidCallback? onPressStartChat;
final void Function(ChatModel chat) onDeleteChat;
final void Function(ChatModel chat) onPressChat;
@override
@ -39,37 +41,56 @@ class _ChatScreenState extends State<ChatScreen> {
children: [
Expanded(
child: ListView(
padding: const EdgeInsets.only(
top: 15.0,
),
padding: const EdgeInsets.only(top: 15.0),
children: [
StreamBuilder<List<ChatModel>>(
stream: widget.chats,
builder: (BuildContext context, snapshot) => Column(
children: [
for (ChatModel chat in snapshot.data ?? [])
GestureDetector(
onTap: () => widget.onPressChat(chat),
child: widget.options.chatRowContainerBuilder(
ChatRow(
image: chat is PersonalChatModel
? chat.user.imageUrl
: (chat as GroupChatModel).imageUrl,
title: chat is PersonalChatModel
? chat.user.name ?? ''
: (chat as GroupChatModel).title,
subTitle: chat.lastMessage != null
? chat.lastMessage is ChatTextMessageModel
? (chat.lastMessage!
as ChatTextMessageModel)
.text
: '📷 ${widget.translations.image}'
: null,
lastUsed: chat.lastUsed != null
? _dateFormatter.format(
date: chat.lastUsed!,
)
: null,
Builder(
builder: (context) => Dismissible(
onDismissed: (_) => widget.onDeleteChat(chat),
background: Container(
color: Colors.red,
child: Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
widget.translations.deleteChatButton,
),
),
),
),
key: Key(
chat.id.toString(),
),
child: GestureDetector(
onTap: () => widget.onPressChat(chat),
child: widget.options.chatRowContainerBuilder(
ChatRow(
image: chat is PersonalChatModel
? chat.user.imageUrl
: (chat as GroupChatModel).imageUrl,
title: chat is PersonalChatModel
? chat.user.name ?? ''
: (chat as GroupChatModel).title,
subTitle: chat.lastMessage != null
? chat.lastMessage
is ChatTextMessageModel
? (chat.lastMessage!
as ChatTextMessageModel)
.text
: '📷 ${widget.translations.image}'
: null,
lastUsed: chat.lastUsed != null
? _dateFormatter.format(
date: chat.lastUsed!,
)
: null,
),
),
),
),
),

View file

@ -179,7 +179,7 @@ packages:
description:
path: "packages/flutter_community_chat_interface"
ref: HEAD
resolved-ref: adc7d063039e44e5a76275bed48d336b18271f3d
resolved-ref: f3cae1bf88f7f55c485aeab86a95cdff776373d4
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
source: git
version: "0.0.1"
@ -238,7 +238,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
logging:
dependency: transitive
description: