Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/blocs/album_list_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class AlbumListCubit extends SingleListCubit<ListAlbum> {
List<ListAlbum> combineDown(
List<ListAlbum> downResults,
ListState<ListAlbum> oldstate,
) => oldstate.results + downResults;
) => getResults(oldstate) + downResults;

@override
ListState<ListAlbum> empty(String? query) => switch (query) {
null => const ListState.failure(message: 'No albums found.'),
'' => const ListState.failure(message: 'Start searching for albums'),
var q => ListState.failure(message: 'No albums found found for query "$q"'),
null => const ErrorState('No albums found.'),
'' => const ResultState(
InnerListState.failure(message: 'Start searching for albums'),
),
var q => ErrorState('No albums found found for query "$q"'),
};
}
5 changes: 4 additions & 1 deletion lib/blocs/calendar_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ class CalendarCubit extends ListCubit<Event, CalendarEvent, CalendarState> {
),
'' => CalendarState(
_truthTime,
const DoubleListState.failure(message: 'Start searching for events'),
const DoubleListState.failure(
message: 'Start searching for events',
retry: false,
),
),
var q => CalendarState(
_truthTime,
Expand Down
137 changes: 37 additions & 100 deletions lib/blocs/event_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,71 @@ import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reaxit/api/api_repository.dart';
import 'package:reaxit/api/exceptions.dart';
import 'package:reaxit/blocs/detail_state.dart';
import 'package:reaxit/blocs/list_cubit.dart';
import 'package:reaxit/blocs/list_state.dart';
import 'package:reaxit/models.dart';

class EventState extends Equatable {
/// The event, will only be null if event has not yet been loaded.
final Event? event;

/// List of all the registrations.
final List<EventRegistration> registrations;

/// 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;

const EventState({
required this.event,
required this.registrations,
required this.isLoading,
required this.message,
required this.isLoadingMore,
required this.isDone,
});

bool get hasException => message != null;

EventState copyWith({
Event? event,
List<EventRegistration>? registrations,
String? message,
bool? isLoading,
bool? isLoadingMore,
bool? isDone,
}) => EventState(
event: event ?? this.event,
registrations: registrations ?? this.registrations,
message: message ?? this.message,
isLoading: isLoading ?? this.isLoading,
isLoadingMore: isLoadingMore ?? this.isLoadingMore,
isDone: isDone ?? this.isDone,
);

@override
List<Object?> get props => [
event,
registrations,
message,
isLoading,
isLoadingMore,
isDone,
];
List<Object?> get props => [event, message, isLoading];

@override
String toString() {
return 'EventState(isLoading: $isLoading, isLoadingMore: $isLoadingMore,'
' isDone: $isDone, message: $message, event: $event, registrations: $registrations)';
return 'EventState(isLoading: $isLoading, message: $message, event: $event)';
}

const EventState.loading({this.event, required this.registrations})
: message = null,
isLoading = true,
isLoadingMore = false,
isDone = true;
const EventState.loading({this.event}) : message = null, isLoading = true;

const EventState.loadingMore({this.event, required this.registrations})
const EventState.loadingMore({this.event})
: message = null,
isLoading = false,
isLoadingMore = true,
isDone = true;

const EventState.success({
this.event,
required this.registrations,
required this.isDone,
}) : message = null,
isLoading = false,
isLoadingMore = false;
isLoading = false;

const EventState.success({this.event}) : message = null, isLoading = false;

const EventState.failure({required String this.message})
: event = null,
registrations = const [],
isLoading = false,
isLoadingMore = false,
isDone = true;
isLoading = false;
}

class EventCubit extends Cubit<EventState> {
final ApiRepository api;
final String? _eventSlug;
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;

EventCubit(this.api, {int? eventPk, String? eventSlug})
: assert(!(eventPk == null && eventSlug == null)),
_eventSlug = eventSlug,
_eventPk = eventPk,
super(const EventState.loading(registrations: []));
super(const EventState.loading());

Future<void> load() async {
emit(state.copyWith(isLoading: true));
Expand All @@ -123,27 +80,11 @@ class EventCubit extends Cubit<EventState> {

_eventPk = event.pk;

final listResponse = await api.getEventRegistrations(
pk: _eventPk!,
limit: firstPageSize,
offset: 0,
);

if (isClosed) {
return;
}

final isDone = listResponse.results.length == listResponse.count;

_nextOffset = firstPageSize;

emit(
EventState.success(
event: event,
registrations: listResponse.results,
isDone: isDone,
),
);
emit(EventState.success(event: event));
} on ApiException catch (exception) {
if (isClosed) {
// If the cubit is closed, the error does not matter at all
Expand Down Expand Up @@ -186,39 +127,35 @@ class EventCubit extends Cubit<EventState> {
await api.thaliaPayRegistration(registrationPk: registrationPk);
await load();
}
}

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

if (oldState.isDone || oldState.isLoading || oldState.isLoadingMore) return;
typedef EventListState = ListState<EventRegistration>;

emit(oldState.copyWith(isLoadingMore: true));
class EventListCubit extends SingleListCubit<EventRegistration> {
final int _eventPk;

try {
var listResponse = await api.getEventRegistrations(
pk: _eventPk!,
limit: pageSize,
offset: _nextOffset,
);
EventListCubit(super.api, this._eventPk);

final registrations = state.registrations + listResponse.results;
final isDone = registrations.length == listResponse.count;
static const int firstPageSize = 30;

_nextOffset += pageSize;
@override
Future<ListResponse<EventRegistration>> getDown(int offset) {
assert(searchQuery == null); // We cannot search registrations

emit(
EventState.success(
event: oldState.event,
registrations: registrations,
isDone: isDone,
),
);
} on ApiException catch (exception) {
emit(
EventState.failure(
message: exception.getMessage(notFound: 'The event does not exist.'),
),
);
}
return api.getEventRegistrations(
pk: _eventPk,
limit: firstPageSize,
offset: offset,
);
}

@override
List<EventRegistration> combineDown(
List<EventRegistration> downResults,
ListState<EventRegistration> oldstate,
) => getResults(oldstate) + downResults;

@override
ListState<EventRegistration> empty(String? query) =>
const ErrorState('No registrations found.');
}
10 changes: 6 additions & 4 deletions lib/blocs/groups_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class GroupsCubit extends SingleListCubit<ListGroup> {
List<ListGroup> combineDown(
List<ListGroup> downResults,
ListState<ListGroup> oldstate,
) => oldstate.results + downResults;
) => getResults(oldstate) + downResults;

@override
ListState<ListGroup> empty(String? query) => switch (query) {
null => const ListState.failure(message: 'No groups found.'),
'' => const ListState.failure(message: 'Start searching for groups'),
var q => ListState.failure(message: 'No groups found found for query "$q"'),
null => const ErrorState('No groups found.'),
'' => const ResultState(
InnerListState.failure(message: 'Start searching for groups'),
),
var q => ErrorState('No groups found found for query "$q"'),
};

@override
Expand Down
Loading
Loading