π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add stable avatar identity colors
Neeraj Guptaf3a7ccfff48f
diff --git a/mobile/packages/ente_components/lib/components/avatar_component.dart b/mobile/packages/ente_components/lib/components/avatar_component.dartindex 0b328456dc..ab0c70b0ff 100644--- a/mobile/packages/ente_components/lib/components/avatar_component.dart+++ b/mobile/packages/ente_components/lib/components/avatar_component.dart@@ -1,3 +1,5 @@+import 'dart:convert';+import 'package:ente_components/theme/colors.dart';import 'package:ente_components/theme/spacing.dart';import 'package:ente_components/theme/text_styles.dart';@@ -104,7 +106,33 @@ enum AvatarComponentSize {final TextStyle textStyle;}-enum AvatarComponentColor { yellow, green, orange, pink, purple, blue, cyan }+enum AvatarComponentColor {+ yellow,+ green,+ orange,+ pink,+ purple,+ blue,+ cyan,+ black,+}++/// A stable FNV-1a seed for identity colors across processes and platforms.+int avatarSeedForIdentity(String identityKey) {+ var hash = 0x811c9dc5;+ for (final byte in utf8.encode(identityKey.trim().toLowerCase())) {+ hash ^= byte;+ hash = (hash * 0x01000193) & 0xffffffff;+ }+ return hash;+}++Color avatarColorForIdentity(BuildContext context, String identityKey) {+ final palette = Theme.of(context).brightness == Brightness.dark+ ? avatarDark+ : avatarLight;+ return palette[avatarSeedForIdentity(identityKey) % palette.length];+}/// Figma: https://www.figma.com/design/BuBNPPytxlVnqfmCUW0mgz/Ente-Visual-Design?node-id=2482-6547&m=dev/// Section: Labels and avatars / Avatar@@ -118,6 +146,18 @@ class AvatarComponent extends StatelessWidget {this.seed,this.semanticLabel,}) : image = null,+ identityKey = null,+ icon = null;++ const AvatarComponent.identity({+ super.key,+ required this.initials,+ required this.identityKey,+ this.size = AvatarComponentSize.defaultSize,+ this.semanticLabel,+ }) : image = null,+ color = AvatarComponentColor.yellow,+ seed = null,icon = null;const AvatarComponent.image({@@ -127,6 +167,7 @@ class AvatarComponent extends StatelessWidget {this.semanticLabel,}) : initials = '',color = AvatarComponentColor.yellow,+ identityKey = null,seed = null,icon = null;@@ -138,6 +179,7 @@ class AvatarComponent extends StatelessWidget {}) : initials = '',image = null,color = AvatarComponentColor.yellow,+ identityKey = null,seed = null;const AvatarComponent.seeded({@@ -148,11 +190,13 @@ class AvatarComponent extends StatelessWidget {this.semanticLabel,}) : image = null,color = AvatarComponentColor.yellow,+ identityKey = null,icon = null;final String initials;final AvatarComponentSize size;final AvatarComponentColor color;+ final String? identityKey;final int? seed;final ImageProvider? image;final Widget? icon;@@ -241,6 +285,9 @@ class AvatarComponent extends StatelessWidget {Color _backgroundColor(BuildContext context) {final colors = context.componentColors;+ if (identityKey != null) {+ return avatarColorForIdentity(context, identityKey!);+ }if (seed != null) {final palette = Theme.of(context).brightness == Brightness.dark? avatarDark@@ -256,6 +303,7 @@ class AvatarComponent extends StatelessWidget {AvatarComponentColor.purple => colors.purple,AvatarComponentColor.blue => colors.blue,AvatarComponentColor.cyan => avatarCyan,+ AvatarComponentColor.black => Colors.black,};}diff --git a/mobile/packages/ente_components/test/components/avatar_test.dart b/mobile/packages/ente_components/test/components/avatar_test.dartindex e7ee054815..7a429bbe74 100644--- a/mobile/packages/ente_components/test/components/avatar_test.dart+++ b/mobile/packages/ente_components/test/components/avatar_test.dart@@ -6,6 +6,14 @@ import 'package:flutter/material.dart';import 'package:flutter_test/flutter_test.dart';void main() {+ test('avatar identity seeds are stable and normalized', () {+ expect(+ avatarSeedForIdentity(' [email protected] '),+ avatarSeedForIdentity('[email protected]'),+ );+ expect(avatarSeedForIdentity('[email protected]'), 2493822278);+ });+testWidgets('AvatarComponent renders the Figma sizes', (tester) async {await tester.pumpWidget(_wrap(@@ -55,6 +63,10 @@ void main() {color: AvatarComponentColor.cyan,),const AvatarComponent.seeded(initials: 'A', seed: 0),+ const AvatarComponent(+ initials: 'P',+ color: AvatarComponentColor.black,+ ),],),),@@ -64,6 +76,7 @@ void main() {expect(find.text('K'), findsOneWidget);expect(find.text('C'), findsOneWidget);expect(find.text('A'), findsOneWidget);+ expect(find.text('P'), findsOneWidget);expect(avatarLight.length, avatarDark.length);final cyanAvatar = tester.widget<DecoratedBox>(@@ -74,11 +87,43 @@ void main() {expect((cyanAvatar.decoration as BoxDecoration).color, avatarCyan);},);++ testWidgets('identity avatars use the theme palette at a stable index', (+ tester,+ ) async {+ const identityKey = '[email protected]';+ final paletteIndex =+ avatarSeedForIdentity(identityKey) % avatarLight.length;++ await tester.pumpWidget(+ _wrap(+ const AvatarComponent.identity(initials: 'A', identityKey: identityKey),+ ),+ );+ expect(_avatarBackground(tester), avatarLight[paletteIndex]);++ await tester.pumpWidget(+ _wrap(+ const AvatarComponent.identity(initials: 'A', identityKey: identityKey),+ theme: ComponentTheme.darkTheme(),+ ),+ );+ expect(_avatarBackground(tester), avatarDark[paletteIndex]);+ });+}++Color? _avatarBackground(WidgetTester tester) {+ final avatar = tester.widget<DecoratedBox>(+ find+ .ancestor(of: find.text('A'), matching: find.byType(DecoratedBox))+ .first,+ );+ return (avatar.decoration as BoxDecoration).color;}-Widget _wrap(Widget child) {+Widget _wrap(Widget child, {ThemeData? theme}) {return MaterialApp(- theme: ComponentTheme.lightTheme(),+ theme: theme ?? ComponentTheme.lightTheme(),home: Scaffold(body: Center(child: child)),);}