Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Use stable colors for Photos user avatars

Neeraj Gupta1246298228e2
diff --git a/mobile/apps/photos/lib/ui/sharing/user_avator_widget.dart b/mobile/apps/photos/lib/ui/sharing/user_avator_widget.dart
index 14091c172d..776c12795b 100644
--- a/mobile/apps/photos/lib/ui/sharing/user_avator_widget.dart
+++ b/mobile/apps/photos/lib/ui/sharing/user_avator_widget.dart
@@ -13,22 +13,30 @@ import "package:photos/services/machine_learning/face_ml/person/person_service.d
import "package:photos/services/photos_contacts_service.dart";
import 'package:photos/theme/ente_theme.dart';
import "package:photos/ui/viewer/people/person_face_widget.dart";
+import "package:photos/utils/avatar_util.dart";
import 'package:tuple/tuple.dart';
enum AvatarType { xs, small, medium, regular, large, huge }
Color getUserAvatarColor(BuildContext context, User user) {
- if (user.email == Configuration.instance.getEmail()) {
- return Colors.black;
- }
- final colorScheme = getEnteColorScheme(context);
- final resolvedDisplayName = resolveDisplayName(user);
- final colorIndex = user.email.contains("unknown.com")
- ? resolvedDisplayName.length
- : user.email.length;
- return colorScheme.avatarColors[colorIndex.remainder(
- colorScheme.avatarColors.length,
- )];
+ return avatarBackgroundColor(context, getUserAvatarIdentity(user));
+}
+
+AvatarIdentity getUserAvatarIdentity(User user) {
+ final resolvedEmail = resolveKnownEmail(user);
+ final currentEmail = normalizeAvatarEmail(Configuration.instance.getEmail());
+ final role = user.id != null && user.id! < 0
+ ? AvatarIdentityRole.publicUploader
+ : currentEmail != null &&
+ normalizeAvatarEmail(resolvedEmail) == currentEmail
+ ? AvatarIdentityRole.currentUser
+ : AvatarIdentityRole.standard;
+ return AvatarIdentity(
+ label: resolveDisplayName(user),
+ email: resolvedEmail,
+ userID: user.id,
+ role: role,
+ );
}
class UserAvatarWidget extends StatefulWidget {
@@ -218,13 +226,7 @@ class _FirstLetterCircularAvatarState
extends State<_FirstLetterCircularAvatar> {
@override
Widget build(BuildContext context) {
- final resolvedDisplayName = resolveDisplayName(widget.user);
- final displayChar = resolvedDisplayName.isEmpty
- ? ((widget.user.email.isEmpty)
- ? " "
- : widget.user.email.substring(0, 1))
- : resolvedDisplayName.substring(0, 1);
- final decorationColor = getUserAvatarColor(context, widget.user);
+ final identity = getUserAvatarIdentity(widget.user);
final avatarStyle = getAvatarStyle(context, widget.type);
final double size = avatarStyle.item1;
@@ -233,9 +235,9 @@ class _FirstLetterCircularAvatarState
height: size,
width: size,
child: CircleAvatar(
- backgroundColor: decorationColor,
+ backgroundColor: avatarBackgroundColor(context, identity),
child: Text(
- displayChar.toUpperCase(),
+ identity.initial,
// fixed color
style: textStyle.copyWith(color: Colors.white),
),
@@ -311,16 +313,12 @@ class _FirstLetterUserAvatarState extends State<FirstLetterUserAvatar> {
@override
Widget build(BuildContext context) {
- final resolvedDisplayName = resolveDisplayName(user);
- final displayChar = resolvedDisplayName.isEmpty
- ? ((user.email.isEmpty) ? " " : user.email.substring(0, 1))
- : resolvedDisplayName.substring(0, 1);
- final decorationColor = getUserAvatarColor(context, user);
+ final identity = getUserAvatarIdentity(user);
return Container(
- color: decorationColor,
+ color: avatarBackgroundColor(context, identity),
child: Center(
child: Text(
- displayChar.toUpperCase(),
+ identity.initial,
style: getEnteTextTheme(context).small.copyWith(color: Colors.white),
),
),
diff --git a/mobile/apps/photos/lib/utils/avatar_util.dart b/mobile/apps/photos/lib/utils/avatar_util.dart
new file mode 100644
index 0000000000..06046849fd
--- /dev/null
+++ b/mobile/apps/photos/lib/utils/avatar_util.dart
@@ -0,0 +1,71 @@
+import "package:ente_components/ente_components.dart";
+import "package:flutter/material.dart";
+
+enum AvatarIdentityRole { standard, currentUser, publicUploader }
+
+class AvatarIdentity {
+ AvatarIdentity({
+ required this.label,
+ String? email,
+ int? userID,
+ String? personID,
+ this.role = AvatarIdentityRole.standard,
+ }) : key = avatarIdentityKey(
+ email: email,
+ userID: userID,
+ personID: personID,
+ name: label,
+ );
+
+ final String label;
+ final String key;
+ final AvatarIdentityRole role;
+
+ String get initial {
+ final trimmed = label.trim();
+ return trimmed.isEmpty ? " " : trimmed.characters.first.toUpperCase();
+ }
+}
+
+String avatarIdentityKey({
+ String? email,
+ int? userID,
+ String? personID,
+ String? name,
+}) {
+ final normalizedEmail = normalizeAvatarEmail(email);
+ if (normalizedEmail != null) {
+ return "email:$normalizedEmail";
+ }
+ if (userID != null) {
+ return "user:$userID";
+ }
+ final normalizedPersonID = personID?.trim();
+ if (normalizedPersonID != null && normalizedPersonID.isNotEmpty) {
+ return "person:$normalizedPersonID";
+ }
+ final normalizedName = name?.trim().toLowerCase().replaceAll(
+ RegExp(r"\s+"),
+ " ",
+ );
+ if (normalizedName != null && normalizedName.isNotEmpty) {
+ return "name:$normalizedName";
+ }
+ return "unknown";
+}
+
+String? normalizeAvatarEmail(String? email) {
+ final normalized = email?.trim().toLowerCase();
+ if (normalized == null ||
+ normalized.isEmpty ||
+ normalized.endsWith("@unknown.com")) {
+ return null;
+ }
+ return normalized;
+}
+
+Color avatarBackgroundColor(BuildContext context, AvatarIdentity identity) {
+ return identity.role == AvatarIdentityRole.standard
+ ? avatarColorForIdentity(context, identity.key)
+ : Colors.black;
+}
diff --git a/mobile/apps/photos/test/utils/avatar_util_test.dart b/mobile/apps/photos/test/utils/avatar_util_test.dart
new file mode 100644
index 0000000000..5dba40debf
--- /dev/null
+++ b/mobile/apps/photos/test/utils/avatar_util_test.dart
@@ -0,0 +1,44 @@
+import "package:flutter_test/flutter_test.dart";
+import "package:photos/utils/avatar_util.dart";
+
+void main() {
+ test("prefers normalized email for avatar identity", () {
+ expect(
+ avatarIdentityKey(
+ email: " [email protected] ",
+ userID: 7,
+ personID: "person-1",
+ name: "Alice",
+ ),
+ );
+ });
+
+ test("falls back through stable IDs before name", () {
+ expect(
+ avatarIdentityKey(
+ email: "[email protected]",
+ userID: 7,
+ personID: "person-1",
+ name: "Alice",
+ ),
+ "user:7",
+ );
+ expect(
+ avatarIdentityKey(personID: "person-1", name: "Alice"),
+ "person:person-1",
+ );
+ expect(avatarIdentityKey(name: " Alice Smith "), "name:alice smith");
+ });
+
+ test("display name changes do not change email identity", () {
+ final before = AvatarIdentity(label: "Alice", email: "[email protected]");
+ final after = AvatarIdentity(
+ label: "Alice Smith",
+ email: "[email protected]",
+ );
+
+ expect(after.key, before.key);
+ expect(after.initial, "A");
+ });
+}