Skip to content
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
3 changes: 1 addition & 2 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"flutterSdkVersion": "3.22.3",
"flavors": {}
"flutterSdkVersion": "3.22.3"
}
4 changes: 4 additions & 0 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutter": "3.22.3",
"flavors": {}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ app.*.map.json
/android/app/release

# fvm
.fvm/flutter_sdk

# FVM Version Cache
.fvm/
14 changes: 7 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"dart.flutterSdkPath": ".fvm/flutter_sdk",
"search.exclude": {
"**/.fvm": true
},
"files.watcherExclude": {
"**/.fvm": true
}
"dart.flutterSdkPath": ".fvm/versions/3.22.3",
"search.exclude": {
"**/.fvm": true
},
"files.watcherExclude": {
"**/.fvm": true
}
}
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
applicationId = "com.example.restaurant_tour"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = flutter.minSdkVersion
minSdk = 23
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
Expand Down
3 changes: 3 additions & 0 deletions assets/icons/heart_empty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/icons/heart_filled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions lib/core/utils/app_exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AppException implements Exception {
final String? message;

AppException({this.message});
}

class ServerException implements Exception {
final String? message;

ServerException({this.message});
}
13 changes: 13 additions & 0 deletions lib/core/utils/app_failures.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
abstract class Failure {
final String? message;

Failure({this.message});
}

class ServerFailure extends Failure {
ServerFailure({super.message});
}

class AppFailure extends Failure {
AppFailure({super.message});
}
6 changes: 6 additions & 0 deletions lib/core/utils/app_keys.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AppKeys {
static const loadingRestaurantsIndicator = "LoadingRestaurantsIndicator";
static const listRestaurantsFetched = "ListRestaurantsFetched";
static const listRestaurantsError = "ListRestaurantsErrorWidget";
static const restaurantListIsEmpty = "RestaurantListIsEmpty";
}
48 changes: 48 additions & 0 deletions lib/di.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';
import 'package:restaurant_tour/features/restaurant/data/data_sources/remote_datasource.dart';
import 'package:restaurant_tour/features/restaurant/data/repositories/yelp_repository_impl.dart';
import 'package:restaurant_tour/features/restaurant/domain/repositories/yelp_repository.dart';
import 'package:restaurant_tour/features/restaurant/domain/use_cases/get_favorites.dart';
import 'package:restaurant_tour/features/restaurant/domain/use_cases/get_restaurants.dart';
import 'package:restaurant_tour/features/restaurant/domain/use_cases/mark_favorite.dart';
import 'package:restaurant_tour/features/restaurant/presentation/manager/home_cubit.dart';

final getIt = GetIt.instance;

void setupDependencies() {
const _apiKey =
'jvWqxC5xhVHIwruAaM4dgsO9Gk6j_nfPeeErabJvh6LllVAVNj1fr4GcWzHXq5JIRlCgiPudUO45KW7g8Wsxmx_sJcwx3YMbD8yAcvbm_0bF_zyMzNbb8UgdE07aZnYx';
getIt.registerLazySingleton<Dio>(
() => Dio(
BaseOptions(
baseUrl: 'https://api.yelp.com',
headers: {
'Authorization': 'Bearer $_apiKey',
'Content-Type': 'application/graphql',
},
),
),
);
getIt.registerLazySingleton<RemoteDatasource>(
() => RemoteDatasourceImpl(dio: getIt()),
);

getIt.registerLazySingleton<YelpRepository>(
() => YelpRepositoryImpl(datasource: getIt()),
);

getIt.registerFactory(() => GetRestaurants(getIt()));

getIt.registerFactory(() => GetFavorites(getIt()));

getIt.registerFactory(() => MarkFavorite(getIt()));

getIt.registerFactory<HomeCubit>(
() => HomeCubit(
getIt(),
getIt(),
getIt(),
),
);
}
61 changes: 61 additions & 0 deletions lib/features/restaurant/data/data_sources/remote_datasource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:dio/dio.dart';
import 'package:restaurant_tour/core/utils/app_exceptions.dart';

abstract class RemoteDatasource {
Future<Map<String, dynamic>> getRestaurants({int offset = 0});
}

