68 lines
1.8 KiB
Dart
68 lines
1.8 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../models/event.dart';
|
|
import '../services/api_service.dart';
|
|
|
|
class EventState {
|
|
final List<EventItem> events;
|
|
final bool isLoading;
|
|
final String? error;
|
|
final bool publishSuccess;
|
|
|
|
const EventState({
|
|
this.events = const [],
|
|
this.isLoading = false,
|
|
this.error,
|
|
this.publishSuccess = false,
|
|
});
|
|
|
|
EventState copyWith({
|
|
List<EventItem>? events,
|
|
bool? isLoading,
|
|
String? error,
|
|
bool? publishSuccess,
|
|
}) {
|
|
return EventState(
|
|
events: events ?? this.events,
|
|
isLoading: isLoading ?? this.isLoading,
|
|
error: error,
|
|
publishSuccess: publishSuccess ?? this.publishSuccess,
|
|
);
|
|
}
|
|
}
|
|
|
|
class EventNotifier extends StateNotifier<EventState> {
|
|
EventNotifier() : super(const EventState());
|
|
|
|
Future<void> loadEvents() async {
|
|
state = state.copyWith(isLoading: true, error: null);
|
|
try {
|
|
final events = await apiService.getEvents();
|
|
state = state.copyWith(events: events, isLoading: false);
|
|
} catch (e) {
|
|
state = state.copyWith(isLoading: false, error: e.toString());
|
|
}
|
|
}
|
|
|
|
Future<void> publishEvent(Map<String, dynamic> data) async {
|
|
state = state.copyWith(isLoading: true, error: null, publishSuccess: false);
|
|
try {
|
|
final event = await apiService.publishEvent(data);
|
|
state = state.copyWith(
|
|
events: [event, ...state.events],
|
|
isLoading: false,
|
|
publishSuccess: true,
|
|
);
|
|
} catch (e) {
|
|
state = state.copyWith(isLoading: false, error: e.toString(), publishSuccess: false);
|
|
}
|
|
}
|
|
|
|
void clearPublishSuccess() {
|
|
state = state.copyWith(publishSuccess: false);
|
|
}
|
|
}
|
|
|
|
final eventProvider = StateNotifierProvider<EventNotifier, EventState>((ref) {
|
|
return EventNotifier();
|
|
});
|