-
Notifications
You must be signed in to change notification settings - Fork 384
feat(ui): add configurable StreamBackButton unread count
#2816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:rxdart/rxdart.dart'; | ||
| import 'package:stream_chat_flutter/src/misc/empty_widget.dart'; | ||
| import 'package:stream_chat_flutter/stream_chat_flutter.dart'; | ||
|
|
||
|
|
@@ -17,12 +18,16 @@ import 'package:stream_chat_flutter/stream_chat_flutter.dart'; | |
| /// {@endtemplate} | ||
| class StreamUnreadIndicator extends StatelessWidget { | ||
| /// Displays the total unread count. | ||
| /// | ||
| /// Optionally, provide [excludeCid] to omit a specific channel's unread | ||
| /// messages from the total — for example, the currently open channel. | ||
| const StreamUnreadIndicator({ | ||
| super.key, | ||
| this.child, | ||
| this.alignment, | ||
| this.offset, | ||
| this.semanticLabel, | ||
| this.excludeCid, | ||
| }) : _unreadType = const _TotalUnreadCount(); | ||
|
|
||
| /// Displays the unreadChannel count. | ||
|
|
@@ -35,7 +40,8 @@ class StreamUnreadIndicator extends StatelessWidget { | |
| this.alignment, | ||
| this.offset, | ||
| this.semanticLabel, | ||
| }) : _unreadType = _UnreadChannels(cid: cid); | ||
| }) : _unreadType = _UnreadChannels(cid: cid), | ||
| excludeCid = null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can support
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory yes, should be feasible. I decided to not do it now because it wasn't part of the initial bug report. However I have one concern: In that case, we could in theory pass both
(Ideally we would have different factory methods for What do you think about this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets skip it until we have a usecase |
||
|
|
||
| /// Displays the unreadThreads count. | ||
| /// | ||
|
|
@@ -47,10 +53,18 @@ class StreamUnreadIndicator extends StatelessWidget { | |
| this.alignment, | ||
| this.offset, | ||
| this.semanticLabel, | ||
| }) : _unreadType = _UnreadThreads(id: id); | ||
| }) : _unreadType = _UnreadThreads(id: id), | ||
| excludeCid = null; | ||
|
|
||
| final _UnreadTypes _unreadType; | ||
|
|
||
| /// The cid of a channel whose unread messages are excluded from the total | ||
| /// unread count. | ||
| /// | ||
| /// Only applies to the default (total) constructor; ignored by | ||
| /// [StreamUnreadIndicator.channels] and [StreamUnreadIndicator.threads]. | ||
| final String? excludeCid; | ||
|
|
||
| /// Optional child widget to overlay the badge on. | ||
| /// | ||
| /// When non-null, the badge is positioned on top of this widget. | ||
|
|
@@ -85,7 +99,7 @@ class StreamUnreadIndicator extends StatelessWidget { | |
| final client = StreamChat.of(context).client; | ||
|
|
||
| final stream = switch (_unreadType) { | ||
| _TotalUnreadCount() => client.state.totalUnreadCountStream, | ||
| _TotalUnreadCount() => _totalUnreadCountStream(client, excludeCid), | ||
| _UnreadChannels(cid: final cid) => switch (cid) { | ||
| final cid? => client.state.channels[cid]?.state?.unreadCountStream, | ||
| _ => client.state.unreadChannelsStream, | ||
|
|
@@ -97,7 +111,7 @@ class StreamUnreadIndicator extends StatelessWidget { | |
| }; | ||
|
|
||
| final initialData = switch (_unreadType) { | ||
| _TotalUnreadCount() => client.state.totalUnreadCount, | ||
| _TotalUnreadCount() => _totalUnreadCount(client, excludeCid), | ||
| _UnreadChannels(cid: final cid) => switch (cid) { | ||
| final cid? => client.state.channels[cid]?.state?.unreadCount, | ||
| _ => client.state.unreadChannels, | ||
|
|
@@ -135,6 +149,42 @@ class StreamUnreadIndicator extends StatelessWidget { | |
| } | ||
| } | ||
|
|
||
| /// Returns the client's total unread message count as a stream, optionally | ||
| /// subtracting the unread messages of the channel identified by [excludeCid]. | ||
| Stream<int> _totalUnreadCountStream( | ||
| StreamChatClient client, | ||
| String? excludeCid, | ||
| ) { | ||
| final totalUnreadCount = client.state.totalUnreadCountStream; | ||
| if (excludeCid == null) return totalUnreadCount; | ||
|
|
||
| final excludedUnreadCount = client.state.channels[excludeCid]?.state?.unreadCountStream ?? Stream.value(0); | ||
|
|
||
| // The total and the excluded channel's unread count update through separate | ||
| // streams. Both settle within the same event-loop turn, so debouncing on a | ||
| // zero duration coalesces them into a single emission and avoids rendering a | ||
| // transient count before the two values agree. | ||
| return Rx.combineLatest2<int, int, int>( | ||
| totalUnreadCount, | ||
| excludedUnreadCount, | ||
| _subtractExcluded, | ||
| ).debounceTime(Duration.zero).distinct(); | ||
| } | ||
|
|
||
| /// Returns the client's total unread message count, optionally subtracting the | ||
| /// unread messages of the channel identified by [excludeCid]. | ||
| int _totalUnreadCount(StreamChatClient client, String? excludeCid) { | ||
| final totalUnreadCount = client.state.totalUnreadCount; | ||
| if (excludeCid == null) return totalUnreadCount; | ||
|
|
||
| final excludedUnreadCount = client.state.channels[excludeCid]?.state?.unreadCount ?? 0; | ||
|
|
||
| return _subtractExcluded(totalUnreadCount, excludedUnreadCount); | ||
| } | ||
|
|
||
| /// Subtracts [excluded] from [total], flooring the result at zero. | ||
| int _subtractExcluded(int total, int excluded) => total > excluded ? total - excluded : 0; | ||
|
|
||
| sealed class _UnreadTypes { | ||
| const _UnreadTypes._(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,19 +9,39 @@ class StreamBackButton extends StatelessWidget { | |
| const StreamBackButton({ | ||
| super.key, | ||
| this.onPressed, | ||
| @Deprecated( | ||
| "Use 'unreadCount: StreamBackButtonUnreadCount.total()' instead. " | ||
| 'This will be removed in a future version.', | ||
| ) | ||
| this.showUnreadCount = false, | ||
| @Deprecated( | ||
| "Use 'unreadCount: StreamBackButtonUnreadCount.channel(cid)' instead. " | ||
| 'This will be removed in a future version.', | ||
| ) | ||
| this.channelId, | ||
| this.unreadCount, | ||
| }); | ||
|
|
||
| /// Callback for when button is pressed | ||
| final VoidCallback? onPressed; | ||
|
|
||
| /// Show unread count | ||
| @Deprecated( | ||
| "Use 'unreadCount: StreamBackButtonUnreadCount.total()' instead. " | ||
| 'This will be removed in a future version.', | ||
| ) | ||
| final bool showUnreadCount; | ||
|
|
||
| /// Channel ID used to retrieve unread count | ||
| @Deprecated( | ||
| "Use 'unreadCount: StreamBackButtonUnreadCount.channel(cid)' instead. " | ||
| 'This will be removed in a future version.', | ||
| ) | ||
| final String? channelId; | ||
|
|
||
| /// The unread count configuration for the back button. | ||
| final StreamBackButtonUnreadCount? unreadCount; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we instead just ask for a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, perhaps passing a pure
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a small caveat with this approach: If we rework it to pass a StreamUnreadIndicator(
offset: .zero,
excludeCid: excludeCid,
child: button, // <--- StreamBackButton
)This would mean that when building a if (_effectiveUnreadIndicator case final ind?) {
button = Stack(
clipBehavior: Clip.none,
children: [
button,
Positioned.fill(
child: FittedBox(
fit: BoxFit.none,
alignment: AlignmentDirectional.topEnd,
child: ind, // <---- StreamUnreadIndicator WITHOUT child
),
),
],
);
}Should be doable, but in my opinion, it makes the API pretty confusing. I wanted to bring this up, before committing to this solution. |
||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final localizations = MaterialLocalizations.of(context); | ||
|
|
@@ -47,13 +67,56 @@ class StreamBackButton extends StatelessWidget { | |
| }, | ||
| ); | ||
|
|
||
| if (showUnreadCount) { | ||
| button = switch (channelId) { | ||
| final cid? => StreamUnreadIndicator.channels(offset: .zero, cid: cid, child: button), | ||
| _ => StreamUnreadIndicator(offset: .zero, child: button), | ||
| if (_effectiveUnreadCount case final effectiveUnreadCount?) { | ||
| button = switch (effectiveUnreadCount) { | ||
| _TotalUnreadCount(:final excludeCid) => StreamUnreadIndicator( | ||
| offset: .zero, | ||
| excludeCid: excludeCid, | ||
| child: button, | ||
| ), | ||
| _ChannelUnreadCount(:final cid) => StreamUnreadIndicator.channels( | ||
| offset: .zero, | ||
| cid: cid, | ||
| child: button, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| return button; | ||
| } | ||
|
|
||
| StreamBackButtonUnreadCount? get _effectiveUnreadCount { | ||
| if (unreadCount case final effective?) return effective; | ||
| if (!showUnreadCount) return null; | ||
| return switch (channelId) { | ||
| final cid? => StreamBackButtonUnreadCount.channel(cid), | ||
| _ => const StreamBackButtonUnreadCount.total(), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// Configures the unread badge on a [StreamBackButton]. | ||
| sealed class StreamBackButtonUnreadCount { | ||
| const StreamBackButtonUnreadCount(); | ||
|
|
||
| /// Shows the total unread message count across all channels. | ||
| /// | ||
| /// Set [excludeCid] to omit a channel's unread messages from the total - | ||
| /// for example, the currently open channel. | ||
| const factory StreamBackButtonUnreadCount.total({String? excludeCid}) = _TotalUnreadCount; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not completely sure about the naming here, let me know if you think we should use something different ( |
||
|
|
||
| /// Shows the unread message count of the channel identified by [cid]. | ||
| const factory StreamBackButtonUnreadCount.channel(String cid) = _ChannelUnreadCount; | ||
| } | ||
|
|
||
| final class _TotalUnreadCount extends StreamBackButtonUnreadCount { | ||
| const _TotalUnreadCount({this.excludeCid}); | ||
|
|
||
| final String? excludeCid; | ||
| } | ||
|
|
||
| final class _ChannelUnreadCount extends StreamBackButtonUnreadCount { | ||
| const _ChannelUnreadCount(this.cid); | ||
|
|
||
| final String cid; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Show the total unread count excluding the current channel.
Configuring the back button to show the current channel's unread count (
.channel(cid)) results in a suboptimal UX, as the count will typically be 0 while the user is actively viewing the channel.Since navigating back from the
ChannelPagetakes the user to the channel list, it's generally preferable to show the total unread count excluding the current channel. This also accurately demonstrates the new default behavior ofStreamChannelHeaderintroduced in this PR.💡 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents