Skip to content

Commit 2d80383

Browse files
committed
compose [nfc]: Pass controller down (5/6); _SendButton
1 parent d89cd7e commit 2d80383

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

lib/widgets/compose_box.dart

+40-28
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,9 @@ class _AttachFromCameraButton extends _AttachUploadsButton {
867867
}
868868

869869
class _SendButton extends StatefulWidget {
870-
const _SendButton({
871-
required this.topicController,
872-
required this.contentController,
873-
required this.getDestination,
874-
});
870+
const _SendButton({required this.controller, required this.getDestination});
875871

876-
final ComposeTopicController? topicController;
877-
final ComposeContentController contentController;
872+
final ComposeBoxController controller;
878873
final MessageDestination Function() getDestination;
879874

880875
@override
@@ -891,43 +886,62 @@ class _SendButtonState extends State<_SendButton> {
891886
@override
892887
void initState() {
893888
super.initState();
894-
widget.topicController?.hasValidationErrors.addListener(_hasErrorsChanged);
895-
widget.contentController.hasValidationErrors.addListener(_hasErrorsChanged);
889+
final controller = widget.controller;
890+
if (controller is StreamComposeBoxController) {
891+
controller.topic.hasValidationErrors.addListener(_hasErrorsChanged);
892+
}
893+
controller.content.hasValidationErrors.addListener(_hasErrorsChanged);
896894
}
897895

898896
@override
899897
void didUpdateWidget(covariant _SendButton oldWidget) {
900898
super.didUpdateWidget(oldWidget);
901-
if (widget.topicController != oldWidget.topicController) {
902-
oldWidget.topicController?.hasValidationErrors.removeListener(_hasErrorsChanged);
903-
widget.topicController?.hasValidationErrors.addListener(_hasErrorsChanged);
899+
900+
final controller = widget.controller;
901+
final oldController = oldWidget.controller;
902+
if (controller == oldController) return;
903+
904+
if (oldController is StreamComposeBoxController) {
905+
oldController.topic.hasValidationErrors.removeListener(_hasErrorsChanged);
904906
}
905-
if (widget.contentController != oldWidget.contentController) {
906-
oldWidget.contentController.hasValidationErrors.removeListener(_hasErrorsChanged);
907-
widget.contentController.hasValidationErrors.addListener(_hasErrorsChanged);
907+
if (controller is StreamComposeBoxController) {
908+
controller.topic.hasValidationErrors.addListener(_hasErrorsChanged);
908909
}
910+
oldController.content.hasValidationErrors.removeListener(_hasErrorsChanged);
911+
controller.content.hasValidationErrors.addListener(_hasErrorsChanged);
909912
}
910913

911914
@override
912915
void dispose() {
913-
widget.topicController?.hasValidationErrors.removeListener(_hasErrorsChanged);
914-
widget.contentController.hasValidationErrors.removeListener(_hasErrorsChanged);
916+
final controller = widget.controller;
917+
if (controller is StreamComposeBoxController) {
918+
controller.topic.hasValidationErrors.removeListener(_hasErrorsChanged);
919+
}
920+
controller.content.hasValidationErrors.removeListener(_hasErrorsChanged);
915921
super.dispose();
916922
}
917923

918924
bool get _hasValidationErrors {
919-
return (widget.topicController?.hasValidationErrors.value ?? false)
920-
|| widget.contentController.hasValidationErrors.value;
925+
bool result = false;
926+
final controller = widget.controller;
927+
if (controller is StreamComposeBoxController) {
928+
result = controller.topic.hasValidationErrors.value;
929+
}
930+
result |= controller.content.hasValidationErrors.value;
931+
return result;
921932
}
922933

923934
void _send() async {
935+
final controller = widget.controller;
936+
924937
if (_hasValidationErrors) {
925938
final zulipLocalizations = ZulipLocalizations.of(context);
926939
List<String> validationErrorMessages = [
927-
for (final error in widget.topicController?.validationErrors
928-
?? const <TopicValidationError>[])
940+
for (final error in (controller is StreamComposeBoxController
941+
? controller.topic.validationErrors
942+
: const <TopicValidationError>[]))
929943
error.message(zulipLocalizations),
930-
for (final error in widget.contentController.validationErrors)
944+
for (final error in controller.content.validationErrors)
931945
error.message(zulipLocalizations),
932946
];
933947
showErrorDialog(
@@ -938,9 +952,9 @@ class _SendButtonState extends State<_SendButton> {
938952
}
939953

940954
final store = PerAccountStoreWidget.of(context);
941-
final content = widget.contentController.textNormalized;
955+
final content = controller.content.textNormalized;
942956

943-
widget.contentController.clear();
957+
controller.content.clear();
944958
// The following `stoppedComposing` call is currently redundant,
945959
// because clearing input sends a "typing stopped" notice.
946960
// It will be necessary once we resolve #720.
@@ -1137,8 +1151,7 @@ class _StreamComposeBoxBody extends _ComposeBoxBody {
11371151
);
11381152

11391153
@override Widget sendButton() => _SendButton(
1140-
topicController: controller.topic,
1141-
contentController: controller.content,
1154+
controller: controller,
11421155
getDestination: () => StreamDestination(
11431156
narrow.streamId, controller.topic.textNormalized),
11441157
);
@@ -1161,8 +1174,7 @@ class _FixedDestinationComposeBoxBody extends _ComposeBoxBody {
11611174
);
11621175

11631176
@override Widget sendButton() => _SendButton(
1164-
topicController: null,
1165-
contentController: controller.content,
1177+
controller: controller,
11661178
getDestination: () => narrow.destination,
11671179
);
11681180
}

0 commit comments

Comments
 (0)