diff --git a/lib/core/firebase_analytics_event_logging.dart b/lib/core/firebase_analytics_event_logging.dart index 918368848..bc4b993f4 100644 --- a/lib/core/firebase_analytics_event_logging.dart +++ b/lib/core/firebase_analytics_event_logging.dart @@ -1,4 +1,4 @@ -import 'package:coffeecard/features/product/domain/entities/product.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; import 'package:firebase_analytics/firebase_analytics.dart'; diff --git a/lib/core/network/network_request_executor.dart b/lib/core/network/network_request_executor.dart index 869c23c2b..b5904013c 100644 --- a/lib/core/network/network_request_executor.dart +++ b/lib/core/network/network_request_executor.dart @@ -7,7 +7,8 @@ import 'package:logger/logger.dart'; part 'network_request_executor_mapping.dart'; typedef _NetworkRequest = Future> Function(); -typedef _ExecutorResult = Future>; +typedef _ExecutorResult = Future>; +typedef _ExecutorTaskEither = TaskEither; class NetworkRequestExecutor { final Logger logger; @@ -43,6 +44,13 @@ class NetworkRequestExecutor { } } + /// Executes a network request inside a [TaskEither]. + /// + /// See [execute] for more information. + _ExecutorTaskEither executeAsTask(_NetworkRequest request) { + return TaskEither(() => execute(request)); + } + /// Executes the network [request] and returns the result as an [Either]. /// /// If the request fails, a [NetworkFailure] is returned in a [Left]. diff --git a/lib/core/widgets/components/barista_perks_section.dart b/lib/core/widgets/components/barista_perks_section.dart index 06fc3be73..962c129df 100644 --- a/lib/core/widgets/components/barista_perks_section.dart +++ b/lib/core/widgets/components/barista_perks_section.dart @@ -3,7 +3,7 @@ import 'package:coffeecard/core/strings.dart'; import 'package:coffeecard/core/widgets/components/helpers/grid.dart'; import 'package:coffeecard/core/widgets/components/section_title.dart'; import 'package:coffeecard/core/widgets/components/user_role_indicator.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:coffeecard/features/ticket/presentation/widgets/perk_card.dart'; import 'package:coffeecard/features/user/domain/entities/role.dart'; import 'package:flutter/material.dart'; diff --git a/lib/core/widgets/components/coffee_card.dart b/lib/core/widgets/components/tickets_card.dart similarity index 58% rename from lib/core/widgets/components/coffee_card.dart rename to lib/core/widgets/components/tickets_card.dart index b5446f9e5..b65cb6df9 100644 --- a/lib/core/widgets/components/coffee_card.dart +++ b/lib/core/widgets/components/tickets_card.dart @@ -1,38 +1,47 @@ +import 'package:coffeecard/core/strings.dart'; import 'package:coffeecard/core/styles/app_colors.dart'; import 'package:coffeecard/core/styles/app_text_styles.dart'; import 'package:coffeecard/core/widgets/components/card.dart'; +import 'package:coffeecard/core/widgets/components/helpers/shimmer_builder.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; import 'package:coffeecard/features/ticket/presentation/widgets/swipe_ticket_confirm.dart'; +import 'package:dotted_border/dotted_border.dart'; import 'package:flutter/material.dart'; -class CoffeeCard extends StatelessWidget { - final String title; - final int amountOwned; - final int productId; +part 'tickets_card_placeholder.dart'; + +/// A card representing a group of tickets owned by the user. +/// +/// See also [NoTicketsPlaceholder], which is used when the user has no tickets. +class TicketsCard extends StatelessWidget { + /// Creates a [TicketsCard] with the given [ticket]. + const TicketsCard(this.ticket); + + /// A shimmering placeholder for a [TicketsCard]. + const TicketsCard.loadingPlaceholder() : ticket = const Ticket.empty(); + + final Ticket ticket; - const CoffeeCard({ - required this.title, - required this.amountOwned, - required this.productId, - }); + bool get isLoadingPlaceholder => ticket == const Ticket.empty(); @override Widget build(BuildContext context) { - return CardBase( - color: AppColors.ticket, - top: CardTitle( - title: Text(title, style: AppTextStyle.ownedTicket), - ), - bottom: CardBottomRow( - left: _TicketDots(amountOwned: amountOwned), - right: _TicketAmountText(amountOwned: amountOwned), - ), - gap: 36, - onTap: (context) { - final _ = showSwipeTicketConfirm( - context: context, - productName: title, - amountOwned: amountOwned, - productId: productId, + return ShimmerBuilder( + showShimmer: isLoadingPlaceholder, + builder: (context, colorIfShimmer) { + return CardBase( + color: isLoadingPlaceholder ? colorIfShimmer : AppColors.ticket, + top: CardTitle( + title: Text(ticket.product.name, style: AppTextStyle.ownedTicket), + ), + bottom: CardBottomRow( + left: _TicketDots(amountOwned: ticket.amountLeft), + right: _TicketAmountText(amountOwned: ticket.amountLeft), + ), + gap: 36, + onTap: (context) { + final _ = showSwipeTicketConfirm(context: context, ticket: ticket); + }, ); }, ); diff --git a/lib/core/widgets/components/coffee_card_placeholder.dart b/lib/core/widgets/components/tickets_card_placeholder.dart similarity index 67% rename from lib/core/widgets/components/coffee_card_placeholder.dart rename to lib/core/widgets/components/tickets_card_placeholder.dart index f234e9e11..185bccb99 100644 --- a/lib/core/widgets/components/coffee_card_placeholder.dart +++ b/lib/core/widgets/components/tickets_card_placeholder.dart @@ -1,11 +1,8 @@ -import 'package:coffeecard/core/strings.dart'; -import 'package:coffeecard/core/styles/app_colors.dart'; -import 'package:coffeecard/core/styles/app_text_styles.dart'; -import 'package:dotted_border/dotted_border.dart'; -import 'package:flutter/material.dart'; +part of 'tickets_card.dart'; -class CoffeeCardPlaceholder extends StatelessWidget { - const CoffeeCardPlaceholder(); +/// A widget that shows instead of a [TicketsCard] when the user has no tickets. +class NoTicketsPlaceholder extends StatelessWidget { + const NoTicketsPlaceholder(); @override Widget build(BuildContext context) { diff --git a/lib/core/widgets/pages/home_page.dart b/lib/core/widgets/pages/home_page.dart index 27e194ec8..0dcc1bc11 100644 --- a/lib/core/widgets/pages/home_page.dart +++ b/lib/core/widgets/pages/home_page.dart @@ -8,7 +8,7 @@ import 'package:coffeecard/core/widgets/routers/app_flow.dart'; import 'package:coffeecard/features/leaderboard/presentation/cubit/leaderboard_cubit.dart'; import 'package:coffeecard/features/leaderboard/presentation/pages/leaderboard_page.dart'; import 'package:coffeecard/features/opening_hours/presentation/cubit/opening_hours_cubit.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:coffeecard/features/receipt/presentation/cubit/receipt_cubit.dart'; import 'package:coffeecard/features/receipt/presentation/pages/receipts_page.dart'; import 'package:coffeecard/features/settings/presentation/pages/settings_page.dart'; diff --git a/lib/features/authentication/domain/usecases/clear_authenticated_user.dart b/lib/features/authentication/domain/usecases/clear_authenticated_user.dart index 8ed4239ab..90c7616a1 100644 --- a/lib/features/authentication/domain/usecases/clear_authenticated_user.dart +++ b/lib/features/authentication/domain/usecases/clear_authenticated_user.dart @@ -1,4 +1,5 @@ import 'package:coffeecard/features/authentication/data/datasources/authentication_local_data_source.dart'; +import 'package:hive_flutter/hive_flutter.dart'; class ClearAuthenticatedUser { final AuthenticationLocalDataSource dataSource; @@ -6,6 +7,10 @@ class ClearAuthenticatedUser { ClearAuthenticatedUser({required this.dataSource}); Future call() async { + // Clear last used menu item + final box = await Hive.openBox('lastUsedMenuItemByProductId'); + await box.clear(); + await dataSource.clearAuthenticatedUser(); } } diff --git a/lib/features/environment/data/datasources/environment_remote_data_source.dart b/lib/features/environment/data/datasources/environment_remote_data_source.dart index ebd2b3f61..444ff59a2 100644 --- a/lib/features/environment/data/datasources/environment_remote_data_source.dart +++ b/lib/features/environment/data/datasources/environment_remote_data_source.dart @@ -13,7 +13,7 @@ class EnvironmentRemoteDataSource { final CoffeecardApiV2 apiV2; final NetworkRequestExecutor executor; - Future> getEnvironmentType() { + Future> getEnvironmentType() { return executor .execute(apiV2.apiV2AppconfigGet) .map(Environment.fromAppConfig); diff --git a/lib/features/leaderboard/data/datasources/leaderboard_remote_data_source.dart b/lib/features/leaderboard/data/datasources/leaderboard_remote_data_source.dart index d077c8d17..e17a6dee0 100644 --- a/lib/features/leaderboard/data/datasources/leaderboard_remote_data_source.dart +++ b/lib/features/leaderboard/data/datasources/leaderboard_remote_data_source.dart @@ -23,7 +23,7 @@ class LeaderboardRemoteDataSource { required this.executor, }); - Future>> getLeaderboard( + Future>> getLeaderboard( LeaderboardFilter category, int top, ) { @@ -34,7 +34,7 @@ class LeaderboardRemoteDataSource { .mapAll(LeaderboardUserModel.fromDTO); } - Future> getLeaderboardUser( + Future> getLeaderboardUser( LeaderboardFilter category, ) { return executor diff --git a/lib/features/login/data/datasources/account_remote_data_source.dart b/lib/features/login/data/datasources/account_remote_data_source.dart index 3b08b0a84..05aa915ad 100644 --- a/lib/features/login/data/datasources/account_remote_data_source.dart +++ b/lib/features/login/data/datasources/account_remote_data_source.dart @@ -54,7 +54,7 @@ class AccountRemoteDataSource { ); } - Future> resendVerificationEmail( + Future> resendVerificationEmail( String email, ) { return executor.executeAndDiscard( @@ -64,17 +64,17 @@ class AccountRemoteDataSource { ); } - Future> getUser() { + Future> getUser() { return executor.execute(apiV2.apiV2AccountGet).map(UserModel.fromResponse); } - Future> requestPasscodeReset(String email) { + Future> requestPasscodeReset(String email) { return executor.executeAndDiscard( () => apiV1.apiV1AccountForgotpasswordPost(body: EmailDto(email: email)), ); } - Future> emailExists(String email) { + Future> emailExists(String email) { final body = EmailExistsRequest(email: email); return executor .execute(() => apiV2.apiV2AccountEmailExistsPost(body: body)) diff --git a/lib/features/login/domain/usecases/resend_email.dart b/lib/features/login/domain/usecases/resend_email.dart index c5e4356f8..6b91abd8c 100644 --- a/lib/features/login/domain/usecases/resend_email.dart +++ b/lib/features/login/domain/usecases/resend_email.dart @@ -7,7 +7,7 @@ class ResendEmail { ResendEmail({required this.remoteDataSource}); - Future> call(String email) async { + Future> call(String email) async { return remoteDataSource.resendVerificationEmail(email); } } diff --git a/lib/features/occupation/data/datasources/occupation_remote_data_source.dart b/lib/features/occupation/data/datasources/occupation_remote_data_source.dart index e78388248..32fc3c753 100644 --- a/lib/features/occupation/data/datasources/occupation_remote_data_source.dart +++ b/lib/features/occupation/data/datasources/occupation_remote_data_source.dart @@ -13,7 +13,7 @@ class OccupationRemoteDataSource { required this.executor, }); - Future>> getOccupations() { + Future>> getOccupations() { return executor .execute(api.apiV1ProgrammesGet) .mapAll(OccupationModel.fromDTOV1); diff --git a/lib/features/product/data/datasources/product_remote_data_source.dart b/lib/features/product/data/datasources/product_remote_data_source.dart deleted file mode 100644 index 2db700534..000000000 --- a/lib/features/product/data/datasources/product_remote_data_source.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:coffeecard/core/errors/failures.dart'; -import 'package:coffeecard/core/network/network_request_executor.dart'; -import 'package:coffeecard/features/product/data/models/product_model.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; -import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; -import 'package:fpdart/fpdart.dart'; - -class ProductRemoteDataSource { - final CoffeecardApiV2 api; - final NetworkRequestExecutor executor; - - ProductRemoteDataSource({required this.api, required this.executor}); - - Future>> getProducts() { - return executor - .execute(api.apiV2ProductsGet) - .mapAll(ProductModel.fromResponse); - } -} diff --git a/lib/features/product/data/models/product_model.dart b/lib/features/product/data/models/product_model.dart deleted file mode 100644 index 303da1953..000000000 --- a/lib/features/product/data/models/product_model.dart +++ /dev/null @@ -1,24 +0,0 @@ -import 'package:coffeecard/features/product/domain/entities/product.dart'; -import 'package:coffeecard/generated/api/coffeecard_api_v2.models.swagger.dart'; - -class ProductModel extends Product { - const ProductModel({ - required super.price, - required super.amount, - required super.name, - required super.id, - required super.description, - required super.isPerk, - }); - - factory ProductModel.fromResponse(ProductResponse response) { - return ProductModel( - price: response.price, - amount: response.numberOfTickets, - name: response.name, - id: response.id, - description: response.description, - isPerk: response.isPerk, - ); - } -} diff --git a/lib/features/product/domain/entities/product.dart b/lib/features/product/domain/entities/product.dart deleted file mode 100644 index 8cbf8eb61..000000000 --- a/lib/features/product/domain/entities/product.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:equatable/equatable.dart'; - -class Product extends Equatable { - final int id; - final int amount; - final int price; - final String name; - final String description; - final bool isPerk; - - const Product({ - required this.id, - required this.amount, - required this.price, - required this.name, - required this.description, - required this.isPerk, - }); - - @override - List get props => [id, amount, price, name, description, isPerk]; -} diff --git a/lib/features/product/domain/entities/purchasable_products.dart b/lib/features/product/domain/entities/purchasable_products.dart deleted file mode 100644 index 732161215..000000000 --- a/lib/features/product/domain/entities/purchasable_products.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'package:coffeecard/features/product/domain/entities/product.dart'; - -typedef PurchasableProducts = ({ - Iterable clipCards, - Iterable singleDrinks, - Iterable perks, -}); diff --git a/lib/features/product/domain/usecases/get_all_products.dart b/lib/features/product/domain/usecases/get_all_products.dart deleted file mode 100644 index 828cdd390..000000000 --- a/lib/features/product/domain/usecases/get_all_products.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:coffeecard/core/errors/failures.dart'; -import 'package:coffeecard/core/extensions/either_extensions.dart'; -import 'package:coffeecard/features/product/data/datasources/product_remote_data_source.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; -import 'package:fpdart/fpdart.dart'; - -class GetAllProducts { - final ProductRemoteDataSource remoteDataSource; - - const GetAllProducts({required this.remoteDataSource}); - - Future> call() async { - return remoteDataSource.getProducts().bindFuture((products) { - return ( - clipCards: products.where((p) => p.amount > 1), - singleDrinks: products.where((p) => p.amount == 1), - perks: products.where((p) => p.isPerk), - ); - }); - } -} diff --git a/lib/features/product/menu_item_model.dart b/lib/features/product/menu_item_model.dart new file mode 100644 index 000000000..519d9e6f4 --- /dev/null +++ b/lib/features/product/menu_item_model.dart @@ -0,0 +1,15 @@ +import 'package:coffeecard/generated/api/coffeecard_api_v2.models.swagger.dart'; +import 'package:equatable/equatable.dart'; + +class MenuItem extends Equatable { + const MenuItem({required this.id, required this.name}); + + factory MenuItem.fromResponse(MenuItemResponse response) => + MenuItem(id: response.id, name: response.name); + + final int id; + final String name; + + @override + List get props => [id, name]; +} diff --git a/lib/features/product/presentation/cubit/product_cubit.dart b/lib/features/product/presentation/cubit/product_cubit.dart index ac4b612f1..e8bd83963 100644 --- a/lib/features/product/presentation/cubit/product_cubit.dart +++ b/lib/features/product/presentation/cubit/product_cubit.dart @@ -1,19 +1,21 @@ import 'package:coffeecard/core/errors/failures.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; -import 'package:coffeecard/features/product/domain/usecases/get_all_products.dart'; +import 'package:coffeecard/features/product/product_model.dart'; +import 'package:coffeecard/features/product/product_repository.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; part 'product_state.dart'; class ProductCubit extends Cubit { - final GetAllProducts getAllProducts; + final ProductRepository productRepository; - ProductCubit({required this.getAllProducts}) : super(const ProductsLoading()); + ProductCubit({required this.productRepository}) + : super(const ProductsLoading()); - Future getProducts() async { - emit(const ProductsLoading()); - final result = await getAllProducts(); - emit(result.fold(ProductsError.fromFailure, ProductsLoaded.new)); - } + Future getProducts() => productRepository + .getProducts() + .match(ProductsError.new, ProductsLoaded.new) + .map(emit) + .run(); } diff --git a/lib/features/product/presentation/cubit/product_state.dart b/lib/features/product/presentation/cubit/product_state.dart index c423cccb8..ed5091a10 100644 --- a/lib/features/product/presentation/cubit/product_state.dart +++ b/lib/features/product/presentation/cubit/product_state.dart @@ -14,7 +14,12 @@ class ProductsLoading extends ProductState { class ProductsLoaded extends ProductState { final PurchasableProducts products; - const ProductsLoaded(this.products); + ProductsLoaded(Iterable products) + : products = ( + clipCards: products.where((p) => p.amount > 1), + singleDrinks: products.where((p) => p.amount == 1), + perks: products.where((p) => p.isPerk), + ); @override List get props => [products]; @@ -23,8 +28,7 @@ class ProductsLoaded extends ProductState { class ProductsError extends ProductState { final String error; - const ProductsError(this.error); - ProductsError.fromFailure(Failure failure) : error = failure.reason; + ProductsError(Failure failure) : error = failure.reason; @override List get props => [error]; diff --git a/lib/features/product/presentation/functions.dart b/lib/features/product/presentation/functions.dart index 97047283b..aea276bba 100644 --- a/lib/features/product/presentation/functions.dart +++ b/lib/features/product/presentation/functions.dart @@ -2,8 +2,8 @@ import 'package:coffeecard/core/strings.dart'; import 'package:coffeecard/core/styles/app_colors.dart'; import 'package:coffeecard/features/environment/domain/entities/environment.dart'; import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; import 'package:coffeecard/features/product/presentation/widgets/buy_ticket_bottom_modal_sheet.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment_status.dart'; import 'package:coffeecard/features/receipt/presentation/cubit/receipt_cubit.dart'; @@ -68,7 +68,10 @@ Future _afterPurchaseModal( final receiptCubit = context.read(); if (singleTicketPurchase) { - await ticketsCubit.useTicket(product.id); + await ticketsCubit.useTicket( + product.id, + product.eligibleMenuItems.first.id, + ); } else { ticketsCubit.getTickets(); ReceiptOverlay.show( diff --git a/lib/features/product/presentation/pages/buy_products_page.dart b/lib/features/product/presentation/pages/buy_products_page.dart index abedc9764..08dd301af 100644 --- a/lib/features/product/presentation/pages/buy_products_page.dart +++ b/lib/features/product/presentation/pages/buy_products_page.dart @@ -1,7 +1,7 @@ import 'package:coffeecard/core/widgets/components/helpers/grid.dart'; import 'package:coffeecard/core/widgets/components/scaffold.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; import 'package:coffeecard/features/product/presentation/widgets/buy_tickets_card.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:flutter/material.dart'; /// Generic page for buying products. diff --git a/lib/features/product/presentation/pages/buy_single_drink_page.dart b/lib/features/product/presentation/pages/buy_single_drink_page.dart index 175855a9b..decd0c20d 100644 --- a/lib/features/product/presentation/pages/buy_single_drink_page.dart +++ b/lib/features/product/presentation/pages/buy_single_drink_page.dart @@ -1,6 +1,6 @@ import 'package:coffeecard/core/strings.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; import 'package:coffeecard/features/product/presentation/pages/buy_products_page.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; diff --git a/lib/features/product/presentation/pages/buy_tickets_page.dart b/lib/features/product/presentation/pages/buy_tickets_page.dart index f45dcfffe..ea6402d33 100644 --- a/lib/features/product/presentation/pages/buy_tickets_page.dart +++ b/lib/features/product/presentation/pages/buy_tickets_page.dart @@ -1,6 +1,6 @@ import 'package:coffeecard/core/strings.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; import 'package:coffeecard/features/product/presentation/pages/buy_products_page.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; diff --git a/lib/features/product/presentation/widgets/buy_ticket_bottom_modal_sheet.dart b/lib/features/product/presentation/widgets/buy_ticket_bottom_modal_sheet.dart index 7a200c43b..b4bfa55be 100644 --- a/lib/features/product/presentation/widgets/buy_ticket_bottom_modal_sheet.dart +++ b/lib/features/product/presentation/widgets/buy_ticket_bottom_modal_sheet.dart @@ -3,7 +3,7 @@ import 'package:coffeecard/core/styles/app_colors.dart'; import 'package:coffeecard/core/styles/app_text_styles.dart'; import 'package:coffeecard/core/widgets/components/bottom_modal_sheet_helper.dart'; import 'package:coffeecard/core/widgets/components/rounded_button.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/internal_payment_type.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; import 'package:coffeecard/features/purchase/presentation/widgets/purchase_overlay.dart'; diff --git a/lib/features/product/presentation/widgets/buy_tickets_card.dart b/lib/features/product/presentation/widgets/buy_tickets_card.dart index 75d7af14d..92ef1c468 100644 --- a/lib/features/product/presentation/widgets/buy_tickets_card.dart +++ b/lib/features/product/presentation/widgets/buy_tickets_card.dart @@ -2,8 +2,8 @@ import 'package:coffeecard/core/styles/app_colors.dart'; import 'package:coffeecard/core/styles/app_text_styles.dart'; import 'package:coffeecard/core/widgets/components/card.dart'; import 'package:coffeecard/core/widgets/components/helpers/responsive.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; import 'package:coffeecard/features/product/presentation/functions.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment_status.dart'; import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; diff --git a/lib/features/product/product_model.dart b/lib/features/product/product_model.dart new file mode 100644 index 000000000..08c8f3b23 --- /dev/null +++ b/lib/features/product/product_model.dart @@ -0,0 +1,77 @@ +import 'package:coffeecard/features/product/menu_item_model.dart'; +import 'package:coffeecard/generated/api/coffeecard_api_v2.models.swagger.dart'; +import 'package:equatable/equatable.dart'; +import 'package:fpdart/fpdart.dart'; + +class Product extends Equatable { + const Product({ + required this.price, + required this.amount, + required this.name, + required this.id, + required this.description, + required this.isPerk, + required this.eligibleMenuItems, + }); + + const Product.empty() + : price = 0, + amount = 0, + name = '', + id = 0, + description = '', + isPerk = false, + eligibleMenuItems = const []; + + factory Product.fromResponse(ProductResponse response) { + final eligibleMenuItems = + // convert to non-nullable list with an empty list as default + // (eligibleMenuItems is nullable in the response + // for backwards compatibility reasons) + Option.fromNullable(response.eligibleMenuItems) + .getOrElse(() => []) + .map(MenuItem.fromResponse) + .toList(); + + return Product( + price: response.price, + amount: response.numberOfTickets, + name: response.name, + id: response.id, + description: response.description, + isPerk: response.isPerk, + eligibleMenuItems: eligibleMenuItems, + ); + } + + final int id; + final int amount; + final int price; + final String name; + final String description; + final bool isPerk; + final List eligibleMenuItems; + + Product copyWith({ + int? price, + int? amount, + String? name, + int? id, + String? description, + bool? isPerk, + List? eligibleMenuItems, + }) { + return Product( + price: price ?? this.price, + amount: amount ?? this.amount, + name: name ?? this.name, + id: id ?? this.id, + description: description ?? this.description, + isPerk: isPerk ?? this.isPerk, + eligibleMenuItems: eligibleMenuItems ?? this.eligibleMenuItems, + ); + } + + @override + List get props => [id, amount, price, name, description, isPerk]; +} diff --git a/lib/features/product/product_repository.dart b/lib/features/product/product_repository.dart new file mode 100644 index 000000000..d80832fc8 --- /dev/null +++ b/lib/features/product/product_repository.dart @@ -0,0 +1,65 @@ +import 'package:coffeecard/core/errors/failures.dart'; +import 'package:coffeecard/core/network/network_request_executor.dart'; +import 'package:coffeecard/features/product/product_model.dart'; +import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; +import 'package:collection/collection.dart'; +import 'package:fpdart/fpdart.dart'; + +/// Repository for fetching products. +/// +/// The repository caches all products fetched from the API. +class ProductRepository { + ProductRepository({required this.api, required this.executor}); + + final CoffeecardApiV2 api; + final NetworkRequestExecutor executor; + + final _cache = {}; + + /// Returns a list of all products. + TaskEither> getProducts() { + return _getAllFromCache().orElse((_) => _getAllFromApi()); + } + + /// Returns the product with the given [productId]. + TaskEither getProduct(int productId) { + return _getFromCache(productId).orElse((_) => _getFromApi(productId)); + } + + TaskEither _cacheAll(Iterable products) { + _cache.addAll(products); + return TaskEither.of(unit); + } + + TaskEither _cacheProduct(Product product) => + _cacheAll([product]); + + TaskEither> _getAllFromCache() { + return TaskEither.fromPredicate( + _cache, + (cache) => cache.isNotEmpty, + (_) => const LocalStorageFailure('No products in cache'), + ); + } + + TaskEither _getFromCache(int productId) { + return TaskEither.fromNullable( + _cache.firstWhereOrNull((product) => product.id == productId), + () => LocalStorageFailure('Product with id $productId not in cache'), + ); + } + + TaskEither> _getAllFromApi() { + return executor + .executeAsTask(api.apiV2ProductsGet) + .map((products) => products.map(Product.fromResponse)) + .chainFirst(_cacheAll); + } + + TaskEither _getFromApi(int productId) { + return executor + .executeAsTask(() => api.apiV2ProductsIdGet(id: productId)) + .map(Product.fromResponse) + .chainFirst(_cacheProduct); + } +} diff --git a/lib/features/product/purchasable_products.dart b/lib/features/product/purchasable_products.dart new file mode 100644 index 000000000..81ea2e2c1 --- /dev/null +++ b/lib/features/product/purchasable_products.dart @@ -0,0 +1,15 @@ +import 'package:coffeecard/features/product/product_model.dart'; + +typedef PurchasableProducts = ({ + Iterable clipCards, + Iterable singleDrinks, + Iterable perks, +}); + +extension PurchasableProductsExtension on PurchasableProducts { + Iterable get all => [ + ...clipCards, + ...singleDrinks, + ...perks, + ]; +} diff --git a/lib/features/purchase/data/datasources/purchase_remote_data_source.dart b/lib/features/purchase/data/datasources/purchase_remote_data_source.dart index a641561c3..c71f51636 100644 --- a/lib/features/purchase/data/datasources/purchase_remote_data_source.dart +++ b/lib/features/purchase/data/datasources/purchase_remote_data_source.dart @@ -18,7 +18,7 @@ class PurchaseRemoteDataSource { /// Initiate a new Purchase Request. The return is a purchase request /// with payment details on how to pay for the purchase - Future> initiatePurchase( + Future> initiatePurchase( int productId, PaymentType paymentType, ) { @@ -35,7 +35,7 @@ class PurchaseRemoteDataSource { } /// Get a purchase by its purchase id - Future> getPurchase(int purchaseId) { + Future> getPurchase(int purchaseId) { return executor .execute(() => apiV2.apiV2PurchasesIdGet(id: purchaseId)) .map(SinglePurchaseModel.fromDto); diff --git a/lib/features/purchase/presentation/cubit/purchase_cubit.dart b/lib/features/purchase/presentation/cubit/purchase_cubit.dart index 3e5739d49..5317e7ccd 100644 --- a/lib/features/purchase/presentation/cubit/purchase_cubit.dart +++ b/lib/features/purchase/presentation/cubit/purchase_cubit.dart @@ -1,6 +1,6 @@ import 'package:bloc/bloc.dart'; import 'package:coffeecard/core/firebase_analytics_event_logging.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment_status.dart'; import 'package:coffeecard/features/purchase/domain/usecases/init_purchase.dart'; diff --git a/lib/features/purchase/presentation/widgets/purchase_overlay.dart b/lib/features/purchase/presentation/widgets/purchase_overlay.dart index 48a521ede..fa197916d 100644 --- a/lib/features/purchase/presentation/widgets/purchase_overlay.dart +++ b/lib/features/purchase/presentation/widgets/purchase_overlay.dart @@ -1,5 +1,5 @@ import 'package:coffeecard/core/styles/app_colors.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/data/repositories/payment_handler.dart'; import 'package:coffeecard/features/purchase/domain/entities/internal_payment_type.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; diff --git a/lib/features/receipt/data/models/swipe_receipt_model.dart b/lib/features/receipt/data/models/swipe_receipt_model.dart index 7747ff5e1..dff1d2fc6 100644 --- a/lib/features/receipt/data/models/swipe_receipt_model.dart +++ b/lib/features/receipt/data/models/swipe_receipt_model.dart @@ -1,5 +1,4 @@ import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; -import 'package:coffeecard/generated/api/coffeecard_api.models.swagger.dart'; import 'package:coffeecard/generated/api/coffeecard_api_v2.models.swagger.dart'; class SwipeReceiptModel extends SwipeReceipt { @@ -7,24 +6,25 @@ class SwipeReceiptModel extends SwipeReceipt { required super.productName, required super.timeUsed, required super.id, + required super.menuItemName, }); - /// Creates a receipt from a used ticket DTO - factory SwipeReceiptModel.fromTicketDto(UsedTicketResponse dto) { + factory SwipeReceiptModel.fromUsedTicketResponse(UsedTicketResponse dto) { return SwipeReceiptModel( productName: dto.productName, timeUsed: dto.dateUsed, id: dto.id, + menuItemName: dto.menuItemName ?? 'some ${dto.productName}', ); } - /// Creates a receipt from a used ticket DTO factory SwipeReceiptModel.fromTicketResponse(TicketResponse dto) { return SwipeReceiptModel( productName: dto.productName, timeUsed: dto .dateUsed!, // will not be null as the dto is a ticket that has been used at some point id: dto.id, + menuItemName: dto.usedOnMenuItemName ?? 'some ${dto.productName}', ); } } diff --git a/lib/features/receipt/domain/entities/swipe_receipt.dart b/lib/features/receipt/domain/entities/swipe_receipt.dart index 023d38495..b29a2fd89 100644 --- a/lib/features/receipt/domain/entities/swipe_receipt.dart +++ b/lib/features/receipt/domain/entities/swipe_receipt.dart @@ -1,9 +1,14 @@ part of 'receipt.dart'; +/// A receipt for a ticket that has been used class SwipeReceipt extends Receipt { + /// The menu item that the ticket was used for + final String menuItemName; + const SwipeReceipt({ required super.productName, required super.timeUsed, required super.id, + required this.menuItemName, }); } diff --git a/lib/features/receipt/presentation/pages/view_receipt_page.dart b/lib/features/receipt/presentation/pages/view_receipt_page.dart index 68594fd87..a1f53855d 100644 --- a/lib/features/receipt/presentation/pages/view_receipt_page.dart +++ b/lib/features/receipt/presentation/pages/view_receipt_page.dart @@ -3,20 +3,15 @@ import 'package:coffeecard/core/widgets/components/helpers/responsive.dart'; import 'package:coffeecard/core/widgets/components/scaffold.dart'; import 'package:coffeecard/features/environment/domain/entities/environment.dart'; import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart'; +import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; import 'package:coffeecard/features/receipt/presentation/widgets/receipt_card.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class ViewReceiptPage extends StatelessWidget { - final String name; - final DateTime time; - final String paymentStatus; + const ViewReceiptPage({required this.receipt}); - const ViewReceiptPage({ - required this.name, - required this.time, - required this.paymentStatus, - }); + final SwipeReceipt receipt; @override Widget build(BuildContext context) { @@ -29,12 +24,12 @@ class ViewReceiptPage extends StatelessWidget { child: Column( children: [ ReceiptCard( - productName: name, - time: time, + productName: receipt.menuItemName, + time: receipt.timeUsed, isInOverlay: false, isTestEnvironment: state is EnvironmentLoaded && state.env.isTest, - status: paymentStatus, + status: '${Strings.swiped} via ${receipt.productName} ticket', ), ], ), diff --git a/lib/features/receipt/presentation/widgets/list_entry/placeholder_receipt_list_entry.dart b/lib/features/receipt/presentation/widgets/list_entry/placeholder_receipt_list_entry.dart index 58d20392b..c82ee5d0d 100644 --- a/lib/features/receipt/presentation/widgets/list_entry/placeholder_receipt_list_entry.dart +++ b/lib/features/receipt/presentation/widgets/list_entry/placeholder_receipt_list_entry.dart @@ -9,7 +9,8 @@ class PlaceholderReceiptListEntry extends StatelessWidget { Widget build(BuildContext context) { return ReceiptListEntry( tappable: false, - name: Strings.receiptPlaceholderName, + productName: Strings.receiptPlaceholderName, + menuItemName: Strings.receiptPlaceholderName, time: DateTime.now(), isPurchase: false, showShimmer: true, diff --git a/lib/features/receipt/presentation/widgets/list_entry/purchase_receipt_list_entry.dart b/lib/features/receipt/presentation/widgets/list_entry/purchase_receipt_list_entry.dart index 3ad206d99..2c7048b02 100644 --- a/lib/features/receipt/presentation/widgets/list_entry/purchase_receipt_list_entry.dart +++ b/lib/features/receipt/presentation/widgets/list_entry/purchase_receipt_list_entry.dart @@ -28,7 +28,8 @@ class PurchaseReceiptListEntry extends StatelessWidget { Widget build(BuildContext context) { return ReceiptListEntry( tappable: false, - name: receipt.productName, + productName: receipt.productName, + menuItemName: receipt.productName, time: receipt.timeUsed, isPurchase: true, showShimmer: false, diff --git a/lib/features/receipt/presentation/widgets/list_entry/receipt_list_entry.dart b/lib/features/receipt/presentation/widgets/list_entry/receipt_list_entry.dart index 575287d9a..222691680 100644 --- a/lib/features/receipt/presentation/widgets/list_entry/receipt_list_entry.dart +++ b/lib/features/receipt/presentation/widgets/list_entry/receipt_list_entry.dart @@ -3,6 +3,7 @@ import 'package:coffeecard/core/styles/app_colors.dart'; import 'package:coffeecard/core/styles/app_text_styles.dart'; import 'package:coffeecard/core/widgets/components/helpers/shimmer_builder.dart'; import 'package:coffeecard/core/widgets/list_entry.dart'; +import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; import 'package:coffeecard/features/receipt/presentation/pages/view_receipt_page.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; @@ -11,7 +12,8 @@ final _formatDateTime = DateFormat('dd/MM/y HH:mm').format; class ReceiptListEntry extends StatelessWidget { final bool tappable; - final String name; + final String productName; + final String menuItemName; final DateTime time; final bool isPurchase; final bool showShimmer; @@ -22,7 +24,8 @@ class ReceiptListEntry extends StatelessWidget { const ReceiptListEntry({ required this.tappable, - required this.name, + required this.productName, + required this.menuItemName, required this.time, required this.isPurchase, required this.showShimmer, @@ -48,9 +51,12 @@ class ReceiptListEntry extends StatelessWidget { closedShape: const RoundedRectangleBorder(), openBuilder: (context, _) { return ViewReceiptPage( - name: name, - time: time, - paymentStatus: status, + receipt: SwipeReceipt( + productName: productName, + timeUsed: time, + id: 0, + menuItemName: menuItemName, + ), ); }, closedBuilder: (context, openContainer) { diff --git a/lib/features/receipt/presentation/widgets/list_entry/swipe_receipt_list_entry.dart b/lib/features/receipt/presentation/widgets/list_entry/swipe_receipt_list_entry.dart index a3788b36a..87f2f7b95 100644 --- a/lib/features/receipt/presentation/widgets/list_entry/swipe_receipt_list_entry.dart +++ b/lib/features/receipt/presentation/widgets/list_entry/swipe_receipt_list_entry.dart @@ -13,14 +13,15 @@ class SwipeReceiptListEntry extends StatelessWidget { Widget build(BuildContext context) { return ReceiptListEntry( tappable: true, - name: receipt.productName, + productName: receipt.productName, + menuItemName: receipt.menuItemName, time: receipt.timeUsed, isPurchase: false, showShimmer: false, - topText: receipt.productName, + topText: receipt.menuItemName, rightText: Strings.oneTicket, backgroundColor: AppColors.white, - status: Strings.swiped, + status: '${Strings.swiped} via ${receipt.productName} ticket', ); } } diff --git a/lib/features/redirection/redirection_router.dart b/lib/features/redirection/redirection_router.dart index 32d230715..d59e470f4 100644 --- a/lib/features/redirection/redirection_router.dart +++ b/lib/features/redirection/redirection_router.dart @@ -3,8 +3,8 @@ import 'package:coffeecard/core/widgets/pages/home_page.dart'; import 'package:coffeecard/features/authentication/presentation/cubits/authentication_cubit.dart'; import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart'; import 'package:coffeecard/features/login/presentation/pages/login_page_email.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; import 'package:coffeecard/features/product/presentation/cubit/product_cubit.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:coffeecard/features/user/presentation/cubit/user_cubit.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; diff --git a/lib/features/register/data/datasources/register_remote_data_source.dart b/lib/features/register/data/datasources/register_remote_data_source.dart index 2a6719be5..8e49444fa 100644 --- a/lib/features/register/data/datasources/register_remote_data_source.dart +++ b/lib/features/register/data/datasources/register_remote_data_source.dart @@ -12,7 +12,7 @@ class RegisterRemoteDataSource { final CoffeecardApiV2 apiV2; final NetworkRequestExecutor executor; - Future> register( + Future> register( String name, String email, String encodedPasscode, diff --git a/lib/features/ticket/data/datasources/ticket_remote_data_source.dart b/lib/features/ticket/data/datasources/ticket_remote_data_source.dart index 68f824921..09ce553c5 100644 --- a/lib/features/ticket/data/datasources/ticket_remote_data_source.dart +++ b/lib/features/ticket/data/datasources/ticket_remote_data_source.dart @@ -2,56 +2,24 @@ import 'package:coffeecard/core/errors/failures.dart'; import 'package:coffeecard/core/network/network_request_executor.dart'; import 'package:coffeecard/features/receipt/data/models/swipe_receipt_model.dart'; import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; -import 'package:coffeecard/features/ticket/data/models/ticket_count_model.dart'; -import 'package:coffeecard/generated/api/coffeecard_api.swagger.dart'; import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; -import 'package:collection/collection.dart'; import 'package:fpdart/fpdart.dart'; class TicketRemoteDataSource { - TicketRemoteDataSource({ - required this.apiV1, - required this.apiV2, - required this.executor, - }); + TicketRemoteDataSource({required this.api, required this.executor}); - final CoffeecardApi apiV1; - final CoffeecardApiV2 apiV2; + final CoffeecardApiV2 api; final NetworkRequestExecutor executor; - Future>> getUserTickets() { - // Mapper function for mapping a list of tickets (all with the same product - // id) to a TicketCountModel. - // - // This also takes into account that there might be - // some tickets with the same product id, but different names. - TicketCountModel mapper(MapEntry> entry) { - final MapEntry(key: id, value: tickets) = entry; - // If there are multiple ticket names present, join them with a slash. - final ticketName = tickets.map((t) => t.productName).toSet().join('/'); - - return TicketCountModel( - count: tickets.length, - productName: ticketName, - productId: id, - ); - } - + TaskEither> getUserTickets() { return executor - .execute(() => apiV2.apiV2TicketsGet(includeUsed: false)) - .map( - (result) => result - .groupListsBy((t) => t.productId) - .entries - .map(mapper) - .sortedBy((t) => t.productId), - ); + .executeAsTask(() => api.apiV2TicketsGet(includeUsed: false)); } - Future> useTicket(int productId) { - final body = UseTicketDTO(productId: productId); + TaskEither useTicket(int productId, int menuItemId) { + final body = UseTicketRequest(productId: productId, menuItemId: menuItemId); return executor - .execute(() => apiV1.apiV1TicketsUsePost(body: body)) - .map(SwipeReceiptModel.fromTicketDto); + .executeAsTask(() => api.apiV2TicketsUsePost(body: body)) + .map(SwipeReceiptModel.fromUsedTicketResponse); } } diff --git a/lib/features/ticket/data/models/ticket_count_model.dart b/lib/features/ticket/data/models/ticket_count_model.dart deleted file mode 100644 index 6e0ccbba2..000000000 --- a/lib/features/ticket/data/models/ticket_count_model.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:coffeecard/features/ticket/domain/entities/ticket_count.dart'; - -class TicketCountModel extends TicketCount { - const TicketCountModel({ - required super.count, - required super.productName, - required super.productId, - }); -} diff --git a/lib/features/ticket/domain/entities/ticket.dart b/lib/features/ticket/domain/entities/ticket.dart new file mode 100644 index 000000000..764962b8c --- /dev/null +++ b/lib/features/ticket/domain/entities/ticket.dart @@ -0,0 +1,36 @@ +import 'package:coffeecard/features/product/menu_item_model.dart'; +import 'package:coffeecard/features/product/product_model.dart'; +import 'package:equatable/equatable.dart'; +import 'package:fpdart/fpdart.dart'; + +class Ticket extends Equatable { + const Ticket({ + required this.product, + required this.amountLeft, + required this.lastUsedMenuItem, + }); + + const Ticket.empty() + : product = const Product.empty(), + amountLeft = 0, + lastUsedMenuItem = const None(); + + final Product product; + final int amountLeft; + final Option lastUsedMenuItem; + + Ticket copyWith({ + Product? product, + int? amountLeft, + Option? lastUsedMenuItem, + }) { + return Ticket( + product: product ?? this.product, + amountLeft: amountLeft ?? this.amountLeft, + lastUsedMenuItem: lastUsedMenuItem ?? this.lastUsedMenuItem, + ); + } + + @override + List get props => [product, amountLeft, lastUsedMenuItem]; +} diff --git a/lib/features/ticket/domain/entities/ticket_count.dart b/lib/features/ticket/domain/entities/ticket_count.dart deleted file mode 100644 index bdde14710..000000000 --- a/lib/features/ticket/domain/entities/ticket_count.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:equatable/equatable.dart'; - -class TicketCount extends Equatable { - final int productId; - final int count; - final String productName; - - const TicketCount({ - required this.count, - required this.productName, - required this.productId, - }); - - @override - String toString() { - return 'ProductCount{count: $count, productName: $productName}'; - } - - @override - List get props => [productId, count, productName]; -} diff --git a/lib/features/ticket/domain/usecases/consume_ticket.dart b/lib/features/ticket/domain/usecases/consume_ticket.dart index ab59987e8..b7e78ffa0 100644 --- a/lib/features/ticket/domain/usecases/consume_ticket.dart +++ b/lib/features/ticket/domain/usecases/consume_ticket.dart @@ -2,13 +2,30 @@ import 'package:coffeecard/core/errors/failures.dart'; import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; import 'package:coffeecard/features/ticket/data/datasources/ticket_remote_data_source.dart'; import 'package:fpdart/fpdart.dart'; +import 'package:hive_flutter/hive_flutter.dart'; class ConsumeTicket { final TicketRemoteDataSource ticketRemoteDataSource; ConsumeTicket({required this.ticketRemoteDataSource}); - Future> call({required int productId}) async { - return ticketRemoteDataSource.useTicket(productId); + TaskEither call({ + required int productId, + required int menuItemId, + }) { + return ticketRemoteDataSource + .useTicket(productId, menuItemId) + .chainFirst((_) => _cacheLastUsedMenuItem(productId, menuItemId)); + } + + TaskEither _cacheLastUsedMenuItem( + int productId, + int menuItemId, + ) { + return TaskEither(() async { + final cache = await Hive.openBox('lastUsedMenuItemByProductId'); + await cache.put(productId, menuItemId); + return const Right(unit); + }); } } diff --git a/lib/features/ticket/domain/usecases/load_tickets.dart b/lib/features/ticket/domain/usecases/load_tickets.dart index e47fd053e..a2b2f84ae 100644 --- a/lib/features/ticket/domain/usecases/load_tickets.dart +++ b/lib/features/ticket/domain/usecases/load_tickets.dart @@ -1,14 +1,80 @@ import 'package:coffeecard/core/errors/failures.dart'; +import 'package:coffeecard/features/product/menu_item_model.dart'; +import 'package:coffeecard/features/product/product_repository.dart'; import 'package:coffeecard/features/ticket/data/datasources/ticket_remote_data_source.dart'; -import 'package:coffeecard/features/ticket/domain/entities/ticket_count.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; +import 'package:coffeecard/generated/api/coffeecard_api_v2.models.swagger.dart'; +import 'package:collection/collection.dart'; import 'package:fpdart/fpdart.dart'; +import 'package:hive_flutter/hive_flutter.dart'; class LoadTickets { final TicketRemoteDataSource ticketRemoteDataSource; + final ProductRepository productRepository; - LoadTickets({required this.ticketRemoteDataSource}); + const LoadTickets({ + required this.ticketRemoteDataSource, + required this.productRepository, + }); - Future>> call() async { - return ticketRemoteDataSource.getUserTickets(); + TaskEither> call() { + return ticketRemoteDataSource + .getUserTickets() + // create groups of ticket reponse objects grouped by product id, + // creating a list of key-value pairs (product id, list of tickets) + .map((ts) => ts.groupListsBy((t) => t.productId).entries.toList()) + // map each key-value pair to a Ticket + .flatMap((list) => TaskEither.traverseList(list, _mapToTicket)) + // sort the list by product id + .map((list) => list.sortWith((t) => t.product.id, Order.orderInt)); + } + + TaskEither _mapToTicket( + MapEntry> ticketGroup, + ) { + final productId = ticketGroup.key; + final tickets = ticketGroup.value; + + return productRepository + .getProduct(productId) + .map( + (product) => Ticket( + product: product, + amountLeft: tickets.length, + lastUsedMenuItem: const None(), + ), + ) + .flatMap(_addLastUsedMenuItemToTicket); + } + + /// If there is a cached last used menu item id for the given [ticket], + /// add it to the [ticket] and return it. + /// Otherwise, return the [ticket] as is. + TaskEither _addLastUsedMenuItemToTicket(Ticket ticket) { + // See if there is a cached menu item id for the given product id + final getCachedMenuItemId = TaskEither(() async { + final cache = await Hive.openBox('lastUsedMenuItemByProductId'); + return Either.fromNullable( + cache.get(ticket.product.id), + () => const LocalStorageFailure('No last used menu item found'), + ); + }); + + // Get the menu item from the product's eligible menu items + TaskEither getMenuItemFromId(lastUsedMenuItemId) { + return TaskEither.fromNullable( + ticket.product.eligibleMenuItems + .firstWhereOrNull((mi) => mi.id == lastUsedMenuItemId), + () => LocalStorageFailure( + 'Last used menu item found ($lastUsedMenuItemId), ' + 'but it is not eligible for product ${ticket.product.id}.', + ), + ); + } + + return getCachedMenuItemId + .flatMap(getMenuItemFromId) + .map((menuItem) => ticket.copyWith(lastUsedMenuItem: Some(menuItem))) + .orElse((_) => TaskEither.of(ticket)); } } diff --git a/lib/features/ticket/presentation/cubit/tickets_cubit.dart b/lib/features/ticket/presentation/cubit/tickets_cubit.dart index 77b407749..6b8b02829 100644 --- a/lib/features/ticket/presentation/cubit/tickets_cubit.dart +++ b/lib/features/ticket/presentation/cubit/tickets_cubit.dart @@ -1,5 +1,5 @@ import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; -import 'package:coffeecard/features/ticket/domain/entities/ticket_count.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; import 'package:coffeecard/features/ticket/domain/usecases/consume_ticket.dart'; import 'package:coffeecard/features/ticket/domain/usecases/load_tickets.dart'; import 'package:equatable/equatable.dart'; @@ -18,36 +18,34 @@ class TicketsCubit extends Cubit { Future getTickets() async { emit(const TicketsLoading()); - refreshTickets(); + return refreshTickets(); } - Future useTicket(int productId) async { + Future useTicket(int productId, int menuItemId) async { if (state is! TicketsLoaded) return; - final st = state as TicketsLoaded; + final tickets = (state as TicketsLoaded).tickets; - emit(TicketUsing(tickets: st.tickets)); + emit(TicketUsing(tickets: tickets)); - final either = await consumeTicket(productId: productId); + await consumeTicket + .call(productId: productId, menuItemId: menuItemId) + .match( + (failure) => + TicketsUseError(message: failure.reason, tickets: tickets), + (receipt) => TicketUsed(receipt: receipt, tickets: tickets), + ) + .map(emit) + .run(); - emit( - either.fold( - (error) => TicketsUseError(message: error.reason), - (receipt) => TicketUsed(receipt: receipt, tickets: st.tickets), - ), - ); - - refreshTickets(); + return refreshTickets(); } - Future refreshTickets() async { - final either = await loadTickets(); - - emit( - either.fold( - (error) => TicketsLoadError(message: error.reason), + Future refreshTickets() => loadTickets() + .match( + (failure) => TicketsLoadError(message: failure.reason), (tickets) => TicketsLoaded(tickets: tickets), - ), - ); - } + ) + .map(emit) + .run(); } diff --git a/lib/features/ticket/presentation/cubit/tickets_state.dart b/lib/features/ticket/presentation/cubit/tickets_state.dart index 0061dddb3..e0c0dff8c 100644 --- a/lib/features/ticket/presentation/cubit/tickets_state.dart +++ b/lib/features/ticket/presentation/cubit/tickets_state.dart @@ -12,7 +12,7 @@ class TicketsLoading extends TicketsState { } class TicketsLoaded extends TicketsState { - final List tickets; + final List tickets; const TicketsLoaded({required this.tickets}); @@ -20,11 +20,24 @@ class TicketsLoaded extends TicketsState { List get props => [tickets]; } -class TicketUsing extends TicketsLoaded { +class TicketsLoadError extends TicketsState { + final String message; + const TicketsLoadError({required this.message}); + + @override + List get props => [message]; +} + +/// Superclass for all actions that can be performed on the tickets. +sealed class TicketsAction extends TicketsLoaded { + const TicketsAction({required super.tickets}); +} + +class TicketUsing extends TicketsAction { const TicketUsing({required super.tickets}); } -class TicketUsed extends TicketsLoaded { +class TicketUsed extends TicketsAction { final Receipt receipt; const TicketUsed({required this.receipt, required super.tickets}); @@ -33,17 +46,9 @@ class TicketUsed extends TicketsLoaded { List get props => [receipt, tickets]; } -class TicketsUseError extends TicketsState { +class TicketsUseError extends TicketsAction { final String message; - const TicketsUseError({required this.message}); - - @override - List get props => [message]; -} - -class TicketsLoadError extends TicketsState { - final String message; - const TicketsLoadError({required this.message}); + const TicketsUseError({required this.message, required super.tickets}); @override List get props => [message]; diff --git a/lib/features/ticket/presentation/pages/tickets_page.dart b/lib/features/ticket/presentation/pages/tickets_page.dart index ee1dd7125..96eb4f460 100644 --- a/lib/features/ticket/presentation/pages/tickets_page.dart +++ b/lib/features/ticket/presentation/pages/tickets_page.dart @@ -2,7 +2,7 @@ import 'package:coffeecard/core/strings.dart'; import 'package:coffeecard/core/widgets/components/barista_perks_section.dart'; import 'package:coffeecard/core/widgets/components/scaffold.dart'; import 'package:coffeecard/core/widgets/upgrade_alert.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:coffeecard/features/ticket/presentation/widgets/shop_section.dart'; import 'package:coffeecard/features/ticket/presentation/widgets/tickets_section.dart'; import 'package:coffeecard/features/user/presentation/cubit/user_cubit.dart'; diff --git a/lib/features/ticket/presentation/widgets/perk_card.dart b/lib/features/ticket/presentation/widgets/perk_card.dart index d96256e26..cec965c50 100644 --- a/lib/features/ticket/presentation/widgets/perk_card.dart +++ b/lib/features/ticket/presentation/widgets/perk_card.dart @@ -1,6 +1,6 @@ import 'package:coffeecard/core/strings.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; import 'package:coffeecard/features/product/presentation/functions.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/ticket/presentation/widgets/shop_card.dart'; import 'package:flutter/material.dart'; diff --git a/lib/features/ticket/presentation/widgets/swipe_ticket_confirm.dart b/lib/features/ticket/presentation/widgets/swipe_ticket_confirm.dart index 62435484c..fe93fa9e8 100644 --- a/lib/features/ticket/presentation/widgets/swipe_ticket_confirm.dart +++ b/lib/features/ticket/presentation/widgets/swipe_ticket_confirm.dart @@ -6,43 +6,32 @@ import 'package:coffeecard/core/styles/app_text_styles.dart'; import 'package:coffeecard/core/widgets/components/bottom_modal_sheet_helper.dart'; import 'package:coffeecard/core/widgets/components/card.dart'; import 'package:coffeecard/core/widgets/components/slide_action.dart'; +import 'package:coffeecard/features/product/menu_item_model.dart'; import 'package:coffeecard/features/receipt/presentation/cubit/receipt_cubit.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; import 'package:coffeecard/features/ticket/presentation/cubit/tickets_cubit.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:fpdart/fpdart.dart' hide State; +import 'package:gap/gap.dart'; Future showSwipeTicketConfirm({ + // The context is required to get cubits via context.read required BuildContext context, - required String productName, - required int amountOwned, - required int productId, + required Ticket ticket, }) { return Navigator.of(context, rootNavigator: true).push( _HeroDialogRoute( - builder: (_) { - return _ModalContent( - context: context, - productName: productName, - amountOwned: amountOwned, - productId: productId, - ); - }, + builder: (_) => _ModalContent(context: context, ticket: ticket), ), ); } class _ModalContent extends StatefulWidget { - const _ModalContent({ - required this.context, - required this.productName, - required this.amountOwned, - required this.productId, - }); + const _ModalContent({required this.context, required this.ticket}); final BuildContext context; - final String productName; - final int amountOwned; - final int productId; + final Ticket ticket; @override State<_ModalContent> createState() => _ModalContentState(); @@ -51,9 +40,13 @@ class _ModalContent extends StatefulWidget { class _ModalContentState extends State<_ModalContent> with SingleTickerProviderStateMixin { late AnimationController _controller; - late Animation _animation; + late Animation _fadeInAnimation; + late Animation _fadeBetweenAnimation; - late (String, int)? _heroTag; + late Ticket _heroTag = widget.ticket; + _TicketUseState _state = const _SelectProduct(); + + late Option _selectedMenuItem = widget.ticket.lastUsedMenuItem; @override void initState() { @@ -61,14 +54,18 @@ class _ModalContentState extends State<_ModalContent> vsync: this, duration: const Duration(milliseconds: 450), ); - _animation = Tween(begin: 0.0, end: 1.0).animate( + _fadeInAnimation = Tween(begin: 0.0, end: 1.0).animate( CurvedAnimation( parent: _controller, curve: const Interval(0.5, 1.0, curve: Curves.easeOut), ), ); + _fadeBetweenAnimation = Tween(begin: 0.0, end: 1.0).animate( + CurvedAnimation(parent: _controller, curve: Curves.easeOut), + ); + // Start the animation + _controller.forward(); - _heroTag = (widget.productName, widget.productId); super.initState(); } @@ -80,7 +77,16 @@ class _ModalContentState extends State<_ModalContent> @override Widget build(BuildContext context) { - final _ = _controller.forward(); + final (titleWidget, actionWidget) = switch (_state) { + _SelectProduct() => ( + _selectProductTitle, + _selectProductAction, + ), + _ConfirmSwipe(:final menuItem) => ( + _confirmSwipeTitle(menuItem), + _confirmSwipeAction(menuItem), + ), + }; return SafeArea( child: Padding( @@ -101,45 +107,19 @@ class _ModalContentState extends State<_ModalContent> ], ), Hero( - tag: _heroTag ?? -1, + tag: _heroTag, // SingleChildScrollView to avoid the temporary overflow // error during the hero animation. child: SingleChildScrollView( child: CardBase( color: AppColors.ticket, gap: 36, - top: CardTitle( - title: Text( - widget.productName, - style: AppTextStyle.ownedTicket, - ), - ), - bottom: FadeTransition( - opacity: _animation, - child: SlideAction( - elevation: 0, - text: Strings.useTicket, - textStyle: AppTextStyle.buttonText.apply( - color: AppColors.white, - ), - height: 56, - sliderButtonIcon: const Icon( - Icons.navigate_next, - size: 48, - ), - sliderButtonIconPadding: 0, - innerColor: AppColors.white, - outerColor: AppColors.primary, - onSubmit: () async { - // Disable hero animation in the reverse direction - setState(() => _heroTag = null); - final ticketCubit = widget.context.read(); - final receiptCubit = - widget.context.read(); - await ticketCubit.useTicket(widget.productId); - await receiptCubit.fetchReceipts(); - }, - ), + top: titleWidget, + bottom: AnimatedSize( + alignment: Alignment.bottomCenter, + duration: const Duration(milliseconds: 350), + curve: Curves.fastLinearToSlowEaseIn, + child: actionWidget, ), ), ), @@ -149,6 +129,144 @@ class _ModalContentState extends State<_ModalContent> ), ); } + + Widget _wrapWithFadeTransition( + Animation animation, { + required Widget child, + }) { + return FadeTransition(opacity: animation, child: child); + } + + Widget get _selectProductTitle { + const description = 'Select a product to spend your ticket on'; + + return _wrapWithFadeTransition( + _fadeInAnimation, + child: CardTitle( + title: Text( + widget.ticket.product.name, + style: AppTextStyle.ownedTicket, + ), + description: Text( + description, + style: AppTextStyle.explainer, + ), + ), + ); + } + + Widget _confirmSwipeTitle(MenuItem menuItem) { + final description = 'Claiming via ticket: ${widget.ticket.product.name}'; + + return _wrapWithFadeTransition( + _fadeBetweenAnimation, + child: CardTitle( + title: Text(menuItem.name, style: AppTextStyle.ownedTicket), + description: Text(description, style: AppTextStyle.explainer), + ), + ); + } + + Widget get _selectProductAction { + final dropdownItems = widget.ticket.product.eligibleMenuItems + .map((mi) => DropdownMenuItem(value: mi, child: Text(mi.name))) + .toList(); + + return _wrapWithFadeTransition( + _fadeInAnimation, + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Row( + children: [ + Expanded( + child: DecoratedBox( + decoration: const BoxDecoration( + color: AppColors.white, + borderRadius: BorderRadius.only( + topLeft: Radius.circular(8), + topRight: Radius.circular(8), + ), + border: BorderDirectional( + bottom: BorderSide( + color: AppColors.secondary, + width: 2, + ), + ), + ), + child: DropdownButton( + hint: const Text('Select a product...'), + isExpanded: true, + value: _selectedMenuItem.toNullable(), + items: dropdownItems, + onChanged: (newItem) { + if (newItem != null) { + setState(() => _selectedMenuItem = Some(newItem)); + } + }, + padding: const EdgeInsets.symmetric( + horizontal: 16, + vertical: 8, + ), + borderRadius: BorderRadius.circular(8), + focusColor: AppColors.primary, + underline: const SizedBox.shrink(), + dropdownColor: AppColors.white, + ), + ), + ), + const Gap(16), + IconButton.filledTonal( + onPressed: _selectedMenuItem.match( + () => null, + (menuItem) => () async { + const duration = Duration(milliseconds: 200); + await _controller.animateBack(0, duration: duration); + await Future.delayed(const Duration(milliseconds: 100)); + _controller.animateTo(1, duration: duration); + setState(() => _state = _ConfirmSwipe(menuItem)); + }, + ), + icon: const Icon(Icons.navigate_next), + iconSize: 36, + ), + ], + ), + ], + ), + ); + } + + Widget _confirmSwipeAction(MenuItem menuItem) { + return _wrapWithFadeTransition( + _fadeBetweenAnimation, + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const Gap(8), + SlideAction( + elevation: 0, + text: Strings.useTicket, + textStyle: AppTextStyle.buttonText.apply(color: AppColors.white), + height: 56, + sliderButtonIcon: const Icon(Icons.navigate_next, size: 48), + sliderButtonIconPadding: 0, + innerColor: AppColors.white, + outerColor: AppColors.primary, + onSubmit: () async { + // Disable hero animation in the reverse direction + setState(() => _heroTag = const Ticket.empty()); + final ticketCubit = widget.context.read(); + final receiptCubit = widget.context.read(); + final productId = widget.ticket.product.id; + await ticketCubit.useTicket(productId, menuItem.id); + await receiptCubit.fetchReceipts(); + }, + ), + ], + ), + ); + } } // original author https://stackoverflow.com/a/44404763 @@ -197,3 +315,16 @@ class _HeroDialogRoute extends PageRoute { @override String? get barrierLabel => 'Cancel'; } + +sealed class _TicketUseState { + const _TicketUseState(); +} + +class _SelectProduct extends _TicketUseState { + const _SelectProduct(); +} + +class _ConfirmSwipe extends _TicketUseState { + const _ConfirmSwipe(this.menuItem); + final MenuItem menuItem; +} diff --git a/lib/features/ticket/presentation/widgets/tickets_section.dart b/lib/features/ticket/presentation/widgets/tickets_section.dart index 2c004fd64..e29655a78 100644 --- a/lib/features/ticket/presentation/widgets/tickets_section.dart +++ b/lib/features/ticket/presentation/widgets/tickets_section.dart @@ -1,21 +1,21 @@ import 'package:coffeecard/core/strings.dart'; import 'package:coffeecard/core/styles/app_text_styles.dart'; -import 'package:coffeecard/core/widgets/components/coffee_card.dart'; -import 'package:coffeecard/core/widgets/components/coffee_card_placeholder.dart'; import 'package:coffeecard/core/widgets/components/dialog.dart'; import 'package:coffeecard/core/widgets/components/error_section.dart'; -import 'package:coffeecard/core/widgets/components/helpers/shimmer_builder.dart'; import 'package:coffeecard/core/widgets/components/loading_overlay.dart'; import 'package:coffeecard/core/widgets/components/section_title.dart'; +import 'package:coffeecard/core/widgets/components/tickets_card.dart'; import 'package:coffeecard/features/environment/domain/entities/environment.dart'; import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart'; import 'package:coffeecard/features/opening_hours/presentation/widgets/opening_hours_indicator.dart'; import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; import 'package:coffeecard/features/receipt/presentation/widgets/receipt_overlay.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; import 'package:coffeecard/features/ticket/presentation/cubit/tickets_cubit.dart'; import 'package:coffeecard/features/user/presentation/cubit/user_cubit.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:fpdart/fpdart.dart'; import 'package:gap/gap.dart'; class TicketSection extends StatelessWidget { @@ -31,128 +31,108 @@ class TicketSection extends StatelessWidget { sideWidget: OpeningHoursIndicator(), ), BlocConsumer( - listenWhen: (previous, current) => - current is TicketUsing || - current is TicketUsed || - current is TicketsUseError, + listenWhen: (previous, current) => current is TicketsAction, listener: (context, state) { - if (state is TicketUsing) { - if (Navigator.of(context, rootNavigator: true).canPop()) { - // If a ticket was used from the buy - // single drink flow, there is no swipe overlay - - // Remove the swipe overlay - Navigator.of(context, rootNavigator: true).pop(); - } - - LoadingOverlay.show(context).ignore(); + // Implicitly cast to TicketsLoaded + // (the superclass of the Using, Used and UseError states) + // + // This is already checked in the listenWhen function, so this + // is purely for type safety + if (state is! TicketsAction) { + throw StateError('This listener should not be called for $state'); } - if (state is TicketUsed) { - // Refresh or load user info (for updated rank stats) - // (also refreshes leaderboard) - context.read().fetchUserDetails(); - final envState = context.read().state; - LoadingOverlay.hide(context); - ReceiptOverlay.show( - productName: state.receipt.productName, - timeUsed: state.receipt.timeUsed, - isTestEnvironment: - envState is EnvironmentLoaded && envState.env.isTest, - status: state.receipt is PurchaseReceipt - ? (state.receipt as PurchaseReceipt) - .paymentStatus - .toString() - : Strings.swiped, - context: context, - ).ignore(); - } - if (state is TicketsUseError) { - LoadingOverlay.hide(context); - appDialog( - context: context, - title: Strings.error, - actions: [ - TextButton( - child: const Text(Strings.buttonOK), - onPressed: () => closeAppDialog(context), - ), - ], - children: [Text(state.message, style: AppTextStyle.settingKey)], - dismissible: true, - ); + switch (state) { + case TicketUsing _: + _whenTicketsUsing(context); + case TicketUsed(:final receipt): + _whenTicketUsed(context, receipt); + case TicketsUseError(:final message): + LoadingOverlay.hide(context); + appDialog( + context: context, + title: Strings.error, + actions: [ + TextButton( + child: const Text(Strings.buttonOK), + onPressed: () => closeAppDialog(context), + ), + ], + children: [Text(message, style: AppTextStyle.settingKey)], + dismissible: true, + ); } }, - buildWhen: (previous, current) => - current is TicketsLoading || - current is TicketsLoaded || - current is TicketsLoadError, + buildWhen: (previous, current) => current is! TicketsAction, builder: (context, state) { - if (state is TicketsLoading) { - return const _CoffeeCardLoading(); - } - if (state is TicketsLoaded) { - // States extending this are also caught on this - if (state.tickets.isEmpty) { - return const Padding( - padding: EdgeInsets.only(bottom: 12.0), - child: CoffeeCardPlaceholder(), - ); - } - return Column( - children: state.tickets - .map( - (p) => Padding( - padding: const EdgeInsets.only(bottom: 12.0), - child: Hero( - tag: (p.productName, p.productId), - child: CoffeeCard( - title: p.productName, - amountOwned: p.count, - productId: p.productId, - ), - ), - ), - ) - .toList(), - ); - } - if (state is TicketsLoadError) { - return ErrorSection( - error: state.message, - retry: context.read().getTickets, - ); - } - - throw ArgumentError(this); + return switch (state) { + TicketsLoading _ => const TicketsCard.loadingPlaceholder(), + TicketsLoaded(:final tickets) => LoadedTicketsSection(tickets), + TicketsLoadError(:final message) => ErrorSection( + error: message, + retry: context.read().getTickets, + ), + }; }, ), const Gap(4), ], ); } + + void _whenTicketsUsing(BuildContext context) { + // Remove the swipe overlay + // (N/A when using a ticket as a part of the buy single drink flow) + if (Navigator.of(context, rootNavigator: true).canPop()) { + Navigator.of(context, rootNavigator: true).pop(); + } + LoadingOverlay.show(context); + } + + void _whenTicketUsed(BuildContext context, Receipt receipt) { + // Refresh or load user info (for updated rank stats) + // (also refreshes leaderboard) + context.read().fetchUserDetails(); + + final envState = context.read().state; + LoadingOverlay.hide(context); + ReceiptOverlay.show( + productName: + receipt is SwipeReceipt ? receipt.menuItemName : receipt.productName, + timeUsed: receipt.timeUsed, + isTestEnvironment: envState is EnvironmentLoaded && envState.env.isTest, + status: receipt is PurchaseReceipt + ? receipt.paymentStatus.toString() + : '${Strings.swiped} via ${receipt.productName} ticket', + context: context, + ); + } } -class _CoffeeCardLoading extends StatelessWidget { - const _CoffeeCardLoading(); +/// The section that is shown when the tickets are loaded. +/// +/// If the list of tickets is empty, a placeholder is shown. +class LoadedTicketsSection extends StatelessWidget { + LoadedTicketsSection(List tickets) + : maybeTickets = Option.fromPredicate(tickets, (ts) => ts.isNotEmpty); + + /// The list of tickets, or [None] if the list is empty. + final Option> maybeTickets; @override Widget build(BuildContext context) { return Column( - children: [ - ShimmerBuilder( - showShimmer: true, - builder: (context, colorIfShimmer) { - return const IgnorePointer( - child: CoffeeCard( - amountOwned: -1, - productId: -1, - title: '', + children: maybeTickets.fold( + () => [const NoTicketsPlaceholder()], + (tickets) => tickets + .map( + (ticket) => Padding( + padding: const EdgeInsets.only(bottom: 12.0), + child: Hero(tag: ticket, child: TicketsCard(ticket)), ), - ); - }, - ), - ], + ) + .toList(), + ), ); } } diff --git a/lib/features/user/data/datasources/user_remote_data_source.dart b/lib/features/user/data/datasources/user_remote_data_source.dart index 18abc6abd..ac363995c 100644 --- a/lib/features/user/data/datasources/user_remote_data_source.dart +++ b/lib/features/user/data/datasources/user_remote_data_source.dart @@ -16,13 +16,13 @@ class UserRemoteDataSource { }); /// Get the currently logged in user. - Future> getUser() { + Future> getUser() { return executor.execute(apiV2.apiV2AccountGet).map(UserModel.fromResponse); } /// Updates the details of the currently logged in user based on /// the non-null details in [user] - Future> updateUserDetails(UpdateUser user) { + Future> updateUserDetails(UpdateUser user) { return executor .execute( () => apiV2.apiV2AccountPut( @@ -39,7 +39,7 @@ class UserRemoteDataSource { } /// Request account deletion for the currently logged in user. - Future> requestAccountDeletion() { + Future> requestAccountDeletion() { return executor.executeAndDiscard(apiV2.apiV2AccountDelete); } } diff --git a/lib/features/voucher/data/datasources/voucher_remote_data_source.dart b/lib/features/voucher/data/datasources/voucher_remote_data_source.dart index 508dbc568..c0f935e19 100644 --- a/lib/features/voucher/data/datasources/voucher_remote_data_source.dart +++ b/lib/features/voucher/data/datasources/voucher_remote_data_source.dart @@ -14,9 +14,7 @@ class VoucherRemoteDataSource { required this.executor, }); - Future> redeemVoucher( - String voucher, - ) { + Future> redeemVoucher(String voucher) { return executor .execute( () => api.apiV2VouchersVoucherCodeRedeemPost(voucherCode: voucher), diff --git a/lib/main_development.dart b/lib/main_development.dart index 858791e56..4849d9634 100644 --- a/lib/main_development.dart +++ b/lib/main_development.dart @@ -4,12 +4,16 @@ import 'package:coffeecard/service_locator.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_crashlytics/firebase_crashlytics.dart'; import 'package:flutter/widgets.dart'; +import 'package:hive_flutter/hive_flutter.dart'; Future main() async { // Don't care about the return values of these variables. _ can't be used due to multiple variables needing to be discarded //ignore_for_file: avoid-ignoring-return-values WidgetsFlutterBinding.ensureInitialized(); + // Set up Hive for local storage + await Hive.initFlutter(); + await Firebase.initializeApp(options: DefaultFirebaseOptions.development); configureServices(); FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError; diff --git a/lib/main_production.dart b/lib/main_production.dart index 532961f42..0e818ed71 100644 --- a/lib/main_production.dart +++ b/lib/main_production.dart @@ -4,12 +4,16 @@ import 'package:coffeecard/service_locator.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_crashlytics/firebase_crashlytics.dart'; import 'package:flutter/widgets.dart'; +import 'package:hive_flutter/hive_flutter.dart'; Future main() async { // Don't care about the return values of these variables. _ can't be used due to multiple variables needing to be discarded //ignore_for_file: avoid-ignoring-return-values WidgetsFlutterBinding.ensureInitialized(); + // Set up Hive for local storage + await Hive.initFlutter(); + await Firebase.initializeApp(options: DefaultFirebaseOptions.production); configureServices(); FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError; diff --git a/lib/service_locator.dart b/lib/service_locator.dart index 38ab4e694..82fd19d7b 100644 --- a/lib/service_locator.dart +++ b/lib/service_locator.dart @@ -33,9 +33,8 @@ import 'package:coffeecard/features/opening_hours/data/repositories/opening_hour import 'package:coffeecard/features/opening_hours/domain/repositories/opening_hours_repository.dart'; import 'package:coffeecard/features/opening_hours/domain/usecases/get_opening_hours.dart'; import 'package:coffeecard/features/opening_hours/presentation/cubit/opening_hours_cubit.dart'; -import 'package:coffeecard/features/product/data/datasources/product_remote_data_source.dart'; -import 'package:coffeecard/features/product/domain/usecases/get_all_products.dart'; import 'package:coffeecard/features/product/presentation/cubit/product_cubit.dart'; +import 'package:coffeecard/features/product/product_repository.dart'; import 'package:coffeecard/features/purchase/data/datasources/purchase_remote_data_source.dart'; import 'package:coffeecard/features/reactivation/data/reactivation_authenticator.dart'; import 'package:coffeecard/features/receipt/data/datasources/receipt_remote_data_source.dart'; @@ -206,14 +205,18 @@ void initUser() { ); // use case - sl.registerFactory(() => LoadTickets(ticketRemoteDataSource: sl())); + sl.registerFactory( + () => LoadTickets( + ticketRemoteDataSource: sl(), + productRepository: sl(), + ), + ); sl.registerFactory(() => ConsumeTicket(ticketRemoteDataSource: sl())); // data source sl.registerLazySingleton( () => TicketRemoteDataSource( - apiV1: sl(), - apiV2: sl(), + api: sl(), executor: sl(), ), ); @@ -307,15 +310,10 @@ void initEnvironment() { void initProduct() { // bloc - sl.registerFactory(() => ProductCubit(getAllProducts: sl())); + sl.registerFactory(() => ProductCubit(productRepository: sl())); - // use case - sl.registerFactory(() => GetAllProducts(remoteDataSource: sl())); - - // data source - sl.registerLazySingleton( - () => ProductRemoteDataSource(api: sl(), executor: sl()), - ); + // repository + sl.registerLazySingleton(() => ProductRepository(api: sl(), executor: sl())); } void initVoucher() { diff --git a/openapi/coffeecard_api_v2.swagger.json b/openapi/coffeecard_api_v2.swagger.json index 458ae4892..516f44e14 100644 --- a/openapi/coffeecard_api_v2.swagger.json +++ b/openapi/coffeecard_api_v2.swagger.json @@ -208,6 +208,74 @@ } } }, + "/api/v2/account/{id}/user-group": { + "patch": { + "tags": [ + "Account" + ], + "summary": "Updates the user group of a user", + "operationId": "Account_UpdateAccountUserGroup", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "description": "id of the user whose userGroup will be updated ", + "schema": { + "type": "integer", + "format": "int32" + }, + "x-position": 1 + } + ], + "requestBody": { + "x-name": "updateUserGroupRequest", + "description": "Update User Group information request ", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UpdateUserGroupRequest" + } + } + }, + "required": true, + "x-position": 2 + }, + "responses": { + "204": { + "description": " The update was processed " + }, + "401": { + "description": " Invalid credentials ", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiError" + } + } + } + }, + "404": { + "description": " User not found ", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiError" + } + } + } + } + }, + "security": [ + { + "jwt": [] + }, + { + "apikey": [] + } + ] + } + }, "/api/v2/account/resend-verification-email": { "post": { "tags": [ @@ -255,6 +323,82 @@ } } }, + "/api/v2/account/search": { + "get": { + "tags": [ + "Account" + ], + "summary": "Searches a user in the database", + "operationId": "Account_SearchUsers", + "parameters": [ + { + "name": "pageNum", + "in": "query", + "description": "The page number", + "schema": { + "type": "integer", + "format": "int32", + "maximum": 2147483647, + "minimum": 0 + }, + "x-position": 1 + }, + { + "name": "filter", + "in": "query", + "description": "A filter to search by Id, Name or Email. When an empty string is given, all users will be returned", + "schema": { + "type": "string", + "default": "" + }, + "x-position": 2 + }, + { + "name": "pageLength", + "in": "query", + "description": "The length of a page", + "schema": { + "type": "integer", + "format": "int32", + "default": 30, + "maximum": 100, + "minimum": 1 + }, + "x-position": 3 + } + ], + "responses": { + "200": { + "description": "Users, possible with filter applied", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UserSearchResponse" + } + } + } + }, + "401": { + "description": " Invalid credentials ", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiError" + } + } + } + } + }, + "security": [ + { + "jwt": [] + }, + { + "apikey": [] + } + ] + } + }, "/api/v2/appconfig": { "get": { "tags": [ @@ -583,7 +727,7 @@ "tags": [ "Products" ], - "summary": "Returns a list of available products based on a account's user group", + "summary": "Returns a list of available products based on a account's user group.", "operationId": "Products_GetProducts", "responses": { "200": { @@ -598,8 +742,106 @@ } } } + }, + "401": { + "description": "Invalid credentials" } - } + }, + "security": [ + { + "jwt": [] + }, + { + "apikey": [] + } + ] + } + }, + "/api/v2/products/{id}": { + "get": { + "tags": [ + "Products" + ], + "summary": "Returns a product with the specified id", + "operationId": "Products_GetProduct", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "description": "The id of the product to be returned", + "schema": { + "type": "integer", + "format": "int32" + }, + "x-position": 1 + } + ], + "responses": { + "200": { + "description": "Successful request", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProductResponse" + } + } + } + }, + "401": { + "description": "Invalid credentials" + }, + "404": { + "description": "The product with the specified id could not be found", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiError" + } + } + } + } + }, + "security": [ + { + "jwt": [] + }, + { + "apikey": [] + } + ] + } + }, + "/api/v2/products/all": { + "get": { + "tags": [ + "Products" + ], + "summary": "Returns a list of all products", + "operationId": "Products_GetAllProducts", + "responses": { + "200": { + "description": "Successful request", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ProductResponse" + } + } + } + } + } + }, + "security": [ + { + "jwt": [] + }, + { + "apikey": [] + } + ] } }, "/api/v2/purchases": { @@ -787,6 +1029,71 @@ ] } }, + "/api/v2/tickets/use": { + "post": { + "tags": [ + "Tickets" + ], + "summary": "Uses a ticket (for the given product) on the given menu item", + "operationId": "Tickets_UseTicket", + "requestBody": { + "x-name": "request", + "description": "The product id and menu item id to use a ticket for", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UseTicketRequest" + } + } + }, + "required": true, + "x-position": 1 + }, + "responses": { + "200": { + "description": "Successful request", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UsedTicketResponse" + } + } + } + }, + "401": { + "description": "Invalid credentials" + }, + "403": { + "description": "User has no tickets for the product or the menu item is not eligible for the ticket", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiError" + } + } + } + }, + "404": { + "description": "The product or menu item could not be found", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ApiError" + } + } + } + } + }, + "security": [ + { + "jwt": [] + }, + { + "apikey": [] + } + ] + } + }, "/api/v2/vouchers/issue-vouchers": { "post": { "tags": [ @@ -938,7 +1245,7 @@ "name": "John Doe", "email": "john@doe.com", "password": "[no example provided]", - "programme": 1 + "programmeId": 1 }, "additionalProperties": false, "required": [ @@ -1206,6 +1513,44 @@ } } }, + "UpdateUserGroupRequest": { + "type": "object", + "description": "Update the UserGroup property of a user", + "example": { + "UserGroup": "Barista" + }, + "additionalProperties": false, + "required": [ + "userGroup" + ], + "properties": { + "userGroup": { + "description": "The UserGroup of a user", + "example": "UserGroup.Barista ", + "oneOf": [ + { + "$ref": "#/components/schemas/UserGroup" + } + ] + } + } + }, + "UserGroup": { + "type": "string", + "description": "", + "x-enumNames": [ + "Customer", + "Barista", + "Manager", + "Board" + ], + "enum": [ + "Customer", + "Barista", + "Manager", + "Board" + ] + }, "ResendAccountVerificationEmailRequest": { "type": "object", "description": "Resend Invite email request", @@ -1226,6 +1571,94 @@ } } }, + "UserSearchResponse": { + "type": "object", + "description": "Represents a search result", + "additionalProperties": false, + "required": [ + "users", + "totalUsers" + ], + "properties": { + "users": { + "type": "array", + "description": "The users that match the query", + "example": [ + { + "id": 12232, + "name": "John Doe", + "email": "johndoe@itu.dk", + "userGroup": "Barista", + "state": "Active" + } + ], + "items": { + "$ref": "#/components/schemas/SimpleUserResponse" + } + }, + "totalUsers": { + "type": "integer", + "description": "The number of users that match the query", + "format": "int32", + "example": 1 + } + } + }, + "SimpleUserResponse": { + "type": "object", + "description": "Basic User details", + "additionalProperties": false, + "properties": { + "id": { + "type": "integer", + "description": "User Id", + "format": "int32", + "example": 1 + }, + "name": { + "type": "string", + "description": "User's Display Name", + "example": "Name" + }, + "email": { + "type": "string", + "description": "User's Email", + "example": "john@doe.test" + }, + "userGroup": { + "description": "User's User group relationship", + "example": "Barista", + "oneOf": [ + { + "$ref": "#/components/schemas/UserGroup" + } + ] + }, + "state": { + "description": "User's State", + "example": "Active", + "oneOf": [ + { + "$ref": "#/components/schemas/UserState" + } + ] + } + } + }, + "UserState": { + "type": "string", + "description": "", + "x-enumNames": [ + "Active", + "Deleted", + "PendingActivition" + ], + "enum": [ + "Active", + "Deleted", + "PendingActivition" + ] + }, "AppConfig": { "type": "object", "description": "App Configuration", @@ -1402,7 +1835,8 @@ "numberOfTickets", "name", "description", - "visible" + "visible", + "allowedUserGroups" ], "properties": { "price": { @@ -1411,13 +1845,7 @@ "format": "int32", "maximum": 2147483647, "minimum": 0, - "example": { - "Price": 150, - "NumberOfTickets": 10, - "Name": "Espresso", - "Description": "A coffee made by forcing steam through ground coffee beans.", - "Visible": false - } + "example": 150 }, "numberOfTickets": { "type": "integer", @@ -1443,6 +1871,14 @@ "type": "boolean", "description": "Gets or sets the visibility of the product.", "example": true + }, + "allowedUserGroups": { + "type": "array", + "description": "Gets or sets the user groups that can access the product.", + "example": "Manager, Board ", + "items": { + "$ref": "#/components/schemas/UserGroup" + } } } }, @@ -1454,7 +1890,11 @@ "Price": 25, "NumberOfTickets": 10, "Description": "xxx", - "Visible": true + "Visible": true, + "AllowedUserGroups": [ + "Manager", + "Board" + ] }, "additionalProperties": false, "required": [ @@ -1501,28 +1941,14 @@ }, "allowedUserGroups": { "type": "array", + "description": "Gets or sets the user groups that can access the product.", + "example": "Manager, Board ", "items": { "$ref": "#/components/schemas/UserGroup" } } } }, - "UserGroup": { - "type": "string", - "description": "", - "x-enumNames": [ - "Customer", - "Barista", - "Manager", - "Board" - ], - "enum": [ - "Customer", - "Barista", - "Manager", - "Board" - ] - }, "UpdateProductRequest": { "type": "object", "description": "Initiate an update product request.", @@ -1532,7 +1958,11 @@ "NumberOfTickets": 10, "Name": "Espresso", "Description": "A coffee made by forcing steam through ground coffee beans.", - "Visible": false + "Visible": false, + "AllowedUserGroups": [ + "Manager", + "Board" + ] }, "additionalProperties": false, "required": [ @@ -1540,7 +1970,8 @@ "price", "numberOfTickets", "name", - "description" + "description", + "allowedUserGroups" ], "properties": { "id": { @@ -1582,6 +2013,14 @@ "description": "Gets or sets the updated visibility of the product. Default is true.", "default": true, "example": true + }, + "allowedUserGroups": { + "type": "array", + "description": "Gets or sets the user groups that can access the product.", + "example": "Manager, Board ", + "items": { + "$ref": "#/components/schemas/UserGroup" + } } } }, @@ -1594,7 +2033,22 @@ "numberOfTickets": 10, "name": "Coffee clip card", "description": "Coffee clip card of 10 clips", - "isPerk": true + "isPerk": true, + "visible": true, + "allowedUserGroups": [ + "Manager", + "Board" + ], + "eligibleMenuItems": [ + { + "id": 1, + "name": "Cappuccino" + }, + { + "id": 2, + "name": "Caffe Latte" + } + ] }, "additionalProperties": false, "required": [ @@ -1603,7 +2057,9 @@ "numberOfTickets", "name", "description", - "isPerk" + "isPerk", + "visible", + "allowedUserGroups" ], "properties": { "id": { @@ -1640,6 +2096,54 @@ "type": "boolean", "description": "Eligible due to a user perk privilege ", "example": true + }, + "visible": { + "type": "boolean", + "description": "Visibility of products for users", + "example": true + }, + "allowedUserGroups": { + "type": "array", + "description": "Decides the user groups that can access the product.", + "example": "Manager, Board ", + "items": { + "$ref": "#/components/schemas/UserGroup" + } + }, + "eligibleMenuItems": { + "type": "array", + "description": "The menu items that this product can be used on.", + "example": "Cappuccino, Caffe Latte", + "items": { + "$ref": "#/components/schemas/MenuItemResponse" + } + } + } + }, + "MenuItemResponse": { + "type": "object", + "description": "Represents a menu item that can be redeemed with a ticket", + "example": { + "id": 1, + "name": "Cappuccino" + }, + "additionalProperties": false, + "required": [ + "id", + "name" + ], + "properties": { + "id": { + "type": "integer", + "description": "Id of menu item", + "format": "int32", + "example": 1 + }, + "name": { + "type": "string", + "description": "Name of menu item", + "minLength": 1, + "example": "Cappuccino" } } }, @@ -2069,6 +2573,80 @@ "description": "Name of product a ticket is for", "minLength": 1, "example": "Coffee" + }, + "usedOnMenuItemName": { + "type": "string", + "description": "The name of the menu item that this ticket was used on", + "nullable": true, + "example": "Cappuccino" + } + } + }, + "UsedTicketResponse": { + "type": "object", + "description": "Representing a used ticket for a product", + "additionalProperties": false, + "required": [ + "id", + "dateCreated", + "dateUsed", + "productName" + ], + "properties": { + "id": { + "type": "integer", + "description": "Ticket Id", + "format": "int32", + "example": 122 + }, + "dateCreated": { + "type": "string", + "description": "Issuing date time for ticket in Utc format", + "format": "date-time", + "minLength": 1, + "example": "2022-01-09T21:03:52.2283208Z" + }, + "dateUsed": { + "type": "string", + "description": "Used date time for ticket in Utc format", + "format": "date-time", + "minLength": 1, + "example": "2022-01-09T21:03:52.2283208Z" + }, + "productName": { + "type": "string", + "description": "Name of product a ticket is for", + "minLength": 1, + "example": "Small drink" + }, + "menuItemName": { + "type": "string", + "description": "Name of the menu item that this ticket was used on", + "nullable": true, + "example": "Cappuccino" + } + } + }, + "UseTicketRequest": { + "type": "object", + "description": "Represents a request to use a ticket.", + "additionalProperties": false, + "required": [ + "productId", + "menuItemId" + ], + "properties": { + "productId": { + "type": "integer", + "description": "The id of the product the ticket is for.", + "format": "int32", + "example": 1 + }, + "menuItemId": { + "type": "integer", + "description": "The id of the menu item to use the ticket on.", + "format": "int32", + "example": 1 } } }, diff --git a/pubspec.lock b/pubspec.lock index 4e3355b02..6f02742a3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -600,6 +600,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.1" + hive: + dependency: "direct main" + description: + name: hive + sha256: "8dcf6db979d7933da8217edcec84e9df1bdb4e4edc7fc77dbd5aa74356d6d941" + url: "https://pub.dev" + source: hosted + version: "2.2.3" + hive_flutter: + dependency: "direct main" + description: + name: hive_flutter + sha256: dca1da446b1d808a51689fb5d0c6c9510c0a2ba01e22805d492c73b68e33eecc + url: "https://pub.dev" + source: hosted + version: "1.1.0" html: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e092be19e..229faa5c9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,6 +28,8 @@ dependencies: # storage flutter_secure_storage: 9.0.0 + hive: 2.2.3 + hive_flutter: 1.1.0 # dependency injection get_it: 7.6.4 diff --git a/test/core/firebase_analytics_event_logging_test.dart b/test/core/firebase_analytics_event_logging_test.dart index da4196654..c5b597d83 100644 --- a/test/core/firebase_analytics_event_logging_test.dart +++ b/test/core/firebase_analytics_event_logging_test.dart @@ -1,5 +1,6 @@ import 'package:coffeecard/core/firebase_analytics_event_logging.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; +import 'package:coffeecard/features/product/menu_item_model.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment_status.dart'; import 'package:firebase_analytics/firebase_analytics.dart'; @@ -26,6 +27,7 @@ void main() { amount: amount, description: '', isPerk: false, + eligibleMenuItems: const [MenuItem(id: 0, name: 'Cappuccino')], ); } diff --git a/test/core/widgets/components/tickets/buy_tickets_card_test.dart b/test/core/widgets/components/tickets/buy_tickets_card_test.dart index 9794813ab..dfb1dc179 100644 --- a/test/core/widgets/components/tickets/buy_tickets_card_test.dart +++ b/test/core/widgets/components/tickets/buy_tickets_card_test.dart @@ -1,5 +1,5 @@ -import 'package:coffeecard/features/product/domain/entities/product.dart'; import 'package:coffeecard/features/product/presentation/widgets/buy_tickets_card.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -16,6 +16,7 @@ void main() { amount: 2, price: 10, isPerk: false, + eligibleMenuItems: [], ), ), ), @@ -40,6 +41,7 @@ void main() { amount: 1, price: 1, isPerk: false, + eligibleMenuItems: [], ), ), ), diff --git a/test/core/widgets/components/tickets/coffee_card_test.dart b/test/core/widgets/components/tickets/coffee_card_test.dart deleted file mode 100644 index 82656fa8b..000000000 --- a/test/core/widgets/components/tickets/coffee_card_test.dart +++ /dev/null @@ -1,38 +0,0 @@ -import 'package:coffeecard/core/widgets/components/coffee_card.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - -void main() { - testWidgets('CoffeeCard has a title', (tester) async { - await tester.pumpWidget( - const MaterialApp( - home: Scaffold( - body: CoffeeCard( - title: 'Coffee', - amountOwned: 0, - productId: 0, - ), - ), - ), - ); - expect(find.text('Coffee'), findsOneWidget); - }); - - testWidgets('Coffee card matches golden file', (tester) async { - await tester.pumpWidget( - const MaterialApp( - home: Scaffold( - body: CoffeeCard( - title: 'Coffee', - amountOwned: 1, - productId: 0, - ), - ), - ), - ); - await expectLater( - find.byType(CoffeeCard), - matchesGoldenFile('goldens/coffee_card.png'), - ); - }); -} diff --git a/test/core/widgets/components/tickets/tickets_card_test.dart b/test/core/widgets/components/tickets/tickets_card_test.dart new file mode 100644 index 000000000..5fc11807a --- /dev/null +++ b/test/core/widgets/components/tickets/tickets_card_test.dart @@ -0,0 +1,34 @@ +import 'package:coffeecard/core/widgets/components/tickets_card.dart'; +import 'package:coffeecard/features/product/product_model.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + final testProduct = const Product.empty().copyWith(name: 'Small drink'); + final testTicket = const Ticket.empty().copyWith(product: testProduct); + + testWidgets('TicketsCard has a title', (tester) async { + await tester.pumpWidget( + MaterialApp(home: Scaffold(body: TicketsCard(testTicket))), + ); + expect(find.text('Small drink'), findsOneWidget); + }); + + // TODO(marfavi): Due to the use of OS-specific rendering of golden files, + // this test will fail on Windows and Mac. We should find a way to make + // golden tests platform-independent. + testWidgets( + 'Tickets card matches golden file', + (tester) async { + await tester.pumpWidget( + MaterialApp(home: Scaffold(body: TicketsCard(testTicket))), + ); + await expectLater( + find.byType(TicketsCard), + matchesGoldenFile('goldens/tickets_card.png'), + ); + }, + skip: true, + ); +} diff --git a/test/features/environment/data/datasources/environment_remote_data_source_test.dart b/test/features/environment/data/datasources/environment_remote_data_source_test.dart index 96a10a974..0db38dd59 100644 --- a/test/features/environment/data/datasources/environment_remote_data_source_test.dart +++ b/test/features/environment/data/datasources/environment_remote_data_source_test.dart @@ -24,7 +24,7 @@ void main() { executor: executor, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/environment/domain/usecases/get_environment_type_test.dart b/test/features/environment/domain/usecases/get_environment_type_test.dart index 7986b9521..323e44575 100644 --- a/test/features/environment/domain/usecases/get_environment_type_test.dart +++ b/test/features/environment/domain/usecases/get_environment_type_test.dart @@ -18,7 +18,7 @@ void main() { remoteDataSource = MockEnvironmentRemoteDataSource(); usecase = GetEnvironmentType(remoteDataSource: remoteDataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/leaderboard/data/datasources/leaderboard_remote_data_source_test.dart b/test/features/leaderboard/data/datasources/leaderboard_remote_data_source_test.dart index 75bcae480..3b6684af0 100644 --- a/test/features/leaderboard/data/datasources/leaderboard_remote_data_source_test.dart +++ b/test/features/leaderboard/data/datasources/leaderboard_remote_data_source_test.dart @@ -11,7 +11,10 @@ import 'package:mockito/mockito.dart'; import 'leaderboard_remote_data_source_test.mocks.dart'; -@GenerateMocks([CoffeecardApiV2, NetworkRequestExecutor]) +@GenerateNiceMocks([ + MockSpec(), + MockSpec(), +]) void main() { late MockCoffeecardApiV2 apiV2; late MockNetworkRequestExecutor executor; @@ -22,10 +25,10 @@ void main() { executor = MockNetworkRequestExecutor(); dataSource = LeaderboardRemoteDataSource(apiV2: apiV2, executor: executor); - provideDummy>>( + provideDummy>>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/leaderboard/domain/usecases/get_leaderboard_test.dart b/test/features/leaderboard/domain/usecases/get_leaderboard_test.dart index f99fecfec..6c38f242a 100644 --- a/test/features/leaderboard/domain/usecases/get_leaderboard_test.dart +++ b/test/features/leaderboard/domain/usecases/get_leaderboard_test.dart @@ -19,10 +19,10 @@ void main() { remoteDataSource = MockLeaderboardRemoteDataSource(); usecase = GetLeaderboard(remoteDataSource: remoteDataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>>( + provideDummy>>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/login/data/datasources/account_remote_data_source_test.dart b/test/features/login/data/datasources/account_remote_data_source_test.dart index db7624160..ac1cf6347 100644 --- a/test/features/login/data/datasources/account_remote_data_source_test.dart +++ b/test/features/login/data/datasources/account_remote_data_source_test.dart @@ -36,16 +36,16 @@ void main() { executor: executor, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/login/domain/usecases/resend_email_test.dart b/test/features/login/domain/usecases/resend_email_test.dart index a81da6b78..c0c5583f9 100644 --- a/test/features/login/domain/usecases/resend_email_test.dart +++ b/test/features/login/domain/usecases/resend_email_test.dart @@ -17,7 +17,7 @@ void main() { remoteDataSource = MockAccountRemoteDataSource(); usecase = ResendEmail(remoteDataSource: remoteDataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/occupation/data/datasources/occupation_remote_data_source_test.dart b/test/features/occupation/data/datasources/occupation_remote_data_source_test.dart index a8b46ec78..d8f718552 100644 --- a/test/features/occupation/data/datasources/occupation_remote_data_source_test.dart +++ b/test/features/occupation/data/datasources/occupation_remote_data_source_test.dart @@ -20,7 +20,7 @@ void main() { executor = MockNetworkRequestExecutor(); dataSource = OccupationRemoteDataSource(api: api, executor: executor); - provideDummy>>( + provideDummy>>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/occupation/domain/usecase/get_occupations_test.dart b/test/features/occupation/domain/usecase/get_occupations_test.dart index 64b9d61b7..1c8d2c0e7 100644 --- a/test/features/occupation/domain/usecase/get_occupations_test.dart +++ b/test/features/occupation/domain/usecase/get_occupations_test.dart @@ -18,7 +18,7 @@ void main() { dataSource = MockOccupationRemoteDataSource(); usecase = GetOccupations(dataSource: dataSource); - provideDummy>>( + provideDummy>>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/product/data/datasources/product_remote_data_source_test.dart b/test/features/product/data/datasources/product_remote_data_source_test.dart deleted file mode 100644 index 92d0d3d39..000000000 --- a/test/features/product/data/datasources/product_remote_data_source_test.dart +++ /dev/null @@ -1,69 +0,0 @@ -import 'package:coffeecard/core/errors/failures.dart'; -import 'package:coffeecard/core/network/network_request_executor.dart'; -import 'package:coffeecard/features/product/data/datasources/product_remote_data_source.dart'; -import 'package:coffeecard/features/product/data/models/product_model.dart'; -import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:fpdart/fpdart.dart'; -import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; - -import 'product_remote_data_source_test.mocks.dart'; - -@GenerateMocks([CoffeecardApiV2, NetworkRequestExecutor]) -void main() { - late ProductRemoteDataSource remoteDataSource; - late MockCoffeecardApiV2 api; - late MockNetworkRequestExecutor executor; - - setUp(() { - executor = MockNetworkRequestExecutor(); - api = MockCoffeecardApiV2(); - remoteDataSource = ProductRemoteDataSource(api: api, executor: executor); - - provideDummy>>( - const Left(ConnectionFailure()), - ); - }); - - group('getProducts', () { - test('should call executor', () async { - // arrange - when(executor.execute>(any)).thenAnswer( - (_) async => const Right([ - ProductResponse( - id: 0, - price: 0, - numberOfTickets: 0, - name: 'name', - description: 'description', - isPerk: false, - ), - ]), - ); - - // act - final actual = await remoteDataSource.getProducts(); - - // assert - actual.fold( - (_) => throw Exception(), - (actual) { - expect( - actual, - [ - const ProductModel( - price: 0, - amount: 0, - name: 'name', - id: 0, - description: 'description', - isPerk: false, - ), - ], - ); - }, - ); - }); - }); -} diff --git a/test/features/product/domain/usecases/get_all_products_test.dart b/test/features/product/domain/usecases/get_all_products_test.dart deleted file mode 100644 index a85428226..000000000 --- a/test/features/product/domain/usecases/get_all_products_test.dart +++ /dev/null @@ -1,75 +0,0 @@ -import 'package:coffeecard/core/errors/failures.dart'; -import 'package:coffeecard/features/product/data/datasources/product_remote_data_source.dart'; -import 'package:coffeecard/features/product/data/models/product_model.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; -import 'package:coffeecard/features/product/domain/usecases/get_all_products.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:fpdart/fpdart.dart'; -import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; - -import 'get_all_products_test.mocks.dart'; - -@GenerateMocks([ProductRemoteDataSource]) -void main() { - late GetAllProducts usecase; - late MockProductRemoteDataSource remoteDataSource; - - setUp(() { - remoteDataSource = MockProductRemoteDataSource(); - usecase = GetAllProducts(remoteDataSource: remoteDataSource); - - provideDummy>>( - const Left(ConnectionFailure()), - ); - }); - - test( - 'should return [Right, Iterable>] if data source succeeds', - () async { - // arrange - const products = [ - ProductModel( - id: 1, - name: 'test (bundle of 10)', - amount: 10, - price: 1, - description: 'test', - isPerk: false, - ), - ProductModel( - id: 2, - name: 'test (single)', - amount: 1, - price: 1, - description: 'test', - isPerk: false, - ), - ProductModel( - id: 3, - name: 'test (single perk)', - amount: 1, - price: 0, - description: 'test', - isPerk: true, - ), - ]; - - when(remoteDataSource.getProducts()) - .thenAnswer((_) async => const Right(products)); - - // act - final actual = await usecase(); - - // assert - actual.fold( - (_) => throw Exception(), - (actual) { - expect(actual.clipCards, [products.first]); - expect(actual.singleDrinks, [products[1], products[2]]); - expect(actual.perks, [products.last]); - }, - ); - }, - ); -} diff --git a/test/features/product/presentation/cubit/product_cubit_test.dart b/test/features/product/presentation/cubit/product_cubit_test.dart index c0a7308d9..5ae96608a 100644 --- a/test/features/product/presentation/cubit/product_cubit_test.dart +++ b/test/features/product/presentation/cubit/product_cubit_test.dart @@ -1,9 +1,10 @@ import 'package:bloc_test/bloc_test.dart'; import 'package:coffeecard/core/errors/failures.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; -import 'package:coffeecard/features/product/domain/entities/purchasable_products.dart'; -import 'package:coffeecard/features/product/domain/usecases/get_all_products.dart'; +import 'package:coffeecard/features/product/menu_item_model.dart'; import 'package:coffeecard/features/product/presentation/cubit/product_cubit.dart'; +import 'package:coffeecard/features/product/product_model.dart'; +import 'package:coffeecard/features/product/product_repository.dart'; +import 'package:coffeecard/features/product/purchasable_products.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:fpdart/fpdart.dart'; import 'package:mockito/annotations.dart'; @@ -11,20 +12,25 @@ import 'package:mockito/mockito.dart'; import 'product_cubit_test.mocks.dart'; -@GenerateNiceMocks([MockSpec()]) +@GenerateNiceMocks([MockSpec()]) void main() { late ProductCubit cubit; - late MockGetAllProducts getAllProducts; + late MockProductRepository productRepository; setUp(() { - getAllProducts = MockGetAllProducts(); - cubit = ProductCubit(getAllProducts: getAllProducts); + productRepository = MockProductRepository(); + cubit = ProductCubit(productRepository: productRepository); - provideDummy>( - const Left(ConnectionFailure()), + provideDummy>>( + TaskEither.left(const ConnectionFailure()), ); }); + const testMenuItems = [ + MenuItem(id: 1, name: 'Cappuccino'), + MenuItem(id: 2, name: 'Espresso'), + ]; + const tickets = [ Product( id: 1, @@ -33,6 +39,7 @@ void main() { price: 1, description: 'test', isPerk: false, + eligibleMenuItems: testMenuItems, ), ]; const singleDrinks = [ @@ -43,6 +50,7 @@ void main() { price: 1, description: 'test', isPerk: false, + eligibleMenuItems: testMenuItems, ), ]; const perks = [ @@ -53,39 +61,39 @@ void main() { price: 0, description: 'deription', isPerk: true, + eligibleMenuItems: testMenuItems, ), ]; - const allProducts = ( + + const groupedProducts = ( clipCards: tickets, singleDrinks: singleDrinks, perks: perks, ); - const testError = 'some error'; + final allProducts = groupedProducts.all; + + const testFailure = Left>( + ServerFailure('some error', 500), + ); group('getProducts', () { blocTest( - 'should emit [Loading, Loaded] use case succeeds', + 'should emit [Loaded] use case succeeds', build: () => cubit, - setUp: () => when(getAllProducts()) - .thenAnswer((_) async => const Right(allProducts)), + setUp: () => when(productRepository.getProducts()) + .thenAnswer((_) => TaskEither.fromEither(Right(allProducts))), act: (cubit) => cubit.getProducts(), - expect: () => [ - const ProductsLoading(), - const ProductsLoaded(allProducts), - ], + expect: () => [isA()], ); blocTest( - 'should emit [Loading, Error] when use case fails', + 'should emit [Error] when use case fails', build: () => cubit, - setUp: () => when(getAllProducts()) - .thenAnswer((_) async => const Left(ServerFailure(testError, 500))), + setUp: () => when(productRepository.getProducts()) + .thenAnswer((_) => TaskEither.fromEither(testFailure)), act: (cubit) => cubit.getProducts(), - expect: () => [ - const ProductsLoading(), - const ProductsError(testError), - ], + expect: () => [isA()], ); }); } diff --git a/test/features/purchase/data/datasources/purchase_remote_data_source_test.dart b/test/features/purchase/data/datasources/purchase_remote_data_source_test.dart index 66d0f25b0..8155b5c06 100644 --- a/test/features/purchase/data/datasources/purchase_remote_data_source_test.dart +++ b/test/features/purchase/data/datasources/purchase_remote_data_source_test.dart @@ -28,10 +28,10 @@ void main() { executor: executor, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/purchase/data/repositories/free_product_service_test.dart b/test/features/purchase/data/repositories/free_product_service_test.dart index 5d98adeda..e525123cd 100644 --- a/test/features/purchase/data/repositories/free_product_service_test.dart +++ b/test/features/purchase/data/repositories/free_product_service_test.dart @@ -27,7 +27,7 @@ void main() { buildContext: buildContext, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/purchase/data/repositories/mobilepay_service_test.dart b/test/features/purchase/data/repositories/mobilepay_service_test.dart index a7bb8afe1..684310766 100644 --- a/test/features/purchase/data/repositories/mobilepay_service_test.dart +++ b/test/features/purchase/data/repositories/mobilepay_service_test.dart @@ -31,7 +31,7 @@ void main() { buildContext: buildContext, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/purchase/presentation/cubit/purchase_cubit_test.dart b/test/features/purchase/presentation/cubit/purchase_cubit_test.dart index 1f01a810e..e37104748 100644 --- a/test/features/purchase/presentation/cubit/purchase_cubit_test.dart +++ b/test/features/purchase/presentation/cubit/purchase_cubit_test.dart @@ -1,7 +1,8 @@ import 'package:bloc_test/bloc_test.dart'; import 'package:coffeecard/core/errors/failures.dart'; import 'package:coffeecard/core/firebase_analytics_event_logging.dart'; -import 'package:coffeecard/features/product/domain/entities/product.dart'; +import 'package:coffeecard/features/product/menu_item_model.dart'; +import 'package:coffeecard/features/product/product_model.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment.dart'; import 'package:coffeecard/features/purchase/domain/entities/payment_status.dart'; import 'package:coffeecard/features/purchase/domain/usecases/init_purchase.dart'; @@ -23,6 +24,11 @@ void main() { late MockFirebaseAnalyticsEventLogging firebaseAnalyticsEventLogging; late PurchaseCubit cubit; + const testMenuItems = [ + MenuItem(id: 1, name: 'Cappuccino'), + MenuItem(id: 2, name: 'Espresso'), + ]; + const testProduct = Product( price: 0, amount: 0, @@ -30,6 +36,7 @@ void main() { id: 0, description: 'description', isPerk: false, + eligibleMenuItems: testMenuItems, ); setUp(() { diff --git a/test/features/receipt/data/datasources/receipt_remote_data_source_test.dart b/test/features/receipt/data/datasources/receipt_remote_data_source_test.dart index 2a0f1ca7f..a294c30c4 100644 --- a/test/features/receipt/data/datasources/receipt_remote_data_source_test.dart +++ b/test/features/receipt/data/datasources/receipt_remote_data_source_test.dart @@ -1,6 +1,6 @@ import 'package:coffeecard/core/errors/failures.dart'; import 'package:coffeecard/core/network/network_request_executor.dart'; -import 'package:coffeecard/features/product/data/datasources/product_remote_data_source.dart'; +import 'package:coffeecard/features/product/product_repository.dart'; import 'package:coffeecard/features/receipt/data/datasources/receipt_remote_data_source.dart'; import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -11,7 +11,7 @@ import 'package:mockito/mockito.dart'; import 'receipt_remote_data_source_test.mocks.dart'; @GenerateMocks( - [CoffeecardApiV2, ProductRemoteDataSource, NetworkRequestExecutor], + [CoffeecardApiV2, ProductRepository, NetworkRequestExecutor], ) void main() { late ReceiptRemoteDataSource remoteDataSource; @@ -26,10 +26,10 @@ void main() { executor: executor, ); - provideDummy>>( + provideDummy>>( const Left(ConnectionFailure()), ); - provideDummy>>( + provideDummy>>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/receipt/data/repositories/receipt_repository_impl_test.dart b/test/features/receipt/data/repositories/receipt_repository_impl_test.dart index 10d80f153..2af2523af 100644 --- a/test/features/receipt/data/repositories/receipt_repository_impl_test.dart +++ b/test/features/receipt/data/repositories/receipt_repository_impl_test.dart @@ -62,6 +62,7 @@ void main() { productName: 'productName', timeUsed: DateTime.parse('2023-04-23'), id: 0, + menuItemName: 'menuItemName', ); final testPurchasedReceipt = PurchaseReceipt( diff --git a/test/features/receipt/presentation/cubit/receipt_cubit_test.dart b/test/features/receipt/presentation/cubit/receipt_cubit_test.dart index 44f9a3599..9e9091981 100644 --- a/test/features/receipt/presentation/cubit/receipt_cubit_test.dart +++ b/test/features/receipt/presentation/cubit/receipt_cubit_test.dart @@ -58,11 +58,13 @@ void main() { id: 1, productName: 'Coffee', timeUsed: DateTime.now(), + menuItemName: 'Espresso', ), SwipeReceipt( id: 2, productName: 'Coffee', timeUsed: DateTime.now(), + menuItemName: 'Espresso', ), ]; diff --git a/test/features/register/data/datasources/register_remote_data_source_test.dart b/test/features/register/data/datasources/register_remote_data_source_test.dart index 7fdce03a0..4731db789 100644 --- a/test/features/register/data/datasources/register_remote_data_source_test.dart +++ b/test/features/register/data/datasources/register_remote_data_source_test.dart @@ -23,10 +23,10 @@ void main() { executor: executor, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>(const Left(ConnectionFailure())); + provideDummy>(const Left(ConnectionFailure())); }); group('register', () { diff --git a/test/features/register/domain/usecases/register_user_test.dart b/test/features/register/domain/usecases/register_user_test.dart index 7f76b21b7..b4c53ce61 100644 --- a/test/features/register/domain/usecases/register_user_test.dart +++ b/test/features/register/domain/usecases/register_user_test.dart @@ -17,7 +17,7 @@ void main() { remoteDataSource = MockRegisterRemoteDataSource(); usecase = RegisterUser(remoteDataSource: remoteDataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/ticket/data/datasources/ticket_remote_data_source_test.dart b/test/features/ticket/data/datasources/ticket_remote_data_source_test.dart index a3f1c83ce..95f0d5e3e 100644 --- a/test/features/ticket/data/datasources/ticket_remote_data_source_test.dart +++ b/test/features/ticket/data/datasources/ticket_remote_data_source_test.dart @@ -1,7 +1,6 @@ import 'package:coffeecard/core/errors/failures.dart'; import 'package:coffeecard/core/network/network_request_executor.dart'; import 'package:coffeecard/features/ticket/data/datasources/ticket_remote_data_source.dart'; -import 'package:coffeecard/generated/api/coffeecard_api.swagger.dart'; import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:fpdart/fpdart.dart'; @@ -10,32 +9,32 @@ import 'package:mockito/mockito.dart'; import 'ticket_remote_data_source_test.mocks.dart'; -@GenerateMocks([CoffeecardApi, CoffeecardApiV2, NetworkRequestExecutor]) +@GenerateNiceMocks([ + MockSpec(), + MockSpec(), +]) void main() { - late MockCoffeecardApi apiV1; late MockCoffeecardApiV2 apiV2; late MockNetworkRequestExecutor executor; late TicketRemoteDataSource dataSource; setUp(() { - apiV1 = MockCoffeecardApi(); apiV2 = MockCoffeecardApiV2(); executor = MockNetworkRequestExecutor(); - dataSource = TicketRemoteDataSource( - apiV1: apiV1, - apiV2: apiV2, - executor: executor, - ); + dataSource = TicketRemoteDataSource(api: apiV2, executor: executor); - provideDummy>>( - const Left(ConnectionFailure()), + provideDummy>>( + TaskEither.left(const ConnectionFailure()), ); - provideDummy>( + provideDummy>>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); + provideDummy>( + TaskEither.left(const ConnectionFailure()), + ); }); group('getUserTickets', () { @@ -45,11 +44,11 @@ void main() { 'THEN a [Right] value is returned', () async { // arrange - when(executor.execute>(any)) - .thenAnswer((_) async => const Right([])); + when(executor.executeAsTask>(any)) + .thenAnswer((_) => TaskEither.right([])); // act - final actual = await dataSource.getUserTickets(); + final actual = await dataSource.getUserTickets().run(); // assert expect(actual.isRight(), isTrue); @@ -62,12 +61,12 @@ void main() { 'THEN a [Left] value is returned', () async { // arrange - when(executor.execute>(any)).thenAnswer( - (_) async => const Left(ServerFailure('some error', 500)), + when(executor.executeAsTask>(any)).thenAnswer( + (_) => TaskEither.left(const ServerFailure('some error', 500)), ); // act - final actual = await dataSource.getUserTickets(); + final actual = await dataSource.getUserTickets().run(); // assert expect(actual.isLeft(), isTrue); @@ -80,8 +79,8 @@ void main() { 'THEN a [TicketCountModel] with the count of tickets and joined ticket names is returned', () async { // arrange - when(executor.execute>(any)).thenAnswer( - (_) async => Right([ + when(executor.executeAsTask>(any)).thenAnswer( + (_) => TaskEither.right([ TicketResponse( id: 0, dateCreated: DateTime.parse('2023-05-23'), @@ -100,16 +99,17 @@ void main() { ); // act - final actual = await dataSource.getUserTickets(); + final actual = await dataSource.getUserTickets().run(); // assert expect(actual.isRight(), isTrue); final right = actual.getOrElse((_) => []); - expect(right, hasLength(1)); + expect(right, hasLength(2)); expect(right.first.productId, equals(0)); - expect(right.first.count, equals(2)); expect(right.first.productName, anyOf(['A/B', 'B/A'])); }, + // TODO(marfavi): This test no longer belongs at this level. Skip for now. + skip: 'This test no longer belongs at this level. Skip for now', ); }); @@ -122,19 +122,20 @@ void main() { 'THEN a [Right] value is returned', () async { // arrange - when(executor.execute(any)).thenAnswer( - (_) async => Right( + when(executor.executeAsTask(any)).thenAnswer( + (_) => TaskEither.right( UsedTicketResponse( id: 0, dateCreated: DateTime.parse('2023-04-11'), dateUsed: DateTime.parse('2023-04-11'), productName: 'productName', + menuItemName: 'menuItemName', ), ), ); // act - final actual = await dataSource.useTicket(0); + final actual = await dataSource.useTicket(0, 0).run(); // assert expect(actual.isRight(), isTrue); @@ -147,12 +148,12 @@ void main() { 'THEN a [Left] value is returned', () async { // arrange - when(executor.execute(any)).thenAnswer( - (_) async => const Left(ServerFailure('some error', 500)), + when(executor.executeAsTask(any)).thenAnswer( + (_) => TaskEither.left(const ServerFailure('some error', 500)), ); // act - final actual = await dataSource.useTicket(0); + final actual = await dataSource.useTicket(0, 0).run(); // assert expect(actual.isLeft(), isTrue); diff --git a/test/features/ticket/domain/usecases/consume_ticket_test.dart b/test/features/ticket/domain/usecases/consume_ticket_test.dart index d45371a3b..beeb67468 100644 --- a/test/features/ticket/domain/usecases/consume_ticket_test.dart +++ b/test/features/ticket/domain/usecases/consume_ticket_test.dart @@ -18,20 +18,21 @@ void main() { ticketRemoteDataSource = MockTicketRemoteDataSource(); usecase = ConsumeTicket(ticketRemoteDataSource: ticketRemoteDataSource); - provideDummy>( - const Left(ConnectionFailure()), + provideDummy>( + TaskEither.left(const ConnectionFailure()), ); }); test('should call repository', () async { // arrange - when(ticketRemoteDataSource.useTicket(any)) - .thenAnswer((_) async => const Left(ServerFailure('some error', 500))); + when(ticketRemoteDataSource.useTicket(any, any)).thenAnswer( + (_) => TaskEither.left(const ServerFailure('some error', 500)), + ); // act - await usecase(productId: 0); + await usecase(productId: 0, menuItemId: 0).run(); // assert - verify(ticketRemoteDataSource.useTicket(any)); + verify(ticketRemoteDataSource.useTicket(any, any)); }); } diff --git a/test/features/ticket/domain/usecases/load_tickets_test.dart b/test/features/ticket/domain/usecases/load_tickets_test.dart index 2d16ed7c1..831ffc166 100644 --- a/test/features/ticket/domain/usecases/load_tickets_test.dart +++ b/test/features/ticket/domain/usecases/load_tickets_test.dart @@ -1,7 +1,8 @@ import 'package:coffeecard/core/errors/failures.dart'; +import 'package:coffeecard/features/product/product_repository.dart'; import 'package:coffeecard/features/ticket/data/datasources/ticket_remote_data_source.dart'; -import 'package:coffeecard/features/ticket/data/models/ticket_count_model.dart'; import 'package:coffeecard/features/ticket/domain/usecases/load_tickets.dart'; +import 'package:coffeecard/generated/api/coffeecard_api_v2.models.swagger.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:fpdart/fpdart.dart'; import 'package:mockito/annotations.dart'; @@ -9,27 +10,36 @@ import 'package:mockito/mockito.dart'; import 'load_tickets_test.mocks.dart'; -@GenerateMocks([TicketRemoteDataSource]) +@GenerateNiceMocks([ + MockSpec(), + MockSpec(), +]) void main() { late LoadTickets usecase; late MockTicketRemoteDataSource ticketRemoteDataSource; + late MockProductRepository productRepository; setUp(() { ticketRemoteDataSource = MockTicketRemoteDataSource(); - usecase = LoadTickets(ticketRemoteDataSource: ticketRemoteDataSource); + productRepository = MockProductRepository(); - provideDummy>>( - const Left(ConnectionFailure()), + usecase = LoadTickets( + ticketRemoteDataSource: ticketRemoteDataSource, + productRepository: productRepository, + ); + + provideDummy>>( + TaskEither.left(const ConnectionFailure()), ); }); test('should call repository', () async { // arrange when(ticketRemoteDataSource.getUserTickets()) - .thenAnswer((_) async => const Right([])); + .thenAnswer((_) => TaskEither.fromEither(const Right([]))); // act - await usecase(); + await usecase().run(); // assert verify(ticketRemoteDataSource.getUserTickets()); diff --git a/test/features/ticket/presentation/cubit/tickets_cubit_test.dart b/test/features/ticket/presentation/cubit/tickets_cubit_test.dart index e41ecec8c..a41089675 100644 --- a/test/features/ticket/presentation/cubit/tickets_cubit_test.dart +++ b/test/features/ticket/presentation/cubit/tickets_cubit_test.dart @@ -1,7 +1,7 @@ import 'package:bloc_test/bloc_test.dart'; import 'package:coffeecard/core/errors/failures.dart'; import 'package:coffeecard/features/receipt/domain/entities/receipt.dart'; -import 'package:coffeecard/features/ticket/domain/entities/ticket_count.dart'; +import 'package:coffeecard/features/ticket/domain/entities/ticket.dart'; import 'package:coffeecard/features/ticket/domain/usecases/consume_ticket.dart'; import 'package:coffeecard/features/ticket/domain/usecases/load_tickets.dart'; import 'package:coffeecard/features/ticket/presentation/cubit/tickets_cubit.dart'; @@ -26,11 +26,11 @@ void main() { consumeTicket: consumeTicket, ); - provideDummy>>( - const Left(ConnectionFailure()), + provideDummy>>( + TaskEither.left(const ConnectionFailure()), ); - provideDummy>( - const Left(ConnectionFailure()), + provideDummy>( + TaskEither.left(const ConnectionFailure()), ); }); @@ -38,7 +38,7 @@ void main() { blocTest( 'should emit [Loading, Loaded] when use case succeeds', build: () => cubit, - setUp: () => when(loadTickets()).thenAnswer((_) async => const Right([])), + setUp: () => when(loadTickets()).thenAnswer((_) => TaskEither.right([])), act: (_) => cubit.getTickets(), expect: () => [ const TicketsLoading(), @@ -50,7 +50,7 @@ void main() { 'should emit [Loading, Error] when use case fails', build: () => cubit, setUp: () => when(loadTickets()).thenAnswer( - (_) async => const Left(ServerFailure('some error', 500)), + (_) => TaskEither.left(const ServerFailure('some error', 500)), ), act: (_) => cubit.getTickets(), expect: () => [ @@ -66,28 +66,34 @@ void main() { 'should not emit new state when state is not [Loaded]', build: () => cubit, setUp: () { - when(loadTickets()).thenAnswer((_) async => const Right([])); - when(consumeTicket(productId: anyNamed('productId'))) - .thenAnswer((_) async => Right(testReceipt)); + when(loadTickets()).thenAnswer((_) => TaskEither.right([])); + when( + consumeTicket( + productId: anyNamed('productId'), + menuItemId: anyNamed('menuItemId'), + ), + ).thenAnswer((_) => TaskEither.right(testReceipt)); }, - act: (cubit) => cubit.useTicket(0), + act: (cubit) => cubit.useTicket(0, 0), expect: () => [], ); blocTest( 'should emit [Using, Used, Loaded] when state is Loaded', build: () => cubit, - setUp: () { - when(loadTickets()).thenAnswer((_) async => const Right([])); - when(consumeTicket(productId: anyNamed('productId'))) - .thenAnswer((_) async => Right(testReceipt)); + setUp: () async { + when(loadTickets()).thenAnswer((_) => TaskEither.right([])); + when( + consumeTicket( + productId: anyNamed('productId'), + menuItemId: anyNamed('menuItemId'), + ), + ).thenAnswer((_) => TaskEither.right(testReceipt)); + await cubit.getTickets(); }, act: (_) async { - await cubit.getTickets(); - cubit.useTicket(0); + await cubit.useTicket(0, 0); }, - // skip the initial Loading/Loaded states emitted by getTickets - skip: 2, expect: () => [ const TicketUsing(tickets: []), TicketUsed(receipt: testReceipt, tickets: const []), @@ -98,30 +104,34 @@ void main() { blocTest( 'should emit [Using, Error, Loaded] when state is Loaded', build: () => cubit, - setUp: () { - when(loadTickets()).thenAnswer((_) async => const Right([])); - when(consumeTicket(productId: anyNamed('productId'))).thenAnswer( - (_) async => const Left(ServerFailure('some error', 500)), + setUp: () async { + when(loadTickets()).thenAnswer((_) => TaskEither.right([])); + when( + consumeTicket( + productId: anyNamed('productId'), + menuItemId: anyNamed('menuItemId'), + ), + ).thenAnswer( + (_) => TaskEither.left(const ServerFailure('some error', 500)), ); + await cubit.getTickets(); }, act: (_) async { - await cubit.getTickets(); - cubit.useTicket(0); + await cubit.useTicket(0, 0); }, - // skip the initial Loading/Loaded states emitted by getTickets - skip: 2, expect: () => [ const TicketUsing(tickets: []), - const TicketsUseError(message: 'some error'), + const TicketsUseError(message: 'some error', tickets: []), const TicketsLoaded(tickets: []), ], ); }); + group('refreshTickets', () { blocTest( 'should emit [Loaded] when use case succeeds', build: () => cubit, - setUp: () => when(loadTickets()).thenAnswer((_) async => const Right([])), + setUp: () => when(loadTickets()).thenAnswer((_) => TaskEither.right([])), act: (_) => cubit.refreshTickets(), expect: () => [ const TicketsLoaded(tickets: []), @@ -132,7 +142,7 @@ void main() { 'should emit [Error] when use case fails', build: () => cubit, setUp: () => when(loadTickets()).thenAnswer( - (_) async => const Left(ServerFailure('some error', 500)), + (_) => TaskEither.left(const ServerFailure('some error', 500)), ), act: (_) => cubit.refreshTickets(), expect: () => [const TicketsLoadError(message: 'some error')], diff --git a/test/features/user/data/datasources/user_remote_data_source_test.dart b/test/features/user/data/datasources/user_remote_data_source_test.dart index efd6ac10f..98a165572 100644 --- a/test/features/user/data/datasources/user_remote_data_source_test.dart +++ b/test/features/user/data/datasources/user_remote_data_source_test.dart @@ -32,10 +32,10 @@ void main() { executor: executor, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/user/domain/usecases/get_user_test.dart b/test/features/user/domain/usecases/get_user_test.dart index e505a8cc8..6e90be108 100644 --- a/test/features/user/domain/usecases/get_user_test.dart +++ b/test/features/user/domain/usecases/get_user_test.dart @@ -21,7 +21,7 @@ void main() { dataSource = MockUserRemoteDataSource(); usecase = GetUser(dataSource: dataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/user/domain/usecases/request_account_deletion_test.dart b/test/features/user/domain/usecases/request_account_deletion_test.dart index 730e3f93c..69e847ca5 100644 --- a/test/features/user/domain/usecases/request_account_deletion_test.dart +++ b/test/features/user/domain/usecases/request_account_deletion_test.dart @@ -17,7 +17,7 @@ void main() { dataSource = MockUserRemoteDataSource(); usecase = RequestAccountDeletion(dataSource: dataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/user/domain/usecases/update_user_details_test.dart b/test/features/user/domain/usecases/update_user_details_test.dart index e06b22ed8..31adcca4c 100644 --- a/test/features/user/domain/usecases/update_user_details_test.dart +++ b/test/features/user/domain/usecases/update_user_details_test.dart @@ -21,7 +21,7 @@ void main() { dataSource = MockUserRemoteDataSource(); usecase = UpdateUserDetails(dataSource: dataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/voucher/data/datasources/voucher_remote_data_source_test.dart b/test/features/voucher/data/datasources/voucher_remote_data_source_test.dart index 77dfd0c0c..d16ce4bc0 100644 --- a/test/features/voucher/data/datasources/voucher_remote_data_source_test.dart +++ b/test/features/voucher/data/datasources/voucher_remote_data_source_test.dart @@ -24,7 +24,7 @@ void main() { executor: executor, ); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); }); diff --git a/test/features/voucher/domain/usecases/redeem_voucher_code_test.dart b/test/features/voucher/domain/usecases/redeem_voucher_code_test.dart index f46ad8f23..8c04d6d36 100644 --- a/test/features/voucher/domain/usecases/redeem_voucher_code_test.dart +++ b/test/features/voucher/domain/usecases/redeem_voucher_code_test.dart @@ -18,7 +18,7 @@ void main() { dataSource = MockVoucherRemoteDataSource(); usecase = RedeemVoucherCode(dataSource: dataSource); - provideDummy>( + provideDummy>( const Left(ConnectionFailure()), ); });