1
0
mirror of https://github.com/immich-app/immich.git synced 2025-08-09 23:17:29 +02:00

refactor: deduplicate MemoryType and ReactionType enums (#11479)

* refactor: deduplicate memorytype and reactiontype enums

* fix mobile
This commit is contained in:
Michel Heusschen
2024-07-31 19:08:31 +02:00
committed by GitHub
parent 281cfc95a4
commit b73f7fe16f
8 changed files with 18 additions and 176 deletions

View File

@@ -29,7 +29,7 @@ class ActivityResponseDto {
String id;
ActivityResponseDtoTypeEnum type;
ReactionType type;
UserResponseDto user;
@@ -86,7 +86,7 @@ class ActivityResponseDto {
comment: mapValueOfType<String>(json, r'comment'),
createdAt: mapDateTime(json, r'createdAt', r'')!,
id: mapValueOfType<String>(json, r'id')!,
type: ActivityResponseDtoTypeEnum.fromJson(json[r'type'])!,
type: ReactionType.fromJson(json[r'type'])!,
user: UserResponseDto.fromJson(json[r'user'])!,
);
}
@@ -143,77 +143,3 @@ class ActivityResponseDto {
};
}
class ActivityResponseDtoTypeEnum {
/// Instantiate a new enum with the provided [value].
const ActivityResponseDtoTypeEnum._(this.value);
/// The underlying value of this enum member.
final String value;
@override
String toString() => value;
String toJson() => value;
static const comment = ActivityResponseDtoTypeEnum._(r'comment');
static const like = ActivityResponseDtoTypeEnum._(r'like');
/// List of all possible values in this [enum][ActivityResponseDtoTypeEnum].
static const values = <ActivityResponseDtoTypeEnum>[
comment,
like,
];
static ActivityResponseDtoTypeEnum? fromJson(dynamic value) => ActivityResponseDtoTypeEnumTypeTransformer().decode(value);
static List<ActivityResponseDtoTypeEnum> listFromJson(dynamic json, {bool growable = false,}) {
final result = <ActivityResponseDtoTypeEnum>[];
if (json is List && json.isNotEmpty) {
for (final row in json) {
final value = ActivityResponseDtoTypeEnum.fromJson(row);
if (value != null) {
result.add(value);
}
}
}
return result.toList(growable: growable);
}
}
/// Transformation class that can [encode] an instance of [ActivityResponseDtoTypeEnum] to String,
/// and [decode] dynamic data back to [ActivityResponseDtoTypeEnum].
class ActivityResponseDtoTypeEnumTypeTransformer {
factory ActivityResponseDtoTypeEnumTypeTransformer() => _instance ??= const ActivityResponseDtoTypeEnumTypeTransformer._();
const ActivityResponseDtoTypeEnumTypeTransformer._();
String encode(ActivityResponseDtoTypeEnum data) => data.value;
/// Decodes a [dynamic value][data] to a ActivityResponseDtoTypeEnum.
///
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
///
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
/// and users are still using an old app with the old code.
ActivityResponseDtoTypeEnum? decode(dynamic data, {bool allowNull = true}) {
if (data != null) {
switch (data) {
case r'comment': return ActivityResponseDtoTypeEnum.comment;
case r'like': return ActivityResponseDtoTypeEnum.like;
default:
if (!allowNull) {
throw ArgumentError('Unknown enum value to decode: $data');
}
}
}
return null;
}
/// Singleton [ActivityResponseDtoTypeEnumTypeTransformer] instance.
static ActivityResponseDtoTypeEnumTypeTransformer? _instance;
}