Skip to content

Commit

Permalink
Merge pull request #38 from NicParmee-Kena/main
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
asmodeoux authored Aug 30, 2024
2 parents 19278b0 + 9d2f119 commit e2c5759
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 161 deletions.
5 changes: 5 additions & 0 deletions flutter_twilio_conversations/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.6+16
* example: Add unread message count, send typing status when typing and added message indexes for debugging
* WEB: Fix crash with `typing`, `getUnreadMessagesCount`, `connectionStateChanged` and `connectionError` methods
* WEB: Refactored `sendMessage` to improve stability during conversation creation

## 2.0.5+15
* WEB: Update `getLastMessages` to return newest messages first and count backwards

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ class GetConversationMessagesAction {
GetConversationMessagesAction(this.channel);
}

class GetConversationUnreadMessagesCountAction {
final Channel channel;

GetConversationUnreadMessagesCountAction(this.channel);
}

class UpdateChatMessagesAction {
final List<Message> messages;

UpdateChatMessagesAction(this.messages);
}

class UpdateUnreadMessagesCountAction {
final Channel channel;
final int unreadMessagesCount;

UpdateUnreadMessagesCountAction(this.channel, this.unreadMessagesCount);
}

class SendTextMessageAction {
final Channel channel;
final String text;
Expand All @@ -27,3 +40,9 @@ class SendImageAction {

SendImageAction(this.channel, this.image);
}

class SendTypingAction {
final Channel channel;

SendTypingAction(this.channel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class MessengerSubscriptionsMiddleware extends MiddlewareClass<AppState> {
handleChatSyncSubscription(store, action);
} else if (action is GetConversationMessagesAction) {
getConversationMessages(store, action);
} else if (action is GetConversationUnreadMessagesCountAction) {
getConversationUnreadMessagesCountAction(store, action);
} else if (action is SubscribeToConversationsUpdatesAction) {
handleNewMessagesSubscription(store, action);
} else if (action is SubscribeToMembersTypingStatus) {
Expand All @@ -37,8 +39,10 @@ class MessengerSubscriptionsMiddleware extends MiddlewareClass<AppState> {
store.state.chatClient?.channels != null)) {
final dialogs = store.state.chatClient!.channels!.subscribedChannels
.map(
(channel) =>
ConversationDialog(channel: channel, name: channel.sid),
(channel) => ConversationDialog(
channel: channel,
name: channel.sid,
),
)
.toList();

Expand All @@ -51,6 +55,8 @@ class MessengerSubscriptionsMiddleware extends MiddlewareClass<AppState> {
for (var conversation
in store.state.chatClient!.channels!.subscribedChannels) {
store.dispatch(GetConversationMessagesAction(conversation));
store.dispatch(
GetConversationUnreadMessagesCountAction(conversation));
store.dispatch(SubscribeToMembersTypingStatus(conversation));
}

Expand Down Expand Up @@ -135,4 +141,23 @@ class MessengerSubscriptionsMiddleware extends MiddlewareClass<AppState> {
});
}
}

void getConversationUnreadMessagesCountAction(
Store<AppState> store,
GetConversationUnreadMessagesCountAction action,
) async {
if (action.channel.synchronizationStatus ==
ChannelSynchronizationStatus.ALL) {
try {
final count = await action.channel.getUnreadMessagesCount();
if (count != null) {
store.dispatch(
UpdateUnreadMessagesCountAction(action.channel, count),
);
}
} catch (e) {
debugPrint('Failed to get messages: $e');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TwilioMiddleware extends EpicMiddleware<AppState> {
_initTwilio(),
_sendTextMessage(),
_sendImage(),
_sendTyping(),
]));

static Epic<AppState> _initTwilio() => TypedEpic((
Expand Down Expand Up @@ -90,4 +91,16 @@ class TwilioMiddleware extends EpicMiddleware<AppState> {
debugPrint('Error while sending image: $e');
}
}));

static Epic<AppState> _sendTyping() => TypedEpic((
Stream<SendTypingAction> stream,
EpicStore<AppState> store,
) =>
stream.asyncExpand((action) async* {
try {
await action.channel.typing();
} catch (e) {
debugPrint('Error while sending typing: $e');
}
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AppReducer extends ReducerClass<AppState> {
TypedReducer(_updateIndicators),
TypedReducer(_updateConversation),
TypedReducer(_updateChatMessages),
TypedReducer(_updateChatUnreadMessagesCount),
TypedReducer(_openConversation),
TypedReducer(_closeConversation),
TypedReducer(_typingStarted),
Expand Down Expand Up @@ -73,6 +74,18 @@ class AppReducer extends ReducerClass<AppState> {
)
: state;

AppState _updateChatUnreadMessagesCount(
AppState state,
UpdateUnreadMessagesCountAction action,
) =>
state.copyWith(
dialogs: state.dialogs
.map((dialog) => dialog.channel.sid == action.channel.sid
? dialog.copyWith(unreadCount: action.unreadMessagesCount)
: dialog)
.toList(),
);

AppState _openConversation(
AppState state,
OpenConversationAction action,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ class ConversationDialog {
final String? name;
final List<Message> messages;
final bool isConversationSyncing;
final int unreadCount;
final List<String> typingMembers;

ConversationDialog({
required this.channel,
this.name,
this.messages = const [],
this.isConversationSyncing = false,
this.unreadCount = 0,
this.typingMembers = const [],
});

Expand Down Expand Up @@ -39,13 +41,15 @@ class ConversationDialog {
Channel? channel,
List<Message>? messages,
bool? isConversationSyncing,
int? unreadCount,
List<String>? typingMembers,
}) =>
ConversationDialog(
channel: channel ?? this.channel,
messages: messages ?? this.messages,
isConversationSyncing:
isConversationSyncing ?? this.isConversationSyncing,
unreadCount: unreadCount ?? this.unreadCount,
typingMembers: typingMembers ?? this.typingMembers,
);

Expand Down
23 changes: 18 additions & 5 deletions flutter_twilio_conversations/example/lib/widgets/dialogs_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,24 @@ class DialogsList extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
viewModel.dialogs[index].title,
style: TextStyle(
fontWeight: FontWeight.bold,
),
Row(
children: [
Text(
viewModel.dialogs[index].title,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Text(
viewModel.dialogs[index].unreadCount > 0
? ' (${viewModel.dialogs[index].unreadCount})'
: '',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
],
),
SizedBox(height: 10),
Text(
Expand Down
10 changes: 10 additions & 0 deletions flutter_twilio_conversations/example/lib/widgets/message_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class MessageItem extends StatelessWidget {
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: [
Row(children: [
Expanded(
child: Text(
'${message.messageIndex}',
style: TextStyle(
fontSize: 12,
color: Colors.grey,
),
))
]),
Padding(
padding: const EdgeInsets.only(
bottom: 6,
Expand Down
Loading

0 comments on commit e2c5759

Please sign in to comment.