From 1803a2b11948d8455095b0b70b4c45ededb9dba2 Mon Sep 17 00:00:00 2001 From: alzalia1 Date: Thu, 14 Aug 2025 13:42:20 +0200 Subject: [PATCH] feat: start working on sell --- lib/data/repositories/owner_repository.dart | 16 ++++++ lib/data/services/api_client.dart | 21 +++++++ lib/routing/routes.dart | 1 + .../add_page/view_model/add_view_model.dart | 15 ++++- .../add_page/widgets/confirmation_popup.dart | 55 ++++++++++--------- .../sell_page/widgets/manual_scan_popup.dart | 9 ++- lib/ui/sell_page/widgets/scan_screen.dart | 7 ++- lib/ui/sell_page/widgets/sell_page.dart | 1 - 8 files changed, 94 insertions(+), 31 deletions(-) diff --git a/lib/data/repositories/owner_repository.dart b/lib/data/repositories/owner_repository.dart index 8245e8d..80a6dfc 100644 --- a/lib/data/repositories/owner_repository.dart +++ b/lib/data/repositories/owner_repository.dart @@ -16,6 +16,22 @@ class OwnerRepository { final WebsocketClient _wsClient; late final StreamSubscription sub; List? _cachedOwners; + Owner? _sectionOwner; + + Future> get sectionOwner async { + if (_sectionOwner != null) { + return Result.ok(_sectionOwner!); + } + final result = await _apiClient.getSectionOwner(); + switch (result) { + case Ok(): + _sectionOwner = result.value; + break; + default: + break; + } + return result; + } /// Adds an [Owner] to the database, and gets the resulting [Owner]. Future> addOwner( diff --git a/lib/data/services/api_client.dart b/lib/data/services/api_client.dart index c9283bc..668a64e 100644 --- a/lib/data/services/api_client.dart +++ b/lib/data/services/api_client.dart @@ -304,6 +304,27 @@ class ApiClient { * ==================== */ + Future> getSectionOwner() async { + final client = Client(); + try { + final headers = await _getHeaders(); + final response = await client.get( + Uri.parse("https://$apiBasePath/owner/self"), + headers: headers, + ); + if (response.statusCode == 200) { + final json = jsonDecode(response.body); + return Result.ok(Owner.fromJSON(json)); + } else { + throw "Unknown error"; + } + } catch (e) { + return Result.error(Exception(e)); + } finally { + client.close(); + } + } + /// Call on `/owners` to get a list of all [Owner]s Future>> getOwners() async { final client = Client(); diff --git a/lib/routing/routes.dart b/lib/routing/routes.dart index b773ff8..8ce06c0 100644 --- a/lib/routing/routes.dart +++ b/lib/routing/routes.dart @@ -8,6 +8,7 @@ abstract final class Routes { // ==[ SELL ]== static const sell = '/sell'; + static const sellScan = '/sell/scan'; // ==[ AUTH ]== static const login = '/login'; diff --git a/lib/ui/add_page/view_model/add_view_model.dart b/lib/ui/add_page/view_model/add_view_model.dart index 9f63196..0e685ba 100644 --- a/lib/ui/add_page/view_model/add_view_model.dart +++ b/lib/ui/add_page/view_model/add_view_model.dart @@ -45,6 +45,9 @@ class AddViewModel extends ChangeNotifier { notifyListeners(); } + Owner? _sectionOwner; + Owner? get sectionOwner => _sectionOwner; + List _owners = []; List? get owners => _owners; Future> addOwner( @@ -176,9 +179,17 @@ class AddViewModel extends ChangeNotifier { ), ); case Error(): - break; + return result; } - return result; + final result2 = await _ownerRepository.sectionOwner; + switch (result2) { + case Ok(): + _sectionOwner = result2.value; + break; + default: + } + + return result2; } } diff --git a/lib/ui/add_page/widgets/confirmation_popup.dart b/lib/ui/add_page/widgets/confirmation_popup.dart index 61aefcf..f026ad6 100644 --- a/lib/ui/add_page/widgets/confirmation_popup.dart +++ b/lib/ui/add_page/widgets/confirmation_popup.dart @@ -129,32 +129,37 @@ class _ConfirmationPopupState extends State { case Ok(): if (context.mounted) { Navigator.of(context).pop(); - } - showDialog( - context: context, - builder: (context) => AlertDialog( - title: Text( - "ID : ${widget.viewModel.currentOwner!.firstName[0]}${widget.viewModel.currentOwner!.lastName[0]}${(price == 0) ? "PL" : price.toString()}", - ), - content: Text( - "Identifiant propriétaire de ce livre. Pensez à l'écrire pour retrouver lae propriétaire du livre lors de la vente ou du retour !", - ), - actions: [ - TextButton( - onPressed: () { - widget.exitPopup(context); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text("Enregistré: ${widget.book.title}"), - behavior: SnackBarBehavior.floating, - ), - ); - }, - child: Text("Ok"), + showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text( + "ID : ${widget.viewModel.currentOwner!.firstName[0].toUpperCase()}${widget.viewModel.currentOwner!.lastName[0].toUpperCase()}${(price == 0) ? "PL" : price.toString()}", ), - ], - ), - ); + content: Text( + (widget.viewModel.currentOwner!.id == + widget.viewModel.sectionOwner!.id) + ? "Ce livre appartient à la section. Vous pouvez mettre le code, ou poser une gomette, ..." + : "Identifiant propriétaire de ce livre. Pensez à l'écrire pour retrouver lae propriétaire du livre lors de la vente ou du retour !", + ), + actions: [ + TextButton( + onPressed: () { + widget.exitPopup(context); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + "Enregistré: ${widget.book.title}", + ), + behavior: SnackBarBehavior.floating, + ), + ); + }, + child: Text("Ok"), + ), + ], + ), + ); + } break; case Error(): if (context.mounted) { diff --git a/lib/ui/sell_page/widgets/manual_scan_popup.dart b/lib/ui/sell_page/widgets/manual_scan_popup.dart index fbbc3b7..37d9ea7 100644 --- a/lib/ui/sell_page/widgets/manual_scan_popup.dart +++ b/lib/ui/sell_page/widgets/manual_scan_popup.dart @@ -1,10 +1,16 @@ import 'package:flutter/material.dart'; +import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:seshat/ui/sell_page/view_model/sell_view_model.dart'; class ManualScanPopup extends StatelessWidget { - ManualScanPopup({required this.viewModel}); + const ManualScanPopup({ + super.key, + required this.viewModel, + required this.controller, + }); final SellViewModel viewModel; + final MobileScannerController controller; @override Widget build(BuildContext context) { @@ -29,6 +35,7 @@ class ManualScanPopup extends StatelessWidget { actions: [ TextButton( onPressed: () { + controller.start(); Navigator.of(context).pop(); }, child: Text("Annuler"), diff --git a/lib/ui/sell_page/widgets/scan_screen.dart b/lib/ui/sell_page/widgets/scan_screen.dart index c0ec4f4..93bbc49 100644 --- a/lib/ui/sell_page/widgets/scan_screen.dart +++ b/lib/ui/sell_page/widgets/scan_screen.dart @@ -61,11 +61,14 @@ class _ScanScreenState extends State { backgroundColor: WidgetStatePropertyAll(theme.cardColor), ), onPressed: () { + controller.stop(); showDialog( context: context, barrierDismissible: false, - builder: (context) => - ManualScanPopup(viewModel: widget.viewModel), + builder: (context) => ManualScanPopup( + viewModel: widget.viewModel, + controller: controller, + ), ); }, child: Text("Vendre un livre sans scanner"), diff --git a/lib/ui/sell_page/widgets/sell_page.dart b/lib/ui/sell_page/widgets/sell_page.dart index b27a8ee..fdc5c07 100644 --- a/lib/ui/sell_page/widgets/sell_page.dart +++ b/lib/ui/sell_page/widgets/sell_page.dart @@ -154,7 +154,6 @@ class _SellPageState extends State { ], ), ), - (widget.viewModel.showScan) ? ScanScreen(viewModel: widget.viewModel) : SizedBox(),