Files
AIOA/mobile/lib/features/tasks/application/notification_controller.dart
2026-07-18 19:20:07 +08:00

40 lines
1.3 KiB
Dart

import 'package:aioa_mobile/core/auth/authenticated_http_client.dart';
import 'package:aioa_mobile/core/config/runtime_config.dart';
import 'package:aioa_mobile/features/tasks/data/notification_repository.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final notificationRepositoryProvider = Provider<NotificationRepository>(
(ref) => NotificationRepository(
client: ref.watch(authenticatedHttpClientProvider),
baseUrl: RuntimeConfig.apiBaseUrl,
),
);
final notificationProvider =
AsyncNotifierProvider<NotificationController, List<AppNotification>>(
NotificationController.new,
);
class NotificationController extends AsyncNotifier<List<AppNotification>> {
@override
Future<List<AppNotification>> build() =>
ref.read(notificationRepositoryProvider).list();
Future<void> refresh() async {
state = const AsyncLoading();
state = await AsyncValue.guard(
() => ref.read(notificationRepositoryProvider).list(),
);
}
Future<void> markRead(String id) async {
final current = state.value;
if (current == null) return;
final updated = await ref.read(notificationRepositoryProvider).markRead(id);
state = AsyncData([
for (final item in current)
if (item.id == id) updated else item,
]);
}
}