Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that the food prices are always visible #518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
104 changes: 43 additions & 61 deletions lib/blocs/food_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,70 +1,52 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:reaxit/api/api_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:reaxit/api/exceptions.dart';
import 'package:reaxit/models.dart';

class FoodState extends Equatable {
sealed class FoodState extends Equatable {
const FoodState();

@override
List<Object?> get props => [];
}

/// FoodEvent is loading.
class LoadingFoodState extends FoodState {
final LoadedFoodState? oldState;

@override
List<Object?> get props => [oldState];

LoadingFoodState({FoodState? oldState})
: oldState = switch (oldState) {
LoadedFoodState state => state,
_ => null,
};
}

/// FoodEvent was unable to load.
class ErrorFoodState extends FoodState {
final String message;

@override
List<Object?> get props => [message];

const ErrorFoodState(this.message);
}

/// FoodEvent has been loaded.
class LoadedFoodState extends FoodState {
/// This can only be null when [isLoading] or [hasException] is true.
final FoodEvent? foodEvent;
final FoodEvent foodEvent;

/// This can only be null when [isLoading] or [hasException] is true.
final List<Product>? products;

/// A message describing why there are no foodEvents.
final String? message;

/// A foodEvent is being loaded. If there
/// already is a foodEvent, it is outdated.
final bool isLoading;

bool get hasException => message != null;

@protected
const FoodState({
required this.foodEvent,
required this.products,
required this.isLoading,
required this.message,
}) : assert(
(foodEvent != null && products != null) ||
isLoading ||
message != null,
'foodEvent and products can only be null '
'when isLoading or hasException is true.',
);
final List<Product> products;

@override
List<Object?> get props => [foodEvent, products, message, isLoading];

FoodState copyWith({
FoodEvent? foodEvent,
List<Product>? products,
bool? isLoading,
String? message,
}) =>
FoodState(
foodEvent: foodEvent ?? this.foodEvent,
products: products ?? this.products,
isLoading: isLoading ?? this.isLoading,
message: message ?? this.message,
);

const FoodState.result({
required FoodEvent this.foodEvent,
required List<Product> this.products,
}) : message = null,
isLoading = false;

const FoodState.loading({this.foodEvent, this.products})
: message = null,
isLoading = true;

const FoodState.failure({required String this.message})
: foodEvent = null,
products = null,
isLoading = false;
List<Object?> get props => [foodEvent, products];

const LoadedFoodState(this.foodEvent, this.products);
}

class FoodCubit extends Cubit<FoodState> {
Expand All @@ -74,10 +56,10 @@ class FoodCubit extends Cubit<FoodState> {

FoodCubit(this.api, {int? foodEventPk})
: _foodEventPk = foodEventPk,
super(const FoodState.loading());
super(LoadingFoodState());

Future<void> load() async {
emit(state.copyWith(isLoading: true));
emit(LoadingFoodState(oldState: state));
try {
late final FoodEvent event;
if (_foodEventPk == null) {
Expand All @@ -88,10 +70,10 @@ class FoodCubit extends Cubit<FoodState> {
}

final products = await api.getFoodEventProducts(_foodEventPk!);
emit(FoodState.result(foodEvent: event, products: products.results));
emit(LoadedFoodState(event, products.results));
} on ApiException catch (exception) {
emit(FoodState.failure(
message: exception.getMessage(
emit(ErrorFoodState(
exception.getMessage(
notFound: 'The food event does not exist.',
),
));
Expand Down
6 changes: 5 additions & 1 deletion lib/models/food_event.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:reaxit/models.dart';

part 'food_event.g.dart';

@JsonSerializable(fieldRename: FieldRename.snake)
class FoodEvent {
class FoodEvent extends Equatable {
final int pk;
final String title;
final Event event;
Expand All @@ -13,6 +14,9 @@ class FoodEvent {
final bool canManage;
final FoodOrder? order;

@override
List<Object?> get props => [pk, title, event, start, end, canManage, order];

bool get hasOrder => order != null;

bool hasEnded() => DateTime.now().isAfter(end);
Expand Down
Loading
Loading