Skip to content

Commit

Permalink
replace: NotificationRepository
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 Sep 7, 2024
1 parent 53e2962 commit 829de27
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 119 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<void> main({String? languageCode}) => i(
() async {
WidgetsFlutterBinding.ensureInitialized();

await NotificationRepository().checkNotification();
await NotificationRepository().ship();

return _runApplication(languageCode: languageCode);
},
Expand Down
37 changes: 4 additions & 33 deletions lib/notifications/notification/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,6 @@ import 'package:mem/framework/repository/entity.dart';
import 'package:mem/framework/repository/key_with_value.dart';
import 'package:mem/notifications/notification/channel.dart';

class Notification extends KeyWithValue<int, Map<String, dynamic>> {
final String title;
final String body;
final NotificationChannel channel;
final Map<String, dynamic> payload;

Notification(
int id,
this.title,
this.body,
this.channel,
this.payload,
) : super(
id,
{
"title": title,
"body": body,
"channel": channel,
"payload": payload,
},
);

@override
String toString() => "${super.toString()}: ${{
"key": key,
"value": value,
}}";
}

class NotificationV2 with Entity, KeyWithValueV2<int, Map<String, dynamic>> {
final String title;
final String body;
Expand All @@ -53,11 +24,11 @@ class NotificationV2 with Entity, KeyWithValueV2<int, Map<String, dynamic>> {
};
}

// coverage:ignore-start
@override
Entity copiedWith() {
// TODO: implement copiedWith
throw UnimplementedError();
}
Entity copiedWith() => throw UnimplementedError();

// coverage:ignore-end

@override
Map<String, dynamic> get toMap => {
Expand Down
6 changes: 3 additions & 3 deletions lib/notifications/notification_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NotificationClient {
final NotificationChannels notificationChannels;

final ScheduleClient _scheduleClient;
final NotificationRepositoryV2 _notificationRepository;
final NotificationRepository _notificationRepository;
final PreferenceClient _preferenceClient;
final MemRepository _memRepository;
final MemNotificationRepository _memNotificationRepository;
Expand All @@ -45,7 +45,7 @@ class NotificationClient {
() => _instance ??= NotificationClient._(
NotificationChannels(buildL10n(context)),
ScheduleClient(),
NotificationRepositoryV2(),
NotificationRepository(),
PreferenceClient(),
MemRepository(),
MemNotificationRepository(),
Expand All @@ -59,7 +59,7 @@ class NotificationClient {
static void resetSingleton() => v(
() {
ScheduleClient.resetSingleton();
NotificationRepository.resetSingleton();
NotificationRepository.reset();
_instance = null;
},
{
Expand Down
92 changes: 12 additions & 80 deletions lib/notifications/notification_repository.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:mem/framework/repository/key_with_value_repository.dart';
import 'package:mem/framework/repository/repository.dart';
import 'package:mem/logger/log_service.dart';
import 'package:mem/notifications/notification/type.dart';
import 'package:mem/permissions/permission.dart';
Expand All @@ -10,85 +9,7 @@ import 'package:mem/values/paths.dart';
import 'notification/notification.dart';
import 'flutter_local_notifications_wrapper.dart';

class NotificationRepository extends KeyWithValueRepository<Notification, int>
with Discarder {
FlutterLocalNotificationsWrapper? _flutterLocalNotificationsWrapper =
defaultTargetPlatform == TargetPlatform.android
? FlutterLocalNotificationsWrapper(androidDefaultIconPath)
: null;

NotificationRepository._();

static NotificationRepository? _instance;

factory NotificationRepository() => v(
() => _instance ??= NotificationRepository._(),
{
"_instance": _instance,
},
);

static void resetSingleton() => v(
() {
FlutterLocalNotificationsWrapper.resetSingleton();
_instance?._flutterLocalNotificationsWrapper = null;
_instance = null;
},
{
'_instance': _instance,
},
);

Future<bool?> checkNotification() => v(
() async => _flutterLocalNotificationsWrapper?.handleAppLaunchDetails(),
);

@override
Future<bool> receive(Notification entity) => v(
() async {
if (await PermissionHandlerWrapper().grant(Permission.notification)) {
await _flutterLocalNotificationsWrapper?.show(
entity.key,
entity.title,
entity.body,
entity.channel,
entity.payload,
);
return true;
} else {
return false;
}
},
{
"entity": entity,
},
);

@override
Future<bool> discard(int key) => v(
() async {
_flutterLocalNotificationsWrapper?.cancel(key);

return true;
},
{
"key": key,
},
);

@override
Future<void> discardAll() => v(
() async {
await _flutterLocalNotificationsWrapper?.cancelAll();
await _flutterLocalNotificationsWrapper
?.deleteNotificationChannels(NotificationType.values.map(
(e) => e.buildNotificationChannel().id,
));
},
);
}

class NotificationRepositoryV2
class NotificationRepository
extends KeyWithValueRepositoryV2<NotificationV2, int> {
final FlutterLocalNotificationsWrapper? _flutterLocalNotificationsWrapper =
defaultTargetPlatform == TargetPlatform.android
Expand All @@ -113,6 +34,11 @@ class NotificationRepositoryV2
},
);

Future<bool?> ship() => v(
() async =>
await _flutterLocalNotificationsWrapper?.handleAppLaunchDetails(),
);

@override
Future<void> discard(int key) => v(
() async => await _flutterLocalNotificationsWrapper?.cancel(key),
Expand All @@ -131,4 +57,10 @@ class NotificationRepositoryV2
));
},
);

static void reset() => v(
() {
FlutterLocalNotificationsWrapper.resetSingleton();
},
);
}
2 changes: 0 additions & 2 deletions test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:mem/act_counter/act_counter_client.dart';
import 'package:mem/act_counter/home_widget_accessor.dart';
import 'package:mem/components/l10n.dart';
import 'package:mem/logger/logger_wrapper.dart';
import 'package:mem/notifications/notification_repository.dart';
import 'package:mem/notifications/flutter_local_notifications_wrapper.dart';
import 'package:mockito/annotations.dart';

Expand All @@ -24,7 +23,6 @@ int randomInt([int max = 42949671]) => Random().nextInt(max);
FlutterLocalNotificationsWrapper,
// FIXME RepositoryではなくTableをmockする
// Repositoryはシステム固有の処理であるのに対して、Tableは永続仮想をラップする役割を持つため
NotificationRepository,
ActCounterClient,
])
void main() {}
Expand Down

0 comments on commit 829de27

Please sign in to comment.