2023-10-02 09:12:24 +02:00
|
|
|
import 'package:appflowy_backend/dispatch/dispatch.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-user/reminder.pb.dart';
|
2024-02-24 20:54:10 +07:00
|
|
|
import 'package:appflowy_result/appflowy_result.dart';
|
2023-10-02 09:12:24 +02:00
|
|
|
|
|
|
|
/// Interface for a Reminder Service that handles
|
|
|
|
/// communication to the backend
|
|
|
|
///
|
|
|
|
abstract class IReminderService {
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<List<ReminderPB>, FlowyError>> fetchReminders();
|
2023-10-02 09:12:24 +02:00
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> removeReminder({
|
|
|
|
required String reminderId,
|
|
|
|
});
|
2023-10-02 09:12:24 +02:00
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> addReminder({
|
|
|
|
required ReminderPB reminder,
|
|
|
|
});
|
2023-10-02 09:12:24 +02:00
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> updateReminder({
|
2023-10-02 09:12:24 +02:00
|
|
|
required ReminderPB reminder,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReminderService implements IReminderService {
|
|
|
|
const ReminderService();
|
|
|
|
|
|
|
|
@override
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> addReminder({
|
2023-10-02 09:12:24 +02:00
|
|
|
required ReminderPB reminder,
|
|
|
|
}) async {
|
|
|
|
final unitOrFailure = await UserEventCreateReminder(reminder).send();
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
return unitOrFailure;
|
2023-10-02 09:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> updateReminder({
|
2023-10-02 09:12:24 +02:00
|
|
|
required ReminderPB reminder,
|
|
|
|
}) async {
|
|
|
|
final unitOrFailure = await UserEventUpdateReminder(reminder).send();
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
return unitOrFailure;
|
2023-10-02 09:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<List<ReminderPB>, FlowyError>> fetchReminders() async {
|
2023-10-02 09:12:24 +02:00
|
|
|
final resultOrFailure = await UserEventGetAllReminders().send();
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
return resultOrFailure.fold(
|
|
|
|
(s) => FlowyResult.success(s.items),
|
|
|
|
(e) => FlowyResult.failure(e),
|
|
|
|
);
|
2023-10-02 09:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-02-24 20:54:10 +07:00
|
|
|
Future<FlowyResult<void, FlowyError>> removeReminder({
|
2023-10-02 09:12:24 +02:00
|
|
|
required String reminderId,
|
|
|
|
}) async {
|
|
|
|
final request = ReminderIdentifierPB(id: reminderId);
|
|
|
|
final unitOrFailure = await UserEventRemoveReminder(request).send();
|
|
|
|
|
2024-02-24 20:54:10 +07:00
|
|
|
return unitOrFailure;
|
2023-10-02 09:12:24 +02:00
|
|
|
}
|
|
|
|
}
|