Skip to content

Commit

Permalink
Convert api to dart_mappable
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 23, 2024
1 parent 458b586 commit 6deff8f
Show file tree
Hide file tree
Showing 52 changed files with 2,760 additions and 5,009 deletions.
2 changes: 1 addition & 1 deletion api/lib/converters/ical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ICalConverter {
break;
case 'BEGIN':
if (value == 'VEVENT') {
currentItem = CalendarItem.fixed(
currentItem = FixedCalendarItem(
eventId: currentEvent.id,
);
} else if (value == 'VTODO') {
Expand Down
33 changes: 0 additions & 33 deletions api/lib/helpers/converter.dart

This file was deleted.

19 changes: 19 additions & 0 deletions api/lib/helpers/mapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:dart_leap/dart_leap.dart';
import 'package:dart_mappable/dart_mappable.dart';

class SecondsDateTimeMapper extends SimpleMapper<DateTime> {
const SecondsDateTimeMapper();

@override
DateTime decode(Object value) {
if (value is int) {
return DateTimeHelper.fromSecondsSinceEpoch(value);
}
return DateTime.now();
}

@override
Object? encode(DateTime? self) {
return self?.secondsSinceEpoch;
}
}
6 changes: 6 additions & 0 deletions api/lib/helpers/setup.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:dart_mappable/dart_mappable.dart';
import 'package:flow_api/helpers/mapper.dart';

void setupAPI() {
MapperContainer.globals.use(SecondsDateTimeMapper());
}
31 changes: 15 additions & 16 deletions api/lib/models/cached.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:dart_mappable/dart_mappable.dart';

import 'event/item/model.dart';
import 'event/model.dart';
import 'note/model.dart';

part 'cached.freezed.dart';
part 'cached.g.dart';
part 'cached.mapper.dart';

@freezed
class CachedData with _$CachedData {
const CachedData._();
class CachedData with CachedDataMappable {
final DateTime? lastUpdated;
final List<Event> events;
final List<Notebook> notebooks;
final List<CalendarItem> items;
final List<Note> notes;

const factory CachedData({
DateTime? lastUpdated,
@Default([]) List<Event> events,
@Default([]) List<Notebook> notebooks,
@Default([]) List<CalendarItem> items,
@Default([]) List<Note> notes,
}) = _CachedData;

factory CachedData.fromJson(Map<String, dynamic> json) =>
_$CachedDataFromJson(json);
const CachedData({
this.lastUpdated,
this.events = const [],
this.notebooks = const [],
this.items = const [],
this.notes = const [],
});

CachedData concat(CachedData other) {
return CachedData(
Expand Down
169 changes: 107 additions & 62 deletions api/lib/models/event/item/model.dart
Original file line number Diff line number Diff line change
@@ -1,76 +1,42 @@
import 'dart:typed_data';

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:dart_mappable/dart_mappable.dart';

import '../../../helpers/converter.dart';
import '../../model.dart';
import '../model.dart';

part 'model.freezed.dart';
part 'model.g.dart';
part 'model.mapper.dart';

@MappableEnum()
enum CalendarItemType { appointment, moment, pending }

@freezed
class CalendarItem
with _$CalendarItem, IdentifiedModel, NamedModel, DescriptiveModel {
const CalendarItem._();
@MappableClass()
sealed class CalendarItem
with CalendarItemMappable, IdentifiedModel, NamedModel, DescriptiveModel {
@override
final Uint8List? id;
@override
final String name, description;
final String location;
final Uint8List? groupId, placeId, eventId;
final DateTime? start, end;
final EventStatus status;

const factory CalendarItem.fixed({
@Uint8ListConverter() Uint8List? id,
@Default('') String name,
@Default('') String description,
@Default('') String location,
@Uint8ListConverter() Uint8List? groupId,
@Uint8ListConverter() Uint8List? placeId,
@Uint8ListConverter() Uint8List? eventId,
@Default(EventStatus.confirmed) EventStatus status,
@DateTimeConverter() DateTime? start,
@DateTimeConverter() DateTime? end,
}) = FixedCalendarItem;

const factory CalendarItem.repeating({
@Uint8ListConverter() Uint8List? id,
@Default('') String name,
@Default('') String description,
@Default('') String location,
@Uint8ListConverter() Uint8List? groupId,
@Uint8ListConverter() Uint8List? placeId,
@Uint8ListConverter() Uint8List? eventId,
@Default(EventStatus.confirmed) EventStatus status,
@DateTimeConverter() DateTime? start,
@DateTimeConverter() DateTime? end,
@Default(RepeatType.daily) RepeatType repeatType,
@Default(1) int interval,
@Default(0) int variation,
@Default(0) int count,
@DateTimeConverter() DateTime? until,
@Default([]) List<int> exceptions,
}) = RepeatingCalendarItem;

const factory CalendarItem.auto({
@Uint8ListConverter() Uint8List? id,
@Default('') String name,
@Default('') String description,
@Default('') String location,
@Uint8ListConverter() Uint8List? groupId,
@Uint8ListConverter() Uint8List? placeId,
@Uint8ListConverter() Uint8List? eventId,
@Default(EventStatus.confirmed) EventStatus status,
@DateTimeConverter() DateTime? start,
@DateTimeConverter() DateTime? end,
@Uint8ListConverter() Uint8List? autoGroupId,
@DateTimeConverter() DateTime? searchStart,
@Default(60) int autoDuration,
}) = AutoCalendarItem;

factory CalendarItem.fromJson(Map<String, dynamic> json) =>
_$CalendarItemFromJson(json);
const CalendarItem({
this.id,
this.name = '',
this.description = '',
this.location = '',
this.groupId,
this.placeId,
this.eventId,
this.start,
this.end,
this.status = EventStatus.confirmed,
});

factory CalendarItem.fromDatabase(Map<String, dynamic> row) =>
CalendarItem.fromJson({
...row,
});
CalendarItemMapper.fromMap(row);

CalendarItemType get type {
if (start == null && end == null) {
Expand All @@ -88,6 +54,85 @@ class CalendarItem
}

Map<String, dynamic> toDatabase() => {
...toJson(),
...toMap(),
};
}

@MappableClass()
final class FixedCalendarItem extends CalendarItem
with FixedCalendarItemMappable {
const FixedCalendarItem({
super.id,
super.name,
super.description,
super.location,
super.groupId,
super.placeId,
super.eventId,
super.start,
super.end,
super.status,
});
}

@MappableClass()
final class RepeatingCalendarItem extends CalendarItem
with RepeatingCalendarItemMappable {
final RepeatType repeatType;
final int interval, variation, count;
final DateTime? until;
final List<int> exceptions;

const RepeatingCalendarItem({
super.id,
super.name,
super.description,
super.location,
super.groupId,
super.placeId,
super.eventId,
super.start,
super.end,
super.status,
this.repeatType = RepeatType.daily,
this.interval = 1,
this.variation = 0,
this.count = 0,
this.until,
this.exceptions = const [],
});
}

@MappableClass()
final class AutoCalendarItem extends CalendarItem
with AutoCalendarItemMappable {
final RepeatType repeatType;
final int interval, variation, count;
final DateTime? until;
final List<int> exceptions;
final Uint8List? autoGroupId;
final DateTime? searchStart;
final int autoDuration;

const AutoCalendarItem({
super.id,
super.name,
super.description,
super.location,
super.groupId,
super.placeId,
super.eventId,
super.status,
super.start,
super.end,
this.repeatType = RepeatType.daily,
this.interval = 1,
this.variation = 0,
this.count = 0,
this.until,
this.exceptions = const [],
this.autoGroupId,
this.searchStart,
this.autoDuration = 60,
});
}
Loading

0 comments on commit 6deff8f

Please sign in to comment.