Skip to content

Commit 6deff8f

Browse files
committed
Convert api to dart_mappable
1 parent 458b586 commit 6deff8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2760
-5009
lines changed

api/lib/converters/ical.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ICalConverter {
7070
break;
7171
case 'BEGIN':
7272
if (value == 'VEVENT') {
73-
currentItem = CalendarItem.fixed(
73+
currentItem = FixedCalendarItem(
7474
eventId: currentEvent.id,
7575
);
7676
} else if (value == 'VTODO') {

api/lib/helpers/converter.dart

-33
This file was deleted.

api/lib/helpers/mapper.dart

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:dart_leap/dart_leap.dart';
2+
import 'package:dart_mappable/dart_mappable.dart';
3+
4+
class SecondsDateTimeMapper extends SimpleMapper<DateTime> {
5+
const SecondsDateTimeMapper();
6+
7+
@override
8+
DateTime decode(Object value) {
9+
if (value is int) {
10+
return DateTimeHelper.fromSecondsSinceEpoch(value);
11+
}
12+
return DateTime.now();
13+
}
14+
15+
@override
16+
Object? encode(DateTime? self) {
17+
return self?.secondsSinceEpoch;
18+
}
19+
}

api/lib/helpers/setup.dart

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'package:dart_mappable/dart_mappable.dart';
2+
import 'package:flow_api/helpers/mapper.dart';
3+
4+
void setupAPI() {
5+
MapperContainer.globals.use(SecondsDateTimeMapper());
6+
}

api/lib/models/cached.dart

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
import 'package:freezed_annotation/freezed_annotation.dart';
1+
import 'package:dart_mappable/dart_mappable.dart';
22

33
import 'event/item/model.dart';
44
import 'event/model.dart';
55
import 'note/model.dart';
66

7-
part 'cached.freezed.dart';
8-
part 'cached.g.dart';
7+
part 'cached.mapper.dart';
98

10-
@freezed
11-
class CachedData with _$CachedData {
12-
const CachedData._();
9+
class CachedData with CachedDataMappable {
10+
final DateTime? lastUpdated;
11+
final List<Event> events;
12+
final List<Notebook> notebooks;
13+
final List<CalendarItem> items;
14+
final List<Note> notes;
1315

14-
const factory CachedData({
15-
DateTime? lastUpdated,
16-
@Default([]) List<Event> events,
17-
@Default([]) List<Notebook> notebooks,
18-
@Default([]) List<CalendarItem> items,
19-
@Default([]) List<Note> notes,
20-
}) = _CachedData;
21-
22-
factory CachedData.fromJson(Map<String, dynamic> json) =>
23-
_$CachedDataFromJson(json);
16+
const CachedData({
17+
this.lastUpdated,
18+
this.events = const [],
19+
this.notebooks = const [],
20+
this.items = const [],
21+
this.notes = const [],
22+
});
2423

2524
CachedData concat(CachedData other) {
2625
return CachedData(

api/lib/models/event/item/model.dart

+107-62
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,42 @@
11
import 'dart:typed_data';
22

3-
import 'package:freezed_annotation/freezed_annotation.dart';
3+
import 'package:dart_mappable/dart_mappable.dart';
44

5-
import '../../../helpers/converter.dart';
65
import '../../model.dart';
76
import '../model.dart';
87

9-
part 'model.freezed.dart';
10-
part 'model.g.dart';
8+
part 'model.mapper.dart';
119

10+
@MappableEnum()
1211
enum CalendarItemType { appointment, moment, pending }
1312

14-
@freezed
15-
class CalendarItem
16-
with _$CalendarItem, IdentifiedModel, NamedModel, DescriptiveModel {
17-
const CalendarItem._();
13+
@MappableClass()
14+
sealed class CalendarItem
15+
with CalendarItemMappable, IdentifiedModel, NamedModel, DescriptiveModel {
16+
@override
17+
final Uint8List? id;
18+
@override
19+
final String name, description;
20+
final String location;
21+
final Uint8List? groupId, placeId, eventId;
22+
final DateTime? start, end;
23+
final EventStatus status;
1824

19-
const factory CalendarItem.fixed({
20-
@Uint8ListConverter() Uint8List? id,
21-
@Default('') String name,
22-
@Default('') String description,
23-
@Default('') String location,
24-
@Uint8ListConverter() Uint8List? groupId,
25-
@Uint8ListConverter() Uint8List? placeId,
26-
@Uint8ListConverter() Uint8List? eventId,
27-
@Default(EventStatus.confirmed) EventStatus status,
28-
@DateTimeConverter() DateTime? start,
29-
@DateTimeConverter() DateTime? end,
30-
}) = FixedCalendarItem;
31-
32-
const factory CalendarItem.repeating({
33-
@Uint8ListConverter() Uint8List? id,
34-
@Default('') String name,
35-
@Default('') String description,
36-
@Default('') String location,
37-
@Uint8ListConverter() Uint8List? groupId,
38-
@Uint8ListConverter() Uint8List? placeId,
39-
@Uint8ListConverter() Uint8List? eventId,
40-
@Default(EventStatus.confirmed) EventStatus status,
41-
@DateTimeConverter() DateTime? start,
42-
@DateTimeConverter() DateTime? end,
43-
@Default(RepeatType.daily) RepeatType repeatType,
44-
@Default(1) int interval,
45-
@Default(0) int variation,
46-
@Default(0) int count,
47-
@DateTimeConverter() DateTime? until,
48-
@Default([]) List<int> exceptions,
49-
}) = RepeatingCalendarItem;
50-
51-
const factory CalendarItem.auto({
52-
@Uint8ListConverter() Uint8List? id,
53-
@Default('') String name,
54-
@Default('') String description,
55-
@Default('') String location,
56-
@Uint8ListConverter() Uint8List? groupId,
57-
@Uint8ListConverter() Uint8List? placeId,
58-
@Uint8ListConverter() Uint8List? eventId,
59-
@Default(EventStatus.confirmed) EventStatus status,
60-
@DateTimeConverter() DateTime? start,
61-
@DateTimeConverter() DateTime? end,
62-
@Uint8ListConverter() Uint8List? autoGroupId,
63-
@DateTimeConverter() DateTime? searchStart,
64-
@Default(60) int autoDuration,
65-
}) = AutoCalendarItem;
66-
67-
factory CalendarItem.fromJson(Map<String, dynamic> json) =>
68-
_$CalendarItemFromJson(json);
25+
const CalendarItem({
26+
this.id,
27+
this.name = '',
28+
this.description = '',
29+
this.location = '',
30+
this.groupId,
31+
this.placeId,
32+
this.eventId,
33+
this.start,
34+
this.end,
35+
this.status = EventStatus.confirmed,
36+
});
6937

7038
factory CalendarItem.fromDatabase(Map<String, dynamic> row) =>
71-
CalendarItem.fromJson({
72-
...row,
73-
});
39+
CalendarItemMapper.fromMap(row);
7440

7541
CalendarItemType get type {
7642
if (start == null && end == null) {
@@ -88,6 +54,85 @@ class CalendarItem
8854
}
8955

9056
Map<String, dynamic> toDatabase() => {
91-
...toJson(),
57+
...toMap(),
9258
};
9359
}
60+
61+
@MappableClass()
62+
final class FixedCalendarItem extends CalendarItem
63+
with FixedCalendarItemMappable {
64+
const FixedCalendarItem({
65+
super.id,
66+
super.name,
67+
super.description,
68+
super.location,
69+
super.groupId,
70+
super.placeId,
71+
super.eventId,
72+
super.start,
73+
super.end,
74+
super.status,
75+
});
76+
}
77+
78+
@MappableClass()
79+
final class RepeatingCalendarItem extends CalendarItem
80+
with RepeatingCalendarItemMappable {
81+
final RepeatType repeatType;
82+
final int interval, variation, count;
83+
final DateTime? until;
84+
final List<int> exceptions;
85+
86+
const RepeatingCalendarItem({
87+
super.id,
88+
super.name,
89+
super.description,
90+
super.location,
91+
super.groupId,
92+
super.placeId,
93+
super.eventId,
94+
super.start,
95+
super.end,
96+
super.status,
97+
this.repeatType = RepeatType.daily,
98+
this.interval = 1,
99+
this.variation = 0,
100+
this.count = 0,
101+
this.until,
102+
this.exceptions = const [],
103+
});
104+
}
105+
106+
@MappableClass()
107+
final class AutoCalendarItem extends CalendarItem
108+
with AutoCalendarItemMappable {
109+
final RepeatType repeatType;
110+
final int interval, variation, count;
111+
final DateTime? until;
112+
final List<int> exceptions;
113+
final Uint8List? autoGroupId;
114+
final DateTime? searchStart;
115+
final int autoDuration;
116+
117+
const AutoCalendarItem({
118+
super.id,
119+
super.name,
120+
super.description,
121+
super.location,
122+
super.groupId,
123+
super.placeId,
124+
super.eventId,
125+
super.status,
126+
super.start,
127+
super.end,
128+
this.repeatType = RepeatType.daily,
129+
this.interval = 1,
130+
this.variation = 0,
131+
this.count = 0,
132+
this.until,
133+
this.exceptions = const [],
134+
this.autoGroupId,
135+
this.searchStart,
136+
this.autoDuration = 60,
137+
});
138+
}

0 commit comments

Comments
 (0)