Skip to content

Commit 3d3153d

Browse files
authored
Merge pull request #68 from Iconica-Development/2.0.0
Improve flutter_chat for usage in safino
2 parents 37e975c + c9a1175 commit 3d3153d

File tree

20 files changed

+262
-98
lines changed

20 files changed

+262
-98
lines changed

.github/workflows/melos-ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ jobs:
1111
secrets: inherit
1212
permissions: write-all
1313
with:
14-
subfolder: '.' # add optional subfolder to run workflow in
14+
subfolder: '.' # add optional subfolder to run workflow in
15+
flutter_version: 3.19.6

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 2.0.0
2+
3+
- Add a serviceBuilder to the userstory configuration
4+
- Add a translationsBuilder to the userstory configuration
5+
- Change onPressUserProfile callback to use a ChatUserModel instead of a String
6+
- Add a enableGroupChatCreation boolean to the userstory configuration to enable or disable group chat creation
7+
- Change the ChatTranslations constructor to require all translations or use the ChatTranslations.empty constructor if you don't want to specify all translations
8+
- Remove the Divider between the users on the new chat screen
9+
- Add option to set a custom padding around the list of chats
10+
- Fix nullpointer when firstWhere returns null because there is only 1 person in a groupchat
11+
112
## 1.4.3
213

314
- Added default styling.

packages/flutter_chat/lib/src/flutter_chat_navigator_userstory.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'package:flutter/material.dart';
66
import 'package:flutter_chat/flutter_chat.dart';
7-
import 'package:uuid/uuid.dart';
87

