Skip to content

Commit 6cf5706

Browse files
committed
api: Bring stream-color int parsing out to the "crunchy shell"
1 parent 59c53ca commit 6cf5706

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

lib/api/model/model.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,16 @@ class Subscription {
319319
final bool isMuted;
320320
// final bool? inHomeView; // deprecated; ignore
321321

322-
final String color;
322+
/// As an int that dart:ui's Color constructor will take:
323+
/// <https://api.flutter.dev/flutter/dart-ui/Color/Color.html>
324+
@JsonKey(readValue: _readColor)
325+
final int color;
326+
327+
static Object? _readColor(Map json, String key) {
328+
final str = (json[key] as String);
329+
assert(RegExp(r'^#[0-9a-f]{6}$').hasMatch(str));
330+
return 0xff000000 | int.parse(str.substring(1), radix: 16);
331+
}
323332

324333
Subscription({
325334
required this.streamId,

lib/api/model/model.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/widgets/message_list.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,13 +485,6 @@ class _UnreadMarker extends StatelessWidget {
485485
}
486486
}
487487

488-
Color colorForStream(Subscription? subscription) {
489-
final color = subscription?.color;
490-
if (color == null) return const Color(0x00c2c2c2);
491-
assert(RegExp(r'^#[0-9a-f]{6}$').hasMatch(color));
492-
return Color(0xff000000 | int.parse(color.substring(1), radix: 16));
493-
}
494-
495488
class StreamTopicRecipientHeader extends StatelessWidget {
496489
const StreamTopicRecipientHeader({super.key, required this.message});
497490

@@ -506,7 +499,7 @@ class StreamTopicRecipientHeader extends StatelessWidget {
506499
final topic = message.subject;
507500

508501
final subscription = store.subscriptions[message.streamId];
509-
final streamColor = colorForStream(subscription);
502+
final streamColor = Color(subscription?.color ?? 0x00c2c2c2);
510503
final contrastingColor =
511504
ThemeData.estimateBrightnessForColor(streamColor) == Brightness.dark
512505
? Colors.white

test/api/model/model_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ void main() {
6565
});
6666
});
6767

68+
group('Subscription', () {
69+
test('converts color to int', () {
70+
Subscription subWithColor(String color) {
71+
return Subscription.fromJson(
72+
deepToJson(eg.subscription(stream: eg.stream())) as Map<String, dynamic>
73+
..['color'] = color,
74+
);
75+
}
76+
check(subWithColor('#e79ab5').color).equals(0xffe79ab5);
77+
check(subWithColor('#ffffff').color).equals(0xffffffff);
78+
check(subWithColor('#000000').color).equals(0xff000000);
79+
});
80+
});
81+
6882
group('Message', () {
6983
test('no crash on unrecognized flag', () {
7084
final m1 = Message.fromJson(

test/example_data.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Subscription subscription({
134134
String? emailAddress,
135135
bool? isMuted,
136136
bool? isWebPublic,
137-
String? color,
137+
int? color,
138138
int? streamPostPolicy,
139139
int? messageRetentionDays,
140140
bool? historyPublicToSubscribers,
@@ -158,7 +158,7 @@ Subscription subscription({
158158
emailAddress: emailAddress ?? '[email protected]', // TODO generate example data
159159
isMuted: isMuted ?? true,
160160
isWebPublic: isWebPublic ?? true,
161-
color: color ?? '#e79ab5',
161+
color: color ?? 0xffe79ab5,
162162
streamPostPolicy: streamPostPolicy ?? 1,
163163
messageRetentionDays: messageRetentionDays ?? 365,
164164
historyPublicToSubscribers: historyPublicToSubscribers ?? true,

0 commit comments

Comments
 (0)