Skip to content

Commit 5714bb9

Browse files
committed
compose [nfc]: Generalize send button beyond stream narrows
Now that much of the state-managing logic has been moved out of this widget and into the text controllers, we mostly just make the topic controller nullable and sprinkle some `?.` operators around. We also let the caller specify how to compute the destination.
1 parent ace674f commit 5714bb9

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

lib/widgets/compose_box.dart

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -557,23 +557,22 @@ class _AttachFromCameraButton extends _AttachUploadsButton {
557557
}
558558
}
559559

560-
/// The send button for _StreamComposeBox.
561-
class _StreamSendButton extends StatefulWidget {
562-
const _StreamSendButton({
563-
required this.narrow,
560+
class _SendButton extends StatefulWidget {
561+
const _SendButton({
564562
required this.topicController,
565563
required this.contentController,
564+
required this.getDestination,
566565
});
567566

568-
final StreamNarrow narrow;
569-
final ComposeTopicController topicController;
567+
final ComposeTopicController? topicController;
570568
final ComposeContentController contentController;
569+
final MessageDestination Function() getDestination;
571570

572571
@override
573-
State<_StreamSendButton> createState() => _StreamSendButtonState();
572+
State<_SendButton> createState() => _SendButtonState();
574573
}
575574

576-
class _StreamSendButtonState extends State<_StreamSendButton> {
575+
class _SendButtonState extends State<_SendButton> {
577576
_hasErrorsChanged() {
578577
setState(() {
579578
// Update disabled/non-disabled state
@@ -583,16 +582,16 @@ class _StreamSendButtonState extends State<_StreamSendButton> {
583582
@override
584583
void initState() {
585584
super.initState();
586-
widget.topicController.hasValidationErrors.addListener(_hasErrorsChanged);
585+
widget.topicController?.hasValidationErrors.addListener(_hasErrorsChanged);
587586
widget.contentController.hasValidationErrors.addListener(_hasErrorsChanged);
588587
}
589588

590589
@override
591-
void didUpdateWidget(covariant _StreamSendButton oldWidget) {
590+
void didUpdateWidget(covariant _SendButton oldWidget) {
592591
super.didUpdateWidget(oldWidget);
593592
if (widget.topicController != oldWidget.topicController) {
594-
oldWidget.topicController.hasValidationErrors.removeListener(_hasErrorsChanged);
595-
widget.topicController.hasValidationErrors.addListener(_hasErrorsChanged);
593+
oldWidget.topicController?.hasValidationErrors.removeListener(_hasErrorsChanged);
594+
widget.topicController?.hasValidationErrors.addListener(_hasErrorsChanged);
596595
}
597596
if (widget.contentController != oldWidget.contentController) {
598597
oldWidget.contentController.hasValidationErrors.removeListener(_hasErrorsChanged);
@@ -602,20 +601,20 @@ class _StreamSendButtonState extends State<_StreamSendButton> {
602601

603602
@override
604603
void dispose() {
605-
widget.topicController.hasValidationErrors.removeListener(_hasErrorsChanged);
604+
widget.topicController?.hasValidationErrors.removeListener(_hasErrorsChanged);
606605
widget.contentController.hasValidationErrors.removeListener(_hasErrorsChanged);
607606
super.dispose();
608607
}
609608

610609
bool get _hasValidationErrors {
611-
return widget.topicController.hasValidationErrors.value
610+
return (widget.topicController?.hasValidationErrors.value ?? false)
612611
|| widget.contentController.hasValidationErrors.value;
613612
}
614613

615614
void _send() {
616615
if (_hasValidationErrors) {
617616
List<String> validationErrorMessages = [
618-
for (final error in widget.topicController.validationErrors)
617+
for (final error in widget.topicController?.validationErrors ?? const [])
619618
error.message(),
620619
for (final error in widget.contentController.validationErrors)
621620
error.message(),
@@ -628,10 +627,8 @@ class _StreamSendButtonState extends State<_StreamSendButton> {
628627
}
629628

630629
final store = PerAccountStoreWidget.of(context);
631-
final destination = StreamDestination(
632-
widget.narrow.streamId, widget.topicController.textNormalized);
633630
final content = widget.contentController.textNormalized;
634-
store.sendMessage(destination: destination, content: content);
631+
store.sendMessage(destination: widget.getDestination(), content: content);
635632

636633
widget.contentController.clear();
637634
}
@@ -743,10 +740,11 @@ class _StreamComposeBoxState extends State<_StreamComposeBox> {
743740
focusNode: _contentFocusNode),
744741
]))),
745742
const SizedBox(width: 8),
746-
_StreamSendButton(
747-
narrow: widget.narrow,
743+
_SendButton(
748744
topicController: _topicController,
749745
contentController: _contentController,
746+
getDestination: () => StreamDestination(
747+
widget.narrow.streamId, _topicController.textNormalized),
750748
),
751749
]),
752750
Theme(

0 commit comments

Comments
 (0)