Skip to content

Commit

Permalink
replace: KeyWithValueRepository
Browse files Browse the repository at this point in the history
wip drop: `RepositoryV2`

wip #377
  • Loading branch information
zin- committed Aug 30, 2024
1 parent e533679 commit 325861d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 64 deletions.
7 changes: 4 additions & 3 deletions integration_test/scenarios/settings/settings_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void main() => group(
"Start of day",
() {
setUp(() async {
await PreferenceClient().discard(startOfDayKey);
await PreferenceClientRepository().discard(startOfDayKey);
});

testWidgets(
Expand Down Expand Up @@ -120,7 +120,8 @@ void main() => group(
timeText(zeroDate),
);
expect(
(await PreferenceClient().shipByKey(startOfDayKey)).value,
(await PreferenceClientRepository().shipByKey(startOfDayKey))
.value,
TimeOfDay.fromDateTime(zeroDate),
);
},
Expand All @@ -131,7 +132,7 @@ void main() => group(
() {
final now = DateTime.now();
setUp(() async {
await PreferenceClient().receive(Preference(
await PreferenceClientRepository().receive(PreferenceEntity(
startOfDayKey,
TimeOfDay.fromDateTime(now),
));
Expand Down
2 changes: 1 addition & 1 deletion integration_test/scenarios/task_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void testTaskScenario() => group(': $_scenarioName', () {
testWidgets(
": start is not all day, end is all day.",
(widgetTester) async {
await PreferenceClient().receive(Preference(
await PreferenceClientRepository().receive(PreferenceEntity(
startOfDayKey,
const TimeOfDay(hour: 1, minute: 0),
));
Expand Down
19 changes: 5 additions & 14 deletions lib/framework/repository/key_with_value.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import 'package:mem/framework/repository/entity.dart';

abstract class KeyWithValue<Key, Value> extends EntityV2 {
final Key key;
final Value value;

KeyWithValue(this.key, this.value);

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

// FIXME IdWithValueの方が命名として適切なのでは?
mixin KeyWithValueV2<KEY, VALUE> on Entity {
mixin KeyWithValue<KEY, VALUE> on Entity {
late final KEY key;
late final VALUE value;

@override
// TODO: implement toMap
Map<String, dynamic> get toMap => {'key': key, 'value': value};
}
13 changes: 2 additions & 11 deletions lib/framework/repository/key_with_value_repository.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import 'package:mem/framework/repository/key_with_value.dart';
import 'package:mem/framework/repository/repository.dart';

abstract class KeyWithValueRepository<E extends KeyWithValue<Key, dynamic>, Key>
extends RepositoryV2<E>
with Receiver<E, bool>, DiscarderByKey<E, Key, bool> {}

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>,
ENTITY extends KeyWithValue<KEY, dynamic>,
KEY> extends Repository<ENTITY> {
Future<void> receive(ENTITY entity);

Future<void> discard(int key);
Future<void> discard(KEY key);

Future<void> discardAll();
}
8 changes: 1 addition & 7 deletions lib/notifications/notification/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:mem/framework/repository/entity.dart';
import 'package:mem/framework/repository/key_with_value.dart';
import 'package:mem/notifications/notification/channel.dart';

class NotificationV2 with Entity, KeyWithValueV2<int, Map<String, dynamic>> {
class NotificationV2 with Entity, KeyWithValue<int, Map<String, dynamic>> {
final String title;
final String body;
final NotificationChannel channel;
Expand All @@ -29,10 +29,4 @@ class NotificationV2 with Entity, KeyWithValueV2<int, Map<String, dynamic>> {
Entity copiedWith() => throw UnimplementedError();

// coverage:ignore-end

@override
Map<String, dynamic> get toMap => {
'key': key,
'value': value,
};
}
9 changes: 5 additions & 4 deletions lib/notifications/notification_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class NotificationClient {

final ScheduleClient _scheduleClient;
final NotificationRepository _notificationRepository;
final PreferenceClient _preferenceClient;
final PreferenceClientRepository _preferenceClientRepository;
final MemRepository _memRepository;
final MemNotificationRepository _memNotificationRepository;

NotificationClient._(
this.notificationChannels,
this._scheduleClient,
this._notificationRepository,
this._preferenceClient,
this._preferenceClientRepository,
this._memRepository,
this._memNotificationRepository,
);
Expand All @@ -46,7 +46,7 @@ class NotificationClient {
NotificationChannels(buildL10n(context)),
ScheduleClient(),
NotificationRepository(),
PreferenceClient(),
PreferenceClientRepository(),
MemRepository(),
MemNotificationRepository(),
),
Expand Down Expand Up @@ -144,7 +144,8 @@ class NotificationClient {
)
.then((value) => value.singleOrNull?.toV1());
final startOfDay =
(await _preferenceClient.shipByKey(startOfDayKey)).value ??
(await _preferenceClientRepository.shipByKey(startOfDayKey))
.value ??
defaultStartOfDay;
for (var schedule in [
...mem.periodSchedules(startOfDay),
Expand Down
22 changes: 14 additions & 8 deletions lib/settings/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import 'package:mem/settings/preference.dart';
import 'package:mem/settings/client.dart';
import 'package:mem/settings/preference_key.dart';

final _client = PreferenceClient();
final _client = PreferenceClientRepository();

Future<T?> loadByKey<T>(PreferenceKey<T> key) => v(
Future<T?> loadByKey<T>(
PreferenceKey<T> key,
) =>
v(
() async => (await _client.shipByKey(key)).value,
{"key": key},
{
'key': key,
},
);

Future<bool> update<Key extends PreferenceKey<Value>, Value>(
Future<void> update<Key extends PreferenceKey<Value>, Value>(
Key key,
Value? value,
) =>
v(
() async => await (value == null
// coverage:ignore-start
? _client.discard(key)
// coverage:ignore-end
: _client.receive(Preference(key, value))),
{"key": key, "value": value},
: _client.receive(PreferenceEntity(key, value))),
{
'key': key,
'value': value,
},
);
25 changes: 11 additions & 14 deletions lib/settings/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import 'package:mem/settings/preference.dart';
import 'package:mem/settings/preference_key.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PreferenceClient
extends KeyWithValueRepository<Preference, PreferenceKey> {
Future<Preference<T>> shipByKey<T>(PreferenceKey<T> key) => v(
class PreferenceClientRepository
extends KeyWithValueRepositoryV2<PreferenceEntity, PreferenceKey> {
Future<PreferenceEntity<T>> shipByKey<T>(PreferenceKey<T> key) => v(
() async {
final saved = (await SharedPreferences.getInstance()).get(key.value);

return Preference(
return PreferenceEntity(
key,
key.deserialize(saved),
);
},
key,
{'key': key},
);

@override
Future<bool> receive(Preference entity) => v(
Future<bool> receive(PreferenceEntity entity) => v(
() async {
final serialized = entity.key.serialize(entity.value);

Expand All @@ -34,18 +34,15 @@ class PreferenceClient
throw UnimplementedError(); // coverage:ignore-line
}
},
entity,
{'entity': entity},
);

@override
Future<bool> discard(PreferenceKey key) => v(
Future<void> discard(PreferenceKey key) => v(
() async => (await SharedPreferences.getInstance()).remove(key.value),
key,
{'key': key},
);

PreferenceClient._();

static PreferenceClient? _instance;

factory PreferenceClient() => _instance ??= PreferenceClient._();
@override
Future<void> discardAll() => throw UnimplementedError();
}
15 changes: 13 additions & 2 deletions lib/settings/preference.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import 'package:mem/framework/repository/entity.dart';
import 'package:mem/framework/repository/key_with_value.dart';
import 'package:mem/settings/preference_key.dart';

class Preference<T> extends KeyWithValue<PreferenceKey<T>, T?> {
Preference(super.key, super.value);
class PreferenceEntity<VALUE>
with Entity, KeyWithValue<PreferenceKey<VALUE>, VALUE?> {
PreferenceEntity(PreferenceKey<VALUE> key, VALUE? value) {
this.key = key;
this.value = value;
}

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

// coverage:ignore-end
}

0 comments on commit 325861d

Please sign in to comment.