Skip to content

Commit

Permalink
Replace CalendricalEventType with EventType enum. (#1317)
Browse files Browse the repository at this point in the history
`CalendricalEventType` had subclasses that we never used and also
creating Enums with members wasn't possible back then.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced an `EventType` enum to standardize event types across the
app, including 'meeting' and 'exam'.
- **Refactor**
- Updated various components to use the new `EventType` enum for better
consistency and readability.
	- Removed obsolete methods and enums related to event type handling.
- **Tests**
	- Adjusted tests to align with the new `EventType` enum usage.
- **Chores**
- Code clean-up including import reordering and removing unused code
blocks.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
Jonas-Sander authored Feb 27, 2024
1 parent 891b610 commit de1193b
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 223 deletions.
28 changes: 23 additions & 5 deletions app/lib/calendrical_events/models/calendrical_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@ import 'package:sharezone/timetable/src/models/lesson.dart';
import 'package:sharezone/timetable/src/models/lesson_length/lesson_length.dart';
import 'package:time/time.dart';

import 'calendrical_event_types.dart';
enum EventType {
event('meeting'),
exam('exam');

/// Database key for the event type.
final String key;

const EventType(this.key);

static EventType fromString(String s) {
switch (s) {
case 'meeting':
return event;
case 'exam':
return exam;
}
throw ArgumentError("Couldn't parse $EventType from unkown event type: $s");
}
}

class CalendricalEvent {
/// The date and time when the event was created.
Expand All @@ -25,7 +43,7 @@ class CalendricalEvent {

final String eventID, groupID, authorID;
final GroupType groupType;
final CalendricalEventType eventType;
final EventType eventType;
final Date date;
final Time startTime, endTime;
final String title;
Expand Down Expand Up @@ -64,7 +82,7 @@ class CalendricalEvent {
endTime: Time.parse(data['endTime'] as String),
title: data['title'] as String,
groupType: GroupType.values.byName(data['groupType'] as String),
eventType: getEventTypeFromString(data['eventType'] as String),
eventType: EventType.fromString(data['eventType'] as String),
detail: data['detail'] as String?,
place: data['place'] as String?,
sendNotification: (data['sendNotification'] as bool?) ?? false,
Expand All @@ -77,7 +95,7 @@ class CalendricalEvent {
'createdOn': createdOn,
'groupID': groupID,
'groupType': groupType.name,
'eventType': getEventTypeToString(eventType),
'eventType': eventType.key,
'authorID': authorID,
'date': date.toDateString,
'startTime': startTime.time,
Expand All @@ -95,7 +113,7 @@ class CalendricalEvent {
String? groupID,
String? authorID,
GroupType? groupType,
CalendricalEventType? eventType,
EventType? eventType,
Date? date,
Time? startTime,
Time? endTime,
Expand Down
102 changes: 0 additions & 102 deletions app/lib/calendrical_events/models/calendrical_event_types.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:flutter/material.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event_types.dart';
import 'package:sharezone/calendrical_events/provider/past_calendrical_events_page_controller.dart';
import 'package:sharezone/calendrical_events/provider/past_calendrical_events_page_controller_factory.dart';
import 'package:sharezone/sharezone_plus/page/sharezone_plus_page.dart';
Expand Down Expand Up @@ -176,7 +175,7 @@ class _SharezonePlusAd extends StatelessWidget {
detail: null,
endTime: Time(hour: 12, minute: 0),
eventID: 'eventId',
eventType: Exam(),
eventType: EventType.exam,
groupType: GroupType.course,
latestEditor: null,
place: null,
Expand Down
5 changes: 2 additions & 3 deletions app/lib/timetable/src/widgets/events/calender_event_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

import 'package:bloc_provider/bloc_provider.dart';
import 'package:flutter/material.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event_types.dart';
import 'package:sharezone/groups/src/pages/course/course_edit/design/course_edit_design.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/report/page/report_page.dart';
import 'package:sharezone/report/report_icon.dart';
import 'package:sharezone/report/report_item.dart';
Expand Down Expand Up @@ -112,7 +111,7 @@ Future<void> onEventLongPress(
final isAuthor = api.uID == event.authorID;
final hasPermissionsToManageEvents = hasPermissionToManageEvents(
api.course.getRoleFromCourseNoSync(event.groupID)!, isAuthor);
final isExam = event.eventType == Exam();
final isExam = event.eventType == EventType.exam;
final result = await showLongPressAdaptiveDialog<_EventLongPressResult>(
context: context,
title: "${isExam ? "Prüfung" : "Termin"}: ${event.title}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import 'package:date/date.dart';
import 'package:equatable/equatable.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event_types.dart';
import 'package:sharezone/util/api.dart';
import 'package:time/time.dart';

export 'package:sharezone/calendrical_events/models/calendrical_event.dart'
show EventType;

class EventDialogApi {
final SharezoneGateway _api;

Expand All @@ -33,10 +35,7 @@ class EventDialogApi {
createdOn: null,
groupID: command.courseId.id,
groupType: GroupType.course,
eventType: switch (command.eventType) {
EventType.event => Meeting(),
EventType.exam => Exam(),
},
eventType: command.eventType,
date: command.date,
place: command.location,
startTime: command.startTime,
Expand All @@ -52,8 +51,6 @@ class EventDialogApi {
}
}

enum EventType { event, exam }

class CreateEventCommand extends Equatable {
final String title;
final CourseId courseId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import 'package:bloc_provider/bloc_provider.dart';
import 'package:date/date.dart';
import 'package:flutter/material.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event_types.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/markdown/markdown_analytics.dart';
import 'package:sharezone/markdown/markdown_support.dart';
import 'package:sharezone/timetable/src/edit_date.dart';
Expand Down Expand Up @@ -90,7 +89,7 @@ class _TimetableEditEventPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
final isExam = initialEvent.eventType == Exam();
final isExam = initialEvent.eventType == EventType.exam;
return PopScope(
canPop: false,
onPopInvoked: (didPop) async {
Expand Down Expand Up @@ -354,7 +353,7 @@ class _DetailField extends StatelessWidget {

@override
Widget build(BuildContext context) {
final isExam = initialEvent.eventType == Exam();
final isExam = initialEvent.eventType == EventType.exam;
final bloc = BlocProvider.of<TimetableEditEventBloc>(context);
return Padding(
padding: const EdgeInsets.only(top: 10),
Expand Down
12 changes: 3 additions & 9 deletions app/lib/timetable/timetable_page/timetable_event_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import 'package:design/design.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:platform_check/platform_check.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event_types.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/report/page/report_page.dart';
import 'package:sharezone/report/report_icon.dart';
import 'package:sharezone/report/report_item.dart';
Expand All @@ -26,7 +26,6 @@ import 'package:sharezone/timetable/timetable_edit/event/timetable_event_edit_pa
import 'package:sharezone/timetable/timetable_permissions.dart';
import 'package:sharezone/util/launch_link.dart';
import 'package:sharezone/util/navigation_service.dart';
import 'package:platform_check/platform_check.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';

enum _EventModelSheetAction { edit, delete, report }
Expand Down Expand Up @@ -152,7 +151,7 @@ class _TimetableEventDetailsPage extends StatelessWidget {
final isAuthor = api.uID == event.authorID;
final hasPermissionsToManageLessons = hasPermissionToManageEvents(
api.course.getRoleFromCourseNoSync(event.groupID)!, isAuthor);
final isExam = event.eventType == Exam();
final isExam = event.eventType == EventType.exam;
final theme = Theme.of(context);

return Scaffold(
Expand Down Expand Up @@ -198,11 +197,6 @@ class _TimetableEventDetailsPage extends StatelessWidget {
),
),
),
if (event.eventType is OtherEventType == false)
ListTile(
leading: const Icon(Icons.label),
title: Text("Art: ${event.eventType.name}"),
),
ListTile(
leading: const Icon(Icons.place),
title: Text("Raum: ${event.place ?? "-"}"),
Expand Down
3 changes: 1 addition & 2 deletions app/test/timetable/timetable_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import 'package:date/weektype.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:group_domain_models/group_domain_models.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event.dart';
import 'package:sharezone/calendrical_events/models/calendrical_event_types.dart';
import 'package:sharezone/timetable/src/bloc/timetable_bloc.dart';
import 'package:sharezone/timetable/src/models/lesson.dart';
import 'package:sharezone/timetable/timetable_page/school_class_filter/school_class_filter_view.dart';
Expand Down Expand Up @@ -73,7 +72,7 @@ void main() {
date: Date.today(),
detail: '',
eventID: 'eventId',
eventType: Meeting(),
eventType: EventType.event,
latestEditor: '',
sendNotification: false,
title: 'title',
Expand Down
Loading

0 comments on commit de1193b

Please sign in to comment.