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 {
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<DateCellPersistenceData> {

View File

@ -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<DateCalEvent, DateCalState> {
// 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<DateCalEvent, DateCalState> {
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<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(
Emitter<DateCalState> emit, {
DateFormat? dateFormat,
@ -161,19 +142,25 @@ class DateCalState with _$DateCalState {
required DateTime focusedDay,
required String time,
required Option<FlowyError> 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(),
);
}
}

View File

@ -112,11 +112,11 @@ class _CellCalendarWidget extends StatelessWidget {
)..add(const DateCalEvent.initial()),
child: BlocConsumer<DateCalBloc, DateCalState>(
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<Widget> 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<DateCalBloc>().add(DateCalEvent.selectDay(selectedDay));