feat: add interface and local service (#11)

Co-authored-by: mike doornenbal <mikedoornenbal9@icloud.com>
This commit is contained in:
mike doornenbal 2024-07-03 11:46:29 +02:00 committed by GitHub
parent ee377c107c
commit fd8afbde03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 266 additions and 4 deletions

View file

@ -7,7 +7,7 @@ abstract class ShopInterface {
});
/// Shop id
final int id;
final String id;
/// Shop name
final String name;
@ -21,7 +21,7 @@ class Shop implements ShopInterface {
required this.name,
});
@override
final int id;
final String id;
@override
final String name;

View file

@ -4,10 +4,10 @@ import "package:flutter_shopping_interface/src/model/product.dart";
/// Product service
abstract class ProductService with ChangeNotifier {
/// Retrieve a list of products
Future<List<Product>> getProducts(int shopId);
Future<List<Product>> getProducts(String shopId);
/// Retrieve a product
Future<Product> getProduct(int id);
Future<Product> getProduct(String id);
/// Retrieve a list of categories
List<String> getCategories();

View file

@ -6,4 +6,10 @@ import "package:flutter_shopping_interface/src/model/shop.dart";
abstract class ShopService with ChangeNotifier {
/// Retrieve a list of shops
Future<List<Shop>> getShops();
/// Select a shop
void selectShop(Shop shop);
/// The currently selected shop
Shop? get selectedShop;
}

View file

@ -0,0 +1,9 @@
include: package:flutter_iconica_analysis/components_options.yaml
# Possible to overwrite the rules from the package
analyzer:
exclude:
linter:
rules:

View file

@ -0,0 +1 @@
export "service/service.dart";

View file

@ -0,0 +1,15 @@
import "package:flutter/material.dart";
import "package:flutter_shopping_interface/flutter_shopping_interface.dart";
/// Local order service
class LocalOrderService with ChangeNotifier implements OrderService {
@override
Future<void> createOrder(
int shopId,
List<Product> products,
Map<String, dynamic> clientInformation,
) {
// No use case for this method yet
throw UnimplementedError();
}
}

View file

@ -0,0 +1,65 @@
import "package:flutter/material.dart";
import "package:flutter_shopping_interface/flutter_shopping_interface.dart";
/// Local product service
class LocalProductService with ChangeNotifier implements ProductService {
List<Product> _products = [];
@override
List<String> getCategories() =>
_products.map((e) => e.category).toSet().toList();
@override
Future<Product> getProduct(String id) =>
Future.value(_products.firstWhere((element) => element.id == id));
@override
Future<List<Product>> getProducts(String shopId) async {
await Future.delayed(const Duration(seconds: 1));
_products = [
Product(
id: "1",
name: "White Bread",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/appshell-demo"
".appspot.com/o/shopping%2Fwhite.png"
"?alt=media&token=e3aa13d5-932a-4119-bdbb-89c1a1d82213",
category: "Bread",
price: 1.0,
description: "This is a delicious white bread",
hasDiscount: true,
discountPrice: 0.5,
),
Product(
id: "2",
name: "Brown Bread",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/appshell-"
"demo.appspot.com/o/shopping%2Fbrown.png?alt=media&"
"token=fbfe280d-44e6-4cde-a491-bcb7f598d22c",
category: "Bread",
price: 2.0,
description: "This is a delicious brown bread",
),
Product(
id: "3",
name: "White fish",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/appshell"
"-demo.appspot.com/o/shopping%2Fwhite-fish.png?alt=media"
"&token=61c44f84-347d-4b42-a10d-c172f167929b",
category: "Fish",
price: 1.5,
description: "This is a delicious white fish",
),
Product(
id: "4",
name: "Brown fish",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/appshel"
"l-demo.appspot.com/o/shopping%2Fbrown-fish.png?alt=media&"
"token=b743a53c-c4bb-49ac-8894-a08132992902",
category: "Fish",
price: 1.5,
description: "This is a delicious Brown fish",
),
];
return Future.value(_products);
}
}

View file

@ -0,0 +1,35 @@
import "package:flutter/material.dart";
import "package:flutter_shopping_interface/flutter_shopping_interface.dart";
/// A service that provides a list of shops.
class LocalShopService with ChangeNotifier implements ShopService {
Shop? _selectedShop;
@override
Future<List<Shop>> getShops() async {
await Future.delayed(const Duration(seconds: 1));
var shops = <Shop>[
const Shop(id: "1", name: "Bakkerij de Goudkorst"),
const Shop(id: "2", name: "Slagerij PuurVlees"),
const Shop(id: "3", name: "De GroenteHut"),
const Shop(id: "4", name: "Pizzeria Ciao"),
const Shop(id: "5", name: "Cafetaria Roos"),
const Shop(id: "6", name: "Zeebries Visdelicatessen"),
const Shop(id: "7", name: "De Oosterse Draak"),
];
return Future.value(shops);
}
/// Updates the selected shop.
@override
void selectShop(Shop shop) {
if (_selectedShop == shop) return;
_selectedShop = shop;
notifyListeners();
}
/// The currently selected shop.
@override
Shop? get selectedShop => _selectedShop;
}

View file

@ -0,0 +1,57 @@
import "package:flutter/material.dart";
import "package:flutter_shopping_interface/flutter_shopping_interface.dart";
/// Local shopping cart service
class LocalShoppingCartService
with ChangeNotifier
implements ShoppingCartService {
final List<Product> _products = [];
@override
void addProduct(Product product) {
if (_products.contains(product)) {
var index = _products.indexOf(product);
_products[index].quantity++;
} else {
_products.add(product);
}
notifyListeners();
}
@override
void clear() {
_products.clear();
notifyListeners();
}
@override
int countProducts() {
var count = 0;
for (var product in _products) {
count += product.quantity;
}
notifyListeners();
return count;
}
@override
void removeOneProduct(Product product) {
if (_products.contains(product)) {
var index = _products.indexOf(product);
if (_products[index].quantity > 1) {
_products[index].quantity--;
} else {
_products.removeAt(index);
}
}
notifyListeners();
}
@override
void removeProduct(Product product) {
if (_products.contains(product)) {
var index = _products.indexOf(product);
_products.removeAt(index);
}
notifyListeners();
}
}

View file

@ -0,0 +1,45 @@
import "package:flutter_shopping_interface/flutter_shopping_interface.dart";
import "package:flutter_shopping_local/service/local_order_service.dart";
import "package:flutter_shopping_local/service/local_product_service.dart";
import "package:flutter_shopping_local/service/local_shop_service.dart";
import "package:flutter_shopping_local/service/local_shopping_cart_service.dart";
/// Local shopping service
class LocalShoppingService implements ShoppingService {
/// Local shopping service constructor
LocalShoppingService({
this.localOrderService,
this.localShopService,
this.localProductService,
this.localShoppingCartService,
}) {
localOrderService ??= LocalOrderService();
localShopService ??= LocalShopService();
localProductService ??= LocalProductService();
localShoppingCartService ??= LocalShoppingCartService();
}
/// Local order service
OrderService? localOrderService;
/// Local shop service
ShopService? localShopService;
/// Local product service
ProductService? localProductService;
/// Local shopping cart service
ShoppingCartService? localShoppingCartService;
@override
OrderService get orderService => localOrderService!;
@override
ProductService get productService => localProductService!;
@override
ShopService get shopService => localShopService!;
@override
ShoppingCartService get shoppingCartService => localShoppingCartService!;
}

View file

@ -0,0 +1 @@
export "local_shop_service.dart";

View file

@ -0,0 +1,28 @@
name: flutter_shopping_local
description: "A local Flutter module for a shopping."
version: 2.0.0
publish_to: 'none'
environment:
sdk: '>=3.3.0 <4.0.0'
flutter: ">=1.17.0"
dependencies:
flutter:
sdk: flutter
flutter_shopping_interface:
git:
url: https://github.com/Iconica-Development/flutter_shopping
path: packages/flutter_shopping_interface
ref: 2.0.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_iconica_analysis:
git:
url: https://github.com/Iconica-Development/flutter_iconica_analysis
ref: 7.0.0
flutter: