92 lines
2.7 KiB
Dart
92 lines
2.7 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../domain/entities/guardian_notification.dart';
|
|
import '../domain/repositories/notification_repository.dart';
|
|
|
|
class NotificationState {
|
|
final bool loading;
|
|
final List<GuardianNotificationEntity> items;
|
|
final String? error;
|
|
final bool markingAll;
|
|
|
|
const NotificationState({
|
|
this.loading = false,
|
|
this.items = const [],
|
|
this.error,
|
|
this.markingAll = false,
|
|
});
|
|
|
|
NotificationState copyWith({
|
|
bool? loading,
|
|
List<GuardianNotificationEntity>? items,
|
|
String? error,
|
|
bool? markingAll,
|
|
}) {
|
|
return NotificationState(
|
|
loading: loading ?? this.loading,
|
|
items: items ?? this.items,
|
|
error: error,
|
|
markingAll: markingAll ?? this.markingAll,
|
|
);
|
|
}
|
|
}
|
|
|
|
class NotificationCubit extends Cubit<NotificationState> {
|
|
final NotificationRepository _repository;
|
|
|
|
NotificationCubit(this._repository) : super(const NotificationState());
|
|
|
|
Future<void> load() async {
|
|
emit(const NotificationState(loading: true));
|
|
final result = await _repository.getNotifications();
|
|
result.fold(
|
|
(failure) => emit(NotificationState(error: failure.message)),
|
|
(items) => emit(NotificationState(items: items)),
|
|
);
|
|
}
|
|
|
|
Future<void> markOneRead(int id) async {
|
|
final result = await _repository.markOneRead(id);
|
|
result.fold(
|
|
(failure) => emit(state.copyWith(error: failure.message)),
|
|
(_) => emit(state.copyWith(
|
|
items: state.items
|
|
.map((item) => item.id == id
|
|
? GuardianNotificationEntity(
|
|
id: item.id,
|
|
notificationType: item.notificationType,
|
|
content: item.content,
|
|
voiceNoteUrl: item.voiceNoteUrl,
|
|
voiceNoteDuration: item.voiceNoteDuration,
|
|
isRead: true,
|
|
createdAt: item.createdAt,
|
|
)
|
|
: item)
|
|
.toList(),
|
|
)),
|
|
);
|
|
}
|
|
|
|
Future<void> markAllRead() async {
|
|
emit(state.copyWith(markingAll: true));
|
|
final result = await _repository.markAllRead();
|
|
result.fold(
|
|
(failure) => emit(state.copyWith(error: failure.message, markingAll: false)),
|
|
(_) => emit(state.copyWith(
|
|
markingAll: false,
|
|
items: state.items
|
|
.map((item) => GuardianNotificationEntity(
|
|
id: item.id,
|
|
notificationType: item.notificationType,
|
|
content: item.content,
|
|
voiceNoteUrl: item.voiceNoteUrl,
|
|
voiceNoteDuration: item.voiceNoteDuration,
|
|
isRead: true,
|
|
createdAt: item.createdAt,
|
|
))
|
|
.toList(),
|
|
)),
|
|
);
|
|
}
|
|
}
|