Skip to content

Commit

Permalink
fix: event date logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mthinh committed Oct 10, 2024
1 parent 87f91cf commit 6b4666c
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class CreateEventBloc extends Bloc<CreateEventEvent, CreateEventState> {
description: state.description,
private: state.private,
approval_required: state.approvalRequired,
start: DateTime.parse(event.start.toIso8601String()),
end: DateTime.parse(event.end.toIso8601String()),
start: event.start.toUtc(),
end: event.end.toUtc(),
timezone: event.timezone,
guest_limit:
state.guestLimit != null ? double.parse(state.guestLimit!) : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:formz/formz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import 'package:app/core/utils/date_utils.dart' as date_utils;
import 'package:timezone/timezone.dart';
import 'package:timezone/timezone.dart' as tz;

part 'event_datetime_settings_bloc.freezed.dart';

Expand All @@ -32,10 +32,13 @@ class EventDateTimeSettingsBloc
emit(state.copyWith(status: FormzSubmissionStatus.initial));
final finalTimezone =
event.timezone ?? date_utils.DateUtils.getUserTimezoneOptionValue();
final location = tz.getLocation(finalTimezone);
final start = tz.TZDateTime.from(event.startDateTime, location);
final end = tz.TZDateTime.from(event.endDateTime, location);
emit(
state.copyWith(
start: DateTimeFormz.dirty(event.startDateTime),
end: DateTimeFormz.dirty(event.endDateTime),
start: DateTimeFormz.dirty(start),
end: DateTimeFormz.dirty(end),
timezone: finalTimezone,
),
);
Expand All @@ -48,16 +51,7 @@ class EventDateTimeSettingsBloc
emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
final newStart = event.newStart;
final newEnd = event.newEnd;
final location = getLocation(state.timezone ?? '');
final startUtcDateTime = newStart
.add(Duration(milliseconds: location.currentTimeZone.offset * -1));
final endUtcDateTime = newEnd
.add(Duration(milliseconds: location.currentTimeZone.offset * -1));

final now = DateTime.now();
final tomorrowMidnight = DateTime.utc(now.year, now.month, now.day + 1);
if (newStart.isBefore(tomorrowMidnight) ||
newEnd.isBefore(tomorrowMidnight)) {
if (newStart.isBefore(DateTime.now())) {
emit(
state.copyWith(
start: DateTimeFormz.dirty(newStart),
Expand All @@ -82,8 +76,8 @@ class EventDateTimeSettingsBloc
if (event.event != null) {
final result = await eventRepository.updateEvent(
input: Input$EventInput(
start: DateTime.parse(startUtcDateTime.toIso8601String()),
end: DateTime.parse(endUtcDateTime.toIso8601String()),
start: newStart.toUtc(),
end: newEnd.toUtc(),
),
id: event.event?.id ?? '',
);
Expand Down Expand Up @@ -117,18 +111,30 @@ class EventDateTimeSettingsBloc
Emitter emit,
) async {
emit(state.copyWith(status: FormzSubmissionStatus.inProgress));
final location = tz.getLocation(event.timezone);
final newStart = tz.TZDateTime(
location,
state.start.value!.year,
state.start.value!.month,
state.start.value!.day,
state.start.value!.hour,
state.start.value!.minute,
);

final newEnd = tz.TZDateTime(
location,
state.end.value!.year,
state.end.value!.month,
state.end.value!.day,
state.end.value!.hour,
state.end.value!.minute,
);
// Edit mode
if (event.event != null) {
final location = getLocation(event.timezone);
final startUtcDateTime = state.start.value!
.add(Duration(milliseconds: location.currentTimeZone.offset * -1));
final endUtcDateTime = state.end.value!
.add(Duration(milliseconds: location.currentTimeZone.offset * -1));

final result = await eventRepository.updateEvent(
input: Input$EventInput(
start: DateTime.parse(startUtcDateTime.toIso8601String()),
end: DateTime.parse(endUtcDateTime.toIso8601String()),
start: newStart.toUtc(),
end: newEnd.toUtc(),
timezone: event.timezone,
),
id: event.event?.id ?? '',
Expand All @@ -147,6 +153,8 @@ class EventDateTimeSettingsBloc
} else {
emit(
state.copyWith(
start: DateTimeFormz.dirty(newStart),
end: DateTimeFormz.dirty(newEnd),
timezone: event.timezone,
isValid: true,
status: FormzSubmissionStatus.success,
Expand Down
33 changes: 20 additions & 13 deletions lib/core/constants/event/event_constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:timezone/timezone.dart' as tz;
import 'package:app/core/utils/date_utils.dart' as date_utils;

class EventConstants {
/// Guest limit per 2
Expand Down Expand Up @@ -287,19 +288,25 @@ class EventConstants {
}

class EventDateTimeConstants {
static final DateTime currentDateTime = DateTime.now();
static DateTime get currentDateTime => tz.TZDateTime.now(
tz.getLocation(date_utils.DateUtils.getUserTimezoneOptionValue()),
);

static final DateTime defaultStartDateTime = DateTime.utc(
currentDateTime.year,
currentDateTime.month,
currentDateTime.day + 3,
10,
);
static DateTime get defaultStartDateTime {
return DateTime(
currentDateTime.year,
currentDateTime.month,
currentDateTime.day + 3,
10,
);
}

static final DateTime defaultEndDateTime = DateTime.utc(
currentDateTime.year,
currentDateTime.month,
currentDateTime.day + 6,
18,
);
static DateTime get defaultEndDateTime {
return DateTime(
currentDateTime.year,
currentDateTime.month,
currentDateTime.day + 6,
18,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:app/core/application/event/event_location_setting_bloc/event_loc
import 'package:app/core/constants/event/event_constants.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import 'package:flutter_bloc/flutter_bloc.dart';

@RoutePage()
Expand Down
Loading

0 comments on commit 6b4666c

Please sign in to comment.