Skip to content

Commit

Permalink
model [nfc]: Shorten StreamColorVariant to StreamColor
Browse files Browse the repository at this point in the history
The old name seems longer than it needs to be.
  • Loading branch information
chrisbobbe committed May 3, 2024
1 parent 39f69e6 commit 3b658bd
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
22 changes: 11 additions & 11 deletions lib/api/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,17 @@ class Subscription extends ZulipStream {
return 0xff000000 | int.parse(str.substring(1), radix: 16);
}

ColorSwatch<StreamColorVariant>? _swatch;
ColorSwatch<StreamColor>? _swatch;
/// A [ColorSwatch<StreamColorVariant>] for the subscription, memoized.
// TODO I'm not sure this is the right home for this; it seems like we might
// instead have chosen to put it in more UI-centered code, like in a custom
// material [ColorScheme] class or something. But it works for now.
ColorSwatch<StreamColorVariant> colorSwatch() =>
ColorSwatch<StreamColor> colorSwatch() =>
_swatch ??= streamColorSwatch(color);

@visibleForTesting
@JsonKey(includeToJson: false)
ColorSwatch<StreamColorVariant>? get debugCachedSwatchValue => _swatch;
ColorSwatch<StreamColor>? get debugCachedSwatchValue => _swatch;

Subscription({
required super.streamId,
Expand Down Expand Up @@ -463,36 +463,36 @@ class Subscription extends ZulipStream {
///
/// Use this in UI code for colors related to [Subscription.color],
/// such as the background of an unread count badge.
ColorSwatch<StreamColorVariant> streamColorSwatch(int base) {
ColorSwatch<StreamColor> streamColorSwatch(int base) {
final baseAsColor = Color(base);

final clamped20to75 = clampLchLightness(baseAsColor, 20, 75);
final clamped20to75AsHsl = HSLColor.fromColor(clamped20to75);

final map = {
StreamColorVariant.base: baseAsColor,
StreamColor.base: baseAsColor,

// Follows `.unread-count` in Vlad's replit:
// <https://replit.com/@VladKorobov/zulip-sidebar#script.js>
// <https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/design.3A.20.23F117.20.22Inbox.22.20screen/near/1624484>
//
// TODO fix bug where our results differ from the replit's (see unit tests)
StreamColorVariant.unreadCountBadgeBackground:
StreamColor.unreadCountBadgeBackground:
clampLchLightness(baseAsColor, 30, 70)
.withOpacity(0.3),

// Follows `.sidebar-row__icon` in Vlad's replit:
// <https://replit.com/@VladKorobov/zulip-topic-feed-colors#script.js>
//
// TODO fix bug where our results differ from the replit's (see unit tests)
StreamColorVariant.iconOnPlainBackground: clamped20to75,
StreamColor.iconOnPlainBackground: clamped20to75,

// Follows `.recepeient__icon` in Vlad's replit:
// <https://replit.com/@VladKorobov/zulip-topic-feed-colors#script.js>
// <https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/design.3A.20.23F117.20.22Inbox.22.20screen/near/1624484>
//
// TODO fix bug where our results differ from the replit's (see unit tests)
StreamColorVariant.iconOnBarBackground:
StreamColor.iconOnBarBackground:
clamped20to75AsHsl
.withLightness(clamped20to75AsHsl.lightness - 0.12)
.toColor(),
Expand All @@ -505,16 +505,16 @@ ColorSwatch<StreamColorVariant> streamColorSwatch(int base) {
// <https://pub.dev/documentation/flutter_color_models/latest/flutter_color_models/ColorModel/interpolate.html>
// which does ordinary RGB mixing. Investigate and send a PR?
// TODO fix bug where our results differ from the replit's (see unit tests)
StreamColorVariant.barBackground:
StreamColor.barBackground:
LabColor.fromColor(const Color(0xfff9f9f9))
.interpolate(LabColor.fromColor(clamped20to75), 0.22)
.toColor(),
};

return ColorSwatch<StreamColorVariant>(base, map);
return ColorSwatch<StreamColor>(base, map);
}

enum StreamColorVariant {
enum StreamColor {
/// The [Subscription.color] int that the swatch is based on.
base,

Expand Down
8 changes: 4 additions & 4 deletions lib/widgets/inbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,13 @@ class _StreamHeaderItem extends _HeaderItem {
@override get title => subscription.name;
@override get icon => iconDataForStream(subscription);
@override get collapsedIconColor =>
subscription.colorSwatch()[StreamColorVariant.iconOnPlainBackground]!;
subscription.colorSwatch()[StreamColor.iconOnPlainBackground]!;
@override get uncollapsedIconColor =>
subscription.colorSwatch()[StreamColorVariant.iconOnBarBackground]!;
subscription.colorSwatch()[StreamColor.iconOnBarBackground]!;
@override get uncollapsedBackgroundColor =>
subscription.colorSwatch()[StreamColorVariant.barBackground]!;
subscription.colorSwatch()[StreamColor.barBackground]!;
@override get unreadCountBadgeBackgroundColor =>
subscription.colorSwatch()[StreamColorVariant.unreadCountBadgeBackground]!;
subscription.colorSwatch()[StreamColor.unreadCountBadgeBackground]!;

@override get onCollapseButtonTap => () async {
await super.onCollapseButtonTap();
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class _MessageListPageState extends State<MessageListPage> {
case StreamNarrow(:final streamId):
case TopicNarrow(:final streamId):
backgroundColor =
store.subscriptions[streamId]?.colorSwatch()[StreamColorVariant.barBackground]!
store.subscriptions[streamId]?.colorSwatch()[StreamColor.barBackground]!
?? _kUnsubscribedStreamRecipientHeaderColor;
// All recipient headers will match this color; remove distracting line
// (but are recipient headers even needed for topic narrows?)
Expand Down Expand Up @@ -656,12 +656,12 @@ class StreamMessageRecipientHeader extends StatelessWidget {
final Color iconColor;
if (subscription != null) {
final swatch = subscription.colorSwatch();
backgroundColor = swatch[StreamColorVariant.barBackground]!;
backgroundColor = swatch[StreamColor.barBackground]!;
contrastingColor =
(ThemeData.estimateBrightnessForColor(backgroundColor) == Brightness.dark)
? Colors.white
: Colors.black;
iconColor = swatch[StreamColorVariant.iconOnBarBackground]!;
iconColor = swatch[StreamColor.iconOnBarBackground]!;
} else {
backgroundColor = _kUnsubscribedStreamRecipientHeaderColor;
contrastingColor = Colors.black;
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/subscription_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class SubscriptionItem extends StatelessWidget {
const SizedBox(width: 16),
Padding(
padding: const EdgeInsets.symmetric(vertical: 11),
child: Icon(size: 18, color: swatch[StreamColorVariant.iconOnPlainBackground]!,
child: Icon(size: 18, color: swatch[StreamColor.iconOnPlainBackground]!,
iconDataForStream(subscription))),
const SizedBox(width: 5),
Expanded(
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/unread_count_badge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class UnreadCountBadge extends StatelessWidget {
Widget build(BuildContext context) {
final backgroundColor = this.backgroundColor;
final effectiveBackgroundColor = switch (backgroundColor) {
ColorSwatch<StreamColorVariant>() =>
backgroundColor[StreamColorVariant.unreadCountBadgeBackground]!,
ColorSwatch<StreamColor>() =>
backgroundColor[StreamColor.unreadCountBadgeBackground]!,
Color() => backgroundColor,
null => const Color.fromRGBO(102, 102, 153, 0.15),
};
Expand Down
14 changes: 7 additions & 7 deletions test/api/model/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,24 @@ void main() {
check(sub.debugCachedSwatchValue).isNull();
sub.colorSwatch();
check(sub.debugCachedSwatchValue).isNotNull()
[StreamColorVariant.base].equals(const Color(0xffffffff));
[StreamColor.base].equals(const Color(0xffffffff));
sub.color = 0xffff0000;
check(sub.debugCachedSwatchValue).isNull();
sub.colorSwatch();
check(sub.debugCachedSwatchValue).isNotNull()
[StreamColorVariant.base].equals(const Color(0xffff0000));
[StreamColor.base].equals(const Color(0xffff0000));
});

group('streamColorSwatch', () {
test('base', () {
check(streamColorSwatch(0xffffffff))[StreamColorVariant.base]
check(streamColorSwatch(0xffffffff))[StreamColor.base]
.equals(const Color(0xffffffff));
});

test('unreadCountBadgeBackground', () {
void runCheck(int base, Color expected) {
check(streamColorSwatch(base))
[StreamColorVariant.unreadCountBadgeBackground].equals(expected);
[StreamColor.unreadCountBadgeBackground].equals(expected);
}

// Check against everything in ZULIP_ASSIGNMENT_COLORS and EXTREME_COLORS
Expand Down Expand Up @@ -202,7 +202,7 @@ void main() {
test('iconOnPlainBackground', () {
void runCheck(int base, Color expected) {
check(streamColorSwatch(base))
[StreamColorVariant.iconOnPlainBackground].equals(expected);
[StreamColor.iconOnPlainBackground].equals(expected);
}

// Check against everything in ZULIP_ASSIGNMENT_COLORS
Expand Down Expand Up @@ -244,7 +244,7 @@ void main() {
test('iconOnBarBackground', () {
void runCheck(int base, Color expected) {
check(streamColorSwatch(base))
[StreamColorVariant.iconOnBarBackground].equals(expected);
[StreamColor.iconOnBarBackground].equals(expected);
}

// Check against everything in ZULIP_ASSIGNMENT_COLORS
Expand Down Expand Up @@ -286,7 +286,7 @@ void main() {
test('barBackground', () {
void runCheck(int base, Color expected) {
check(streamColorSwatch(base))
[StreamColorVariant.barBackground].equals(expected);
[StreamColor.barBackground].equals(expected);
}

// Check against everything in ZULIP_ASSIGNMENT_COLORS
Expand Down
4 changes: 2 additions & 2 deletions test/widgets/inbox_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void main() {
check(collapseIcon).icon.equals(ZulipIcons.arrow_down);
final streamIcon = findStreamHeaderIcon(tester, streamId);
check(streamIcon).color
.equals(subscription.colorSwatch()[StreamColorVariant.iconOnBarBackground]!);
.equals(subscription.colorSwatch()[StreamColor.iconOnBarBackground]!);
// TODO check bar background color
check(tester.widgetList(findSectionContent)).isNotEmpty();
}
Expand All @@ -434,7 +434,7 @@ void main() {
check(collapseIcon).icon.equals(ZulipIcons.arrow_right);
final streamIcon = findStreamHeaderIcon(tester, streamId);
check(streamIcon).color
.equals(subscription.colorSwatch()[StreamColorVariant.iconOnPlainBackground]!);
.equals(subscription.colorSwatch()[StreamColor.iconOnPlainBackground]!);
// TODO check bar background color
check(tester.widgetList(findSectionContent)).isEmpty();
}
Expand Down
4 changes: 2 additions & 2 deletions test/widgets/message_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void main() {
find.descendant(
of: find.byType(StreamMessageRecipientHeader),
matching: find.byType(ColoredBox),
))).color.equals(swatch[StreamColorVariant.barBackground]!);
))).color.equals(swatch[StreamColor.barBackground]!);
});

testWidgets('color of stream icon', (tester) async {
Expand All @@ -294,7 +294,7 @@ void main() {
subscriptions: [subscription]);
await tester.pump();
check(tester.widget<Icon>(find.byIcon(ZulipIcons.globe)))
.color.equals(swatch[StreamColorVariant.iconOnBarBackground]!);
.color.equals(swatch[StreamColor.iconOnBarBackground]!);
});

testWidgets('normal streams show hash icon', (tester) async {
Expand Down
2 changes: 1 addition & 1 deletion test/widgets/subscription_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void main() {
], unreadMsgs: unreadMsgs);
check(getItemCount()).equals(1);
check(tester.widget<Icon>(find.byType(Icon)).color)
.equals(swatch[StreamColorVariant.iconOnPlainBackground]!);
.equals(swatch[StreamColor.iconOnPlainBackground]!);
check(tester.widget<UnreadCountBadge>(find.byType(UnreadCountBadge)).backgroundColor)
.equals(swatch);
});
Expand Down
2 changes: 1 addition & 1 deletion test/widgets/unread_count_badge_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void main() {
testWidgets('stream color', (WidgetTester tester) async {
final swatch = streamColorSwatch(0xff76ce90);
await prepare(tester, swatch);
check(findBackgroundColor(tester)).equals(swatch[StreamColorVariant.unreadCountBadgeBackground]!);
check(findBackgroundColor(tester)).equals(swatch[StreamColor.unreadCountBadgeBackground]!);
});
});
});
Expand Down

0 comments on commit 3b658bd

Please sign in to comment.