feat(ui): add ability to override delete confirm button tap.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2023-06-15 19:15:04 +05:30
committed by xsahil03x
parent fe60a72ab3
commit d9be58ab8f
2 changed files with 28 additions and 23 deletions
@@ -19,6 +19,7 @@ class MessageActionsModal extends StatefulWidget {
this.showDeleteMessage = true,
this.showEditMessage = true,
this.onReplyTap,
this.onConfirmDeleteTap,
this.onThreadReplyTap,
this.showCopyMessage = true,
this.showReplyMessage = true,
@@ -44,6 +45,9 @@ class MessageActionsModal extends StatefulWidget {
/// The action to perform when "reply" is tapped
final OnMessageTap? onReplyTap;
/// The action to perform when delete confirmation button is tapped.
final Future<void> Function(Message)? onConfirmDeleteTap;
/// Message in focus for actions
final Message message;
@@ -363,7 +367,12 @@ class _MessageActionsModalState extends State<MessageActionsModal> {
if (answer == true) {
try {
Navigator.of(context).pop();
await StreamChannel.of(context).channel.deleteMessage(widget.message);
final onConfirmDeleteTap = widget.onConfirmDeleteTap;
if (onConfirmDeleteTap != null) {
await onConfirmDeleteTap(widget.message);
} else {
await StreamChannel.of(context).channel.deleteMessage(widget.message);
}
} catch (err) {
_showErrorAlertBottomSheet();
}
@@ -61,6 +61,7 @@ class StreamMessageWidget extends StatefulWidget {
this.showInChannelIndicator = false,
this.onReplyTap,
this.onThreadTap,
this.onConfirmDeleteTap,
this.showUsername = true,
this.showTimestamp = true,
this.showReactions = true,
@@ -307,6 +308,11 @@ class StreamMessageWidget extends StatefulWidget {
/// {@endtemplate}
final void Function(Message)? onReplyTap;
/// {@template onDeleteTap}
/// The function called when delete confirmation button is tapped.
/// {@endtemplate}
final Future<void> Function(Message)? onConfirmDeleteTap;
/// {@template editMessageInputBuilder}
/// Widget builder for edit message layout
/// {@endtemplate}
@@ -568,6 +574,7 @@ class StreamMessageWidget extends StatefulWidget {
void Function(User)? onMentionTap,
void Function(Message)? onThreadTap,
void Function(Message)? onReplyTap,
Future<void> Function(Message)? onConfirmDeleteTap,
Widget Function(BuildContext, Message)? editMessageInputBuilder,
Widget Function(BuildContext, Message)? textBuilder,
@Deprecated('''
@@ -659,6 +666,7 @@ class StreamMessageWidget extends StatefulWidget {
onMentionTap: onMentionTap ?? this.onMentionTap,
onThreadTap: onThreadTap ?? this.onThreadTap,
onReplyTap: onReplyTap ?? this.onReplyTap,
onConfirmDeleteTap: onConfirmDeleteTap ?? this.onConfirmDeleteTap,
editMessageInputBuilder:
editMessageInputBuilder ?? this.editMessageInputBuilder,
textBuilder: textBuilder ?? this.textBuilder,
@@ -1098,16 +1106,21 @@ class _StreamMessageWidgetState extends State<StreamMessageWidget>
),
onClick: () async {
Navigator.of(context, rootNavigator: true).pop();
final deleted = await showDialog(
final deleted = await showDialog<bool?>(
context: context,
barrierDismissible: false,
builder: (_) => const DeleteMessageDialog(),
);
if (deleted) {
if (deleted == true) {
try {
await StreamChannel.of(context)
.channel
.deleteMessage(widget.message);
final onConfirmDeleteTap = widget.onConfirmDeleteTap;
if (onConfirmDeleteTap != null) {
await onConfirmDeleteTap(widget.message);
} else {
await StreamChannel.of(context)
.channel
.deleteMessage(widget.message);
}
} catch (e) {
showDialog(
context: context,
@@ -1197,21 +1210,4 @@ class _StreamMessageWidgetState extends State<StreamMessageWidget>
),
);
}
void retryMessage(BuildContext context) {
final channel = StreamChannel.of(context).channel;
if (widget.message.status == MessageSendingStatus.failed) {
channel.sendMessage(widget.message);
return;
}
if (widget.message.status == MessageSendingStatus.failed_update) {
channel.updateMessage(widget.message);
return;
}
if (widget.message.status == MessageSendingStatus.failed_delete) {
channel.deleteMessage(widget.message);
return;
}
}
}