feat: add pagination

This commit is contained in:
mike doornenbal 2023-12-20 11:45:10 +01:00
parent 4fd823511f
commit 69bafc33e6
20 changed files with 566 additions and 193 deletions

View file

@ -17,19 +17,21 @@ List<GoRoute> getCommunityChatStoryRoutes(
path: CommunityChatUserStoryRoutes.chatScreen, path: CommunityChatUserStoryRoutes.chatScreen,
pageBuilder: (context, state) { pageBuilder: (context, state) {
var chatScreen = ChatScreen( var chatScreen = ChatScreen(
pageSize: configuration.pageSize,
service: configuration.service, service: configuration.service,
options: configuration.chatOptionsBuilder(context), options: configuration.chatOptionsBuilder(context),
onNoChats: () => onNoChats: () async =>
context.push(CommunityChatUserStoryRoutes.newChatScreen), await context.push(CommunityChatUserStoryRoutes.newChatScreen),
onPressStartChat: () => onPressStartChat: () async =>
configuration.onPressStartChat?.call() ?? await configuration.onPressStartChat?.call() ??
context.push(CommunityChatUserStoryRoutes.newChatScreen), await context.push(CommunityChatUserStoryRoutes.newChatScreen),
onPressChat: (chat) => onPressChat: (chat) =>
configuration.onPressChat?.call(context, chat) ?? configuration.onPressChat?.call(context, chat) ??
context.push( context.push(
CommunityChatUserStoryRoutes.chatDetailViewPath(chat.id!)), CommunityChatUserStoryRoutes.chatDetailViewPath(chat.id!)),
onDeleteChat: (chat) => onDeleteChat: (chat) =>
configuration.onDeleteChat?.call(context, chat), configuration.onDeleteChat?.call(context, chat) ??
configuration.service.deleteChat(chat),
deleteChatDialog: configuration.deleteChatDialog, deleteChatDialog: configuration.deleteChatDialog,
translations: configuration.translations, translations: configuration.translations,
); );
@ -52,6 +54,7 @@ List<GoRoute> getCommunityChatStoryRoutes(
var chatId = state.pathParameters['id']; var chatId = state.pathParameters['id'];
var chat = PersonalChatModel(user: ChatUserModel(), id: chatId); var chat = PersonalChatModel(user: ChatUserModel(), id: chatId);
var chatDetailScreen = ChatDetailScreen( var chatDetailScreen = ChatDetailScreen(
pageSize: configuration.messagePageSize,
options: configuration.chatOptionsBuilder(context), options: configuration.chatOptionsBuilder(context),
translations: configuration.translations, translations: configuration.translations,
chatUserService: configuration.userService, chatUserService: configuration.userService,
@ -110,8 +113,9 @@ List<GoRoute> getCommunityChatStoryRoutes(
); );
} }
if (context.mounted) { if (context.mounted) {
context.push(CommunityChatUserStoryRoutes.chatDetailViewPath( await context.push(
chat.id ?? '')); CommunityChatUserStoryRoutes.chatDetailViewPath(
chat.id ?? ''));
} }
}); });
return buildScreenWithoutTransition( return buildScreenWithoutTransition(

View file

@ -14,6 +14,7 @@ class CommunityChatUserStoryConfiguration {
required this.messageService, required this.messageService,
required this.service, required this.service,
required this.chatOptionsBuilder, required this.chatOptionsBuilder,
this.pageSize = 10,
this.onPressStartChat, this.onPressStartChat,
this.onPressChat, this.onPressChat,
this.onDeleteChat, this.onDeleteChat,
@ -29,6 +30,7 @@ class CommunityChatUserStoryConfiguration {
this.chatPageBuilder, this.chatPageBuilder,
this.onPressChatTitle, this.onPressChatTitle,
this.afterMessageSent, this.afterMessageSent,
this.messagePageSize = 20,
}); });
final ChatService service; final ChatService service;
final ChatUserService userService; final ChatUserService userService;
@ -48,6 +50,8 @@ class CommunityChatUserStoryConfiguration {
/// If true, the user will be routed to the new chat screen if there are no chats. /// If true, the user will be routed to the new chat screen if there are no chats.
final bool routeToNewChatIfEmpty; final bool routeToNewChatIfEmpty;
final int pageSize;
final int messagePageSize;
final Future<bool?> Function(BuildContext, ChatModel)? deleteChatDialog; final Future<bool?> Function(BuildContext, ChatModel)? deleteChatDialog;
final Function(BuildContext context, ChatModel chat)? onPressChatTitle; final Function(BuildContext context, ChatModel chat)? onPressChatTitle;

View file

@ -4,7 +4,7 @@
name: flutter_community_chat name: flutter_community_chat
description: A new Flutter package project. description: A new Flutter package project.
version: 0.6.0 version: 1.0.0
publish_to: none publish_to: none
@ -20,12 +20,12 @@ dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_community_chat url: https://github.com/Iconica-Development/flutter_community_chat
path: packages/flutter_community_chat_view path: packages/flutter_community_chat_view
ref: 0.6.0 ref: 1.0.0
flutter_community_chat_interface: flutter_community_chat_interface:
git: git:
url: https://github.com/Iconica-Development/flutter_community_chat url: https://github.com/Iconica-Development/flutter_community_chat
path: packages/flutter_community_chat_interface path: packages/flutter_community_chat_interface
ref: 0.6.0 ref: 1.0.0
dev_dependencies: dev_dependencies:
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0

View file

@ -0,0 +1,2 @@
sdk.dir=/Users/mikedoornenbal/Library/Android/sdk
flutter.sdk=/opt/homebrew/Caskroom/flutter/3.10.2/flutter

View file

@ -11,18 +11,26 @@ class FirebaseChatOptions {
this.chatsCollectionName = 'chats', this.chatsCollectionName = 'chats',
this.messagesCollectionName = 'messages', this.messagesCollectionName = 'messages',
this.usersCollectionName = 'users', this.usersCollectionName = 'users',
this.chatsMetaDataCollectionName = 'chat_metadata',
this.userChatsCollectionName = 'chats',
}); });
final String groupChatsCollectionName; final String groupChatsCollectionName;
final String chatsCollectionName; final String chatsCollectionName;
final String messagesCollectionName; final String messagesCollectionName;
final String usersCollectionName; final String usersCollectionName;
final String chatsMetaDataCollectionName;
///This is the collection inside the user document.
final String userChatsCollectionName;
FirebaseChatOptions copyWith({ FirebaseChatOptions copyWith({
String? groupChatsCollectionName, String? groupChatsCollectionName,
String? chatsCollectionName, String? chatsCollectionName,
String? messagesCollectionName, String? messagesCollectionName,
String? usersCollectionName, String? usersCollectionName,
String? chatsMetaDataCollectionName,
String? userChatsCollectionName,
}) { }) {
return FirebaseChatOptions( return FirebaseChatOptions(
groupChatsCollectionName: groupChatsCollectionName:
@ -31,6 +39,10 @@ class FirebaseChatOptions {
messagesCollectionName: messagesCollectionName:
messagesCollectionName ?? this.messagesCollectionName, messagesCollectionName ?? this.messagesCollectionName,
usersCollectionName: usersCollectionName ?? this.usersCollectionName, usersCollectionName: usersCollectionName ?? this.usersCollectionName,
chatsMetaDataCollectionName:
chatsMetaDataCollectionName ?? this.chatsMetaDataCollectionName,
userChatsCollectionName:
userChatsCollectionName ?? this.userChatsCollectionName,
); );
} }
} }

