diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_persistence.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_persistence.dart index 5aec905007..b6a615a1b0 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_persistence.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/cell/cell_service/data_persistence.dart @@ -27,13 +27,9 @@ class CellDataPersistence implements _GridCellDataPersistence { } } -class DateCellPersistenceData { - final DateTime date; - final String? time; - DateCellPersistenceData({ - required this.date, - this.time, - }); +@freezed +class DateCellPersistenceData with _$DateCellPersistenceData { + const factory DateCellPersistenceData({required DateTime date, String? time}) = _DateCellPersistenceData; } class NumberCellDataPersistence implements _GridCellDataPersistence { diff --git a/frontend/app_flowy/lib/workspace/application/grid/cell/date_cal_bloc.dart b/frontend/app_flowy/lib/workspace/application/grid/cell/date_cal_bloc.dart index 92c9a03243..0f980d4a98 100644 --- a/frontend/app_flowy/lib/workspace/application/grid/cell/date_cal_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/grid/cell/date_cal_bloc.dart @@ -9,7 +9,6 @@ import 'package:table_calendar/table_calendar.dart'; import 'dart:async'; import 'cell_service/cell_service.dart'; import 'package:dartz/dartz.dart'; -import 'package:fixnum/fixnum.dart' as $fixnum; import 'package:protobuf/protobuf.dart'; part 'date_cal_bloc.freezed.dart'; @@ -30,9 +29,13 @@ class DateCalBloc extends Bloc { // await _loadDateTypeOption(emit); }, selectDay: (_SelectDay value) { - if (!isSameDay(state.selectedDay, value.day)) { - _updateCellData(value.day); - emit(state.copyWith(selectedDay: value.day)); + if (state.dateData != null) { + if (!isSameDay(state.dateData!.date, value.day)) { + final newDateData = state.dateData!.copyWith(date: value.day); + emit(state.copyWith(dateData: newDateData)); + } + } else { + emit(state.copyWith(dateData: DateCellPersistenceData(date: value.day))); } }, setCalFormat: (_CalendarFormat value) { @@ -52,7 +55,12 @@ class DateCalBloc extends Bloc { await _updateTypeOption(emit, timeFormat: value.timeFormat); }, setTime: (_Time value) { - // + if (state.dateData != null) { + final newDateData = state.dateData!.copyWith(time: value.time); + emit(state.copyWith(dateData: newDateData)); + } else { + emit(state.copyWith(dateData: DateCellPersistenceData(date: DateTime.now(), time: value.time))); + } }, ); }, @@ -79,33 +87,6 @@ class DateCalBloc extends Bloc { ); } - // ignore: unused_element - Future _loadDateTypeOption(Emitter emit) async { - final result = await cellContext.getTypeOptionData(); - result.fold( - (data) { - final typeOptionData = DateTypeOption.fromBuffer(data); - DateTime? selectedDay; - final cellData = cellContext.getCellData()?.data; - - if (cellData != null) { - final timestamp = $fixnum.Int64.parseInt(cellData).toInt(); - selectedDay = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000); - } - - emit(state.copyWith( - dateTypeOption: typeOptionData, - selectedDay: selectedDay, - )); - }, - (err) => Log.error(err), - ); - } - - void _updateCellData(DateTime day) { - cellContext.saveCellData(DateCellPersistenceData(date: day)); - } - Future? _updateTypeOption( Emitter emit, { DateFormat? dateFormat, @@ -161,19 +142,25 @@ class DateCalState with _$DateCalState { required DateTime focusedDay, required String time, required Option inputTimeError, - DateTime? selectedDay, + DateCellPersistenceData? dateData, }) = _DateCalState; factory DateCalState.initial( DateTypeOption dateTypeOption, DateTime? selectedDay, - ) => - DateCalState( - dateTypeOption: dateTypeOption, - format: CalendarFormat.month, - focusedDay: DateTime.now(), - selectedDay: selectedDay, - time: "", - inputTimeError: none(), - ); + ) { + DateCellPersistenceData? dateData; + if (selectedDay != null) { + dateData = DateCellPersistenceData(date: selectedDay); + } + + return DateCalState( + dateTypeOption: dateTypeOption, + format: CalendarFormat.month, + focusedDay: DateTime.now(), + dateData: dateData, + time: "", + inputTimeError: none(), + ); + } } diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/calendar.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/calendar.dart index 6de870f0d3..84a4760f0c 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/calendar.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/calendar.dart @@ -112,11 +112,11 @@ class _CellCalendarWidget extends StatelessWidget { )..add(const DateCalEvent.initial()), child: BlocConsumer( listener: (context, state) { - if (state.selectedDay != null) { - onSelected(DateCellPersistenceData(date: state.selectedDay!)); + if (state.dateData != null) { + onSelected(state.dateData!); } }, - listenWhen: (p, c) => p.selectedDay != c.selectedDay, + listenWhen: (p, c) => p.dateData != c.dateData, builder: (context, state) { List children = []; @@ -190,7 +190,11 @@ class _CellCalendarWidget extends StatelessWidget { ), ), selectedDayPredicate: (day) { - return isSameDay(state.selectedDay, day); + if (state.dateData != null) { + return isSameDay(state.dateData!.date, day); + } else { + return false; + } }, onDaySelected: (selectedDay, focusedDay) { context.read().add(DateCalEvent.selectDay(selectedDay));