98
/// Navigates to the chat user story screen.
109
///
@@ -96,17 +95,17 @@ Widget _chatDetailScreenRoute(
9695
service: configuration.chatService,
9796
chatId: chatId,
9897
textfieldBottomPadding: configuration.textfieldBottomPadding ?? 0,
99-
onPressUserProfile: (userId) async {
98+
onPressUserProfile: (user) async {
10099
if (configuration.onPressUserProfile != null) {
101-
return configuration.onPressUserProfile?.call();
100+
return configuration.onPressUserProfile?.call(context, user);
102101
}
103102
return Navigator.of(context).push(
104103
MaterialPageRoute(
105104
builder: (context) => _chatProfileScreenRoute(
106105
configuration,
107106
context,
108107
chatId,
109-
userId,
108+
user.id,
110109
),
111110
),
112111
);
@@ -172,7 +171,7 @@ Widget _chatProfileScreenRoute(
172171
userId: userId,
173172
onTapUser: (user) async {
174173
if (configuration.onPressUserProfile != null) {
175-
return configuration.onPressUserProfile!.call();
174+
return configuration.onPressUserProfile!.call(context, user);
176175
}
177176

178177
return Navigator.of(context).push(
@@ -200,6 +199,7 @@ Widget _newChatScreenRoute(
200199
options: configuration.chatOptionsBuilder(context),
201200
translations: configuration.translations,
202201
service: configuration.chatService,
202+
showGroupChatButton: configuration.enableGroupChatCreation,
203203
onPressCreateGroupChat: () async {
204204
configuration.onPressCreateGroupChat?.call();
205205
if (context.mounted) {

packages/flutter_chat/lib/src/flutter_chat_userstory.dart

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ List<GoRoute> getChatStoryRoutes(
1414
GoRoute(
1515
path: ChatUserStoryRoutes.chatScreen,
1616
pageBuilder: (context, state) {
17+
var service = configuration.chatServiceBuilder?.call(context) ??
18+
configuration.chatService;
1719
var chatScreen = ChatScreen(
1820
unreadMessageTextStyle: configuration.unreadMessageTextStyle,
19-
service: configuration.chatService,
21+
service: service,
2022
options: configuration.chatOptionsBuilder(context),
2123
onNoChats: () async =>
2224
context.push(ChatUserStoryRoutes.newChatScreen),
@@ -34,7 +36,8 @@ List<GoRoute> getChatStoryRoutes(
3436
configuration.onDeleteChat?.call(context, chat) ??
3537
configuration.chatService.chatOverviewService.deleteChat(chat),
3638
deleteChatDialog: configuration.deleteChatDialog,
37-
translations: configuration.translations,
39+
translations: configuration.translationsBuilder?.call(context) ??
40+
configuration.translations,
3841
);
3942
return buildScreenWithoutTransition(
4043
context: context,
@@ -53,23 +56,26 @@ List<GoRoute> getChatStoryRoutes(
5356
path: ChatUserStoryRoutes.chatDetailScreen,
5457
pageBuilder: (context, state) {
5558
var chatId = state.pathParameters['id'];
59+
var service = configuration.chatServiceBuilder?.call(context) ??
60+
configuration.chatService;
5661
var chatDetailScreen = ChatDetailScreen(
5762
chatTitleBuilder: configuration.chatTitleBuilder,
5863
usernameBuilder: configuration.usernameBuilder,
5964
loadingWidgetBuilder: configuration.loadingWidgetBuilder,
6065
iconDisabledColor: configuration.iconDisabledColor,
6166
pageSize: configuration.messagePageSize,
6267
options: configuration.chatOptionsBuilder(context),
63-
translations: configuration.translations,
64-
service: configuration.chatService,
68+
translations: configuration.translationsBuilder?.call(context) ??
69+
configuration.translations,
70+
service: service,
6571
chatId: chatId!,
6672
textfieldBottomPadding: configuration.textfieldBottomPadding ?? 0,
67-
onPressUserProfile: (userId) async {
73+
onPressUserProfile: (user) async {
6874
if (configuration.onPressUserProfile != null) {
69-
return configuration.onPressUserProfile?.call();
75+
return configuration.onPressUserProfile?.call(context, user);
7076
}
7177
return context.push(
72-
ChatUserStoryRoutes.chatProfileScreenPath(chatId, userId),
78+
ChatUserStoryRoutes.chatProfileScreenPath(chatId, user.id),
7379
);
7480
},
7581
onMessageSubmit: (message) async {
@@ -120,10 +126,14 @@ List<GoRoute> getChatStoryRoutes(
120126
GoRoute(
121127
path: ChatUserStoryRoutes.newChatScreen,
122128
pageBuilder: (context, state) {
129+
var service = configuration.chatServiceBuilder?.call(context) ??
130+
configuration.chatService;
123131
var newChatScreen = NewChatScreen(
124132
options: configuration.chatOptionsBuilder(context),
125-
translations: configuration.translations,
126-
service: configuration.chatService,
133+
translations: configuration.translationsBuilder?.call(context) ??
134+
configuration.translations,
135+
service: service,
136+
showGroupChatButton: configuration.enableGroupChatCreation,
127137
onPressCreateChat: (user) async {
128138
configuration.onPressCreateChat?.call(user);
129139
if (configuration.onPressCreateChat != null) return;
@@ -163,10 +173,13 @@ List<GoRoute> getChatStoryRoutes(
163173
GoRoute(
164174
path: ChatUserStoryRoutes.newGroupChatScreen,
165175
pageBuilder: (context, state) {
176+
var service = configuration.chatServiceBuilder?.call(context) ??
177+
configuration.chatService;
166178
var newGroupChatScreen = NewGroupChatScreen(
167179
options: configuration.chatOptionsBuilder(context),
168-
translations: configuration.translations,
169-
service: configuration.chatService,
180+
translations: configuration.translationsBuilder?.call(context) ??
181+
configuration.translations,
182+
service: service,
170183
onPressGroupChatOverview: (users) async => context.push(
171184
ChatUserStoryRoutes.newGroupChatOverviewScreen,
172185
extra: users,
@@ -188,11 +201,14 @@ List<GoRoute> getChatStoryRoutes(
188201
GoRoute(
189202
path: ChatUserStoryRoutes.newGroupChatOverviewScreen,
190203
pageBuilder: (context, state) {
204+
var service = configuration.chatServiceBuilder?.call(context) ??
205+
configuration.chatService;
191206
var users = state.extra! as List<ChatUserModel>;
192207
var newGroupChatOverviewScreen = NewGroupChatOverviewScreen(
193208
options: configuration.chatOptionsBuilder(context),
194-
translations: configuration.translations,
195-
service: configuration.chatService,
209+
translations: configuration.translationsBuilder?.call(context) ??
210+
configuration.translations,
211+
service: service,
196212
users: users,
197213
onPressCompleteGroupChatCreation: (users, groupChatName) async {
198214
configuration.onPressCompleteGroupChatCreation
@@ -232,18 +248,21 @@ List<GoRoute> getChatStoryRoutes(
232248
var chatId = state.pathParameters['id'];
233249
var userId = state.pathParameters['userId'];
234250
var id = userId == 'null' ? null : userId;
251+
var service = configuration.chatServiceBuilder?.call(context) ??
252+
configuration.chatService;
235253
var profileScreen = ChatProfileScreen(
236-
translations: configuration.translations,
237-
chatService: configuration.chatService,
254+
translations: configuration.translationsBuilder?.call(context) ??
255+
configuration.translations,
256+
chatService: service,
238257
chatId: chatId!,
239258
userId: id,
240259
onTapUser: (user) async {
241260
if (configuration.onPressUserProfile != null) {
242-
return configuration.onPressUserProfile!.call();
261+
return configuration.onPressUserProfile!.call(context, user);
243262
}
244263

245264
return context.push(
246-
ChatUserStoryRoutes.chatProfileScreenPath(chatId, user),
265+
ChatUserStoryRoutes.chatProfileScreenPath(chatId, user.id),
247266
);
248267
},
249268
);

packages/flutter_chat/lib/src/models/chat_configuration.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ChatUserStoryConfiguration {
1414
const ChatUserStoryConfiguration({
1515
required this.chatService,
1616
required this.chatOptionsBuilder,
17+
this.chatServiceBuilder,
1718
this.onPressStartChat,
1819
this.onPressChat,
1920
this.onDeleteChat,
@@ -27,7 +28,9 @@ class ChatUserStoryConfiguration {
2728
this.deleteChatDialog,
2829
this.disableDismissForPermanentChats = false,
2930
this.routeToNewChatIfEmpty = true,
30-
this.translations = const ChatTranslations(),
31+
this.enableGroupChatCreation = true,
32+
this.translations = const ChatTranslations.empty(),
33+
this.translationsBuilder,
3134
this.chatPageBuilder,
3235
this.onPressChatTitle,
3336
this.afterMessageSent,
@@ -44,6 +47,9 @@ class ChatUserStoryConfiguration {
4447
/// The service responsible for handling chat-related functionalities.
4548
final ChatService chatService;
4649

50+
/// A method to get the chat service only when needed and with a context.
51+
final ChatService Function(BuildContext context)? chatServiceBuilder;
52+
4753
/// Callback function triggered when a chat is pressed.
4854
final Function(BuildContext, ChatModel)? onPressChat;
4955

@@ -53,6 +59,9 @@ class ChatUserStoryConfiguration {
5359
/// Translations for internationalization/localization support.
5460
final ChatTranslations translations;
5561

62+
/// Translations builder because context might be needed for translations.
63+
final ChatTranslations Function(BuildContext context)? translationsBuilder;
64+
5665
/// Determines whether dismissing is disabled for permanent chats.
5766
final bool disableDismissForPermanentChats;
5867

@@ -74,7 +83,10 @@ class ChatUserStoryConfiguration {
7483

7584
/// Builder for chat options based on context.
7685
final Function(List<ChatUserModel>, String)? onPressCompleteGroupChatCreation;
86+
7787
final Function()? onPressCreateGroupChat;
88+
89+
/// Builder for the chat options which can be used to style the UI of the chat
7890
final ChatOptions Function(BuildContext context) chatOptionsBuilder;
7991

8092
/// If true, the user will be routed to the new chat screen if there are
@@ -84,6 +96,10 @@ class ChatUserStoryConfiguration {
8496
/// The size of each page of messages.
8597
final int messagePageSize;
8698

99+
/// Whether to enable group chat creation for the user. If false,
100+
/// the button will be hidden
101+
final bool enableGroupChatCreation;
102+
87103
/// Dialog for confirming chat deletion.
88104
final Future<bool?> Function(BuildContext, ChatModel)? deleteChatDialog;
89105

@@ -100,11 +116,18 @@ class ChatUserStoryConfiguration {
100116
final Function()? onPressStartChat;
101117

102118
/// Callback function triggered when user profile is pressed.
103-
final Function()? onPressUserProfile;
119+
final Function(BuildContext context, ChatUserModel user)? onPressUserProfile;
120+
104121
final double? textfieldBottomPadding;
122+
105123
final Color? iconDisabledColor;
124+
125+
/// The text style used for the unread message counter.
106126
final TextStyle? unreadMessageTextStyle;
127+
107128
final Widget? Function(BuildContext context)? loadingWidgetBuilder;
129+
108130
final Widget Function(String userFullName)? usernameBuilder;
131+
109132
final Widget Function(String chatTitle)? chatTitleBuilder;
110133
}

packages/flutter_chat/pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: flutter_chat
66
description: A new Flutter package project.
7-
version: 1.4.3
7+
version: 2.0.0
88

99
publish_to: none
1010

@@ -20,17 +20,17 @@ dependencies:
2020
git:
2121
url: https://github.com/Iconica-Development/flutter_chat
2222
path: packages/flutter_chat_view
23-
ref: 1.4.3
23+
ref: 2.0.0
2424
flutter_chat_interface:
2525
git:
2626
url: https://github.com/Iconica-Development/flutter_chat
2727
path: packages/flutter_chat_interface
28-
ref: 1.4.3
28+
ref: 2.0.0
2929
flutter_chat_local:
3030
git:
3131
url: https://github.com/Iconica-Development/flutter_chat
3232
path: packages/flutter_chat_local
33-
ref: 1.4.3
33+
ref: 2.0.0
3434
uuid: ^4.3.3
3535

3636
dev_dependencies:

packages/flutter_chat_firebase/lib/service/firebase_chat_overview_service.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ class FirebaseChatOverviewService implements ChatOverviewService {
9191
var chat = element.doc.data();
9292
if (chat == null) return;
9393

94-
var otherUser = await _userService.getUser(
95-
chat.users.firstWhere(
96-
(element) => element != currentUser?.id,
97-
),
98-
);
94+
var otherUser = chat.users.any(
95+
(element) => element != currentUser?.id,
96+
)
97+
? await _userService.getUser(
98+
chat.users.firstWhere(
99+
(element) => element != currentUser?.id,
100+
),
101+
)
102+
: null;
99103

100104
var unread =
101105
await _addUnreadChatSubscription(chat.id!, currentUser!.id!);
@@ -144,18 +148,18 @@ class FirebaseChatOverviewService implements ChatOverviewService {
144148
imageUrl: chat.imageUrl ?? '',
145149
unreadMessages: unread,
146150
users: users,
147-
lastMessage: chat.lastMessage != null
151+
lastMessage: chat.lastMessage != null && otherUser != null
148152
? chat.lastMessage!.imageUrl == null
149153
? ChatTextMessageModel(
150-
sender: otherUser!,
154+
sender: otherUser,
151155
text: chat.lastMessage!.text!,
152156
timestamp: DateTime.fromMillisecondsSinceEpoch(
153157
chat.lastMessage!.timestamp
154158
.millisecondsSinceEpoch,
155159
),
156160
)
157161
: ChatImageMessageModel(
158-
sender: otherUser!,
162+
sender: otherUser,
159163
imageUrl: chat.lastMessage!.imageUrl!,
160164
timestamp: DateTime.fromMillisecondsSinceEpoch(
161165
chat.lastMessage!.timestamp

packages/flutter_chat_firebase/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: flutter_chat_firebase
66
description: A new Flutter package project.
7-
version: 1.4.3
7+
version: 2.0.0
88
publish_to: none
99

1010
environment:
@@ -23,7 +23,7 @@ dependencies:
2323
git:
2424
url: https://github.com/Iconica-Development/flutter_chat
2525
path: packages/flutter_chat_interface
26-
ref: 1.4.3
26+
ref: 2.0.0
2727

2828
dev_dependencies:
2929
flutter_iconica_analysis:

packages/flutter_chat_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: flutter_chat_interface
66
description: A new Flutter package project.
7-
version: 1.4.3
7+
version: 2.0.0
88
publish_to: none
99

1010
environment:

packages/flutter_chat_local/pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: flutter_chat_local
22
description: "A new Flutter package project."
3-
version: 1.4.3
3+
version: 2.0.0
44
publish_to: none
5-
homepage:
65

76
environment:
87
sdk: ">=3.2.5 <4.0.0"
@@ -15,7 +14,7 @@ dependencies:
1514
git:
1615
url: https://github.com/Iconica-Development/flutter_chat
1716
path: packages/flutter_chat_interface
18-
ref: 1.4.3
17+
ref: 2.0.0
1918

2019
dev_dependencies:
2120
flutter_test:

0 commit comments

Comments
 (0)