View file

@ -15,6 +15,10 @@ class FirebaseChatService implements ChatService {
late FirebaseStorage _storage; late FirebaseStorage _storage;
late ChatUserService _userService; late ChatUserService _userService;
late FirebaseChatOptions _options; late FirebaseChatOptions _options;
DocumentSnapshot<Object?>? lastUserDocument;
String? lastGroupId;
List<String> chatIds = [];
int pageNumber = 1;
FirebaseChatService({ FirebaseChatService({
required ChatUserService userService, required ChatUserService userService,
@ -37,7 +41,7 @@ class FirebaseChatService implements ChatService {
var snapshots = _db var snapshots = _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(userId) .doc(userId)
.collection('chats') .collection(_options.userChatsCollectionName)
.doc(chatId) .doc(chatId)
.snapshots(); .snapshots();
@ -52,7 +56,7 @@ class FirebaseChatService implements ChatService {
Function(List<ChatModel>) onReceivedChats, Function(List<ChatModel>) onReceivedChats,
) { ) {
var snapshots = _db var snapshots = _db
.collection(_options.chatsCollectionName) .collection(_options.chatsMetaDataCollectionName)
.where( .where(
FieldPath.documentId, FieldPath.documentId,
whereIn: chatIds, whereIn: chatIds,
@ -228,19 +232,29 @@ class FirebaseChatService implements ChatService {
} }
@override @override
Stream<List<ChatModel>> getChatsStream() { Stream<List<ChatModel>> getChatsStream(int pageSize) {
late StreamController<List<ChatModel>> controller; late StreamController<List<ChatModel>> controller;
StreamSubscription? chatsSubscription; StreamSubscription? chatsSubscription;
controller = StreamController( controller = StreamController(
onListen: () async { onListen: () async {
QuerySnapshot<Map<String, dynamic>> userSnapshot;
List<String> userChatIds;
var currentUser = await _userService.getCurrentUser(); var currentUser = await _userService.getCurrentUser();
var userSnapshot = await _db var userQuery = _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(currentUser?.id) .doc(currentUser?.id)
.collection('chats') .collection(_options.userChatsCollectionName);
.get(); if (lastUserDocument == null) {
var userChatIds = userSnapshot.docs.map((chat) => chat.id).toList(); userSnapshot = await userQuery.limit(pageSize).get();
userChatIds = userSnapshot.docs.map((chat) => chat.id).toList();
} else {
userSnapshot = await userQuery
.limit(pageSize)
.startAfterDocument(lastUserDocument!)
.get();
userChatIds = userSnapshot.docs.map((chat) => chat.id).toList();
}
var userGroupChatIds = await _db var userGroupChatIds = await _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
@ -248,10 +262,27 @@ class FirebaseChatService implements ChatService {
.get() .get()
.then((userCollection) => .then((userCollection) =>
userCollection.data()?[_options.groupChatsCollectionName]) userCollection.data()?[_options.groupChatsCollectionName])
.then((groupChatLabels) => groupChatLabels?.cast<String>()); .then((groupChatLabels) => groupChatLabels?.cast<String>())
.then((groupChatIds) {
var startIndex = (pageNumber - 1) * pageSize;
var endIndex = startIndex + pageSize;
var chatsStream = if (startIndex >= groupChatIds.length) {
_getSpecificChatsStream([...userChatIds, ...userGroupChatIds]); return [];
}
var groupIds = groupChatIds.sublist(
startIndex, endIndex.clamp(0, groupChatIds.length));
lastGroupId = groupIds.last;
return groupIds;
});
if (userSnapshot.docs.isNotEmpty) {
lastUserDocument = userSnapshot.docs.last;
}
pageNumber++;
chatIds.addAll([...userChatIds, ...userGroupChatIds]);
var chatsStream = _getSpecificChatsStream(chatIds);
chatsSubscription = chatsStream.listen((event) { chatsSubscription = chatsStream.listen((event) {
controller.add(event); controller.add(event);
@ -270,7 +301,7 @@ class FirebaseChatService implements ChatService {
var collection = await _db var collection = await _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(currentUser?.id) .doc(currentUser?.id)
.collection('chats') .collection(_options.userChatsCollectionName)
.where('users', arrayContains: user.id) .where('users', arrayContains: user.id)
.get(); .get();
@ -288,7 +319,7 @@ class FirebaseChatService implements ChatService {
var chatCollection = await _db var chatCollection = await _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(currentUser?.id) .doc(currentUser?.id)
.collection('chats') .collection(_options.userChatsCollectionName)
.doc(chatId) .doc(chatId)
.get(); .get();
@ -333,7 +364,7 @@ class FirebaseChatService implements ChatService {
@override @override
Future<void> deleteChat(ChatModel chat) async { Future<void> deleteChat(ChatModel chat) async {
var chatCollection = await _db var chatCollection = await _db
.collection(_options.chatsCollectionName) .collection(_options.chatsMetaDataCollectionName)
.doc(chat.id) .doc(chat.id)
.withConverter( .withConverter(
fromFirestore: (snapshot, _) => fromFirestore: (snapshot, _) =>
@ -349,7 +380,7 @@ class FirebaseChatService implements ChatService {
_db _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(userId) .doc(userId)
.collection('chats') .collection(_options.userChatsCollectionName)
.doc(chat.id) .doc(chat.id)
.delete(); .delete();
} }
@ -387,7 +418,7 @@ class FirebaseChatService implements ChatService {
]; ];
var reference = await _db var reference = await _db
.collection(_options.chatsCollectionName) .collection(_options.chatsMetaDataCollectionName)
.withConverter( .withConverter(
fromFirestore: (snapshot, _) => fromFirestore: (snapshot, _) =>
FirebaseChatDocument.fromJson(snapshot.data()!, snapshot.id), FirebaseChatDocument.fromJson(snapshot.data()!, snapshot.id),
@ -406,12 +437,13 @@ class FirebaseChatService implements ChatService {
await _db await _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(userId) .doc(userId)
.collection('chats') .collection(_options.userChatsCollectionName)
.doc(reference.id) .doc(reference.id)
.set({'users': userIds}); .set({'users': userIds});
} }
chat.id = reference.id; chat.id = reference.id;
chatIds.add(chat.id!);
} else if (chat is GroupChatModel) { } else if (chat is GroupChatModel) {
if (currentUser?.id == null) { if (currentUser?.id == null) {
return chat; return chat;
@ -448,6 +480,7 @@ class FirebaseChatService implements ChatService {
} }
chat.id = reference.id; chat.id = reference.id;
chatIds.add(chat.id!);
} else { } else {
throw Exception('Chat type not supported for firebase'); throw Exception('Chat type not supported for firebase');
} }
@ -467,7 +500,7 @@ class FirebaseChatService implements ChatService {
var userSnapshot = _db var userSnapshot = _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(currentUser?.id) .doc(currentUser?.id)
.collection('chats') .collection(_options.userChatsCollectionName)
.snapshots(); .snapshots();
unreadChatSubscription = userSnapshot.listen((event) { unreadChatSubscription = userSnapshot.listen((event) {
@ -498,7 +531,7 @@ class FirebaseChatService implements ChatService {
await _db await _db
.collection(_options.usersCollectionName) .collection(_options.usersCollectionName)
.doc(currentUser!.id!) .doc(currentUser!.id!)
.collection('chats') .collection(_options.userChatsCollectionName)
.doc(chat.id) .doc(chat.id)
.set({'amount_unread_messages': 0}, SetOptions(merge: true)); .set({'amount_unread_messages': 0}, SetOptions(merge: true));
} }

View file

@ -21,6 +21,10 @@ class FirebaseMessageService implements MessageService {
StreamController<List<ChatMessageModel>>? _controller; StreamController<List<ChatMessageModel>>? _controller;
StreamSubscription<QuerySnapshot>? _subscription; StreamSubscription<QuerySnapshot>? _subscription;
DocumentSnapshot<Object>? lastMessage;
List<ChatMessageModel> _cumulativeMessages = [];
ChatModel? lastChat;
int? chatPageSize;
FirebaseMessageService({ FirebaseMessageService({
required ChatUserService userService, required ChatUserService userService,
@ -60,7 +64,13 @@ class FirebaseMessageService implements MessageService {
) )
.add(message); .add(message);
await chatReference.update({ var metadataReference = _db
.collection(
_options.chatsMetaDataCollectionName,
)
.doc(chat.id);
await metadataReference.update({
'last_used': DateTime.now(), 'last_used': DateTime.now(),
'last_message': message, 'last_message': message,
}); });
@ -76,7 +86,7 @@ class FirebaseMessageService implements MessageService {
// update the chat counter for the other users // update the chat counter for the other users
// get all users from the chat // get all users from the chat
// there is a field in the chat document called users that has a list of user ids // there is a field in the chat document called users that has a list of user ids
var fetchedChat = await chatReference.get(); var fetchedChat = await metadataReference.get();
var chatUsers = fetchedChat.data()?['users'] as List<dynamic>; var chatUsers = fetchedChat.data()?['users'] as List<dynamic>;
// for all users except the message sender update the unread counter // for all users except the message sender update the unread counter
for (var userId in chatUsers) { for (var userId in chatUsers) {
@ -86,7 +96,7 @@ class FirebaseMessageService implements MessageService {
_options.usersCollectionName, _options.usersCollectionName,
) )
.doc(userId) .doc(userId)
.collection('chats') .collection(_options.userChatsCollectionName)
.doc(chat.id); .doc(chat.id);
// what if the amount_unread_messages field does not exist? // what if the amount_unread_messages field does not exist?
// it should be created when the chat is create // it should be created when the chat is create
@ -110,13 +120,14 @@ class FirebaseMessageService implements MessageService {
Future<void> sendTextMessage({ Future<void> sendTextMessage({
required String text, required String text,
required ChatModel chat, required ChatModel chat,
}) => }) {
_sendMessage( return _sendMessage(
chat, chat,
{ {
'text': text, 'text': text,
}, },
); );
}
@override @override
Future<void> sendImageMessage({ Future<void> sendImageMessage({
@ -144,19 +155,42 @@ class FirebaseMessageService implements MessageService {
); );
} }
Query<FirebaseMessageDocument> _getMessagesQuery(ChatModel chat) => _db Query<FirebaseMessageDocument> _getMessagesQuery(ChatModel chat) {
.collection(_options.chatsCollectionName) if (lastChat == null) {
.doc(chat.id) lastChat = chat;
.collection(_options.messagesCollectionName) } else if (lastChat?.id != chat.id) {
.orderBy('timestamp', descending: false) _cumulativeMessages = [];
.withConverter<FirebaseMessageDocument>( lastChat = chat;
lastMessage = null;
}
var query = _db
.collection(_options.chatsCollectionName)
.doc(chat.id)
.collection(_options.messagesCollectionName)
.orderBy('timestamp', descending: true)
.limit(chatPageSize!);
if (lastMessage == null) {
return query.withConverter<FirebaseMessageDocument>(
fromFirestore: (snapshot, _) => fromFirestore: (snapshot, _) =>
FirebaseMessageDocument.fromJson(snapshot.data()!, snapshot.id), FirebaseMessageDocument.fromJson(snapshot.data()!, snapshot.id),
toFirestore: (user, _) => user.toJson(), toFirestore: (user, _) => user.toJson(),
); );
}
return query
.startAfterDocument(lastMessage!)
.withConverter<FirebaseMessageDocument>(
fromFirestore: (snapshot, _) =>
FirebaseMessageDocument.fromJson(snapshot.data()!, snapshot.id),
toFirestore: (user, _) => user.toJson(),
);
}
@override @override
Stream<List<ChatMessageModel>> getMessagesStream(ChatModel chat) { Stream<List<ChatMessageModel>> getMessagesStream(
ChatModel chat, int pageSize) {
chatPageSize = pageSize;
_controller = StreamController<List<ChatMessageModel>>( _controller = StreamController<List<ChatMessageModel>>(
onListen: () { onListen: () {
if (chat.id != null) { if (chat.id != null) {
@ -175,39 +209,54 @@ class FirebaseMessageService implements MessageService {
StreamSubscription<QuerySnapshot> _startListeningForMessages(ChatModel chat) { StreamSubscription<QuerySnapshot> _startListeningForMessages(ChatModel chat) {
debugPrint('Start listening for messages in chat ${chat.id}'); debugPrint('Start listening for messages in chat ${chat.id}');
var snapshots = _getMessagesQuery(chat).snapshots(); var snapshots = _getMessagesQuery(chat).snapshots();
return snapshots.listen( return snapshots.listen(
(snapshot) async { (snapshot) async {
var messages = <ChatMessageModel>[]; List<ChatMessageModel> messages =
List<ChatMessageModel>.from(_cumulativeMessages);
for (var messageDoc in snapshot.docs) { if (snapshot.docs.isNotEmpty) {
var messageData = messageDoc.data(); lastMessage = snapshot.docs.last;
var sender = await _userService.getUser(messageData.sender); for (var messageDoc in snapshot.docs) {
var messageData = messageDoc.data();
if (sender != null) { // Check if the message is already in the list to avoid duplicates
var timestamp = DateTime.fromMillisecondsSinceEpoch( if (!messages.any((message) {
(messageData.timestamp).millisecondsSinceEpoch, var timestamp = DateTime.fromMillisecondsSinceEpoch(
); (messageData.timestamp).millisecondsSinceEpoch,
);
return timestamp == message.timestamp;
})) {
var sender = await _userService.getUser(messageData.sender);
messages.add( if (sender != null) {
messageData.imageUrl != null var timestamp = DateTime.fromMillisecondsSinceEpoch(
? ChatImageMessageModel( (messageData.timestamp).millisecondsSinceEpoch,
sender: sender, );
imageUrl: messageData.imageUrl!,
timestamp: timestamp, messages.add(
) messageData.imageUrl != null
: ChatTextMessageModel( ? ChatImageMessageModel(
sender: sender, sender: sender,
text: messageData.text!, imageUrl: messageData.imageUrl!,
timestamp: timestamp, timestamp: timestamp,
), )
); : ChatTextMessageModel(
sender: sender,
text: messageData.text!,
timestamp: timestamp,
),
);
}
}
} }
} }
_cumulativeMessages = messages;
messages.sort((a, b) => a.timestamp.compareTo(b.timestamp));
_controller?.add(messages); _controller?.add(messages);
}, },
); );

View file

@ -4,7 +4,7 @@
name: flutter_community_chat_firebase name: flutter_community_chat_firebase
description: A new Flutter package project. description: A new Flutter package project.
version: 0.6.0 version: 1.0.0
publish_to: none publish_to: none
environment: environment:
@ -23,7 +23,7 @@ dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_community_chat url: https://github.com/Iconica-Development/flutter_community_chat
path: packages/flutter_community_chat_interface path: packages/flutter_community_chat_interface
ref: 0.6.0 ref: 1.0.0
dev_dependencies: dev_dependencies:
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0

View file

@ -1,7 +1,7 @@
import 'package:flutter_community_chat_interface/flutter_community_chat_interface.dart'; import 'package:flutter_community_chat_interface/flutter_community_chat_interface.dart';
abstract class ChatService { abstract class ChatService {
Stream<List<ChatModel>> getChatsStream(); Stream<List<ChatModel>> getChatsStream(int pageSize);
Future<ChatModel> getChatByUser(ChatUserModel user); Future<ChatModel> getChatByUser(ChatUserModel user);
Future<ChatModel> getChatById(String id); Future<ChatModel> getChatById(String id);
Future<void> deleteChat(ChatModel chat); Future<void> deleteChat(ChatModel chat);

View file

@ -14,5 +14,6 @@ abstract class MessageService {
Stream<List<ChatMessageModel>> getMessagesStream( Stream<List<ChatMessageModel>> getMessagesStream(
ChatModel chat, ChatModel chat,
int pageSize,
); );
} }

View file

@ -4,7 +4,7 @@
name: flutter_community_chat_interface name: flutter_community_chat_interface
description: A new Flutter package project. description: A new Flutter package project.
version: 0.6.0 version: 1.0.0
publish_to: none publish_to: none
environment: environment:

View file

@ -0,0 +1,14 @@
// This is a generated file; do not edit or check into version control.
FLUTTER_ROOT=/opt/homebrew/Caskroom/flutter/3.10.2/flutter
FLUTTER_APPLICATION_PATH=/Users/mikedoornenbal/Documents/iconica/flutter_community_chat/packages/flutter_community_chat_view
COCOAPODS_PARALLEL_CODE_SIGN=true
FLUTTER_TARGET=lib/main.dart
FLUTTER_BUILD_DIR=build
FLUTTER_BUILD_NAME=0.6.0
FLUTTER_BUILD_NUMBER=0.6.0
EXCLUDED_ARCHS[sdk=iphonesimulator*]=i386
EXCLUDED_ARCHS[sdk=iphoneos*]=armv7
DART_OBFUSCATION=false
TRACK_WIDGET_CREATION=true
TREE_SHAKE_ICONS=false
PACKAGE_CONFIG=.dart_tool/package_config.json

View file

@ -0,0 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/opt/homebrew/Caskroom/flutter/3.10.2/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/mikedoornenbal/Documents/iconica/flutter_community_chat/packages/flutter_community_chat_view"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "FLUTTER_BUILD_NAME=0.6.0"
export "FLUTTER_BUILD_NUMBER=0.6.0"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.dart_tool/package_config.json"

View file

@ -0,0 +1,41 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}
def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
flutter_ios_podfile_setup
target 'Runner' do
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
inherit! :search_paths
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end

View file

@ -0,0 +1,19 @@
//
// Generated file. Do not edit.
//
// clang-format off
#ifndef GeneratedPluginRegistrant_h
#define GeneratedPluginRegistrant_h
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
@interface GeneratedPluginRegistrant : NSObject
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry;
@end
NS_ASSUME_NONNULL_END
#endif /* GeneratedPluginRegistrant_h */

View file

@ -0,0 +1,63 @@
//
// Generated file. Do not edit.
//
// clang-format off
#import "GeneratedPluginRegistrant.h"
#if __has_include(<cloud_firestore/FLTFirebaseFirestorePlugin.h>)
#import <cloud_firestore/FLTFirebaseFirestorePlugin.h>
#else
@import cloud_firestore;
#endif
#if __has_include(<firebase_auth/FLTFirebaseAuthPlugin.h>)
#import <firebase_auth/FLTFirebaseAuthPlugin.h>
#else
@import firebase_auth;
#endif
#if __has_include(<firebase_core/FLTFirebaseCorePlugin.h>)
#import <firebase_core/FLTFirebaseCorePlugin.h>
#else
@import firebase_core;
#endif
#if __has_include(<firebase_storage/FLTFirebaseStoragePlugin.h>)
#import <firebase_storage/FLTFirebaseStoragePlugin.h>
#else
@import firebase_storage;
#endif
#if __has_include(<image_picker_ios/FLTImagePickerPlugin.h>)
#import <image_picker_ios/FLTImagePickerPlugin.h>
#else
@import image_picker_ios;
#endif
#if __has_include(<path_provider_foundation/PathProviderPlugin.h>)
#import <path_provider_foundation/PathProviderPlugin.h>
#else
@import path_provider_foundation;
#endif
#if __has_include(<sqflite/SqflitePlugin.h>)
#import <sqflite/SqflitePlugin.h>
#else
@import sqflite;
#endif
@implementation GeneratedPluginRegistrant
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
[FLTFirebaseFirestorePlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTFirebaseFirestorePlugin"]];
[FLTFirebaseAuthPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTFirebaseAuthPlugin"]];
[FLTFirebaseCorePlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTFirebaseCorePlugin"]];
[FLTFirebaseStoragePlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTFirebaseStoragePlugin"]];
[FLTImagePickerPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTImagePickerPlugin"]];
[PathProviderPlugin registerWithRegistrar:[registry registrarForPlugin:@"PathProviderPlugin"]];
[SqflitePlugin registerWithRegistrar:[registry registrarForPlugin:@"SqflitePlugin"]];
}
@end

View file

@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_community_chat_view/flutter_community_chat_view.dart'; import 'package:flutter_community_chat_view/flutter_community_chat_view.dart';
import 'package:flutter_community_chat_view/src/components/chat_bottom.dart'; import 'package:flutter_community_chat_view/src/components/chat_bottom.dart';
import 'package:flutter_community_chat_view/src/components/chat_detail_row.dart'; import 'package:flutter_community_chat_view/src/components/chat_detail_row.dart';
@ -20,6 +21,7 @@ class ChatDetailScreen extends StatefulWidget {
required this.service, required this.service,
required this.chatUserService, required this.chatUserService,
required this.messageService, required this.messageService,
required this.pageSize,
this.translations = const ChatTranslations(), this.translations = const ChatTranslations(),
this.chat, this.chat,
this.onPressChatTitle, this.onPressChatTitle,
@ -47,6 +49,7 @@ class ChatDetailScreen extends StatefulWidget {
final ChatService service; final ChatService service;
final ChatUserService chatUserService; final ChatUserService chatUserService;
final MessageService messageService; final MessageService messageService;
final int pageSize;
@override @override
State<ChatDetailScreen> createState() => _ChatDetailScreenState(); State<ChatDetailScreen> createState() => _ChatDetailScreenState();
@ -58,6 +61,8 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
Stream<List<ChatMessageModel>>? _chatMessages; Stream<List<ChatMessageModel>>? _chatMessages;
ChatModel? chat; ChatModel? chat;
ChatUserModel? currentUser; ChatUserModel? currentUser;
ScrollController controller = ScrollController();
bool showIndicator = false;
@override @override
void initState() { void initState() {
@ -65,7 +70,7 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
// create a broadcast stream from the chat messages // create a broadcast stream from the chat messages
if (widget.chat != null) { if (widget.chat != null) {
_chatMessages = widget.messageService _chatMessages = widget.messageService
.getMessagesStream(widget.chat!) .getMessagesStream(widget.chat!, widget.pageSize)
.asBroadcastStream(); .asBroadcastStream();
} }
_chatMessagesSubscription = _chatMessages?.listen((event) { _chatMessagesSubscription = _chatMessages?.listen((event) {
@ -121,7 +126,6 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
future: widget.service.getChatById(widget.chat?.id ?? ''), future: widget.service.getChatById(widget.chat?.id ?? ''),
builder: (context, AsyncSnapshot<ChatModel> snapshot) { builder: (context, AsyncSnapshot<ChatModel> snapshot) {
var chatModel = snapshot.data; var chatModel = snapshot.data;
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
centerTitle: true, centerTitle: true,
@ -186,11 +190,44 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
); );
previousMessage = message; previousMessage = message;
} }
return Listener(
onPointerMove: (event) {
var isTop = controller.position.pixels ==
controller.position.maxScrollExtent;
return ListView( if (showIndicator == false &&
reverse: true, isTop &&
padding: const EdgeInsets.only(top: 24.0), !(controller.position.userScrollDirection ==
children: messageWidgets.reversed.toList(), ScrollDirection.reverse)) {
setState(() {
showIndicator = true;
});
_chatMessages = widget.messageService
.getMessagesStream(widget.chat!, widget.pageSize)
.asBroadcastStream();
Future.delayed(const Duration(seconds: 2), () {
if (mounted) {
setState(() {
showIndicator = false;
});
}
});
}
},
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
controller: controller,
reverse: true,
padding: const EdgeInsets.only(top: 24.0),
children: [
...messageWidgets.reversed.toList(),
if (snapshot.connectionState !=
ConnectionState.active ||
showIndicator) ...[
const Center(child: CircularProgressIndicator()),
],
],
),
); );
}, },
), ),

View file

@ -4,7 +4,10 @@
// ignore_for_file: lines_longer_than_80_chars // ignore_for_file: lines_longer_than_80_chars
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_community_chat_view/flutter_community_chat_view.dart'; import 'package:flutter_community_chat_view/flutter_community_chat_view.dart';
import 'package:flutter_community_chat_view/src/services/date_formatter.dart'; import 'package:flutter_community_chat_view/src/services/date_formatter.dart';
@ -15,6 +18,7 @@ class ChatScreen extends StatefulWidget {
required this.onPressChat, required this.onPressChat,
required this.onDeleteChat, required this.onDeleteChat,
required this.service, required this.service,
required this.pageSize,
this.onNoChats, this.onNoChats,
this.deleteChatDialog, this.deleteChatDialog,
this.translations = const ChatTranslations(), this.translations = const ChatTranslations(),
@ -25,10 +29,11 @@ class ChatScreen extends StatefulWidget {
final ChatOptions options; final ChatOptions options;
final ChatTranslations translations; final ChatTranslations translations;
final ChatService service; final ChatService service;
final VoidCallback? onPressStartChat; final Function? onPressStartChat;
final VoidCallback? onNoChats; final Function? onNoChats;
final void Function(ChatModel chat) onDeleteChat; final void Function(ChatModel chat) onDeleteChat;
final void Function(ChatModel chat) onPressChat; final void Function(ChatModel chat) onPressChat;
final int pageSize;
/// Disable the swipe to dismiss feature for chats that are not deletable /// Disable the swipe to dismiss feature for chats that are not deletable
final bool disableDismissForPermanentChats; final bool disableDismissForPermanentChats;
@ -42,6 +47,28 @@ class ChatScreen extends StatefulWidget {
class _ChatScreenState extends State<ChatScreen> { class _ChatScreenState extends State<ChatScreen> {
final DateFormatter _dateFormatter = DateFormatter(); final DateFormatter _dateFormatter = DateFormatter();
bool _hasCalledOnNoChats = false; bool _hasCalledOnNoChats = false;
ScrollController controller = ScrollController();
bool showIndicator = false;
Stream<List<ChatModel>>? chats;
List<String> deletedChats = [];
@override
void initState() {
getChats();
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
void getChats() {
setState(() {
chats = widget.service.getChatsStream(widget.pageSize);
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -72,145 +99,197 @@ class _ChatScreenState extends State<ChatScreen> {
Column( Column(
children: [ children: [
Expanded( Expanded(
child: ListView( child: Listener(
padding: const EdgeInsets.only(top: 15.0), onPointerMove: (event) {
children: [ var isTop = controller.position.pixels ==
StreamBuilder<List<ChatModel>>( controller.position.maxScrollExtent;
stream: widget.service.getChatsStream(),
builder: (BuildContext context, snapshot) { if (showIndicator == false &&
// if the stream is done, empty and noChats is set we should call that !isTop &&
if (snapshot.connectionState == ConnectionState.done && controller.position.userScrollDirection ==
(snapshot.data?.isEmpty ?? true)) { ScrollDirection.reverse) {
if (widget.onNoChats != null && !_hasCalledOnNoChats) { setState(() {
_hasCalledOnNoChats = true; // Set the flag to true showIndicator = true;
WidgetsBinding.instance.addPostFrameCallback((_) { });
widget.onNoChats!.call(); getChats();
}); Future.delayed(const Duration(seconds: 2), () {
} if (mounted) {
} else { setState(() {
_hasCalledOnNoChats = showIndicator = false;
false; // Reset the flag if there are chats });
} }
return Column( });
children: [ }
for (ChatModel chat in snapshot.data ?? []) ...[ },
Builder( child: ListView(
builder: (context) => !(widget controller: controller,
.disableDismissForPermanentChats && physics: const AlwaysScrollableScrollPhysics(),
!chat.canBeDeleted) padding: const EdgeInsets.only(top: 15.0),
? Dismissible( children: [
confirmDismiss: (_) => StreamBuilder<List<ChatModel>>(
widget.deleteChatDialog stream: chats,
?.call(context, chat) ?? builder: (BuildContext context, snapshot) {
showModalBottomSheet( // if the stream is done, empty and noChats is set we should call that
context: context, if (snapshot.connectionState == ConnectionState.done &&
builder: (BuildContext context) => (snapshot.data?.isEmpty ?? true)) {
Container( if (widget.onNoChats != null && !_hasCalledOnNoChats) {
padding: const EdgeInsets.all(16.0), _hasCalledOnNoChats = true; // Set the flag to true
child: Column( WidgetsBinding.instance
mainAxisSize: MainAxisSize.min, .addPostFrameCallback((_) async {
children: [ await widget.onNoChats!.call();
Text( getChats();
chat.canBeDeleted });
? translations }
.deleteChatModalTitle } else {
: translations _hasCalledOnNoChats =
.chatCantBeDeleted, false; // Reset the flag if there are chats
style: const TextStyle( }
fontSize: 20, return Column(
fontWeight: FontWeight.bold, children: [
), for (ChatModel chat in (snapshot.data ?? []).where(
), (chat) => !deletedChats.contains(chat.id),
const SizedBox(height: 16), )) ...[
if (chat.canBeDeleted) Builder(
builder: (context) => !(widget
.disableDismissForPermanentChats &&
!chat.canBeDeleted)
? Dismissible(
confirmDismiss: (_) async =>
widget.deleteChatDialog
?.call(context, chat) ??
showModalBottomSheet(
context: context,
builder: (BuildContext context) =>
Container(
padding:
const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text( Text(
translations chat.canBeDeleted
.deleteChatModalDescription, ? translations
.deleteChatModalTitle
: translations
.chatCantBeDeleted,
style: const TextStyle( style: const TextStyle(
fontSize: 16, fontSize: 20,
fontWeight:
FontWeight.bold,
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
Row( if (chat.canBeDeleted)
mainAxisAlignment: Text(
MainAxisAlignment.center, translations
children: [ .deleteChatModalDescription,
TextButton( style: const TextStyle(
child: Text( fontSize: 16,
translations
.deleteChatModalCancel,
style: const TextStyle(
fontSize: 16,
),
), ),
onPressed: () =>
Navigator.of(context)
.pop(false),
), ),
if (chat.canBeDeleted) const SizedBox(height: 16),
ElevatedButton( Row(
onPressed: () => mainAxisAlignment:
Navigator.of( MainAxisAlignment
context, .center,
).pop(true), children: [
TextButton(
child: Text( child: Text(
translations translations
.deleteChatModalConfirm, .deleteChatModalCancel,
style: style:
const TextStyle( const TextStyle(
fontSize: 16, fontSize: 16,
), ),
), ),
onPressed: () =>
Navigator.of(
context,
).pop(false),
), ),
], if (chat.canBeDeleted)
), ElevatedButton(
], onPressed: () =>
Navigator.of(
context,
).pop(true),
child: Text(
translations
.deleteChatModalConfirm,
style:
const TextStyle(
fontSize: 16,
),
),
),
],
),
],
),
),
),
onDismissed: (_) {
setState(() {
deletedChats.add(chat.id!);
});
widget.onDeleteChat(chat);
},
background: Container(
color: Colors.red,
child: Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
translations.deleteChatButton,
), ),
), ),
), ),
onDismissed: (_) =>
widget.onDeleteChat(chat),
background: Container(
color: Colors.red,
child: Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
translations.deleteChatButton,
),
),
), ),
), key: ValueKey(
key: ValueKey( chat.id.toString(),
chat.id.toString(), ),
), child: ChatListItem(
child: ChatListItem( widget: widget,
chat: chat,
translations: translations,
dateFormatter: _dateFormatter,
),
)
: ChatListItem(
widget: widget, widget: widget,
chat: chat, chat: chat,
translations: translations, translations: translations,
dateFormatter: _dateFormatter, dateFormatter: _dateFormatter,
), ),
) ),
: ChatListItem( ],
widget: widget, if (showIndicator &&
chat: chat, snapshot.connectionState !=
translations: translations, ConnectionState.done) ...[
dateFormatter: _dateFormatter, const SizedBox(
), height: 10,
), ),
const CircularProgressIndicator(),
const SizedBox(
height: 10,
),
],
], ],
], );
); },
}, ),
), ],
], ),
), ),
), ),
if (widget.onPressStartChat != null) if (widget.onPressStartChat != null)
widget.options.newChatButtonBuilder( widget.options.newChatButtonBuilder(
context, context,
widget.onPressStartChat!, () async {
await widget.onPressStartChat!.call();
getChats();
},
translations, translations,
), ),
], ],

View file

@ -122,7 +122,9 @@ class _NewChatScreenState extends State<NewChatScreen> {
title: user.fullName ?? widget.translations.anonymousUser, title: user.fullName ?? widget.translations.anonymousUser,
), ),
), ),
onTap: () => widget.onPressCreateChat(user), onTap: () async {
await widget.onPressCreateChat(user);
},
); );
}, },
); );

View file

@ -4,7 +4,7 @@
name: flutter_community_chat_view name: flutter_community_chat_view
description: A standard flutter package. description: A standard flutter package.
version: 0.6.0 version: 1.0.0
publish_to: none publish_to: none
@ -20,7 +20,7 @@ dependencies:
git: git:
url: https://github.com/Iconica-Development/flutter_community_chat url: https://github.com/Iconica-Development/flutter_community_chat
path: packages/flutter_community_chat_interface path: packages/flutter_community_chat_interface
ref: 0.6.0 ref: 1.0.0
cached_network_image: ^3.2.2 cached_network_image: ^3.2.2
flutter_image_picker: flutter_image_picker:
git: git: