Fix/calendar event update (#2442)

* fix: update event pos on calendar

* fix: replace text field id with date field id
This commit is contained in:
Nathan.fooo 2023-05-03 15:47:09 +08:00 committed by GitHub
parent 39b1ff0910
commit 75262c0683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 49 deletions

View File

@ -46,7 +46,13 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
emit(state.copyWith(database: Some(database)));
},
didLoadAllEvents: (events) {
emit(state.copyWith(initialEvents: events, allEvents: events));
final calenderEvents = _calendarEventDataFromEventPBs(events);
emit(
state.copyWith(
initialEvents: calenderEvents,
allEvents: calenderEvents,
),
);
},
didReceiveNewLayoutField: (CalendarLayoutSettingsPB layoutSettings) {
_loadAllEvents();
@ -57,9 +63,7 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
},
didCreateEvent: (CalendarEventData<CalendarDayEvent> event) {
emit(
state.copyWith(
createdEvent: event,
),
state.copyWith(editEvent: event),
);
},
updateCalendarLayoutSetting:
@ -69,7 +73,7 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
didUpdateEvent: (CalendarEventData<CalendarDayEvent> eventData) {
var allEvents = [...state.allEvents];
final index = allEvents.indexWhere(
(element) => element.event!.cellId == eventData.event!.cellId,
(element) => element.event!.eventId == eventData.event!.eventId,
);
if (index != -1) {
allEvents[index] = eventData;
@ -83,7 +87,7 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
didDeleteEvents: (List<String> deletedRowIds) {
var events = [...state.allEvents];
events.retainWhere(
(element) => !deletedRowIds.contains(element.event!.cellId.rowId),
(element) => !deletedRowIds.contains(element.event!.eventId),
);
emit(
state.copyWith(
@ -208,15 +212,7 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
result.fold(
(events) {
if (!isClosed) {
final calendarEvents = <CalendarEventData<CalendarDayEvent>>[];
for (final eventPB in events.items) {
final calendarEvent = _calendarEventDataFromEventPB(eventPB);
if (calendarEvent != null) {
calendarEvents.add(calendarEvent);
}
}
add(CalendarEvent.didLoadAllEvents(calendarEvents));
add(CalendarEvent.didLoadAllEvents(events.items));
}
},
(r) => Log.error(r),
@ -224,20 +220,28 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
});
}
List<CalendarEventData<CalendarDayEvent>> _calendarEventDataFromEventPBs(
List<CalendarEventPB> eventPBs,
) {
final calendarEvents = <CalendarEventData<CalendarDayEvent>>[];
for (final eventPB in eventPBs) {
final event = _calendarEventDataFromEventPB(eventPB);
if (event != null) {
calendarEvents.add(event);
}
}
return calendarEvents;
}
CalendarEventData<CalendarDayEvent>? _calendarEventDataFromEventPB(
CalendarEventPB eventPB,
) {
final fieldInfo = fieldInfoByFieldId[eventPB.titleFieldId];
final fieldInfo = fieldInfoByFieldId[eventPB.dateFieldId];
if (fieldInfo != null) {
final cellId = CellIdentifier(
viewId: viewId,
rowId: eventPB.rowId,
fieldInfo: fieldInfo,
);
final eventData = CalendarDayEvent(
event: eventPB,
cellId: cellId,
eventId: eventPB.rowId,
dateFieldId: eventPB.dateFieldId,
);
// The timestamp is using UTC in the backend, so we need to convert it
@ -266,25 +270,29 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
for (var fieldInfo in fieldInfos) fieldInfo.field.id: fieldInfo
};
},
onRowsChanged: ((onRowsChanged, rowByRowId, reason) {}),
onRowsCreated: ((ids) async {
for (final id in ids) {
onRowsCreated: ((rowIds) async {
for (final id in rowIds) {
final event = await _loadEvent(id);
if (event != null && !isClosed) {
add(CalendarEvent.didReceiveEvent(event));
}
}
}),
onRowsDeleted: (ids) {
onRowsDeleted: (rowIds) {
if (isClosed) return;
add(CalendarEvent.didDeleteEvents(ids));
add(CalendarEvent.didDeleteEvents(rowIds));
},
onRowsUpdated: (ids) async {
onRowsUpdated: (rowIds) async {
if (isClosed) return;
for (final id in ids) {
for (final id in rowIds) {
final event = await _loadEvent(id);
if (event != null) {
add(CalendarEvent.didUpdateEvent(event));
if (event != null && isEventDayChanged(event)) {
if (isEventDayChanged(event)) {
add(CalendarEvent.didDeleteEvents([id]));
add(CalendarEvent.didReceiveEvent(event));
} else {
add(CalendarEvent.didUpdateEvent(event));
}
}
}
},
@ -319,6 +327,19 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
add(CalendarEvent.didReceiveNewLayoutField(layoutSetting.calendar));
}
}
bool isEventDayChanged(
CalendarEventData<CalendarDayEvent> event,
) {
final index = state.allEvents.indexWhere(
(element) => element.event!.eventId == event.event!.eventId,
);
if (index != -1) {
return state.allEvents[index].date.day != event.date.day;
} else {
return false;
}
}
}
typedef Events = List<CalendarEventData<CalendarDayEvent>>;
@ -333,7 +354,7 @@ class CalendarEvent with _$CalendarEvent {
) = _ReceiveCalendarSettings;
// Called after loading all the current evnets
const factory CalendarEvent.didLoadAllEvents(Events events) =
const factory CalendarEvent.didLoadAllEvents(List<CalendarEventPB> events) =
_ReceiveCalendarEvents;
// Called when specific event was updated
@ -376,9 +397,10 @@ class CalendarEvent with _$CalendarEvent {
class CalendarState with _$CalendarState {
const factory CalendarState({
required Option<DatabasePB> database,
// events by row id
required Events allEvents,
required Events initialEvents,
CalendarEventData<CalendarDayEvent>? createdEvent,
CalendarEventData<CalendarDayEvent>? editEvent,
CalendarEventData<CalendarDayEvent>? newEvent,
required List<String> deleteEventIds,
required Option<CalendarLayoutSettingsPB> settings,
@ -417,9 +439,12 @@ class CalendarEditingRow {
class CalendarDayEvent {
final CalendarEventPB event;
final CellIdentifier cellId;
final String dateFieldId;
final String eventId;
String get eventId => cellId.rowId;
String get fieldId => cellId.fieldId;
CalendarDayEvent({required this.cellId, required this.event});
CalendarDayEvent({
required this.dateFieldId,
required this.eventId,
required this.event,
});
}

View File

@ -190,7 +190,7 @@ class CalendarDayCard extends StatelessWidget {
row: rowInfo!.rowPB,
viewId: viewId,
rowCache: _rowCache,
cardData: event.fieldId,
cardData: event.dateFieldId,
isEditing: false,
cellBuilder: cellBuilder,
openCard: (context) => _showRowDetailPage(event, context),
@ -226,7 +226,7 @@ class CalendarDayCard extends StatelessWidget {
void _showRowDetailPage(CalendarDayEvent event, BuildContext context) {
final dataController = RowController(
rowId: event.cellId.rowId,
rowId: event.eventId,
viewId: viewId,
rowCache: _rowCache,
);

View File

@ -73,14 +73,16 @@ class _CalendarPageState extends State<CalendarPage> {
},
),
BlocListener<CalendarBloc, CalendarState>(
listenWhen: (p, c) => p.createdEvent != c.createdEvent,
listenWhen: (p, c) => p.editEvent != c.editEvent,
listener: (context, state) {
if (state.createdEvent != null) {
_showRowDetailPage(state.createdEvent!.event!, context);
if (state.editEvent != null) {
_showEditEventPage(state.editEvent!.event!, context);
}
},
),
BlocListener<CalendarBloc, CalendarState>(
// Event create by click the + button or double click on the
// calendar
listenWhen: (p, c) => p.newEvent != c.newEvent,
listener: (context, state) {
if (state.newEvent != null) {
@ -212,9 +214,9 @@ class _CalendarPageState extends State<CalendarPage> {
return WeekDays.values[(dayOfWeek + 1) % 7];
}
void _showRowDetailPage(CalendarDayEvent event, BuildContext context) {
void _showEditEventPage(CalendarDayEvent event, BuildContext context) {
final dataController = RowController(
rowId: event.cellId.rowId,
rowId: event.eventId,
viewId: widget.view.id,
rowCache: _calendarBloc.rowCache,
);

View File

@ -108,7 +108,7 @@ pub struct CalendarEventPB {
pub row_id: String,
#[pb(index = 2)]
pub title_field_id: String,
pub date_field_id: String,
#[pb(index = 3)]
pub title: String,

View File

@ -867,7 +867,7 @@ impl DatabaseViewEditor {
Some(CalendarEventPB {
row_id: row_id.to_string(),
title_field_id: primary_field.id.clone(),
date_field_id: date_field.id.clone(),
title,
timestamp,
})
@ -906,7 +906,6 @@ impl DatabaseViewEditor {
let mut events: Vec<CalendarEventPB> = vec![];
for text_cell in text_cells {
let title_field_id = text_cell.field_id.clone();
let row_id = text_cell.row_id.clone();
let timestamp = timestamp_by_row_id
.get(&row_id)
@ -920,7 +919,7 @@ impl DatabaseViewEditor {
let event = CalendarEventPB {
row_id,
title_field_id,
date_field_id: calendar_setting.layout_field_id.clone(),
title,
timestamp,
};