| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import '../core/network/dio_client.dart';
- import '../data/models/announcement/announcement.dart';
- import '../data/services/announcement_service.dart';
- // ── State ──────────────────────────────────────────────────
- class AppNotificationsState {
- final List<AnnouncementContent> notifications;
- final bool isLoading;
- final bool hasMore;
- final int currentPage;
- final String? errorMessage;
- const AppNotificationsState({
- this.notifications = const [],
- this.isLoading = false,
- this.hasMore = true,
- this.currentPage = 1,
- this.errorMessage,
- });
- AppNotificationsState copyWith({
- List<AnnouncementContent>? notifications,
- bool? isLoading,
- bool? hasMore,
- int? currentPage,
- String? errorMessage,
- }) =>
- AppNotificationsState(
- notifications: notifications ?? this.notifications,
- isLoading: isLoading ?? this.isLoading,
- hasMore: hasMore ?? this.hasMore,
- currentPage: currentPage ?? this.currentPage,
- errorMessage: errorMessage,
- );
- }
- // ── Notifier ───────────────────────────────────────────────
- class AppNotificationsNotifier
- extends AutoDisposeNotifier<AppNotificationsState> {
- static const _pageSize = 10;
- AnnouncementService get _service =>
- AnnouncementService(ref.read(dioClientProvider));
- @override
- AppNotificationsState build() {
- Future.microtask(_loadFirst);
- return const AppNotificationsState(isLoading: true);
- }
- Future<void> _loadFirst() async {
- try {
- final result = await _service.getAnnouncementPage(
- pageNo: 1,
- pageSize: _pageSize,
- );
- state = state.copyWith(
- notifications: result.content,
- isLoading: false,
- currentPage: 1,
- hasMore: result.content.length >= _pageSize,
- );
- } catch (e) {
- state = state.copyWith(isLoading: false, errorMessage: e.toString());
- }
- }
- /// 下拉刷新
- Future<void> refresh() async {
- state = state.copyWith(isLoading: true, errorMessage: null);
- try {
- final result = await _service.getAnnouncementPage(
- pageNo: 1,
- pageSize: _pageSize,
- );
- state = state.copyWith(
- notifications: result.content,
- isLoading: false,
- currentPage: 1,
- hasMore: result.content.length >= _pageSize,
- );
- } catch (e) {
- state = state.copyWith(isLoading: false, errorMessage: e.toString());
- }
- }
- /// 上拉加载更多
- Future<void> loadMore() async {
- if (state.isLoading || !state.hasMore) return;
- state = state.copyWith(isLoading: true);
- try {
- final nextPage = state.currentPage + 1;
- final result = await _service.getAnnouncementPage(
- pageNo: nextPage,
- pageSize: _pageSize,
- );
- state = state.copyWith(
- notifications: [...state.notifications, ...result.content],
- isLoading: false,
- currentPage: nextPage,
- hasMore: result.content.length >= _pageSize,
- );
- } catch (e) {
- state = state.copyWith(isLoading: false, errorMessage: e.toString());
- }
- }
- }
- // ── Provider ───────────────────────────────────────────────
- final notificationsProvider = AutoDisposeNotifierProvider<
- AppNotificationsNotifier, AppNotificationsState>(
- AppNotificationsNotifier.new,
- );
|