Skip to content

Commit

Permalink
wip add: NotificationRepositoryV2
Browse files Browse the repository at this point in the history
wip add: `KeyWithValueRepositoryV2`
wip drop: on `KeyWithValueRepository`
wip drop: `RepositoryV2`

wip #377
  • Loading branch information
zin- committed Aug 29, 2024
1 parent e74756f commit 0a20c4c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/framework/repository/key_with_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ abstract class KeyWithValue<Key, Value> extends EntityV2 {
"value": value,
}.toString();
}

// FIXME IdWithValueの方が命名として適切なのでは?
mixin KeyWithValueV2<KEY, VALUE> on Entity {
late final KEY key;
late final VALUE value;
}
8 changes: 8 additions & 0 deletions lib/framework/repository/key_with_value_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ mixin DiscarderByKey<E extends KeyWithValue<Key, dynamic>, Key, Result>
on RepositoryV2<E> {
Future<Result> discard(Key key);
}

abstract class KeyWithValueRepositoryV2<
ENTITY extends KeyWithValueV2<Key, dynamic>,
Key> extends Repository<ENTITY> {
Future<void> receive(ENTITY entity);

Future<void> discard(int key);
}
36 changes: 36 additions & 0 deletions lib/notifications/notification/notification.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:mem/framework/repository/entity.dart';
import 'package:mem/framework/repository/key_with_value.dart';
import 'package:mem/notifications/notification/channel.dart';

Expand Down Expand Up @@ -29,3 +30,38 @@ class Notification extends KeyWithValue<int, Map<String, dynamic>> {
"value": value,
}}";
}

class NotificationV2 with Entity, KeyWithValueV2<int, Map<String, dynamic>> {
final String title;
final String body;
final NotificationChannel channel;
final Map<String, dynamic> payload;

NotificationV2(

Check warning on line 40 in lib/notifications/notification/notification.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification/notification.dart#L40

Added line #L40 was not covered by tests
int id,
this.title,
this.body,
this.channel,
this.payload,
) {
key = id;
value = {
'title': title,
'body': body,
'channel': channel,
'payload': payload,

Check warning on line 52 in lib/notifications/notification/notification.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification/notification.dart#L47-L52

Added lines #L47 - L52 were not covered by tests
};
}

@override

Check warning on line 56 in lib/notifications/notification/notification.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification/notification.dart#L56

Added line #L56 was not covered by tests
Entity copiedWith() {
// TODO: implement copiedWith
throw UnimplementedError();

Check warning on line 59 in lib/notifications/notification/notification.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification/notification.dart#L59

Added line #L59 was not covered by tests
}

@override
Map<String, dynamic> get toMap => {
'key': key,
'value': value,

Check warning on line 65 in lib/notifications/notification/notification.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification/notification.dart#L62-L65

Added lines #L62 - L65 were not covered by tests
};
}
16 changes: 10 additions & 6 deletions lib/notifications/notification_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ class NotificationClient {
final NotificationChannels notificationChannels;

final ScheduleClient _scheduleClient;
final NotificationRepository _notificationRepository;
final NotificationRepository _notificationRepositoryV1;
final NotificationRepositoryV2 _notificationRepository;
final PreferenceClient _preferenceClient;
final MemRepository _memRepository;
final MemNotificationRepository _memNotificationRepository;

NotificationClient._(
this.notificationChannels,
this._scheduleClient,
this._notificationRepositoryV1,
this._notificationRepository,
this._preferenceClient,
this._memRepository,
Expand All @@ -46,6 +48,7 @@ class NotificationClient {
NotificationChannels(buildL10n(context)),
ScheduleClient(),
NotificationRepository(),
NotificationRepositoryV2(),
PreferenceClient(),
MemRepository(),
MemNotificationRepository(),
Expand Down Expand Up @@ -112,7 +115,7 @@ class NotificationClient {
.discard(NotificationType.activeAct.buildNotificationId(memId));
}

await _notificationRepository.receive(
await _notificationRepositoryV1.receive(
await notificationChannels.buildNotification(
notificationType,
memId,
Expand Down Expand Up @@ -173,7 +176,7 @@ class NotificationClient {
Future<void> cancelMemNotifications(int memId) => v(
() async {
for (var id in AllMemNotificationsId.of(memId)) {
await _notificationRepository.discard(id);
await _notificationRepositoryV1.discard(id);
await _scheduleClient.discard(id);
}
},
Expand Down Expand Up @@ -242,9 +245,10 @@ class NotificationClient {

Future<void> cancelActNotification(int memId) => v(
() async {
await _notificationRepository.discard(activeActNotificationId(memId));
await _notificationRepositoryV1
.discard(activeActNotificationId(memId));

await _notificationRepository
await _notificationRepositoryV1
.discard(afterActStartedNotificationId(memId));
await _scheduleClient.discard(afterActStartedNotificationId(memId));
},
Expand Down Expand Up @@ -299,7 +303,7 @@ class NotificationClient {

Future<void> resetAll() => v(
() async {
await _notificationRepository.discardAll();
await _notificationRepositoryV1.discardAll();

final allSavedMems = await _memRepository.ship(
archived: false,
Expand Down
34 changes: 34 additions & 0 deletions lib/notifications/notification_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,37 @@ class NotificationRepository extends KeyWithValueRepository<Notification, int>
},
);
}

class NotificationRepositoryV2
extends KeyWithValueRepositoryV2<NotificationV2, int> {
final FlutterLocalNotificationsWrapper? _flutterLocalNotificationsWrapper =
defaultTargetPlatform == TargetPlatform.android
? FlutterLocalNotificationsWrapper(androidDefaultIconPath)
: null;

@override
Future<void> receive(NotificationV2 entity) => v(
() async {
if (await PermissionHandlerWrapper().grant(Permission.notification)) {
await _flutterLocalNotificationsWrapper?.show(
entity.key,
entity.title,
entity.body,
entity.channel,
entity.payload,

Check warning on line 107 in lib/notifications/notification_repository.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification_repository.dart#L98-L107

Added lines #L98 - L107 were not covered by tests
);
}
},
{

Check warning on line 111 in lib/notifications/notification_repository.dart

View check run for this annotation

Codecov / codecov/patch

lib/notifications/notification_repository.dart#L111

Added line #L111 was not covered by tests
'entity': entity,
},
);

@override
Future<void> discard(int key) => v(
() async => await _flutterLocalNotificationsWrapper?.cancel(key),
{
'key': key,
},
);
}

0 comments on commit 0a20c4c

Please sign in to comment.