-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # ecommerce_app/lib/src/app.dart # ecommerce_app/pubspec.lock # ecommerce_app/pubspec.yaml # Conflicts: # ecommerce_app/lib/src/features/products/presentation/product_screen/leave_review_action.dart
- Loading branch information
Showing
25 changed files
with
480 additions
and
91 deletions.
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
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
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
22 changes: 18 additions & 4 deletions
22
ecommerce_app/lib/src/features/orders/application/user_orders_provider.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 |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart'; | ||
import 'package:ecommerce_app/src/features/orders/data/fake_orders_repository.dart'; | ||
import 'package:ecommerce_app/src/features/orders/domain/order.dart'; | ||
import 'package:ecommerce_app/src/features/products/domain/product.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
/// Watch the list of user orders | ||
/// NOTE: Only watch this provider if the user is signed in. | ||
final userOrdersProvider = StreamProvider.autoDispose<List<Order>>((ref) { | ||
final user = ref.watch(authStateChangesProvider).value; | ||
if (user != null) { | ||
final ordersRepository = ref.watch(ordersRepositoryProvider); | ||
return ordersRepository.watchUserOrders(user.uid); | ||
return ref.watch(ordersRepositoryProvider).watchUserOrders(user.uid); | ||
} else { | ||
// If the user is null, just return an empty screen. | ||
return const Stream.empty(); | ||
// If the user is null, return an empty list (no orders) | ||
return Stream.value([]); | ||
} | ||
}); | ||
|
||
/// Check if a product was previously purchased by the user | ||
final matchingUserOrdersProvider = | ||
StreamProvider.autoDispose.family<List<Order>, ProductID>((ref, productId) { | ||
final user = ref.watch(authStateChangesProvider).value; | ||
if (user != null) { | ||
return ref | ||
.watch(ordersRepositoryProvider) | ||
.watchUserOrders(user.uid, productId: productId); | ||
} else { | ||
// If the user is null, return an empty list (no orders) | ||
return Stream.value([]); | ||
} | ||
}); |
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
12 changes: 0 additions & 12 deletions
12
ecommerce_app/lib/src/features/orders/domain/purchase.dart
This file was deleted.
Oops, something went wrong.
33 changes: 26 additions & 7 deletions
33
ecommerce_app/lib/src/features/products/data/fake_products_repository.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
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
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
59 changes: 59 additions & 0 deletions
59
ecommerce_app/lib/src/features/reviews/application/reviews_service.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:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart'; | ||
import 'package:ecommerce_app/src/features/products/domain/product.dart'; | ||
import 'package:ecommerce_app/src/features/reviews/data/fake_reviews_repository.dart'; | ||
import 'package:ecommerce_app/src/features/reviews/domain/review.dart'; | ||
import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
class ReviewsService { | ||
ReviewsService(this.ref); | ||
final Ref ref; | ||
|
||
Future<void> submitReview({ | ||
required ProductID productId, | ||
required Review review, | ||
}) async { | ||
final user = ref.read(authRepositoryProvider).currentUser; | ||
// * we should only call this method when the user is signed in | ||
assert(user != null); | ||
if (user == null) { | ||
throw AssertionError( | ||
'Can\'t submit a review if the user is not signed in'.hardcoded); | ||
} | ||
await ref.read(reviewsRepositoryProvider).setReview( | ||
productId: productId, | ||
uid: user.uid, | ||
review: review, | ||
); | ||
} | ||
} | ||
|
||
final reviewsServiceProvider = Provider<ReviewsService>((ref) { | ||
return ReviewsService(ref); | ||
}); | ||
|
||
/// Check if a product was previously reviewed by the user | ||
final userReviewFutureProvider = | ||
FutureProvider.autoDispose.family<Review?, ProductID>((ref, productId) { | ||
final user = ref.watch(authStateChangesProvider).value; | ||
if (user != null) { | ||
return ref | ||
.watch(reviewsRepositoryProvider) | ||
.fetchUserReview(productId, user.uid); | ||
} else { | ||
return Future.value(null); | ||
} | ||
}); | ||
|
||
/// Check if a product was previously reviewed by the user | ||
final userReviewStreamProvider = | ||
StreamProvider.autoDispose.family<Review?, ProductID>((ref, productId) { | ||
final user = ref.watch(authStateChangesProvider).value; | ||
if (user != null) { | ||
return ref | ||
.watch(reviewsRepositoryProvider) | ||
.watchUserReview(productId, user.uid); | ||
} else { | ||
return Stream.value(null); | ||
} | ||
}); |
Oops, something went wrong.