Skip to content

Commit

Permalink
Remove old liststate
Browse files Browse the repository at this point in the history
It's now used only for the calendar, as that will be refactored a lot soon.
  • Loading branch information
DeD1rk committed Jan 31, 2023
1 parent 54d62b3 commit 786b50c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 92 deletions.
83 changes: 81 additions & 2 deletions lib/blocs/calendar_cubit.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -103,7 +103,86 @@ class CalendarEvent {
);
}

typedef CalendarState = ListState<CalendarEvent>;
/// 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<CalendarEvent> 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<CalendarEvent>? 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<Object?> 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<CalendarState> {
static const int firstPageSize = 20;
Expand Down
91 changes: 5 additions & 86 deletions lib/blocs/list_state.dart
Original file line number Diff line number Diff line change
@@ -1,86 +1,5 @@
import 'package:equatable/equatable.dart';

/// Generic class to be used as state for paginated lists.
class ListState<T> extends Equatable {
/// The results to be shown. These are outdated if `isLoading` is true.
final List<T> 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<T> copyWith({
List<T>? results,
String? message,
bool? isLoading,
bool? isLoadingMore,
bool? isDone,
}) =>
ListState<T>(
results: results ?? this.results,
message: message ?? this.message,
isLoading: isLoading ?? this.isLoading,
isLoadingMore: isLoadingMore ?? this.isLoadingMore,
isDone: isDone ?? this.isDone,
);

@override
List<Object?> 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:
Expand All @@ -89,8 +8,8 @@ class ListState<T> 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<T> extends Equatable {
const XListState();
abstract class ListState<T> extends Equatable {
const ListState();

/// A convenience method to get the results if they are available.
///
Expand All @@ -108,11 +27,11 @@ abstract class XListState<T> extends Equatable {
List<Object?> get props => [];
}

class LoadingListState<T> extends XListState<T> {
class LoadingListState<T> extends ListState<T> {
const LoadingListState();
}

class ErrorListState<T> extends XListState<T> {
class ErrorListState<T> extends ListState<T> {
@override
final String message;

Expand All @@ -122,7 +41,7 @@ class ErrorListState<T> extends XListState<T> {
List<Object?> get props => [message];
}

class ResultsListState<T> extends XListState<T> {
class ResultsListState<T> extends ListState<T> {
@override
final List<T> results;

Expand Down
4 changes: 2 additions & 2 deletions lib/ui/screens/event_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class _EventScreenState extends State<EventScreen> {
);
}

SliverPadding _makeRegistrations(XListState<EventRegistration> state) {
SliverPadding _makeRegistrations(ListState<EventRegistration> state) {
if (state is ErrorListState) {
return SliverPadding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 16),
Expand Down Expand Up @@ -520,7 +520,7 @@ class _EventScreenState extends State<EventScreen> {
await _eventCubit.load();
},
child: BlocBuilder<RegistrationsCubit,
XListState<EventRegistration>>(
ListState<EventRegistration>>(
bloc: _registrationsCubit,
builder: (context, listState) {
return Scrollbar(
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/widgets/paginated_scroll_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<E> extends Cubit<XListState<E>> {
abstract class PaginatedCubit<E> extends Cubit<ListState<E>> {
final int firstPageSize;
final int pageSize;

Expand Down Expand Up @@ -76,7 +76,7 @@ class _PaginatedScrollViewState<E, B extends PaginatedCubit<E>>

@override
Widget build(BuildContext context) {
return BlocBuilder<B, XListState<E>>(
return BlocBuilder<B, ListState<E>>(
builder: (context, state) {
late final List<Widget> slivers;
if (state is ErrorListState) {
Expand Down

0 comments on commit 786b50c

Please sign in to comment.