Explorar o código

fix: default include time (#2444)

* fix: default include time

* chore: clarify logic and add comments
Richard Shiue %!s(int64=2) %!d(string=hai) anos
pai
achega
5d0a349236

+ 1 - 6
frontend/appflowy_flutter/lib/plugins/database_view/application/cell/cell_data_persistence.dart

@@ -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;

+ 29 - 6
frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/date_cell/date_cal_bloc.dart

@@ -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) {