Skip to content

Commit dd7dd32

Browse files
committed
compose: Change content input hint text if topic is empty and mandatory
Previously, "Message #stream > (no topic)" would appear as the hint text when the topic input is empty but mandatory. Now, it is shown as "Message #stream" instead, since the "(no topic)" isn't allowed when topics are mandatory. The control flow of `getDestinationString` can be simplified. However, it is structured this way to prepare for a later change to support showing "general chat" in the hint text, that adds some more advanced checks. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 100aece commit dd7dd32

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

lib/widgets/compose_box.dart

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,32 @@ class ComposeTopicController extends ComposeController<TopicValidationError> {
170170
/// (see [TopicValidationError.mandatoryButVacuous]).
171171
bool get _isTopicVacuous => textNormalized == kNoTopicTopic;
172172

173+
/// The send destination as a string.
174+
///
175+
/// This returns a string formatted like "#stream name" when topics are
176+
/// [mandatory] but the trimmed input is empty.
177+
///
178+
/// Otherwise, returns a string formatted like "#stream name > topic name".
179+
// No i18n of the use of "#" and ">" strings; those are part of how
180+
// Zulip expresses channels and topics, not any normal English punctuation,
181+
// so don't make sense to translate. See:
182+
// https://github.com/zulip/zulip-flutter/pull/1148#discussion_r1941990585
183+
String getDestinationString({required String streamName}) {
184+
final textTrimmed = text.trim();
185+
if (textTrimmed.isNotEmpty) {
186+
return '#$streamName > $textTrimmed';
187+
}
188+
189+
assert(_isTopicVacuous);
190+
// Sending to a vacuous topic (see [isTopicVacuous]) is not possible if
191+
// topics are [mandatory].
192+
if (mandatory) {
193+
return '#$streamName';
194+
}
195+
196+
return '#$streamName > $kNoTopicTopic';
197+
}
198+
173199
@override
174200
List<TopicValidationError> _computeValidationErrors() {
175201
return [
@@ -588,17 +614,13 @@ class _StreamContentInputState extends State<_StreamContentInput> {
588614
final zulipLocalizations = ZulipLocalizations.of(context);
589615
final streamName = store.streams[widget.narrow.streamId]?.name
590616
?? zulipLocalizations.unknownChannelName;
591-
final topic = TopicName(widget.controller.topic.textNormalized);
592617
return _ContentInput(
593618
narrow: widget.narrow,
594-
destination: TopicNarrow(widget.narrow.streamId, topic),
619+
destination: TopicNarrow(widget.narrow.streamId,
620+
TopicName(widget.controller.topic.textNormalized)),
595621
controller: widget.controller,
596622
hintText: zulipLocalizations.composeBoxChannelContentHint(
597-
// No i18n of this use of "#" and ">" string; those are part of how
598-
// Zulip expresses channels and topics, not any normal English punctuation,
599-
// so don't make sense to translate. See:
600-
// https://github.com/zulip/zulip-flutter/pull/1148#discussion_r1941990585
601-
'#$streamName > ${topic.displayName}'));
623+
widget.controller.topic.getDestinationString(streamName: streamName)));
602624
}
603625
}
604626

test/widgets/compose_box_test.dart

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,56 @@ void main() {
353353
.decoration.isNotNull().hintText.equals(contentHintText);
354354
}
355355

356-
group('to ChannelNarrow', () {
356+
group('to ChannelNarrow, topics not mandatory', () {
357357
final narrow = ChannelNarrow(channel.streamId);
358358

359359
testWidgets('with empty topic', (tester) async {
360-
await prepare(tester, narrow: narrow);
360+
await prepare(tester, narrow: narrow, mandatoryTopics: false);
361+
checkComposeBoxHintTexts(tester,
362+
topicHintText: 'Topic',
363+
contentHintText: 'Message #${channel.name} > (no topic)');
364+
});
365+
366+
testWidgets('with non-empty but vacuous topic', (tester) async {
367+
await prepare(tester, narrow: narrow, mandatoryTopics: false);
368+
await enterTopic(tester, narrow: narrow, topic: '(no topic)');
369+
await tester.pump();
370+
checkComposeBoxHintTexts(tester,
371+
topicHintText: 'Topic',
372+
contentHintText: 'Message #${channel.name} > (no topic)');
373+
});
374+
375+
testWidgets('with non-empty topic', (tester) async {
376+
await prepare(tester, narrow: narrow, mandatoryTopics: false);
377+
await enterTopic(tester, narrow: narrow, topic: 'new topic');
378+
await tester.pump();
379+
checkComposeBoxHintTexts(tester,
380+
topicHintText: 'Topic',
381+
contentHintText: 'Message #${channel.name} > new topic');
382+
});
383+
});
384+
385+
group('to ChannelNarrow, mandatory topics', () {
386+
final narrow = ChannelNarrow(channel.streamId);
387+
388+
testWidgets('with empty topic', (tester) async {
389+
await prepare(tester, narrow: narrow, mandatoryTopics: true);
390+
checkComposeBoxHintTexts(tester,
391+
topicHintText: 'Topic',
392+
contentHintText: 'Message #${channel.name}');
393+
});
394+
395+
testWidgets('with non-empty but vacuous topic', (tester) async {
396+
await prepare(tester, narrow: narrow, mandatoryTopics: true);
397+
await enterTopic(tester, narrow: narrow, topic: '(no topic)');
398+
await tester.pump();
361399
checkComposeBoxHintTexts(tester,
362400
topicHintText: 'Topic',
363401
contentHintText: 'Message #${channel.name} > (no topic)');
364402
});
365403

366404
testWidgets('with non-empty topic', (tester) async {
367-
await prepare(tester, narrow: narrow);
405+
await prepare(tester, narrow: narrow, mandatoryTopics: true);
368406
await enterTopic(tester, narrow: narrow, topic: 'new topic');
369407
await tester.pump();
370408
checkComposeBoxHintTexts(tester,

0 commit comments

Comments
 (0)