chore: save date cell data with time

This commit is contained in:
appflowy 2022-05-11 21:34:58 +08:00
parent 6fb163b296
commit a178546acd
3 changed files with 40 additions and 53 deletions

View File

@ -27,13 +27,9 @@ class CellDataPersistence implements _GridCellDataPersistence<String> {
} }
} }
class DateCellPersistenceData { @freezed
final DateTime date; class DateCellPersistenceData with _$DateCellPersistenceData {
final String? time; const factory DateCellPersistenceData({required DateTime date, String? time}) = _DateCellPersistenceData;
DateCellPersistenceData({
required this.date,
this.time,
});
} }
class NumberCellDataPersistence implements _GridCellDataPersistence<DateCellPersistenceData> { class NumberCellDataPersistence implements _GridCellDataPersistence<DateCellPersistenceData> {

View File

@ -9,7 +9,6 @@ import 'package:table_calendar/table_calendar.dart';
import 'dart:async'; import 'dart:async';
import 'cell_service/cell_service.dart'; import 'cell_service/cell_service.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:fixnum/fixnum.dart' as $fixnum;
import 'package:protobuf/protobuf.dart'; import 'package:protobuf/protobuf.dart';
part 'date_cal_bloc.freezed.dart'; part 'date_cal_bloc.freezed.dart';
@ -30,9 +29,13 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
// await _loadDateTypeOption(emit); // await _loadDateTypeOption(emit);
}, },
selectDay: (_SelectDay value) { selectDay: (_SelectDay value) {
if (!isSameDay(state.selectedDay, value.day)) { if (state.dateData != null) {
_updateCellData(value.day); if (!isSameDay(state.dateData!.date, value.day)) {
emit(state.copyWith(selectedDay: 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) { setCalFormat: (_CalendarFormat value) {
@ -52,7 +55,12 @@ class DateCalBloc extends Bloc<DateCalEvent, DateCalState> {
await _updateTypeOption(emit, timeFormat: value.timeFormat); await _updateTypeOption(emit, timeFormat: value.timeFormat);
}, },
setTime: (_Time value) { 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<DateCalEvent, DateCalState> {
); );
} }
// ignore: unused_element
Future<void> _loadDateTypeOption(Emitter<DateCalState> 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<void>? _updateTypeOption( Future<void>? _updateTypeOption(
Emitter<DateCalState> emit, { Emitter<DateCalState> emit, {
DateFormat? dateFormat, DateFormat? dateFormat,
@ -161,19 +142,25 @@ class DateCalState with _$DateCalState {
required DateTime focusedDay, required DateTime focusedDay,
required String time, required String time,
required Option<FlowyError> inputTimeError, required Option<FlowyError> inputTimeError,
DateTime? selectedDay, DateCellPersistenceData? dateData,
}) = _DateCalState; }) = _DateCalState;
factory DateCalState.initial( factory DateCalState.initial(
DateTypeOption dateTypeOption, DateTypeOption dateTypeOption,
DateTime? selectedDay, DateTime? selectedDay,
) => ) {
DateCalState( DateCellPersistenceData? dateData;
dateTypeOption: dateTypeOption, if (selectedDay != null) {
format: CalendarFormat.month, dateData = DateCellPersistenceData(date: selectedDay);
focusedDay: DateTime.now(), }
selectedDay: selectedDay,
time: "", return DateCalState(
inputTimeError: none(), dateTypeOption: dateTypeOption,
); format: CalendarFormat.month,
focusedDay: DateTime.now(),
dateData: dateData,
time: "",
inputTimeError: none(),
);
}
} }

View File

@ -112,11 +112,11 @@ class _CellCalendarWidget extends StatelessWidget {
)..add(const DateCalEvent.initial()), )..add(const DateCalEvent.initial()),
child: BlocConsumer<DateCalBloc, DateCalState>( child: BlocConsumer<DateCalBloc, DateCalState>(
listener: (context, state) { listener: (context, state) {
if (state.selectedDay != null) { if (state.dateData != null) {
onSelected(DateCellPersistenceData(date: state.selectedDay!)); onSelected(state.dateData!);
} }
}, },
listenWhen: (p, c) => p.selectedDay != c.selectedDay, listenWhen: (p, c) => p.dateData != c.dateData,
builder: (context, state) { builder: (context, state) {
List<Widget> children = []; List<Widget> children = [];
@ -190,7 +190,11 @@ class _CellCalendarWidget extends StatelessWidget {
), ),
), ),
selectedDayPredicate: (day) { selectedDayPredicate: (day) {
return isSameDay(state.selectedDay, day); if (state.dateData != null) {
return isSameDay(state.dateData!.date, day);
} else {
return false;
}
}, },
onDaySelected: (selectedDay, focusedDay) { onDaySelected: (selectedDay, focusedDay) {
context.read<DateCalBloc>().add(DateCalEvent.selectDay(selectedDay)); context.read<DateCalBloc>().add(DateCalEvent.selectDay(selectedDay));