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

Superformula Flutter test Completed (David Saborio Alvarado) #4

Open
wants to merge 15 commits 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
70 changes: 70 additions & 0 deletions lib/aplication/yelp/yelp_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import 'package:restaurant_tour/domain/models/restaurant.dart';
import 'package:restaurant_tour/domain/repositories/yelp_repository.dart';
import 'package:restaurant_tour/presentation/core/utils/favorite_restaurant_utils.dart';

part 'yelp_bloc.freezed.dart';

part 'yelp_state.dart';
part 'yelp_event.dart';

@injectable
class YelpBloc extends Bloc<YelpEvent, YelpState> {
final YelpRepository _yelpRepository;

YelpBloc(this._yelpRepository) : super(YelpState.initial()) {
_setupEventActions();
}

_setupEventActions() {
on<GetRestaurantsData>(((event, emit) async {
emit(state.copyWith(isGettingData: true));

/// This delay is not need, just added to see the CircularProgressIndicator
await Future.delayed(const Duration(milliseconds: 500));
emit(await _performGetRestaurantsData(_yelpRepository.getRestaurants));
}));

on<AddFavoriteRestaurant>((event, emit) async {
final updatedFavorites = [
...state.favoriteRestaurants,
event.restaurant,
];
await FavoriteRestaurantUtils.updateFavoriteRestaurants(updatedFavorites);
emit(state.copyWith(favoriteRestaurants: updatedFavorites));
});

on<RemoveFavoriteRestaurant>((event, emit) async {
final updatedFavorites = state.favoriteRestaurants
.where(
(restaurant) => restaurant.id != event.id,
)
.toList();
await FavoriteRestaurantUtils.updateFavoriteRestaurants(updatedFavorites);
emit(state.copyWith(favoriteRestaurants: updatedFavorites));
});

on<LoadFavoriteRestaurants>((event, emit) async {
final favoriteRestaurants =
await FavoriteRestaurantUtils.getFavoriteRestaurants();
if (favoriteRestaurants.isNotEmpty) {
emit(state.copyWith(favoriteRestaurants: favoriteRestaurants));
}
});
}

Future<YelpState> _performGetRestaurantsData(
Future<RestaurantQueryResult?> Function() forwardedCall) async {
final response = await forwardedCall();

if (response != null) {
return state.copyWith(restaurantsData: response, isGettingData: false);
} else {
return state.copyWith(
errorMessage: "failureMessage", isGettingData: false);
}
}
}
Loading