1
+ import 'package:flutter/foundation.dart' ;
2
+ import 'package:flutter/painting.dart' ;
1
3
import 'package:json_annotation/json_annotation.dart' ;
2
4
3
5
import 'reaction.dart' ;
@@ -328,14 +330,29 @@ class Subscription extends ZulipStream {
328
330
/// As an int that dart:ui's Color constructor will take:
329
331
/// <https://api.flutter.dev/flutter/dart-ui/Color/Color.html>
330
332
@JsonKey (readValue: _readColor)
331
- int color;
332
-
333
+ int get color => _color;
334
+ int _color;
335
+ set color (int value) {
336
+ _color = value;
337
+ _swatch = null ;
338
+ }
333
339
static Object ? _readColor (Map json, String key) {
334
340
final str = (json[key] as String );
335
341
assert (RegExp (r'^#[0-9a-f]{6}$' ).hasMatch (str));
336
342
return 0xff000000 | int .parse (str.substring (1 ), radix: 16 );
337
343
}
338
344
345
+ StreamColorSwatch ? _swatch;
346
+ /// A [StreamColorSwatch] for the subscription, memoized.
347
+ // TODO I'm not sure this is the right home for this; it seems like we might
348
+ // instead have chosen to put it in more UI-centered code, like in a custom
349
+ // material [ColorScheme] class or something. But it works for now.
350
+ StreamColorSwatch colorSwatch () => _swatch ?? = StreamColorSwatch (color);
351
+
352
+ @visibleForTesting
353
+ @JsonKey (includeToJson: false )
354
+ StreamColorSwatch ? get debugCachedSwatchValue => _swatch;
355
+
339
356
Subscription ({
340
357
required super .streamId,
341
358
required super .name,
@@ -357,8 +374,8 @@ class Subscription extends ZulipStream {
357
374
required this .audibleNotifications,
358
375
required this .pinToTop,
359
376
required this .isMuted,
360
- required this . color,
361
- });
377
+ required int color,
378
+ }) : _color = color ;
362
379
363
380
factory Subscription .fromJson (Map <String , dynamic > json) =>
364
381
_$SubscriptionFromJson (json);
@@ -367,6 +384,29 @@ class Subscription extends ZulipStream {
367
384
Map <String , dynamic > toJson () => _$SubscriptionToJson (this );
368
385
}
369
386
387
+ /// A [ColorSwatch] with colors related to a base stream color.
388
+ ///
389
+ /// Use this in UI code for colors related to [Subscription.color] ,
390
+ /// such as the background of an unread count badge.
391
+ class StreamColorSwatch extends ColorSwatch <_StreamColorVariant > {
392
+ StreamColorSwatch (int base ) : super (base , _compute (base ));
393
+
394
+ Color get base => this [_StreamColorVariant .base ]! ;
395
+
396
+ static Map <_StreamColorVariant , Color > _compute (int base ) {
397
+ final baseAsColor = Color (base );
398
+
399
+ return {
400
+ _StreamColorVariant .base : baseAsColor,
401
+ };
402
+ }
403
+ }
404
+
405
+ enum _StreamColorVariant {
406
+ base ,
407
+ // TODO more, like the unread-count badge background color
408
+ }
409
+
370
410
/// As in the get-messages response.
371
411
///
372
412
/// https://zulip.com/api/get-messages#response
0 commit comments