|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:zulip/widgets/dialog.dart'; |
| 3 | + |
| 4 | +import 'app.dart'; |
| 5 | +import '../api/route/messages.dart'; |
| 6 | + |
| 7 | +enum TopicValidationError { mandatoryButEmpty, tooLong } |
| 8 | + |
| 9 | +class TopicTextEditingController extends TextEditingController { |
| 10 | + // TODO: subscribe to this value: |
| 11 | + // https://zulip.com/help/require-topics |
| 12 | + final mandatory = true; |
| 13 | + |
| 14 | + String textNormalized() { |
| 15 | + String trimmed = text.trim(); |
| 16 | + return trimmed.isEmpty ? kNoTopicTopic : trimmed; |
| 17 | + } |
| 18 | + |
| 19 | + List<TopicValidationError> validationErrors() { |
| 20 | + List<TopicValidationError> result = []; |
| 21 | + final normalized = textNormalized(); |
| 22 | + if (mandatory && normalized == kNoTopicTopic) { |
| 23 | + result.add(TopicValidationError.mandatoryButEmpty); |
| 24 | + } |
| 25 | + if (normalized.length > kMaxTopicLength) { |
| 26 | + result.add(TopicValidationError.tooLong); |
| 27 | + } |
| 28 | + return result; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +enum ContentValidationError { |
| 33 | + empty, |
| 34 | + tooLong, |
| 35 | + // TODO: upload in progress |
| 36 | + // TODO: quote-and-reply in progress |
| 37 | +} |
| 38 | + |
| 39 | +class ContentTextEditingController extends TextEditingController { |
| 40 | + String textNormalized() { |
| 41 | + return text.trim(); |
| 42 | + } |
| 43 | + |
| 44 | + List<ContentValidationError> validationErrors() { |
| 45 | + List<ContentValidationError> result = []; |
| 46 | + final normalized = textNormalized(); |
| 47 | + if (normalized.isEmpty) { |
| 48 | + result.add(ContentValidationError.empty); |
| 49 | + } |
| 50 | + // normalized.length is actually UTF-16 code units, while the server API |
| 51 | + // expresses the max in Unicode code points. So this comparison will be |
| 52 | + // conservative and may cut the user off shorter than necessary. |
| 53 | + if (normalized.length > kMaxMessageLengthCodePoints) { |
| 54 | + result.add(ContentValidationError.tooLong); |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// The send button for StreamComposeBox. |
| 61 | +class _StreamSendButton extends StatefulWidget { |
| 62 | + const _StreamSendButton({required this.topicController, required this.contentController}); |
| 63 | + |
| 64 | + final TopicTextEditingController topicController; |
| 65 | + final ContentTextEditingController contentController; |
| 66 | + |
| 67 | + @override |
| 68 | + State<_StreamSendButton> createState() => _StreamSendButtonState(); |
| 69 | +} |
| 70 | + |
| 71 | +class _StreamSendButtonState extends State<_StreamSendButton> { |
| 72 | + late bool topicHasValidationErrors; |
| 73 | + late bool contentHasValidationErrors; |
| 74 | + |
| 75 | + topicValueChanged() { |
| 76 | + setState(() { |
| 77 | + topicHasValidationErrors = widget.topicController.validationErrors().isNotEmpty; |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + contentValueChanged() { |
| 82 | + setState(() { |
| 83 | + contentHasValidationErrors = widget.contentController.validationErrors().isNotEmpty; |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + @override |
| 88 | + void initState() { |
| 89 | + super.initState(); |
| 90 | + |
| 91 | + topicHasValidationErrors = widget.topicController.validationErrors().isNotEmpty; |
| 92 | + contentHasValidationErrors = widget.contentController.validationErrors().isNotEmpty; |
| 93 | + |
| 94 | + widget.topicController.addListener(topicValueChanged); |
| 95 | + widget.contentController.addListener(contentValueChanged); |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + void dispose() { |
| 100 | + widget.topicController.removeListener(topicValueChanged); |
| 101 | + widget.contentController.removeListener(contentValueChanged); |
| 102 | + super.dispose(); |
| 103 | + } |
| 104 | + |
| 105 | + void _handleSendPressed (BuildContext context) { |
| 106 | + if (topicHasValidationErrors || contentHasValidationErrors) { |
| 107 | + _showSendFailedDialog(context); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + final store = PerAccountStoreWidget.of(context); |
| 112 | + |
| 113 | + store.sendStreamMessage( |
| 114 | + topic: widget.topicController.textNormalized(), |
| 115 | + content: widget.contentController.textNormalized(), |
| 116 | + ); |
| 117 | + |
| 118 | + widget.contentController.clear(); |
| 119 | + } |
| 120 | + |
| 121 | + void _showSendFailedDialog(BuildContext context) { |
| 122 | + return showErrorDialog( |
| 123 | + context: context, |
| 124 | + title: 'Message not sent', |
| 125 | + message: [ |
| 126 | + ...widget.topicController.validationErrors().map((error) { |
| 127 | + switch (error) { |
| 128 | + case TopicValidationError.tooLong: |
| 129 | + return "Topic length shouldn't be greater than 60 characters."; |
| 130 | + case TopicValidationError.mandatoryButEmpty: |
| 131 | + return 'Topics are required in this organization.'; |
| 132 | + } |
| 133 | + }), |
| 134 | + ...widget.contentController.validationErrors().map((error) { |
| 135 | + switch (error) { |
| 136 | + case ContentValidationError.tooLong: |
| 137 | + // TODO: UI text copied from web; is it right? Discussion: |
| 138 | + // https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/max.20message.20size/near/1505518 |
| 139 | + return "Message length shouldn't be greater than 10000 characters."; |
| 140 | + case ContentValidationError.empty: |
| 141 | + return 'You have nothing to send!'; |
| 142 | + } |
| 143 | + }), |
| 144 | + ].join('\n\n')); |
| 145 | + } |
| 146 | + |
| 147 | + @override |
| 148 | + Widget build(BuildContext context) { |
| 149 | + ColorScheme colorScheme = Theme.of(context).colorScheme; |
| 150 | + |
| 151 | + bool submitButtonDisabled = topicHasValidationErrors || contentHasValidationErrors; |
| 152 | + |
| 153 | + // Copy FilledButton defaults (_FilledButtonDefaultsM3.backgroundColor) |
| 154 | + final backgroundColor = submitButtonDisabled |
| 155 | + ? colorScheme.onSurface.withOpacity(0.12) |
| 156 | + : colorScheme.primary; |
| 157 | + |
| 158 | + // Copy FilledButton defaults (_FilledButtonDefaultsM3.foregroundColor) |
| 159 | + final foregroundColor = submitButtonDisabled |
| 160 | + ? colorScheme.onSurface.withOpacity(0.38) |
| 161 | + : colorScheme.onPrimary; |
| 162 | + |
| 163 | + return Ink( |
| 164 | + decoration: BoxDecoration( |
| 165 | + borderRadius: const BorderRadius.all(Radius.circular(8.0)), |
| 166 | + color: backgroundColor, |
| 167 | + ), |
| 168 | + child: IconButton( |
| 169 | + // Empirically, match the height of the content input. Ideally, this |
| 170 | + // would be dynamic and respond to the actual height of the content |
| 171 | + // input. Zeroing the padding lets the constraints take over. |
| 172 | + constraints: const BoxConstraints(minWidth: 35, minHeight: 35), |
| 173 | + padding: const EdgeInsets.all(0), |
| 174 | + |
| 175 | + color: foregroundColor, |
| 176 | + icon: const Icon(Icons.send), |
| 177 | + onPressed: () => _handleSendPressed(context), |
| 178 | + ), |
| 179 | + ); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/// The compose box for writing a stream message. |
| 184 | +class StreamComposeBox extends StatefulWidget { |
| 185 | + const StreamComposeBox({super.key}); |
| 186 | + |
| 187 | + @override |
| 188 | + State<StreamComposeBox> createState() => _StreamComposeBoxState(); |
| 189 | +} |
| 190 | + |
| 191 | +class _StreamComposeBoxState extends State<StreamComposeBox> { |
| 192 | + final _topicController = TopicTextEditingController(); |
| 193 | + final _contentController = ContentTextEditingController(); |
| 194 | + |
| 195 | + @override |
| 196 | + void dispose() { |
| 197 | + _topicController.dispose(); |
| 198 | + _contentController.dispose(); |
| 199 | + super.dispose(); |
| 200 | + } |
| 201 | + |
| 202 | + @override |
| 203 | + Widget build(BuildContext context) { |
| 204 | + ThemeData themeData = Theme.of(context); |
| 205 | + ColorScheme colorScheme = themeData.colorScheme; |
| 206 | + |
| 207 | + final inputThemeData = themeData.copyWith( |
| 208 | + inputDecorationTheme: InputDecorationTheme( |
| 209 | + isDense: true, |
| 210 | + |
| 211 | + // Override the default, which even with isDense is |
| 212 | + // `EdgeInsets.fromLTRB(12.0, 20.0, 12.0, 12.0)`; see |
| 213 | + // https://github.com/flutter/flutter/blob/3.7.0-1.2.pre/packages/flutter/lib/src/material/input_decorator.dart#L2360 |
| 214 | + // We'd like to be even denser. |
| 215 | + contentPadding: const EdgeInsets.symmetric( |
| 216 | + horizontal: 12.0, vertical: 8.0), |
| 217 | + |
| 218 | + border: const OutlineInputBorder( |
| 219 | + borderRadius: BorderRadius.all(Radius.circular(4.0)), |
| 220 | + |
| 221 | + // Opt out of special formatting for various |
| 222 | + // `MaterialState`s (focused, etc.), to match RN app |
| 223 | + borderSide: BorderSide.none, |
| 224 | + ), |
| 225 | + // InputBorder.none, |
| 226 | + |
| 227 | + filled: true, |
| 228 | + fillColor: colorScheme.surface, |
| 229 | + ), |
| 230 | + ); |
| 231 | + |
| 232 | + final topicInput = TextField( |
| 233 | + controller: _topicController, |
| 234 | + style: TextStyle(color: colorScheme.onSurface), |
| 235 | + decoration: const InputDecoration(hintText: 'Topic'), |
| 236 | + ); |
| 237 | + |
| 238 | + final contentInput = ConstrainedBox( |
| 239 | + // TODO constrain height adaptively (i.e. not hard-coded 200) |
| 240 | + constraints: const BoxConstraints(maxHeight: 200), |
| 241 | + |
| 242 | + child: TextField( |
| 243 | + controller: _contentController, |
| 244 | + style: TextStyle(color: colorScheme.onSurface), |
| 245 | + decoration: InputDecoration( |
| 246 | + hintText: "Message #test here > ${_topicController.textNormalized()}", |
| 247 | + ), |
| 248 | + |
| 249 | + maxLines: null, |
| 250 | + ), |
| 251 | + ); |
| 252 | + |
| 253 | + return Material( |
| 254 | + color: colorScheme.surfaceVariant, |
| 255 | + |
| 256 | + // Not sure what to choose here; 4 is AppBar's default. |
| 257 | + elevation: 4, |
| 258 | + |
| 259 | + child: SafeArea( |
| 260 | + minimum: const EdgeInsets.fromLTRB(8, 0, 8, 8), |
| 261 | + child: Padding( |
| 262 | + padding: const EdgeInsets.only(top: 8.0), |
| 263 | + child: Row(crossAxisAlignment: CrossAxisAlignment.end, children: [ |
| 264 | + Expanded( |
| 265 | + child: Theme( |
| 266 | + data: inputThemeData, |
| 267 | + child: Column( |
| 268 | + children: [ |
| 269 | + topicInput, |
| 270 | + const SizedBox(height: 8), |
| 271 | + contentInput, |
| 272 | + ], |
| 273 | + )), |
| 274 | + ), |
| 275 | + const SizedBox(width: 8), |
| 276 | + _StreamSendButton(topicController: _topicController, contentController: _contentController), |
| 277 | + ]), |
| 278 | + )), |
| 279 | + ); |
| 280 | + } |
| 281 | +} |
0 commit comments