diff --git a/lib/blocs/calendar_cubit.dart b/lib/blocs/calendar_cubit.dart index c0939e367..e66d24132 100644 --- a/lib/blocs/calendar_cubit.dart +++ b/lib/blocs/calendar_cubit.dart @@ -1,10 +1,10 @@ import 'dart:async'; +import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:intl/intl.dart'; import 'package:reaxit/api/api_repository.dart'; import 'package:reaxit/api/exceptions.dart'; -import 'package:reaxit/blocs.dart'; import 'package:reaxit/config.dart' as config; import 'package:reaxit/models.dart'; @@ -103,7 +103,86 @@ class CalendarEvent { ); } -typedef CalendarState = ListState; +/// Generic class to be used as state for paginated lists. +class CalendarState extends Equatable { + /// The results to be shown. These are outdated if `isLoading` is true. + final List results; + + /// A message describing why there are no results. + final String? message; + + /// Different results are being loaded. The results are outdated. + final bool isLoading; + + /// More of the same results are being loaded. The results are not outdated. + final bool isLoadingMore; + + /// The last results have been loaded. There are no more pages left. + final bool isDone; + + bool get hasException => message != null; + + const CalendarState({ + required this.results, + required this.message, + required this.isLoading, + required this.isLoadingMore, + required this.isDone, + }); + + CalendarState copyWith({ + List? results, + String? message, + bool? isLoading, + bool? isLoadingMore, + bool? isDone, + }) => + CalendarState( + results: results ?? this.results, + message: message ?? this.message, + isLoading: isLoading ?? this.isLoading, + isLoadingMore: isLoadingMore ?? this.isLoadingMore, + isDone: isDone ?? this.isDone, + ); + + @override + List get props => [ + results, + message, + isLoading, + isLoadingMore, + isDone, + ]; + + @override + String toString() { + return 'ListState<$CalendarEvent>(isLoading: $isLoading, isLoadingMore: $isLoadingMore,' + ' isDone: $isDone, message: $message, ${results.length} ${CalendarEvent}s)'; + } + + const CalendarState.loading({required this.results}) + : message = null, + isLoading = true, + isLoadingMore = false, + isDone = true; + + const CalendarState.loadingMore({required this.results}) + : message = null, + isLoading = false, + isLoadingMore = true, + isDone = true; + + const CalendarState.success({required this.results, required this.isDone}) + : message = null, + isLoading = false, + isLoadingMore = false; + + const CalendarState.failure({required String this.message}) + : results = const [], + isLoading = false, + isLoadingMore = false, + isDone = true; +} class CalendarCubit extends Cubit { static const int firstPageSize = 20; diff --git a/lib/blocs/list_state.dart b/lib/blocs/list_state.dart index e0bf5972f..05d551c5d 100644 --- a/lib/blocs/list_state.dart +++ b/lib/blocs/list_state.dart @@ -1,86 +1,5 @@ import 'package:equatable/equatable.dart'; -/// Generic class to be used as state for paginated lists. -class ListState extends Equatable { - /// The results to be shown. These are outdated if `isLoading` is true. - final List results; - - /// A message describing why there are no results. - final String? message; - - /// Different results are being loaded. The results are outdated. - final bool isLoading; - - /// More of the same results are being loaded. The results are not outdated. - final bool isLoadingMore; - - /// The last results have been loaded. There are no more pages left. - final bool isDone; - - bool get hasException => message != null; - - const ListState({ - required this.results, - required this.message, - required this.isLoading, - required this.isLoadingMore, - required this.isDone, - }); - - ListState copyWith({ - List? results, - String? message, - bool? isLoading, - bool? isLoadingMore, - bool? isDone, - }) => - ListState( - results: results ?? this.results, - message: message ?? this.message, - isLoading: isLoading ?? this.isLoading, - isLoadingMore: isLoadingMore ?? this.isLoadingMore, - isDone: isDone ?? this.isDone, - ); - - @override - List get props => [ - results, - message, - isLoading, - isLoadingMore, - isDone, - ]; - - @override - String toString() { - return 'ListState<$T>(isLoading: $isLoading, isLoadingMore: $isLoadingMore,' - ' isDone: $isDone, message: $message, ${results.length} ${T}s)'; - } - - const ListState.loading({required this.results}) - : message = null, - isLoading = true, - isLoadingMore = false, - isDone = true; - - const ListState.loadingMore({required this.results}) - : message = null, - isLoading = false, - isLoadingMore = true, - isDone = true; - - const ListState.success({required this.results, required this.isDone}) - : message = null, - isLoading = false, - isLoadingMore = false; - - const ListState.failure({required String this.message}) - : results = const [], - isLoading = false, - isLoadingMore = false, - isDone = true; -} - /// Generic type for states with a paginated list of results. /// /// There are a number of subtypes: @@ -89,8 +8,8 @@ class ListState extends Equatable { /// * [ResultsListState] - indicates that there are results. /// * [DoneListState] - indicates that there are no more results. /// * [LoadingMoreListState] - indicates that we are loading more results. -abstract class XListState extends Equatable { - const XListState(); +abstract class ListState extends Equatable { + const ListState(); /// A convenience method to get the results if they are available. /// @@ -108,11 +27,11 @@ abstract class XListState extends Equatable { List get props => []; } -class LoadingListState extends XListState { +class LoadingListState extends ListState { const LoadingListState(); } -class ErrorListState extends XListState { +class ErrorListState extends ListState { @override final String message; @@ -122,7 +41,7 @@ class ErrorListState extends XListState { List get props => [message]; } -class ResultsListState extends XListState { +class ResultsListState extends ListState { @override final List results; diff --git a/lib/ui/screens/event_screen.dart b/lib/ui/screens/event_screen.dart index 4ea7ad8e8..6edea2c70 100644 --- a/lib/ui/screens/event_screen.dart +++ b/lib/ui/screens/event_screen.dart @@ -429,7 +429,7 @@ class _EventScreenState extends State { ); } - SliverPadding _makeRegistrations(XListState state) { + SliverPadding _makeRegistrations(ListState state) { if (state is ErrorListState) { return SliverPadding( padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 16), @@ -520,7 +520,7 @@ class _EventScreenState extends State { await _eventCubit.load(); }, child: BlocBuilder>( + ListState>( bloc: _registrationsCubit, builder: (context, listState) { return Scrollbar( diff --git a/lib/ui/widgets/paginated_scroll_view.dart b/lib/ui/widgets/paginated_scroll_view.dart index be6a4d08b..1354f82ff 100644 --- a/lib/ui/widgets/paginated_scroll_view.dart +++ b/lib/ui/widgets/paginated_scroll_view.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:reaxit/blocs/list_state.dart'; -abstract class PaginatedCubit extends Cubit> { +abstract class PaginatedCubit extends Cubit> { final int firstPageSize; final int pageSize; @@ -76,7 +76,7 @@ class _PaginatedScrollViewState> @override Widget build(BuildContext context) { - return BlocBuilder>( + return BlocBuilder>( builder: (context, state) { late final List slivers; if (state is ErrorListState) {