user.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import 'dart:ui';
  2. import 'package:immich_mobile/shared/models/album.dart';
  3. import 'package:immich_mobile/utils/hash.dart';
  4. import 'package:isar/isar.dart';
  5. import 'package:openapi/api.dart';
  6. part 'user.g.dart';
  7. @Collection(inheritance: false)
  8. class User {
  9. User({
  10. required this.id,
  11. required this.updatedAt,
  12. required this.email,
  13. required this.name,
  14. required this.isAdmin,
  15. this.isPartnerSharedBy = false,
  16. this.isPartnerSharedWith = false,
  17. this.profileImagePath = '',
  18. this.avatarColor = AvatarColorEnum.primary,
  19. this.memoryEnabled = true,
  20. this.inTimeline = false,
  21. });
  22. Id get isarId => fastHash(id);
  23. User.fromUserDto(UserResponseDto dto)
  24. : id = dto.id,
  25. updatedAt = dto.updatedAt,
  26. email = dto.email,
  27. name = dto.name,
  28. isPartnerSharedBy = false,
  29. isPartnerSharedWith = false,
  30. profileImagePath = dto.profileImagePath,
  31. isAdmin = dto.isAdmin,
  32. memoryEnabled = dto.memoriesEnabled ?? false,
  33. avatarColor = dto.avatarColor.toAvatarColor(),
  34. inTimeline = false;
  35. User.fromPartnerDto(PartnerResponseDto dto)
  36. : id = dto.id,
  37. updatedAt = dto.updatedAt,
  38. email = dto.email,
  39. name = dto.name,
  40. isPartnerSharedBy = false,
  41. isPartnerSharedWith = false,
  42. profileImagePath = dto.profileImagePath,
  43. isAdmin = dto.isAdmin,
  44. memoryEnabled = dto.memoriesEnabled ?? false,
  45. avatarColor = dto.avatarColor.toAvatarColor(),
  46. inTimeline = dto.inTimeline ?? false;
  47. @Index(unique: true, replace: false, type: IndexType.hash)
  48. String id;
  49. DateTime updatedAt;
  50. String email;
  51. String name;
  52. bool isPartnerSharedBy;
  53. bool isPartnerSharedWith;
  54. bool isAdmin;
  55. String profileImagePath;
  56. @Enumerated(EnumType.ordinal)
  57. AvatarColorEnum avatarColor;
  58. bool memoryEnabled;
  59. bool inTimeline;
  60. @Backlink(to: 'owner')
  61. final IsarLinks<Album> albums = IsarLinks<Album>();
  62. @Backlink(to: 'sharedUsers')
  63. final IsarLinks<Album> sharedAlbums = IsarLinks<Album>();
  64. @override
  65. bool operator ==(other) {
  66. if (other is! User) return false;
  67. return id == other.id &&
  68. updatedAt.isAtSameMomentAs(other.updatedAt) &&
  69. avatarColor == other.avatarColor &&
  70. email == other.email &&
  71. name == other.name &&
  72. isPartnerSharedBy == other.isPartnerSharedBy &&
  73. isPartnerSharedWith == other.isPartnerSharedWith &&
  74. profileImagePath == other.profileImagePath &&
  75. isAdmin == other.isAdmin &&
  76. memoryEnabled == other.memoryEnabled &&
  77. inTimeline == other.inTimeline;
  78. }
  79. @override
  80. @ignore
  81. int get hashCode =>
  82. id.hashCode ^
  83. updatedAt.hashCode ^
  84. email.hashCode ^
  85. name.hashCode ^
  86. isPartnerSharedBy.hashCode ^
  87. isPartnerSharedWith.hashCode ^
  88. profileImagePath.hashCode ^
  89. avatarColor.hashCode ^
  90. isAdmin.hashCode ^
  91. memoryEnabled.hashCode ^
  92. inTimeline.hashCode;
  93. }
  94. enum AvatarColorEnum {
  95. // do not change this order or reuse indices for other purposes, adding is OK
  96. primary,
  97. pink,
  98. red,
  99. yellow,
  100. blue,
  101. green,
  102. purple,
  103. orange,
  104. gray,
  105. amber,
  106. }
  107. extension AvatarColorEnumHelper on UserAvatarColor {
  108. AvatarColorEnum toAvatarColor() {
  109. switch (this) {
  110. case UserAvatarColor.primary:
  111. return AvatarColorEnum.primary;
  112. case UserAvatarColor.pink:
  113. return AvatarColorEnum.pink;
  114. case UserAvatarColor.red:
  115. return AvatarColorEnum.red;
  116. case UserAvatarColor.yellow:
  117. return AvatarColorEnum.yellow;
  118. case UserAvatarColor.blue:
  119. return AvatarColorEnum.blue;
  120. case UserAvatarColor.green:
  121. return AvatarColorEnum.green;
  122. case UserAvatarColor.purple:
  123. return AvatarColorEnum.purple;
  124. case UserAvatarColor.orange:
  125. return AvatarColorEnum.orange;
  126. case UserAvatarColor.gray:
  127. return AvatarColorEnum.gray;
  128. case UserAvatarColor.amber:
  129. return AvatarColorEnum.amber;
  130. }
  131. return AvatarColorEnum.primary;
  132. }
  133. }
  134. extension AvatarColorToColorHelper on AvatarColorEnum {
  135. Color toColor([bool isDarkTheme = false]) {
  136. switch (this) {
  137. case AvatarColorEnum.primary:
  138. return isDarkTheme ? const Color(0xFFABCBFA) : const Color(0xFF4250AF);
  139. case AvatarColorEnum.pink:
  140. return const Color.fromARGB(255, 244, 114, 182);
  141. case AvatarColorEnum.red:
  142. return const Color.fromARGB(255, 239, 68, 68);
  143. case AvatarColorEnum.yellow:
  144. return const Color.fromARGB(255, 234, 179, 8);
  145. case AvatarColorEnum.blue:
  146. return const Color.fromARGB(255, 59, 130, 246);
  147. case AvatarColorEnum.green:
  148. return const Color.fromARGB(255, 22, 163, 74);
  149. case AvatarColorEnum.purple:
  150. return const Color.fromARGB(255, 147, 51, 234);
  151. case AvatarColorEnum.orange:
  152. return const Color.fromARGB(255, 234, 88, 12);
  153. case AvatarColorEnum.gray:
  154. return const Color.fromARGB(255, 75, 85, 99);
  155. case AvatarColorEnum.amber:
  156. return const Color.fromARGB(255, 217, 119, 6);
  157. }
  158. }
  159. }