Skip to content

Commit

Permalink
fixup! wip drop: MemItem v1 drop: EntityV1
Browse files Browse the repository at this point in the history
  • Loading branch information
zin- committed Sep 15, 2024
1 parent ebc1169 commit 51f97d3
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 116 deletions.
4 changes: 2 additions & 2 deletions lib/core/mem_detail.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:mem/core/mem.dart';
import 'package:mem/core/mem_item.dart';
import 'package:mem/core/mem_notification.dart';
import 'package:mem/repositories/mem_item_entity.dart';

// FIXME 定義するべきではない気がする
// - Mem, MemItems, MemNotificationsの関係はどのレイヤーのもの?
Expand All @@ -9,7 +9,7 @@ import 'package:mem/core/mem_notification.dart';
// - Repositoryも絡んでいくはず
class MemDetail {
final MemV1 mem;
final List<MemItem> memItems;
final List<MemItemEntity> memItems;
final List<MemNotification>? notifications;

MemDetail(this.mem, this.memItems, [this.notifications]);
Expand Down
36 changes: 0 additions & 36 deletions lib/core/mem_item.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import 'package:mem/framework/repository/entity.dart';
import 'package:mem/mems/mem_item.dart';
import 'package:mem/repositories/mem_item_entity.dart';

enum MemItemType {
memo,
}
Expand All @@ -12,36 +8,4 @@ class MemItemV2 {
final dynamic value;

MemItemV2(this.memId, this.type, this.value);

static MemItemV2 fromV1(MemItem v1) => v1 is SavedMemItem
? MemItemEntity.fromV1(v1)
: MemItemV2(v1.memId, v1.type, v1.value);

MemItem toV1() => MemItem(memId, type, value);
}

class MemItem extends EntityV1 {
// 未保存のMemに紐づくMemItemはmemIdをintで持つことができないため暫定的にnullableにしている
final int? memId;
final MemItemType type;
final dynamic value;

MemItem(this.memId, this.type, this.value);

MemItem copiedWith({
int Function()? memId,
dynamic Function()? value,
}) =>
MemItem(
memId == null ? this.memId : memId(),
type,
value == null ? this.value : value(),
);

@override
String toString() => "${super.toString()}: ${{
"memId": memId,
"type": type,
"value": value,
}}";
}
5 changes: 1 addition & 4 deletions lib/mems/actions.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mem/logger/log_service.dart';
import 'package:mem/mems/mem_item.dart';
import 'package:mem/mems/mem_service.dart';
import 'package:mem/mems/states.dart';
import 'package:mem/repositories/mem.dart';
Expand All @@ -21,9 +20,7 @@ final undoRemoveMem = FutureProvider.autoDispose.family<void, int>(
for (var element in removeUndone.memItems) {
ref.read(memItemsProvider.notifier).upsertAll(
[
element is SavedMemItem
? SavedMemItemEntity.fromV1(element)
: MemItemEntity.fromV1(element)
element,
],
(current, updating) =>
current is SavedMemItemEntity &&
Expand Down
8 changes: 1 addition & 7 deletions lib/mems/detail/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import 'package:mem/core/mem_detail.dart';
import 'package:mem/logger/log_service.dart';
import 'package:mem/mems/detail/states.dart';
import 'package:mem/mems/mem_client.dart';
import 'package:mem/mems/mem_item.dart';
import 'package:mem/repositories/mem.dart';
import 'package:mem/mems/states.dart';
import 'package:mem/repositories/mem_item_entity.dart';
import 'package:mem/repositories/mem_notification_entity.dart';

final _memClient = MemClient();
Expand All @@ -31,11 +29,7 @@ final saveMem =
.read(editingMemByMemIdProvider(memId).notifier)
.updatedBy(saved.mem);
ref.read(memItemsProvider.notifier).upsertAll(
saved.memItems.map(
(e) => e is SavedMemItem
? SavedMemItemEntity.fromV1(e)
: MemItemEntity.fromV1(e),
),
saved.memItems,
(current, updating) =>
current.memId == updating.memId &&
current.type == updating.type,
Expand Down
6 changes: 3 additions & 3 deletions lib/mems/mem_client.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:mem/core/mem.dart';
import 'package:mem/core/mem_detail.dart';
import 'package:mem/core/mem_item.dart';
import 'package:mem/core/mem_notification.dart';
import 'package:mem/logger/log_service.dart';
import 'package:mem/notifications/notification_client.dart';
import 'package:mem/repositories/mem.dart';
import 'package:mem/repositories/mem_item_entity.dart';
import 'package:mem/repositories/mem_notification_entity.dart';

import 'mem_service.dart';
Expand All @@ -15,15 +15,15 @@ class MemClient {

Future<MemDetail> save(
MemV1 mem,
List<MemItemV2> memItemList,
List<MemItemEntity> memItemList,
List<MemNotification> memNotificationList,
) =>
v(
() async {
final saved = await _memService.save(
MemDetail(
mem,
memItemList.map((e) => e.toV1()).toList(),
memItemList,
memNotificationList,
),
);
Expand Down
17 changes: 0 additions & 17 deletions lib/mems/mem_item.dart

This file was deleted.

32 changes: 14 additions & 18 deletions lib/mems/mem_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:collection/collection.dart';
import 'package:mem/core/mem_detail.dart';
import 'package:mem/core/mem_notification.dart';
import 'package:mem/logger/log_service.dart';
import 'package:mem/mems/mem_item.dart';
import 'package:mem/mems/mem_item_repository.dart';
import 'package:mem/repositories/mem.dart';
import 'package:mem/repositories/mem_entity.dart';
Expand All @@ -29,17 +28,16 @@ class MemService {
: await _memRepository.receive(MemEntity.fromV1(mem)))
.toV1();

final savedMemItems = (await Future.wait(
memDetail.memItems.map((e) => (e is SavedMemItem && !undo
? _memItemRepository.replace(
SavedMemItemEntity.fromV1(
e.copiedWith(memId: () => savedMem.id)),
)
: _memItemRepository.receive(
MemItemEntity.fromV1(
e.copiedWith(memId: () => savedMem.id)),
))
.then((value) => value.toV1()))));
final savedMemItems = await Future.wait(
memDetail.memItems.map((e) => (e is SavedMemItemEntity && !undo
? _memItemRepository.replace(
e.copiedWith(memId: () => savedMem.id)
as SavedMemItemEntity,
)
: _memItemRepository.receive(
e.copiedWith(memId: () => savedMem.id),
))),
);

final memNotifications = memDetail.notifications;
final returnMemNotifications =
Expand Down Expand Up @@ -129,9 +127,8 @@ class MemService {
final archivedMem = await _memRepository
.archive(SavedMemEntity.fromV1(mem))
.then((v) => v.toV1());
final archivedMemItems = await _memItemRepository
.archiveBy(memId: archivedMem.id)
.then((v) => v.map((e) => e.toV1()));
final archivedMemItems =
await _memItemRepository.archiveBy(memId: archivedMem.id);
final archivedMemNotifications =
await _memNotificationRepository.archiveBy(memId: archivedMem.id);

Expand All @@ -151,9 +148,8 @@ class MemService {
final unarchivedMem = await _memRepository
.unarchive(SavedMemEntity.fromV1(mem))
.then((value) => value.toV1());
final unarchivedMemItems = await _memItemRepository
.unarchiveBy(memId: unarchivedMem.id)
.then((v) => v.map((e) => e.toV1()));
final unarchivedMemItems =
await _memItemRepository.unarchiveBy(memId: unarchivedMem.id);
final unarchivedMemNotifications = await _memNotificationRepository
.unarchiveBy(memId: unarchivedMem.id);

Expand Down
2 changes: 1 addition & 1 deletion lib/mems/states.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final removedMemDetailProvider = StateNotifierProvider.autoDispose
if (removedMem != null && removedMemItems != null) {
removedMemDetail = MemDetail(
removedMem,
removedMemItems.map((e) => e.toV1()).toList(),
removedMemItems,
);
} else {
removedMemDetail = null;
Expand Down
28 changes: 0 additions & 28 deletions lib/repositories/mem_item_entity.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:mem/core/mem_item.dart';
import 'package:mem/databases/table_definitions/base.dart';
import 'package:mem/databases/table_definitions/mem_items.dart';
import 'package:mem/framework/repository/database_tuple_entity.dart';
import 'package:mem/framework/repository/entity.dart';
import 'package:mem/mems/mem_item.dart';

class MemItemEntity extends MemItemV2 with Entity, Copyable<MemItemEntity> {
MemItemEntity(super.memId, super.type, super.value);
Expand Down Expand Up @@ -34,12 +32,6 @@ class MemItemEntity extends MemItemV2 with Entity, Copyable<MemItemEntity> {
type,
value == null ? this.value : value(),
);

static MemItemEntity fromV1(MemItem v1) =>
MemItemEntity(v1.memId, v1.type, v1.value);

@override
MemItem toV1() => MemItem(memId, type, value);
}

class SavedMemItemEntity extends MemItemEntity with DatabaseTupleEntity<int> {
Expand All @@ -63,24 +55,4 @@ class SavedMemItemEntity extends MemItemEntity with DatabaseTupleEntity<int> {
.toMap,
),
);

static SavedMemItemEntity fromV1(SavedMemItem savedMemItem) =>
SavedMemItemEntity.fromMap(
MemItemEntity.fromV1(savedMemItem).toMap
..addAll(
{
defPkId.name: savedMemItem.id,
defColCreatedAt.name: savedMemItem.createdAt,
defColUpdatedAt.name: savedMemItem.updatedAt,
defColArchivedAt.name: savedMemItem.archivedAt
},
),
);

@override
SavedMemItem toV1() => SavedMemItem(memId, type, value)
..id = id
..createdAt = createdAt
..updatedAt = updatedAt
..archivedAt = archivedAt;
}

0 comments on commit 51f97d3

Please sign in to comment.