mirror of
https://github.com/immich-app/immich.git
synced 2025-02-11 01:18:24 -05:00
refactor: deduplicate MemoryType and ReactionType enums (#11479)
* refactor: deduplicate memorytype and reactiontype enums * fix mobile
This commit is contained in:
parent
281cfc95a4
commit
b73f7fe16f
8 changed files with 18 additions and 176 deletions
|
@ -43,7 +43,7 @@ class Activity {
|
||||||
assetId = dto.assetId,
|
assetId = dto.assetId,
|
||||||
comment = dto.comment,
|
comment = dto.comment,
|
||||||
createdAt = dto.createdAt,
|
createdAt = dto.createdAt,
|
||||||
type = dto.type == ActivityResponseDtoTypeEnum.comment
|
type = dto.type == ReactionType.comment
|
||||||
? ActivityType.comment
|
? ActivityType.comment
|
||||||
: ActivityType.like,
|
: ActivityType.like,
|
||||||
user = User.fromSimpleUserDto(dto.user);
|
user = User.fromSimpleUserDto(dto.user);
|
||||||
|
|
78
mobile/openapi/lib/model/activity_response_dto.dart
generated
78
mobile/openapi/lib/model/activity_response_dto.dart
generated
|
@ -29,7 +29,7 @@ class ActivityResponseDto {
|
||||||
|
|
||||||
String id;
|
String id;
|
||||||
|
|
||||||
ActivityResponseDtoTypeEnum type;
|
ReactionType type;
|
||||||
|
|
||||||
UserResponseDto user;
|
UserResponseDto user;
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ class ActivityResponseDto {
|
||||||
comment: mapValueOfType<String>(json, r'comment'),
|
comment: mapValueOfType<String>(json, r'comment'),
|
||||||
createdAt: mapDateTime(json, r'createdAt', r'')!,
|
createdAt: mapDateTime(json, r'createdAt', r'')!,
|
||||||
id: mapValueOfType<String>(json, r'id')!,
|
id: mapValueOfType<String>(json, r'id')!,
|
||||||
type: ActivityResponseDtoTypeEnum.fromJson(json[r'type'])!,
|
type: ReactionType.fromJson(json[r'type'])!,
|
||||||
user: UserResponseDto.fromJson(json[r'user'])!,
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
75
mobile/openapi/lib/model/memory_response_dto.dart
generated
75
mobile/openapi/lib/model/memory_response_dto.dart
generated
|
@ -56,7 +56,7 @@ class MemoryResponseDto {
|
||||||
///
|
///
|
||||||
DateTime? seenAt;
|
DateTime? seenAt;
|
||||||
|
|
||||||
MemoryResponseDtoTypeEnum type;
|
MemoryType type;
|
||||||
|
|
||||||
DateTime updatedAt;
|
DateTime updatedAt;
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ class MemoryResponseDto {
|
||||||
memoryAt: mapDateTime(json, r'memoryAt', r'')!,
|
memoryAt: mapDateTime(json, r'memoryAt', r'')!,
|
||||||
ownerId: mapValueOfType<String>(json, r'ownerId')!,
|
ownerId: mapValueOfType<String>(json, r'ownerId')!,
|
||||||
seenAt: mapDateTime(json, r'seenAt', r''),
|
seenAt: mapDateTime(json, r'seenAt', r''),
|
||||||
type: MemoryResponseDtoTypeEnum.fromJson(json[r'type'])!,
|
type: MemoryType.fromJson(json[r'type'])!,
|
||||||
updatedAt: mapDateTime(json, r'updatedAt', r'')!,
|
updatedAt: mapDateTime(json, r'updatedAt', r'')!,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -194,74 +194,3 @@ class MemoryResponseDto {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class MemoryResponseDtoTypeEnum {
|
|
||||||
/// Instantiate a new enum with the provided [value].
|
|
||||||
const MemoryResponseDtoTypeEnum._(this.value);
|
|
||||||
|
|
||||||
/// The underlying value of this enum member.
|
|
||||||
final String value;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() => value;
|
|
||||||
|
|
||||||
String toJson() => value;
|
|
||||||
|
|
||||||
static const onThisDay = MemoryResponseDtoTypeEnum._(r'on_this_day');
|
|
||||||
|
|
||||||
/// List of all possible values in this [enum][MemoryResponseDtoTypeEnum].
|
|
||||||
static const values = <MemoryResponseDtoTypeEnum>[
|
|
||||||
onThisDay,
|
|
||||||
];
|
|
||||||
|
|
||||||
static MemoryResponseDtoTypeEnum? fromJson(dynamic value) => MemoryResponseDtoTypeEnumTypeTransformer().decode(value);
|
|
||||||
|
|
||||||
static List<MemoryResponseDtoTypeEnum> listFromJson(dynamic json, {bool growable = false,}) {
|
|
||||||
final result = <MemoryResponseDtoTypeEnum>[];
|
|
||||||
if (json is List && json.isNotEmpty) {
|
|
||||||
for (final row in json) {
|
|
||||||
final value = MemoryResponseDtoTypeEnum.fromJson(row);
|
|
||||||
if (value != null) {
|
|
||||||
result.add(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result.toList(growable: growable);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Transformation class that can [encode] an instance of [MemoryResponseDtoTypeEnum] to String,
|
|
||||||
/// and [decode] dynamic data back to [MemoryResponseDtoTypeEnum].
|
|
||||||
class MemoryResponseDtoTypeEnumTypeTransformer {
|
|
||||||
factory MemoryResponseDtoTypeEnumTypeTransformer() => _instance ??= const MemoryResponseDtoTypeEnumTypeTransformer._();
|
|
||||||
|
|
||||||
const MemoryResponseDtoTypeEnumTypeTransformer._();
|
|
||||||
|
|
||||||
String encode(MemoryResponseDtoTypeEnum data) => data.value;
|
|
||||||
|
|
||||||
/// Decodes a [dynamic value][data] to a MemoryResponseDtoTypeEnum.
|
|
||||||
///
|
|
||||||
/// 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.
|
|
||||||
MemoryResponseDtoTypeEnum? decode(dynamic data, {bool allowNull = true}) {
|
|
||||||
if (data != null) {
|
|
||||||
switch (data) {
|
|
||||||
case r'on_this_day': return MemoryResponseDtoTypeEnum.onThisDay;
|
|
||||||
default:
|
|
||||||
if (!allowNull) {
|
|
||||||
throw ArgumentError('Unknown enum value to decode: $data');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Singleton [MemoryResponseDtoTypeEnumTypeTransformer] instance.
|
|
||||||
static MemoryResponseDtoTypeEnumTypeTransformer? _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7216,11 +7216,7 @@
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"type": {
|
"type": {
|
||||||
"enum": [
|
"$ref": "#/components/schemas/ReactionType"
|
||||||
"comment",
|
|
||||||
"like"
|
|
||||||
],
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
"user": {
|
"user": {
|
||||||
"$ref": "#/components/schemas/UserResponseDto"
|
"$ref": "#/components/schemas/UserResponseDto"
|
||||||
|
@ -9311,10 +9307,7 @@
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"type": {
|
"type": {
|
||||||
"enum": [
|
"$ref": "#/components/schemas/MemoryType"
|
||||||
"on_this_day"
|
|
||||||
],
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
"updatedAt": {
|
"updatedAt": {
|
||||||
"format": "date-time",
|
"format": "date-time",
|
||||||
|
|
|
@ -26,7 +26,7 @@ export type ActivityResponseDto = {
|
||||||
comment?: string | null;
|
comment?: string | null;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
id: string;
|
id: string;
|
||||||
"type": Type;
|
"type": ReactionType;
|
||||||
user: UserResponseDto;
|
user: UserResponseDto;
|
||||||
};
|
};
|
||||||
export type ActivityCreateDto = {
|
export type ActivityCreateDto = {
|
||||||
|
@ -572,7 +572,7 @@ export type MemoryResponseDto = {
|
||||||
memoryAt: string;
|
memoryAt: string;
|
||||||
ownerId: string;
|
ownerId: string;
|
||||||
seenAt?: string;
|
seenAt?: string;
|
||||||
"type": Type2;
|
"type": MemoryType;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
};
|
};
|
||||||
export type MemoryCreateDto = {
|
export type MemoryCreateDto = {
|
||||||
|
@ -3065,10 +3065,6 @@ export enum ReactionType {
|
||||||
Comment = "comment",
|
Comment = "comment",
|
||||||
Like = "like"
|
Like = "like"
|
||||||
}
|
}
|
||||||
export enum Type {
|
|
||||||
Comment = "comment",
|
|
||||||
Like = "like"
|
|
||||||
}
|
|
||||||
export enum UserAvatarColor {
|
export enum UserAvatarColor {
|
||||||
Primary = "primary",
|
Primary = "primary",
|
||||||
Pink = "pink",
|
Pink = "pink",
|
||||||
|
@ -3164,9 +3160,6 @@ export enum MapTheme {
|
||||||
Light = "light",
|
Light = "light",
|
||||||
Dark = "dark"
|
Dark = "dark"
|
||||||
}
|
}
|
||||||
export enum Type2 {
|
|
||||||
OnThisDay = "on_this_day"
|
|
||||||
}
|
|
||||||
export enum MemoryType {
|
export enum MemoryType {
|
||||||
OnThisDay = "on_this_day"
|
OnThisDay = "on_this_day"
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ export type MaybeDuplicate<T> = { duplicate: boolean; value: T };
|
||||||
export class ActivityResponseDto {
|
export class ActivityResponseDto {
|
||||||
id!: string;
|
id!: string;
|
||||||
createdAt!: Date;
|
createdAt!: Date;
|
||||||
|
@ApiProperty({ enumName: 'ReactionType', enum: ReactionType })
|
||||||
type!: ReactionType;
|
type!: ReactionType;
|
||||||
user!: UserResponseDto;
|
user!: UserResponseDto;
|
||||||
assetId!: string | null;
|
assetId!: string | null;
|
||||||
|
@ -53,7 +54,7 @@ export class ActivitySearchDto extends ActivityDto {
|
||||||
userId?: string;
|
userId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isComment = (dto: ActivityCreateDto) => dto.type === 'comment';
|
const isComment = (dto: ActivityCreateDto) => dto.type === ReactionType.COMMENT;
|
||||||
|
|
||||||
export class ActivityCreateDto extends ActivityDto {
|
export class ActivityCreateDto extends ActivityDto {
|
||||||
@IsEnum(ReactionType)
|
@IsEnum(ReactionType)
|
||||||
|
|
|
@ -61,6 +61,7 @@ export class MemoryResponseDto {
|
||||||
memoryAt!: Date;
|
memoryAt!: Date;
|
||||||
seenAt?: Date;
|
seenAt?: Date;
|
||||||
ownerId!: string;
|
ownerId!: string;
|
||||||
|
@ApiProperty({ enumName: 'MemoryType', enum: MemoryType })
|
||||||
type!: MemoryType;
|
type!: MemoryType;
|
||||||
data!: MemoryData;
|
data!: MemoryData;
|
||||||
isSaved!: boolean;
|
isSaved!: boolean;
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
import { isTenMinutesApart } from '$lib/utils/timesince';
|
import { isTenMinutesApart } from '$lib/utils/timesince';
|
||||||
import {
|
import {
|
||||||
ReactionType,
|
ReactionType,
|
||||||
Type,
|
|
||||||
createActivity,
|
createActivity,
|
||||||
deleteActivity,
|
deleteActivity,
|
||||||
getActivities,
|
getActivities,
|
||||||
|
@ -111,15 +110,15 @@
|
||||||
await deleteActivity({ id: reaction.id });
|
await deleteActivity({ id: reaction.id });
|
||||||
reactions.splice(index, 1);
|
reactions.splice(index, 1);
|
||||||
reactions = reactions;
|
reactions = reactions;
|
||||||
if (isLiked && reaction.type === 'like' && reaction.id == isLiked.id) {
|
if (isLiked && reaction.type === ReactionType.Like && reaction.id == isLiked.id) {
|
||||||
dispatch('deleteLike');
|
dispatch('deleteLike');
|
||||||
} else {
|
} else {
|
||||||
dispatch('deleteComment');
|
dispatch('deleteComment');
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteMessages: Record<Type, string> = {
|
const deleteMessages: Record<ReactionType, string> = {
|
||||||
[Type.Comment]: $t('comment_deleted'),
|
[ReactionType.Comment]: $t('comment_deleted'),
|
||||||
[Type.Like]: $t('like_deleted'),
|
[ReactionType.Like]: $t('like_deleted'),
|
||||||
};
|
};
|
||||||
notificationController.show({
|
notificationController.show({
|
||||||
message: deleteMessages[reaction.type],
|
message: deleteMessages[reaction.type],
|
||||||
|
@ -172,7 +171,7 @@
|
||||||
style="height: {divHeight}px;padding-bottom: {chatHeight}px"
|
style="height: {divHeight}px;padding-bottom: {chatHeight}px"
|
||||||
>
|
>
|
||||||
{#each reactions as reaction, index (reaction.id)}
|
{#each reactions as reaction, index (reaction.id)}
|
||||||
{#if reaction.type === 'comment'}
|
{#if reaction.type === ReactionType.Comment}
|
||||||
<div class="flex dark:bg-gray-800 bg-gray-200 py-3 pl-3 mt-3 rounded-lg gap-4 justify-start">
|
<div class="flex dark:bg-gray-800 bg-gray-200 py-3 pl-3 mt-3 rounded-lg gap-4 justify-start">
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<UserAvatar user={reaction.user} size="sm" />
|
<UserAvatar user={reaction.user} size="sm" />
|
||||||
|
@ -216,7 +215,7 @@
|
||||||
{timeSince(luxon.DateTime.fromISO(reaction.createdAt, { locale: $locale }))}
|
{timeSince(luxon.DateTime.fromISO(reaction.createdAt, { locale: $locale }))}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
{:else if reaction.type === 'like'}
|
{:else if reaction.type === ReactionType.Like}
|
||||||
<div class="relative">
|
<div class="relative">
|
||||||
<div class="flex py-3 pl-3 mt-3 gap-4 items-center text-sm">
|
<div class="flex py-3 pl-3 mt-3 gap-4 items-center text-sm">
|
||||||
<div class="text-red-600"><Icon path={mdiHeart} size={20} /></div>
|
<div class="text-red-600"><Icon path={mdiHeart} size={20} /></div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue