Skip to content

Commit

Permalink
use copy_with_gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Leptopoda committed Oct 14, 2022
1 parent 1d0f312 commit 69f80c5
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 75 deletions.
2 changes: 1 addition & 1 deletion lib/dualis/service/fake_data_dualis_scraper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class FakeDataDualisScraper implements DualisScraper {
CancellationToken cancellationToken,
) async {
await Future.delayed(const Duration(milliseconds: 200));
return Future.value(Schedule.fromList([]));
return Future.value(const Schedule());
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/dualis/service/parsing/monthly_schedule_extract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MonthlyScheduleExtract {
(ScheduleEntry e1, ScheduleEntry e2) => e1.start.compareTo(e2.start),
);

return Schedule.fromList(allEntries);
return Schedule(entries: allEntries);
}

ScheduleEntry _extractEntry(Element appointment) {
Expand Down
13 changes: 5 additions & 8 deletions lib/schedule/data/schedule_entry_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ class ScheduleEntryRepository {
],
);

final schedule = Schedule();

final entries = <ScheduleEntry>[];
for (final row in rows) {
schedule.addEntry(
ScheduleEntryEntity.fromMap(row).asScheduleEntry(),
);
entries.add(ScheduleEntryEntity.fromMap(row).asScheduleEntry());
}

return schedule;
return Schedule(entries: entries);
}

Future<ScheduleEntry?> queryExistingScheduleEntry(ScheduleEntry entry) async {
Expand Down Expand Up @@ -85,14 +82,14 @@ class ScheduleEntryRepository {
final existingEntry = await queryExistingScheduleEntry(entry);

if (existingEntry != null) {
entry.id = existingEntry.id;
entry = entry.copyWith.id(existingEntry.id);
return;
}

final row = ScheduleEntryEntity.fromModel(entry).toMap();
if (entry.id == null) {
final id = await _database.insert(ScheduleEntryEntity.tableName(), row);
entry.id = id;
entry = entry.copyWith.id(id);
} else {
await _database.update(ScheduleEntryEntity.tableName(), row);
}
Expand Down
31 changes: 12 additions & 19 deletions lib/schedule/model/schedule.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:dhbwstudentapp/schedule/model/schedule_entry.dart';

part 'schedule.g.dart';

@CopyWith()
class Schedule {
final List<ScheduleEntry> entries;
final List<String> urls = [];

Schedule() : entries = <ScheduleEntry>[];
final List<String> urls;

Schedule.fromList(this.entries);

void addEntry(ScheduleEntry entry) {
entries.add(entry);
}
const Schedule({
this.entries = const <ScheduleEntry>[],
this.urls = const <String>[],
});

void merge(Schedule schedule) {
// TODO: Return new schedule instead of adding it to the list
urls.addAll(schedule.urls);

for (final newEntry in schedule.entries) {
if (entries.any((element) => element.equalsWithIdIgnored(newEntry))) {
if (entries.any((element) => element == newEntry)) {
continue;
}

entries.add(newEntry);
}
}

// TODO: [Leptopoda] improve nullability
Schedule trim(DateTime? startDate, DateTime? endDate) {
final newList = <ScheduleEntry>[];

Expand All @@ -34,8 +36,7 @@ class Schedule {
}
}

final schedule = Schedule.fromList(newList);
schedule.urls.addAll(urls);
final schedule = Schedule(entries: newList, urls: urls);

return schedule;
}
Expand Down Expand Up @@ -105,12 +106,4 @@ class Schedule {

return latestTime;
}

Schedule copyWith({required List<ScheduleEntry> entries}) {
final schedule = Schedule.fromList(entries);

schedule.urls.addAll(urls);

return schedule;
}
}
67 changes: 67 additions & 0 deletions lib/schedule/model/schedule.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 11 additions & 33 deletions lib/schedule/model/schedule_entry.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';

part 'schedule_entry.g.dart';

enum ScheduleEntryType {
Unknown,
Class,
Expand All @@ -6,8 +11,9 @@ enum ScheduleEntryType {
Exam,
}

class ScheduleEntry {
int? id;
@CopyWith()
class ScheduleEntry extends Equatable {
final int? id;
final DateTime start;
final DateTime end;
final String title;
Expand All @@ -32,16 +38,6 @@ class ScheduleEntry {
room = room ?? "",
title = title ?? "";

bool equalsWithIdIgnored(ScheduleEntry other) {
return start == other.start &&
end == other.end &&
title == other.title &&
details == other.details &&
professor == other.professor &&
room == other.room &&
type == other.type;
}

List<String> getDifferentProperties(ScheduleEntry entry) {
final changedProperties = <String>[];

Expand Down Expand Up @@ -70,25 +66,7 @@ class ScheduleEntry {
return changedProperties;
}

// TODO: [Leptopoda] use buildrunner
ScheduleEntry copyWith({
DateTime? start,
DateTime? end,
String? title,
String? details,
String? professor,
String? room,
ScheduleEntryType? type,
}) {
return ScheduleEntry(
id: id,
start: start ?? this.start,
end: end ?? this.end,
title: title ?? this.title,
details: details ?? this.details,
professor: professor ?? this.professor,
room: room ?? this.room,
type: type ?? this.type,
);
}
@override
List<Object?> get props =>
[start, end, title, details, professor, room, type];
}
133 changes: 133 additions & 0 deletions lib/schedule/model/schedule_entry.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/schedule/service/dualis/dualis_schedule_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DualisScheduleSource extends ScheduleSource {

DateTime current = toStartOfMonth(from)!;

var schedule = Schedule();
var schedule = const Schedule();
final allErrors = <ParseError>[];

if (!_dualisScraper.isLoggedIn()) {
Expand Down
Loading

0 comments on commit 69f80c5

Please sign in to comment.