|
| 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';
|
@@ -321,14 +323,25 @@ class Subscription {
|
321 | 323 | /// As an int that dart:ui's Color constructor will take:
|
322 | 324 | /// <https://api.flutter.dev/flutter/dart-ui/Color/Color.html>
|
323 | 325 | @JsonKey(readValue: _readColor)
|
324 |
| - final int color; |
325 |
| - |
| 326 | + final int color; // TODO(#135) Clear out _swatch cache on changes to this |
326 | 327 | static Object? _readColor(Map json, String key) {
|
327 | 328 | final str = (json[key] as String);
|
328 | 329 | assert(RegExp(r'^#[0-9a-f]{6}$').hasMatch(str));
|
329 | 330 | return 0xff000000 | int.parse(str.substring(1), radix: 16);
|
330 | 331 | }
|
331 | 332 |
|
| 333 | + @JsonKey(includeToJson: false) |
| 334 | + StreamColorSwatch? _swatch; |
| 335 | + /// A [StreamColorSwatch] for the subscription, memoized. |
| 336 | + // TODO I'm not sure this is the right home for this; it seems like we might |
| 337 | + // instead have chosen to put it in more UI-centered code, like in a custom |
| 338 | + // material [ColorScheme] class or something. But it works for now. |
| 339 | + StreamColorSwatch colorSwatch() => _swatch ??= StreamColorSwatch(color); |
| 340 | + |
| 341 | + @visibleForTesting |
| 342 | + @JsonKey(includeToJson: false) |
| 343 | + StreamColorSwatch? get debugCachedSwatchValue => _swatch; |
| 344 | + |
332 | 345 | Subscription({
|
333 | 346 | required this.streamId,
|
334 | 347 | required this.name,
|
@@ -359,6 +372,29 @@ class Subscription {
|
359 | 372 | Map<String, dynamic> toJson() => _$SubscriptionToJson(this);
|
360 | 373 | }
|
361 | 374 |
|
| 375 | +/// A [ColorSwatch] with colors related to a base stream color. |
| 376 | +/// |
| 377 | +/// Use this in UI code for colors related to [Subscription.color], |
| 378 | +/// such as the background of an unread count badge. |
| 379 | +class StreamColorSwatch extends ColorSwatch<_StreamColorVariant> { |
| 380 | + StreamColorSwatch(int base) : super(base, _compute(base)); |
| 381 | + |
| 382 | + Color get base => this[_StreamColorVariant.base]!; |
| 383 | + |
| 384 | + static Map<_StreamColorVariant, Color> _compute(int base) { |
| 385 | + final baseAsColor = Color(base); |
| 386 | + |
| 387 | + return { |
| 388 | + _StreamColorVariant.base: baseAsColor, |
| 389 | + }; |
| 390 | + } |
| 391 | +} |
| 392 | + |
| 393 | +enum _StreamColorVariant { |
| 394 | + base, |
| 395 | + // TODO more, like the unread-count badge background color |
| 396 | +} |
| 397 | + |
362 | 398 | /// As in the get-messages response.
|
363 | 399 | ///
|
364 | 400 | /// https://zulip.com/api/get-messages#response
|
|
0 commit comments