-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update MutableCart extension, add corresponding tests * Use dateFormatterProvider and currencyFormatterProvider rather than global formatters * Set addDelay to false for faster loading * Add FakeLocalCartRepository and FakeRemoteCartRepository * Cleanup LocalCartRepository, RemoteCartRepository * Update MutableCart extension and tests * Updated to latest package versions * Improved documentation # Conflicts: # ecommerce_app/pubspec.lock # ecommerce_app/pubspec.yaml
- Loading branch information
Showing
19 changed files
with
332 additions
and
84 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
ecommerce_app/lib/src/features/cart/data/local/fake_local_cart_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'package:ecommerce_app/src/features/cart/data/local/local_cart_repository.dart'; | ||
import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; | ||
import 'package:ecommerce_app/src/utils/delay.dart'; | ||
import 'package:ecommerce_app/src/utils/in_memory_store.dart'; | ||
|
||
class FakeLocalCartRepository implements LocalCartRepository { | ||
FakeLocalCartRepository({this.addDelay = true}); | ||
final bool addDelay; | ||
|
||
final _cart = InMemoryStore<Cart>(const Cart()); | ||
|
||
@override | ||
Future<Cart> fetchCart() => Future.value(_cart.value); | ||
|
||
@override | ||
Stream<Cart> watchCart() => _cart.stream; | ||
|
||
@override | ||
Future<void> setCart(Cart cart) async { | ||
await delay(addDelay); | ||
_cart.value = cart; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
ecommerce_app/lib/src/features/cart/data/local/local_cart_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; | ||
|
||
/// API for reading, watching and writing local cart data (guest user) | ||
abstract class LocalCartRepository { | ||
Future<Cart> fetchCart(); | ||
|
||
Stream<Cart> watchCart(); | ||
|
||
Future<void> setCart(Cart cart); | ||
} | ||
|
||
final localCartRepositoryProvider = Provider<LocalCartRepository>((ref) { | ||
// * Override this in the main method | ||
throw UnimplementedError(); | ||
}); |
35 changes: 35 additions & 0 deletions
35
ecommerce_app/lib/src/features/cart/data/remote/fake_remote_cart_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:ecommerce_app/src/features/cart/data/remote/remote_cart_repository.dart'; | ||
import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; | ||
import 'package:ecommerce_app/src/utils/delay.dart'; | ||
import 'package:ecommerce_app/src/utils/in_memory_store.dart'; | ||
|
||
class FakeRemoteCartRepository implements RemoteCartRepository { | ||
FakeRemoteCartRepository({this.addDelay = true}); | ||
final bool addDelay; | ||
|
||
/// An InMemoryStore containing the shopping cart data for all users, where: | ||
/// key: uid of the user | ||
/// value: Cart of that user | ||
final _carts = InMemoryStore<Map<String, Cart>>({}); | ||
|
||
@override | ||
Future<Cart> fetchCart(String uid) { | ||
return Future.value(_carts.value[uid] ?? const Cart()); | ||
} | ||
|
||
@override | ||
Stream<Cart> watchCart(String uid) { | ||
return _carts.stream.map((cartData) => cartData[uid] ?? const Cart()); | ||
} | ||
|
||
@override | ||
Future<void> setCart(String uid, Cart cart) async { | ||
await delay(addDelay); | ||
// First, get the current carts data for all users | ||
final carts = _carts.value; | ||
// Then, set the cart for the given uid | ||
carts[uid] = cart; | ||
// Finally, update the carts data (will emit a new value) | ||
_carts.value = carts; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ecommerce_app/lib/src/features/cart/data/remote/remote_cart_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:ecommerce_app/src/features/cart/data/remote/fake_remote_cart_repository.dart'; | ||
import 'package:ecommerce_app/src/features/cart/domain/cart.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
/// API for reading, watching and writing cart data for a specific user ID | ||
abstract class RemoteCartRepository { | ||
Future<Cart> fetchCart(String uid); | ||
|
||
Stream<Cart> watchCart(String uid); | ||
|
||
Future<void> setCart(String uid, Cart cart); | ||
} | ||
|
||
final remoteCartRepositoryProvider = Provider<RemoteCartRepository>((ref) { | ||
// TODO: replace with "real" remote cart repository | ||
return FakeRemoteCartRepository(); | ||
}); |
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
9 changes: 5 additions & 4 deletions
9
ecommerce_app/lib/src/features/cart/presentation/cart_total/cart_total_text.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
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
5 changes: 3 additions & 2 deletions
5
ecommerce_app/lib/src/features/products/presentation/home_app_bar/shopping_cart_icon.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
Oops, something went wrong.