fix: default include time (#2444)

* fix: default include time

* chore: clarify logic and add comments
This commit is contained in:
Richard Shiue 2023-05-04 12:41:48 +08:00 committed by GitHub
parent db8d030a85
commit 5d0a349236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 12 deletions

View File

@ -45,12 +45,7 @@ class DateCellDataPersistence implements CellDataPersistence<DateCellData> {
Future<Option<FlowyError>> save(DateCellData data) {
var payload = DateChangesetPB.create()..cellPath = _makeCellPath(cellId);
// This is a bit of a hack. This converts the data.date which is in
// UTC to Local but actually changes the timestamp instead of just
// changing the isUtc flag
final dateTime = DateTime(data.date.year, data.date.month, data.date.day);
final date = (dateTime.millisecondsSinceEpoch ~/ 1000).toString();
final date = (data.date.millisecondsSinceEpoch ~/ 1000).toString();
payload.date = date;
payload.isUtc = data.date.isUtc;
payload.includeTime = data.includeTime;

View File

@ -84,15 +84,23 @@ class DateCellCalendarBloc
bool? includeTime,
}) {
final DateCellData newDateData = state.dateCellData.fold(
() => DateCellData(
date: date ?? DateTime.now(),
time: time,
includeTime: includeTime ?? false,
),
() {
DateTime newDate = DateTime.now();
if (date != null) {
newDate = _utcToLocalAddTime(date);
}
return DateCellData(
date: newDate,
time: time,
includeTime: includeTime ?? false,
);
},
(dateData) {
var newDateData = dateData;
if (date != null && !isSameDay(newDateData.date, date)) {
newDateData = newDateData.copyWith(date: date);
newDateData = newDateData.copyWith(
date: _utcToLocalAddTime(date),
);
}
if (newDateData.time != time) {
@ -151,6 +159,21 @@ class DateCellCalendarBloc
);
}
DateTime _utcToLocalAddTime(DateTime date) {
final now = DateTime.now();
// the incoming date is Utc. this trick converts it into Local
// and add the current time, though
// the time may be overwritten by explicitly provided time string
return DateTime(
date.year,
date.month,
date.day,
now.hour,
now.minute,
now.second,
);
}
String timeFormatPrompt(FlowyError error) {
String msg = "${LocaleKeys.grid_field_invalidTimeFormat.tr()}.";
switch (state.dateTypeOptionPB.timeFormat) {