class RemoteDatasourceImpl implements RemoteDatasource {
final Dio dio;

RemoteDatasourceImpl({required this.dio});

@override
Future<Map<String, dynamic>> getRestaurants({int offset = 0}) async {
final Response response = await dio.post(
'/v3/graphql',
data: _getQuery(offset),
);

if (response.statusCode != 200) throw ServerException();

return response.data;
}

String _getQuery(int offset) {
return '''
query getRestaurants {
search(location: "Las Vegas", limit: 20, offset: \\$offset) {
total
business {
id
name
price
rating
photos
reviews {
id
rating
text
user {
id
image_url
name
}
}
categories {
title
alias
}
hours {
is_open_now
}
location {
formatted_address
}
}
}
}
''';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ class Restaurant {
final List<Hours>? hours;
final List<Review>? reviews;
final Location? location;
bool favorite;

const Restaurant({
Restaurant({
this.id,
this.name,
this.price,
Expand All @@ -106,6 +107,7 @@ class Restaurant {
this.hours,
this.reviews,
this.location,
this.favorite = false,
});

factory Restaurant.fromJson(Map<String, dynamic> json) =>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions lib/features/restaurant/data/repositories/yelp_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'package:dartz/dartz.dart';
import 'package:hive/hive.dart';
import 'package:restaurant_tour/core/utils/app_exceptions.dart';
import 'package:restaurant_tour/core/utils/app_failures.dart';
import 'package:restaurant_tour/features/restaurant/data/data_sources/remote_datasource.dart';
import 'package:restaurant_tour/features/restaurant/data/models/restaurant.dart';
import 'package:restaurant_tour/features/restaurant/domain/repositories/yelp_repository.dart';

class YelpRepositoryImpl implements YelpRepository {
final RemoteDatasource datasource;

YelpRepositoryImpl({
required this.datasource,
});

@override
Future<Either<Failure, List<Restaurant>>> getRestaurantsFromRepo(
{int offset = 0}) async {
try {
List<Restaurant> restaurants = [];
final result = await datasource.getRestaurants(offset: offset);
for (final r in result["data"]["search"]["business"]) {
restaurants.add(Restaurant.fromJson(r));
}
return Right(restaurants);
} on ServerException catch (e) {
return Left(ServerFailure(message: e.message));
} catch (e) {
return Left(AppFailure());
}
}

@override
Future markFavorite(String id) async {
if (await Hive.boxExists("favorites")) {
final box = Hive.box("favorites");
if (!box.values.contains(id)) {
box.add(id);
}
} else {
Hive.openBox("favorites");
final box = Hive.box("favorites");
if (!box.values.contains(id)) {
box.add(id);
}
}
}

@override
Future<Either<Failure, List<String>>> getFavorites() async {
try {
if (await Hive.boxExists("favorites")) {
final box = await Hive.openBox("favorites");
List<String> favorites = [];
for (final b in box.values) {
favorites.add(b);
}
return Right(favorites);
} else {
return const Right([]);
}
} catch (e) {
return Left(AppFailure());
}
}

/// Returns a response in this shape
/// {
/// "data": {
/// "search": {
/// "total": 5056,
/// "business": [
/// {
/// "id": "faPVqws-x-5k2CQKDNtHxw",
/// "name": "Yardbird Southern Table & Bar",
/// "price": "$$",
/// "rating": 4.5,
/// "photos": [
/// "https:///s3-media4.fl.yelpcdn.com/bphoto/_zXRdYX4r1OBfF86xKMbDw/o.jpg"
/// ],
/// "reviews": [
/// {
/// "id": "sjZoO8wcK1NeGJFDk5i82Q",
/// "rating": 5,
/// "user": {
/// "id": "BuBCkWFNT_O2dbSnBZvpoQ",
/// "image_url": "https:///s3-media2.fl.yelpcdn.com/photo/v8tbTjYaFvkzh1d7iE-pcQ/o.jpg",
/// "name": "Gina T.",
/// "text": "I love this place! The food is amazing and the service is great."
/// }
/// },
/// {
/// "id": "okpO9hfpxQXssbTZTKq9hA",
/// "rating": 5,
/// "user": {
/// "id": "0x9xu_b0Ct_6hG6jaxpztw",
/// "image_url": "https:///s3-media3.fl.yelpcdn.com/photo/gjz8X6tqE3e4praK4HfCiA/o.jpg",
/// "name": "Crystal L.",
/// "text": "Greate place to eat"
/// }
/// },
/// ...
/// ]
/// }
/// }
///
}
12 changes: 12 additions & 0 deletions lib/features/restaurant/domain/repositories/yelp_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:dartz/dartz.dart';
import 'package:restaurant_tour/core/utils/app_failures.dart';
import 'package:restaurant_tour/features/restaurant/data/models/restaurant.dart';

abstract class YelpRepository {
Future<Either<Failure, List<Restaurant>>> getRestaurantsFromRepo(
{int offset = 0});

Future markFavorite(String id);

Future<Either<Failure, List<String>>> getFavorites();
}
13 changes: 13 additions & 0 deletions lib/features/restaurant/domain/use_cases/get_favorites.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:dartz/dartz.dart';
import 'package:restaurant_tour/core/utils/app_failures.dart';
import 'package:restaurant_tour/features/restaurant/domain/repositories/yelp_repository.dart';

class GetFavorites {
final YelpRepository repository;

GetFavorites(this.repository);

Future<Either<Failure, List<String>>> call() async {
return await repository.getFavorites();
}
}
14 changes: 14 additions & 0 deletions lib/features/restaurant/domain/use_cases/get_restaurants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:dartz/dartz.dart';
import 'package:restaurant_tour/core/utils/app_failures.dart';
import 'package:restaurant_tour/features/restaurant/data/models/restaurant.dart';
import 'package:restaurant_tour/features/restaurant/domain/repositories/yelp_repository.dart';

class GetRestaurants {
final YelpRepository repository;

GetRestaurants(this.repository);

Future<Either<Failure, List<Restaurant>>> call() async {
return repository.getRestaurantsFromRepo();
}
}
11 changes: 11 additions & 0 deletions lib/features/restaurant/domain/use_cases/mark_favorite.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:restaurant_tour/features/restaurant/domain/repositories/yelp_repository.dart';

class MarkFavorite {
final YelpRepository repository;

MarkFavorite(this.repository);

Future<void> call(String id) async {
await repository.markFavorite(id);
}
}
Loading