File tree Expand file tree Collapse file tree 5 files changed +28
-12
lines changed Expand file tree Collapse file tree 5 files changed +28
-12
lines changed Original file line number Diff line number Diff line change @@ -319,7 +319,16 @@ class Subscription {
319
319
final bool isMuted;
320
320
// final bool? inHomeView; // deprecated; ignore
321
321
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
+ }
323
332
324
333
Subscription ({
325
334
required this .streamId,
Original file line number Diff line number Diff line change @@ -485,13 +485,6 @@ class _UnreadMarker extends StatelessWidget {
485
485
}
486
486
}
487
487
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
-
495
488
class StreamTopicRecipientHeader extends StatelessWidget {
496
489
const StreamTopicRecipientHeader ({super .key, required this .message});
497
490
@@ -506,7 +499,7 @@ class StreamTopicRecipientHeader extends StatelessWidget {
506
499
final topic = message.subject;
507
500
508
501
final subscription = store.subscriptions[message.streamId];
509
- final streamColor = colorForStream (subscription);
502
+ final streamColor = Color (subscription? .color ?? 0x00c2c2c2 );
510
503
final contrastingColor =
511
504
ThemeData .estimateBrightnessForColor (streamColor) == Brightness .dark
512
505
? Colors .white
Original file line number Diff line number Diff line change @@ -65,6 +65,20 @@ void main() {
65
65
});
66
66
});
67
67
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
+
68
82
group ('Message' , () {
69
83
test ('no crash on unrecognized flag' , () {
70
84
final m1 = Message .fromJson (
Original file line number Diff line number Diff line change @@ -134,7 +134,7 @@ Subscription subscription({
134
134
String ? emailAddress,
135
135
bool ? isMuted,
136
136
bool ? isWebPublic,
137
- String ? color,
137
+ int ? color,
138
138
int ? streamPostPolicy,
139
139
int ? messageRetentionDays,
140
140
bool ? historyPublicToSubscribers,
@@ -158,7 +158,7 @@ Subscription subscription({
158
158
emailAddress
: emailAddress
?? '[email protected] ' ,
// TODO generate example data
159
159
isMuted: isMuted ?? true ,
160
160
isWebPublic: isWebPublic ?? true ,
161
- color: color ?? '#e79ab5' ,
161
+ color: color ?? 0xffe79ab5 ,
162
162
streamPostPolicy: streamPostPolicy ?? 1 ,
163
163
messageRetentionDays: messageRetentionDays ?? 365 ,
164
164
historyPublicToSubscribers: historyPublicToSubscribers ?? true ,
You can’t perform that action at this time.
0 commit comments