This repository was archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Feat/jackson cuevas test #13
Open
jcuevashub
wants to merge
12
commits into
Superformula:master
Choose a base branch
from
jcuevashub:feat/jackson_cuevas_test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b28ff34
feat: Created the home view to show the tabs. I’m using MVVM. BREAKI…
jcuevashub dbf10f4
feat: Created the restaurant list view, view model to show the list …
jcuevashub 79dfa43
feat: Added the theme file for a global design and constants file for…
jcuevashub 3b87bab
feat: Create the restaurant details with the ability to add favorite …
jcuevashub 949dd71
feat: Some units and integrations test for Restaurant View Model and …
jcuevashub dcde9c0
feat: Updated the pubspec tile with the necessaries packages for this…
jcuevashub c301ca7
feat: Updated the yelp repository and using the data examples because…
jcuevashub 128dd27
fix: refactored the code.
jcuevashub d0ec6ef
feat: Addded some hardcode texts just to show the comments space and …
jcuevashub 9c6ea21
Feature: Implemented a caching system to eliminate redundant data ret…
jcuevashub b0eb2d8
feat: Added the dotnet package.
jcuevashub 3d91c0a
fix: Improve the code and some refactored.
jcuevashub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
final ThemeData lightTheme = ThemeData( | ||
brightness: Brightness.light, | ||
primaryColor: Colors.white, | ||
fontFamily: 'Montserrat', | ||
textTheme: const TextTheme( | ||
displayLarge: TextStyle(fontSize: 22.0, fontWeight: FontWeight.bold), | ||
titleLarge: TextStyle(fontSize: 16.0, fontStyle: FontStyle.italic), | ||
bodyMedium: TextStyle(fontSize: 14.0, fontFamily: 'Hind'), | ||
), | ||
appBarTheme: const AppBarTheme( | ||
color: Colors.white, | ||
elevation: 0, | ||
iconTheme: IconThemeData(color: Colors.black), | ||
titleTextStyle: TextStyle( | ||
color: Colors.black, fontSize: 20.0, fontWeight: FontWeight.bold), | ||
), | ||
tabBarTheme: const TabBarTheme( | ||
labelColor: Colors.black, | ||
unselectedLabelColor: Colors.grey, | ||
indicator: UnderlineTabIndicator( | ||
borderSide: BorderSide(color: Colors.black, width: 2.0), | ||
), | ||
), | ||
cardTheme: CardTheme( | ||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)), | ||
elevation: 4.0, | ||
margin: const EdgeInsets.all(10.0), | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
abstract class Constants { | ||
static const appName = 'RestauranTour'; | ||
static const allrestaurants = 'All Restaurant'; | ||
static const myFavorites = 'My Favorites'; | ||
static const openNow = 'Open Now'; | ||
static const closed = 'Closed'; | ||
static const address = 'Address'; | ||
static const overallRating = 'Overall Rating'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
import '../../../models/restaurant.dart'; | ||
|
||
class Utils { | ||
static Future<String> get _localPath async { | ||
final directory = await getApplicationDocumentsDirectory(); | ||
return directory.path; | ||
} | ||
|
||
static Future<File> get _localFile async { | ||
final path = await _localPath; | ||
return File('$path/favorites.json'); | ||
} | ||
|
||
static Future<List<Restaurant>> loadFavorites() async { | ||
try { | ||
final file = await _localFile; | ||
if (await file.exists()) { | ||
final contents = await file.readAsString(); | ||
final List<dynamic> jsonData = json.decode(contents); | ||
return jsonData.map((item) => Restaurant.fromJson(item)).toList(); | ||
} | ||
return []; | ||
} catch (e) { | ||
// Handle the exception, possibly logging or showing a message to the user | ||
return []; | ||
} | ||
} | ||
|
||
static Future<void> addFavorite(Restaurant restaurant) async { | ||
final favorites = await loadFavorites(); | ||
if (!favorites.any((element) => element.id == restaurant.id)) { | ||
favorites.add(restaurant); | ||
await _saveToFile(favorites); | ||
} | ||
} | ||
|
||
static Future<void> removeFavorite(String id) async { | ||
final favorites = await loadFavorites(); | ||
favorites.removeWhere((restaurant) => restaurant.id == id); | ||
await _saveToFile(favorites); | ||
} | ||
|
||
static Future<void> _saveToFile(List<Restaurant> restaurants) async { | ||
final file = await _localFile; | ||
final String jsonString = | ||
json.encode(restaurants.map((e) => e.toJson()).toList()); | ||
await file.writeAsString(jsonString); | ||
} | ||
|
||
static Future<void> toggleFavorite(Restaurant restaurant) async { | ||
final favorites = await loadFavorites(); | ||
final existingIndex = favorites.indexWhere((r) => r.id == restaurant.id); | ||
|
||
if (existingIndex >= 0) { | ||
// Restaurant is already a favorite, remove it | ||
await removeFavorite(restaurant.id!); | ||
} else { | ||
// Restaurant is not a favorite, add it | ||
await addFavorite(restaurant); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:stacked/stacked.dart'; | ||
|
||
class HomeViewModel extends BaseViewModel { | ||
late BuildContext context; | ||
HomeViewModel(this.context); | ||
|
||
bool _isLoading = false; | ||
bool get isLoading => _isLoading; | ||
set isLoading(bool value) { | ||
_isLoading = value; | ||
notifyListeners(); | ||
} | ||
|
||
ready() async { | ||
isLoading = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: seems like this viewmodel is not needed, what do you think? |
||
isLoading = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:restaurantour/common/constants.dart'; | ||
import 'package:stacked/stacked.dart'; | ||
|
||
import '../../restaurant/views/favorites_restaurants_list_view.dart'; | ||
import '../../restaurant/views/restaurants_list_view.dart'; | ||
import '../view_models/home_view_model.dart'; | ||
|
||
class HomeView extends StatefulWidget { | ||
const HomeView({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<HomeView> createState() => _HomeViewState(); | ||
} | ||
|
||
class _HomeViewState extends State<HomeView> | ||
with SingleTickerProviderStateMixin { | ||
late TabController _tabController; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_tabController = TabController(vsync: this, length: 2); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_tabController.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ViewModelBuilder.reactive( | ||
viewModelBuilder: () => HomeViewModel(context), | ||
onViewModelReady: (HomeViewModel viewModel) => viewModel.ready(), | ||
builder: (context, HomeViewModel viewModel, child) => Scaffold( | ||
appBar: AppBar( | ||
title: const Text(Constants.appName), | ||
bottom: TabBar( | ||
indicatorSize: TabBarIndicatorSize.label, | ||
controller: _tabController, | ||
tabAlignment: TabAlignment.center, | ||
tabs: const [ | ||
Tab( | ||
text: Constants.allrestaurants, | ||
), | ||
Tab( | ||
text: Constants.myFavorites, | ||
), | ||
], | ||
), | ||
), | ||
body: TabBarView( | ||
controller: _tabController, | ||
children: const [ | ||
RestaurantsListView(), | ||
FavoritesRestaurantsListView() | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
lib/modules/restaurant/view_models/restaurant_view_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:restaurantour/models/restaurant.dart'; | ||
import 'package:stacked/stacked.dart'; | ||
|
||
import '../../../repositories/yelp_repository.dart'; | ||
|
||
class RestaurantViewModel extends BaseViewModel { | ||
YelpRepository yelpRepo; | ||
List<Restaurant>? restaurants; | ||
|
||
RestaurantViewModel({required this.yelpRepo}); | ||
|
||
ready() async {} | ||
|
||
Future<List<Restaurant>> fetchData() async { | ||
try { | ||
final result = await yelpRepo.getRestaurants(); | ||
return result!.restaurants!; | ||
} catch (e) { | ||
throw Exception(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: who catches this exception? |
||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
lib/modules/restaurant/views/favorites_restaurants_list_view.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:restaurantour/common/utils.dart'; | ||
|
||
import '../../../models/restaurant.dart'; | ||
import '../../../widgets/restaurant_card_widget.dart'; | ||
import 'restaurant_detail_view.dart'; | ||
|
||
class FavoritesRestaurantsListView extends StatelessWidget { | ||
const FavoritesRestaurantsListView({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FutureBuilder<List<Restaurant>>( | ||
future: Utils.loadFavorites(), | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState != ConnectionState.done) { | ||
return const Center( | ||
child: CircularProgressIndicator( | ||
color: Colors.black, | ||
)); | ||
} | ||
|
||
if (snapshot.hasData) { | ||
return ListView.builder( | ||
itemCount: snapshot.data!.length, | ||
itemBuilder: (context, index) { | ||
var restaurant = snapshot.data![index]; | ||
return InkWell( | ||
onTap: () => { | ||
Navigator.push( | ||
context, | ||
MaterialPageRoute( | ||
builder: (context) => RestaurantDetailView( | ||
restaurantIndex: index, | ||
restaurant: restaurant, | ||
), | ||
), | ||
), | ||
}, | ||
child: RestaurantCardWidget( | ||
restaurantIndex: index, | ||
restaurant: restaurant, | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
|
||
if (snapshot.hasError) { | ||
return Center(child: Text('Error: ${snapshot.error}')); | ||
} | ||
|
||
return const Center( | ||
child: Text('Press a button to start fetching data'), | ||
); | ||
}, | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: avoid_relative_lib_imports