mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-08-25 07:52:27 +00:00
Refactor memory event handling: replace MemoryBookableSpaceService with a new implementation and integrate caching logic in CalendarEventsBloc
This commit is contained in:
@ -0,0 +1,63 @@
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/data/services/remote_calendar_service.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/domain/LoadEventsParam.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/domain/models/calendar_event_booking.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/domain/services/calendar_system_service.dart';
|
||||
|
||||
class MemoryBookableSpaceService implements CalendarSystemService {
|
||||
final Map<String, CalendarEventsResponse> _eventsCache = {};
|
||||
|
||||
@override
|
||||
Future<CalendarEventsResponse> getCalendarEvents({
|
||||
required LoadEventsParam params,
|
||||
}) async {
|
||||
final key = params.generateKey();
|
||||
|
||||
return _eventsCache[key]!;
|
||||
}
|
||||
|
||||
void setEvents(
|
||||
LoadEventsParam param,
|
||||
CalendarEventsResponse events,
|
||||
) {
|
||||
final key = param.generateKey();
|
||||
_eventsCache[key] = events;
|
||||
}
|
||||
|
||||
void addEvent(LoadEventsParam param, CalendarEventsResponse event) {
|
||||
final key = param.generateKey();
|
||||
|
||||
_eventsCache[key] = event;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_eventsCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
class MemoryCalendarServiceWithRemoteFallback implements CalendarSystemService {
|
||||
final MemoryBookableSpaceService memoryService;
|
||||
final RemoteCalendarService remoteService;
|
||||
|
||||
MemoryCalendarServiceWithRemoteFallback({
|
||||
required this.memoryService,
|
||||
required this.remoteService,
|
||||
});
|
||||
|
||||
@override
|
||||
Future<CalendarEventsResponse> getCalendarEvents({
|
||||
required LoadEventsParam params,
|
||||
}) async {
|
||||
final key = params.generateKey();
|
||||
final doesExistInMemory = memoryService._eventsCache.containsKey(key);
|
||||
|
||||
if (doesExistInMemory) {
|
||||
return memoryService.getCalendarEvents(params: params);
|
||||
} else {
|
||||
final remoteResult =
|
||||
await remoteService.getCalendarEvents(params: params);
|
||||
memoryService.setEvents(params, remoteResult);
|
||||
|
||||
return remoteResult;
|
||||
}
|
||||
}
|
||||
}
|
@ -26,3 +26,9 @@ class LoadEventsParam extends Equatable {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyGenerator on LoadEventsParam {
|
||||
String generateKey() {
|
||||
return '$id-${startDate.year}-${startDate.month.toString().padLeft(2, '0')}';
|
||||
}
|
||||
}
|
@ -5,21 +5,16 @@ import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/domain/LoadEventsParam.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/domain/models/calendar_event_booking.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/domain/services/calendar_system_service.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/model/memory_bookable_space_service.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/data/services/memory_bookable_space_service.dart';
|
||||
part 'events_event.dart';
|
||||
part 'events_state.dart';
|
||||
|
||||
class CalendarEventsBloc extends Bloc<CalendarEventsEvent, CalendarEventState> {
|
||||
final EventController eventController = EventController();
|
||||
final CalendarSystemService calendarService;
|
||||
final Map<String, List<CalendarEventData>> _eventsCache = {};
|
||||
final MemoryBookableSpaceService memoryService;
|
||||
|
||||
|
||||
|
||||
CalendarEventsBloc({
|
||||
required this.calendarService,
|
||||
required this.memoryService,
|
||||
}) : super(EventsInitial()) {
|
||||
on<LoadEvents>(_onLoadEvents);
|
||||
on<AddEvent>(_onAddEvent);
|
||||
@ -34,24 +29,12 @@ class CalendarEventsBloc extends Bloc<CalendarEventsEvent, CalendarEventState> {
|
||||
final month = param.endDate.month;
|
||||
final year = param.endDate.year;
|
||||
final spaceId = param.id;
|
||||
final cachedEvents = memoryService.getEvents(spaceId, year, month);
|
||||
if (cachedEvents != null) {
|
||||
eventController.addAll(cachedEvents);
|
||||
emit(EventsLoaded(
|
||||
events: cachedEvents,
|
||||
spaceId: spaceId,
|
||||
month: month,
|
||||
year: year,
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(EventsLoading());
|
||||
try {
|
||||
final response = await calendarService.getCalendarEvents(params: param);
|
||||
|
||||
final events = response.data.map(_toCalendarEventData).toList();
|
||||
memoryService.setEvents(spaceId, year, month, events);
|
||||
eventController.addAll(events);
|
||||
emit(EventsLoaded(
|
||||
events: events,
|
||||
@ -69,15 +52,6 @@ class CalendarEventsBloc extends Bloc<CalendarEventsEvent, CalendarEventState> {
|
||||
|
||||
if (state is EventsLoaded) {
|
||||
final loaded = state as EventsLoaded;
|
||||
final cacheKey =
|
||||
'${loaded.spaceId}-${loaded.year}-${loaded.month.toString().padLeft(2, '0')}';
|
||||
|
||||
if (_eventsCache.containsKey(cacheKey)) {
|
||||
final cachedEvents =
|
||||
List<CalendarEventData>.from(_eventsCache[cacheKey]!);
|
||||
cachedEvents.add(event.event);
|
||||
_eventsCache[cacheKey] = cachedEvents;
|
||||
}
|
||||
|
||||
emit(EventsLoaded(
|
||||
events: [...eventController.events],
|
||||
|
@ -1,35 +0,0 @@
|
||||
import 'package:calendar_view/calendar_view.dart';
|
||||
|
||||
class MemoryBookableSpaceService {
|
||||
final Map<String, List<CalendarEventData>> _eventsCache = {};
|
||||
|
||||
List<CalendarEventData>? getEvents(String spaceId, int year, int month) {
|
||||
final key = _generateKey(spaceId, year, month);
|
||||
return _eventsCache[key];
|
||||
}
|
||||
|
||||
void setEvents(
|
||||
String spaceId,
|
||||
int year,
|
||||
int month,
|
||||
List<CalendarEventData> events,
|
||||
) {
|
||||
final key = _generateKey(spaceId, year, month);
|
||||
_eventsCache[key] = events;
|
||||
}
|
||||
|
||||
void addEvent(String spaceId, int year, int month, CalendarEventData event) {
|
||||
final key = _generateKey(spaceId, year, month);
|
||||
final events = _eventsCache[key] ?? [];
|
||||
events.add(event);
|
||||
_eventsCache[key] = events;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_eventsCache.clear();
|
||||
}
|
||||
|
||||
String _generateKey(String spaceId, int year, int month) {
|
||||
return '$spaceId-$year-${month.toString().padLeft(2, '0')}';
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import 'package:syncrow_web/pages/access_management/booking_system/presentation/
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/bloc/date_selection/date_selection_event.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/bloc/date_selection/date_selection_state.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/bloc/selected_bookable_space_bloc/selected_bookable_space_bloc.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/model/memory_bookable_space_service.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/data/services/memory_bookable_space_service.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/view/widgets/booking_sidebar.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/view/widgets/custom_calendar_page.dart';
|
||||
import 'package:syncrow_web/pages/access_management/booking_system/presentation/view/widgets/icon_text_button.dart';
|
||||
@ -65,13 +65,14 @@ class _BookingPageState extends State<BookingPage> {
|
||||
BlocProvider(create: (_) => SelectedBookableSpaceBloc()),
|
||||
BlocProvider(create: (_) => DateSelectionBloc()),
|
||||
BlocProvider(
|
||||
create: (_) => CalendarEventsBloc(
|
||||
memoryService: MemoryBookableSpaceService(),
|
||||
calendarService: RemoteCalendarService(
|
||||
HTTPService(),
|
||||
),
|
||||
),
|
||||
),
|
||||
create: (_) => CalendarEventsBloc(
|
||||
calendarService: MemoryCalendarServiceWithRemoteFallback(
|
||||
remoteService: RemoteCalendarService(
|
||||
HTTPService(),
|
||||
),
|
||||
memoryService: MemoryBookableSpaceService(),
|
||||
),
|
||||
)),
|
||||
],
|
||||
child: Builder(
|
||||
builder: (context) =>
|
||||
|
Reference in New Issue
Block a user