Skip to content

Commit

Permalink
Use new ListState for eventregistrations
Browse files Browse the repository at this point in the history
  • Loading branch information
DeD1rk committed Jan 31, 2023
1 parent b1b4787 commit 54d62b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
41 changes: 18 additions & 23 deletions lib/blocs/registrations_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reaxit/api/api_repository.dart';
import 'package:reaxit/api/exceptions.dart';
import 'package:reaxit/blocs.dart';
import 'package:reaxit/models.dart';
import 'package:reaxit/ui/widgets.dart';

typedef RegistrationsState = ListState<EventRegistration>;

class RegistrationsCubit extends Cubit<RegistrationsState> {
class RegistrationsCubit extends PaginatedCubit<EventRegistration> {
final ApiRepository api;
final int eventPk;

static const int firstPageSize = 60;
static const int pageSize = 30;

/// The offset to be used for the next paginated request.
int _nextOffset = 0;

RegistrationsCubit(this.api, {required this.eventPk})
: super(const RegistrationsState.loading(results: []));
: super(firstPageSize: 60, pageSize: 30);

@override
Future<void> load() async {
emit(state.copyWith(isLoading: true));
try {
final listResponse = await api.getEventRegistrations(
pk: eventPk, limit: firstPageSize, offset: 0);
Expand All @@ -30,27 +25,27 @@ class RegistrationsCubit extends Cubit<RegistrationsState> {
_nextOffset = firstPageSize;

if (listResponse.results.isNotEmpty) {
emit(RegistrationsState.success(
results: listResponse.results, isDone: isDone));
emit(ResultsListState.withDone(listResponse.results, isDone));
} else {
emit(const RegistrationsState.failure(
message: 'There are no registrations yet.',
));
emit(const ErrorListState('There are no registrations yet.'));
}
} on ApiException catch (exception) {
emit(RegistrationsState.failure(
message: exception.getMessage(notFound: 'The event does not exist.'),
emit(ErrorListState(
exception.getMessage(notFound: 'The event does not exist.'),
));
}
}

@override
Future<void> more() async {
final oldState = state;

if (oldState.isDone || oldState.isLoading || oldState.isLoadingMore) return;
// Ignore calls to `more()` if there is no data, or already more coming.
if (state is! ResultsListState ||
state is LoadingMoreListState ||
state is DoneListState) return;

emit(oldState.copyWith(isLoadingMore: true));
final oldState = state as ResultsListState<EventRegistration>;

emit(LoadingMoreListState.from(oldState));
try {
var listResponse = await api.getEventRegistrations(
pk: eventPk,
Expand All @@ -63,10 +58,10 @@ class RegistrationsCubit extends Cubit<RegistrationsState> {

_nextOffset += pageSize;

emit(RegistrationsState.success(results: registrations, isDone: isDone));
emit(ResultsListState.withDone(registrations, isDone));
} on ApiException catch (exception) {
emit(RegistrationsState.failure(
message: exception.getMessage(notFound: 'The event does not exist.'),
emit(ErrorListState(
exception.getMessage(notFound: 'The event does not exist.'),
));
}
}
Expand Down
51 changes: 24 additions & 27 deletions lib/ui/screens/event_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class _EventScreenState extends State<EventScreen> {
if (_controller.position.pixels >=
_controller.position.maxScrollExtent - 300) {
// Only request loading more if that's not already happening.
if (!_registrationsCubit.state.isLoadingMore) {
if (_registrationsCubit.state is ResultsListState &&
_registrationsCubit.state is! LoadingMoreListState) {
_registrationsCubit.more();
}
}
Expand Down Expand Up @@ -416,7 +417,7 @@ class _EventScreenState extends State<EventScreen> {
);
}

SliverPadding _makeRegistrationsHeader(RegistrationsState state) {
SliverPadding _makeRegistrationsHeader() {
return SliverPadding(
padding: const EdgeInsets.only(left: 16),
sliver: SliverToBoxAdapter(
Expand All @@ -428,22 +429,21 @@ class _EventScreenState extends State<EventScreen> {
);
}

SliverPadding _makeRegistrations(RegistrationsState state) {
if (state.isLoading && state.results.isEmpty) {
SliverPadding _makeRegistrations(XListState<EventRegistration> state) {
if (state is ErrorListState) {
return SliverPadding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 16),
sliver: SliverToBoxAdapter(child: Text(state.message!)),
);
} else if (state is LoadingListState) {
return const SliverPadding(
padding: EdgeInsets.all(16),
sliver: SliverToBoxAdapter(
child: Center(
child: CircularProgressIndicator(),
),
child: Center(child: CircularProgressIndicator()),
),
);
} else if (state.hasException) {
return SliverPadding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 16),
sliver: SliverToBoxAdapter(child: Text(state.message!)),
);
} else {
final results = state.results;
return SliverPadding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 16),
sliver: SliverGrid(
Expand All @@ -453,18 +453,10 @@ class _EventScreenState extends State<EventScreen> {
crossAxisSpacing: 8,
),
delegate: SliverChildBuilderDelegate(
(context, index) {
if (state.results[index].member != null) {
return MemberTile(
member: state.results[index].member!,
);
} else {
return DefaultMemberTile(
name: state.results[index].name!,
);
}
},
childCount: state.results.length,
childCount: results.length,
(context, index) => results[index].member != null
? MemberTile(member: results[index].member!)
: DefaultMemberTile(name: results[index].name!),
),
),
);
Expand Down Expand Up @@ -527,7 +519,8 @@ class _EventScreenState extends State<EventScreen> {
_registrationsCubit.load();
await _eventCubit.load();
},
child: BlocBuilder<RegistrationsCubit, RegistrationsState>(
child: BlocBuilder<RegistrationsCubit,
XListState<EventRegistration>>(
bloc: _registrationsCubit,
builder: (context, listState) {
return Scrollbar(
Expand All @@ -551,9 +544,9 @@ class _EventScreenState extends State<EventScreen> {
if (event.registrationIsOptional ||
event.registrationIsRequired) ...[
const SliverToBoxAdapter(child: Divider()),
_makeRegistrationsHeader(listState),
_makeRegistrationsHeader(),
_makeRegistrations(listState),
if (listState.isLoadingMore)
if (listState is LoadingMoreListState)
const SliverPadding(
padding: EdgeInsets.all(8),
sliver: SliverList(
Expand All @@ -563,6 +556,10 @@ class _EventScreenState extends State<EventScreen> {
),
),
],
const SliverSafeArea(
minimum: EdgeInsets.only(bottom: 8),
sliver: SliverPadding(padding: EdgeInsets.zero),
),
],
),
);
Expand Down

0 comments on commit 54d62b3

Please sign in to comment.