fix: continuing error managment and documentation
This commit is contained in:
parent
59e1c2558c
commit
dad000a1b9
24 changed files with 389 additions and 182 deletions
|
|
@ -38,18 +38,29 @@ class AddViewModel extends ChangeNotifier {
|
|||
* ====================
|
||||
*/
|
||||
|
||||
/// Owner currently selected in the ui
|
||||
Owner? _currentOwner;
|
||||
|
||||
/// Owner currently selected in the ui
|
||||
Owner? get currentOwner => _currentOwner;
|
||||
set currentOwner(Owner? owner) {
|
||||
_currentOwner = owner;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Owner? _sectionOwner;
|
||||
Owner? get sectionOwner => _sectionOwner;
|
||||
/// Owner of the current user
|
||||
Owner? _ownerOfUser;
|
||||
|
||||
/// Owner of the current user
|
||||
Owner? get ownerOfUser => _ownerOfUser;
|
||||
|
||||
/// All the [Owner]
|
||||
List<Owner> _owners = [];
|
||||
|
||||
/// All the [Owner]
|
||||
List<Owner>? get owners => _owners;
|
||||
|
||||
/// Adds an owner from it's [firstName], [lastName] and [contact]
|
||||
Future<Result<Owner>> addOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
|
|
@ -85,8 +96,11 @@ class AddViewModel extends ChangeNotifier {
|
|||
* =================
|
||||
*/
|
||||
|
||||
Bal? _currentBal;
|
||||
Bal? get currentBal => _currentBal;
|
||||
/// Ongoing [Bal]
|
||||
Bal? _ongoingBal;
|
||||
|
||||
/// Ongoing [Bal]
|
||||
Bal? get ongoingBal => _ongoingBal;
|
||||
|
||||
/*
|
||||
* ===================
|
||||
|
|
@ -94,7 +108,10 @@ class AddViewModel extends ChangeNotifier {
|
|||
* ===================
|
||||
*/
|
||||
|
||||
/// Wether to ask for a price
|
||||
bool _askPrice = true;
|
||||
|
||||
/// Wether to ask for a price
|
||||
bool get askPrice => _askPrice;
|
||||
set askPrice(bool newValue) {
|
||||
_askPrice = newValue;
|
||||
|
|
@ -107,21 +124,26 @@ class AddViewModel extends ChangeNotifier {
|
|||
* =================================
|
||||
*/
|
||||
|
||||
/// Sends an api request with a [bacorde], then gets the [Book] that was
|
||||
/// either created or retrieved. Sens the [Book] back wrapped in a [Result].
|
||||
/// Retrieves the book associated with an ean through a [barcode]
|
||||
Future<Result<Book>> scanBook(BarcodeCapture barcode) async {
|
||||
var ean = barcode.barcodes.first.rawValue!;
|
||||
var result = await _bookRepository.getBookByEAN(ean);
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Result<BookInstance>> sendBook(
|
||||
/// Creates a new Book Instance from its [book], [owner], [bal] and [price]
|
||||
Future<Result<BookInstance>> sendNewBookInstance(
|
||||
Book book,
|
||||
Owner owner,
|
||||
Bal bal,
|
||||
double price,
|
||||
) async {
|
||||
return await _bookInstanceRepository.sendBook(book, owner, bal, price);
|
||||
return await _bookInstanceRepository.sendNewBookInstance(
|
||||
book,
|
||||
owner,
|
||||
bal,
|
||||
price,
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -130,9 +152,11 @@ class AddViewModel extends ChangeNotifier {
|
|||
* =================================
|
||||
*/
|
||||
|
||||
/// Command to load the view model
|
||||
late final Command0 load;
|
||||
bool isLoaded = false;
|
||||
|
||||
/// Manages the loaders
|
||||
Future<Result<void>> _load() async {
|
||||
final result1 = await _loadOwners();
|
||||
switch (result1) {
|
||||
|
|
@ -153,11 +177,12 @@ class AddViewModel extends ChangeNotifier {
|
|||
return result2;
|
||||
}
|
||||
|
||||
/// Loads all necessary data about [Bal]s
|
||||
Future<Result<void>> _loadBal() async {
|
||||
final result = await _balRepository.getBals();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_currentBal = result.value
|
||||
_ongoingBal = result.value
|
||||
.where((bal) => bal.state == BalState.ongoing)
|
||||
.firstOrNull;
|
||||
break;
|
||||
|
|
@ -168,6 +193,7 @@ class AddViewModel extends ChangeNotifier {
|
|||
return result;
|
||||
}
|
||||
|
||||
/// Loads all the necessary data about [Owner]s
|
||||
Future<Result<void>> _loadOwners() async {
|
||||
final result = await _ownerRepository.getOwners();
|
||||
switch (result) {
|
||||
|
|
@ -182,10 +208,10 @@ class AddViewModel extends ChangeNotifier {
|
|||
return result;
|
||||
}
|
||||
|
||||
final result2 = await _ownerRepository.sectionOwner;
|
||||
final result2 = await _ownerRepository.ownerOfUser;
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
_sectionOwner = result2.value;
|
||||
_ownerOfUser = result2.value;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class _AddPageState extends State<AddPage> {
|
|||
listenable: widget.viewModel,
|
||||
builder: (context, child) => switch (widget.viewModel.isLoaded) {
|
||||
false => AwaitLoading(),
|
||||
true => switch (widget.viewModel.currentBal) {
|
||||
true => switch (widget.viewModel.ongoingBal) {
|
||||
null => Center(
|
||||
child: SizedBox(
|
||||
width: 300,
|
||||
|
|
|
|||
|
|
@ -123,10 +123,10 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
_formKey.currentState!.save();
|
||||
}
|
||||
|
||||
var result = await widget.viewModel.sendBook(
|
||||
var result = await widget.viewModel.sendNewBookInstance(
|
||||
widget.book,
|
||||
widget.viewModel.currentOwner!,
|
||||
widget.viewModel.currentBal!,
|
||||
widget.viewModel.ongoingBal!,
|
||||
price,
|
||||
);
|
||||
|
||||
|
|
@ -142,7 +142,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
|||
),
|
||||
content: Text(
|
||||
(widget.viewModel.currentOwner!.id ==
|
||||
widget.viewModel.sectionOwner!.id)
|
||||
widget.viewModel.ownerOfUser!.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 !",
|
||||
),
|
||||
|
|
|
|||
|
|
@ -13,8 +13,10 @@ class LoginViewModel extends ChangeNotifier {
|
|||
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
/// Command to login with added capabilities
|
||||
late Command1 login;
|
||||
|
||||
/// Logins the user with credentials [(String username, String password)]
|
||||
Future<Result<void>> _login((String, String) credentials) async {
|
||||
final (username, password) = credentials;
|
||||
final result = await _authRepository.login(username, password);
|
||||
|
|
@ -27,10 +29,12 @@ class LoginViewModel extends ChangeNotifier {
|
|||
* =================================
|
||||
*/
|
||||
|
||||
/// Loads all necessary data
|
||||
late final Command0 load;
|
||||
bool isLoaded = false;
|
||||
bool isUpToDate = false;
|
||||
|
||||
/// Manages loaders
|
||||
Future<Result<void>> _load() async {
|
||||
final result1 = await _loadApiVersion();
|
||||
switch (result1) {
|
||||
|
|
@ -44,6 +48,7 @@ class LoginViewModel extends ChangeNotifier {
|
|||
return result1;
|
||||
}
|
||||
|
||||
/// Loads the current remote api version and compares to local hardcoded [apiVersion]
|
||||
Future<Result<void>> _loadApiVersion() async {
|
||||
final result = await _authRepository.getRemoteApiVersion();
|
||||
switch (result) {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import 'package:seshat/utils/result.dart';
|
|||
class BalViewModel extends ChangeNotifier {
|
||||
BalViewModel({
|
||||
required BalRepository balRepository,
|
||||
required this.id,
|
||||
required this.selectedBalId,
|
||||
required OwnerRepository ownerRepository,
|
||||
}) : _balRepository = balRepository,
|
||||
_ownerRepository = ownerRepository {
|
||||
|
|
@ -30,18 +30,26 @@ class BalViewModel extends ChangeNotifier {
|
|||
* =====================
|
||||
*/
|
||||
|
||||
Bal? _bal;
|
||||
int id;
|
||||
Bal? get bal => _bal;
|
||||
/// Selected [Bal]
|
||||
Bal? _selectedBal;
|
||||
|
||||
/// Selected [Bal]
|
||||
Bal? get selectedBal => _selectedBal;
|
||||
|
||||
/// Selected [Bal.id] from path parameters
|
||||
int selectedBalId;
|
||||
|
||||
/// Is one of the [Bal] [BalState.ongoing]
|
||||
bool isABalOngoing = false;
|
||||
|
||||
Future<Result<void>> stopBal(int id) async {
|
||||
/// Stops a [Bal]
|
||||
Future<Result<void>> stopBal(int balId) async {
|
||||
isLoaded = false;
|
||||
notifyListeners();
|
||||
final result = await _balRepository.stopBal(id);
|
||||
final result = await _balRepository.stopBal(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_bal = result.value;
|
||||
_selectedBal = result.value;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
|
@ -57,14 +65,15 @@ class BalViewModel extends ChangeNotifier {
|
|||
return result;
|
||||
}
|
||||
|
||||
Future<Result<void>> startBal(int id) async {
|
||||
/// Starts a [Bal]
|
||||
Future<Result<void>> startBal(int balId) async {
|
||||
if (isABalOngoing) {
|
||||
return Result.error(Exception("Cannot have multiple BALs ongoing !"));
|
||||
}
|
||||
final result = await _balRepository.startBal(id);
|
||||
final result = await _balRepository.startBal(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_bal = result.value;
|
||||
_selectedBal = result.value;
|
||||
notifyListeners();
|
||||
break;
|
||||
default:
|
||||
|
|
@ -72,21 +81,20 @@ class BalViewModel extends ChangeNotifier {
|
|||
return result;
|
||||
}
|
||||
|
||||
/// Edits a [Bal]'s [name], [startTime] or [endTime]
|
||||
Future<Result<void>> editBal(
|
||||
int id,
|
||||
String name,
|
||||
DateTime start,
|
||||
DateTime end,
|
||||
DateTime startTime,
|
||||
DateTime endTime,
|
||||
) async {
|
||||
final result = await _balRepository.editBal(id, name, start, end);
|
||||
final result = await _balRepository.editBal(id, name, startTime, endTime);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
debugPrint("\n\n\n\nDID EDIT\n\n\n\n");
|
||||
_bal = result.value;
|
||||
_selectedBal = result.value;
|
||||
notifyListeners();
|
||||
break;
|
||||
case Error():
|
||||
debugPrint("\n\n\n\nERROR: ${result.error}");
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
|
|
@ -99,11 +107,16 @@ class BalViewModel extends ChangeNotifier {
|
|||
*/
|
||||
|
||||
// Specific to ended state
|
||||
|
||||
/// Owners a book or money is owed to
|
||||
List<ReturnOwner>? owedToOwners;
|
||||
double? totalOwed;
|
||||
|
||||
/// Statistics about the [_selectedBal]
|
||||
BalStats? stats;
|
||||
|
||||
Future<void> applyAccountingOwners(
|
||||
/// Froms [books], updates [owners] to include all the necessary information
|
||||
/// See [the api doc](https://bal.ueauvergne.fr/docs/#/bal-api/get_bal_accounting) for more details
|
||||
Future<void> _updateOwedToOwnersWithBooks(
|
||||
List<ReturnOwner> owners,
|
||||
Map<String, Book> books,
|
||||
) async {
|
||||
|
|
@ -124,15 +137,20 @@ class BalViewModel extends ChangeNotifier {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns either Books, Money or All ([ReturnType]) to an [Owner]
|
||||
Future<Result<void>> returnById(ReturnType type, int ownerId) async {
|
||||
final result = await _balRepository.returnToId(id, ownerId, type);
|
||||
final result2 = await _balRepository.getAccounting(id);
|
||||
final result = await _balRepository.returnToId(
|
||||
selectedBalId,
|
||||
ownerId,
|
||||
type,
|
||||
);
|
||||
final result2 = await _balRepository.getAccounting(selectedBalId);
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
applyAccountingOwners(result2.value.owners, result2.value.books);
|
||||
_updateOwedToOwnersWithBooks(result2.value.owners, result2.value.books);
|
||||
break;
|
||||
case Error():
|
||||
debugPrint(result2.error.toString());
|
||||
break;
|
||||
}
|
||||
notifyListeners();
|
||||
return result;
|
||||
|
|
@ -144,22 +162,25 @@ class BalViewModel extends ChangeNotifier {
|
|||
* =================================
|
||||
*/
|
||||
|
||||
/// Loads all the necessary information
|
||||
late final Command0 load;
|
||||
bool isLoaded = false;
|
||||
|
||||
/// Manages loaders
|
||||
Future<Result<void>> _load() async {
|
||||
isABalOngoing = _balRepository.isABalOngoing();
|
||||
final result1 = await _loadBal();
|
||||
switch (result1) {
|
||||
case Ok():
|
||||
isLoaded = (_bal == null || _bal?.state != BalState.ended)
|
||||
isLoaded =
|
||||
(_selectedBal == null || _selectedBal?.state != BalState.ended)
|
||||
? true
|
||||
: false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (_bal?.state == BalState.ended) {
|
||||
if (_selectedBal?.state == BalState.ended) {
|
||||
final result2 = await _loadEnded();
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
|
|
@ -173,11 +194,12 @@ class BalViewModel extends ChangeNotifier {
|
|||
return result1;
|
||||
}
|
||||
|
||||
/// Loads all common [Bal] information
|
||||
Future<Result<void>> _loadBal() async {
|
||||
final result = await _balRepository.balById(id);
|
||||
final result = await _balRepository.balById(selectedBalId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_bal = result.value;
|
||||
_selectedBal = result.value;
|
||||
break;
|
||||
case Error():
|
||||
break;
|
||||
|
|
@ -186,15 +208,16 @@ class BalViewModel extends ChangeNotifier {
|
|||
return result;
|
||||
}
|
||||
|
||||
/// Loads [Bal] information when it is [BalState.ended]
|
||||
Future<Result<void>> _loadEnded() async {
|
||||
final result = await _balRepository.getAccountingNoCache(id);
|
||||
final result = await _balRepository.getAccountingNoCache(selectedBalId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
applyAccountingOwners(result.value.owners, result.value.books);
|
||||
_updateOwedToOwnersWithBooks(result.value.owners, result.value.books);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
final result2 = await _balRepository.getBalStats(id);
|
||||
final result2 = await _balRepository.getBalStats(selectedBalId);
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
stats = result2.value;
|
||||
|
|
|
|||
|
|
@ -27,14 +27,14 @@ class _BalPageState extends State<BalPage> {
|
|||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||
body: AwaitLoading(),
|
||||
),
|
||||
true => switch (widget.viewModel.bal == null) {
|
||||
true => switch (widget.viewModel.selectedBal == null) {
|
||||
true => Scaffold(
|
||||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||
body: Center(
|
||||
child: Text("La BAL référencée n'est pas accessible"),
|
||||
),
|
||||
),
|
||||
false => switch (widget.viewModel.bal!.state) {
|
||||
false => switch (widget.viewModel.selectedBal!.state) {
|
||||
BalState.pending => BalPendingScreen(viewModel: widget.viewModel),
|
||||
BalState.ongoing => BalOngoingScreen(viewModel: widget.viewModel),
|
||||
BalState.ended => BalEndedScreen(viewModel: widget.viewModel),
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ class _BalEndedScreenState extends State<BalEndedScreen>
|
|||
return Scaffold(
|
||||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||
appBar: AppBar(
|
||||
title: Text(widget.viewModel.bal!.name),
|
||||
title: Text(widget.viewModel.selectedBal!.name),
|
||||
bottom: TabBar(
|
||||
controller: tabController,
|
||||
tabs: [
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ class BalOngoingScreen extends StatelessWidget {
|
|||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||
appBar: AppBar(title: Text(viewModel.bal!.name)),
|
||||
appBar: AppBar(title: Text(viewModel.selectedBal!.name)),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
child: Column(
|
||||
|
|
@ -38,7 +38,9 @@ class BalOngoingScreen extends StatelessWidget {
|
|||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await viewModel.stopBal(viewModel.bal!.id);
|
||||
await viewModel.stopBal(
|
||||
viewModel.selectedBal!.id,
|
||||
);
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ class BalPendingScreen extends StatelessWidget {
|
|||
return Scaffold(
|
||||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||
appBar: AppBar(
|
||||
title: Text(viewModel.bal!.name),
|
||||
title: Text(viewModel.selectedBal!.name),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
|
|
@ -57,7 +57,9 @@ class BalPendingScreen extends StatelessWidget {
|
|||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await viewModel.startBal(viewModel.bal!.id);
|
||||
await viewModel.startBal(
|
||||
viewModel.selectedBal!.id,
|
||||
);
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
|
@ -96,8 +98,8 @@ class _EditPopup extends State<EditPopup> {
|
|||
firstDate: DateTime(DateTime.now().year - 1),
|
||||
lastDate: DateTime(DateTime.now().year + 2),
|
||||
initialDateRange: DateTimeRange(
|
||||
start: start ?? widget.viewModel.bal!.startTime,
|
||||
end: end ?? widget.viewModel.bal!.endTime,
|
||||
start: start ?? widget.viewModel.selectedBal!.startTime,
|
||||
end: end ?? widget.viewModel.selectedBal!.endTime,
|
||||
),
|
||||
);
|
||||
|
||||
|
|
@ -126,7 +128,7 @@ class _EditPopup extends State<EditPopup> {
|
|||
labelText: "Nom de la BAL",
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
initialValue: widget.viewModel.bal!.name,
|
||||
initialValue: widget.viewModel.selectedBal!.name,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return "Veuillez entrer un nom";
|
||||
|
|
@ -169,7 +171,7 @@ class _EditPopup extends State<EditPopup> {
|
|||
if (_formKey.currentState!.validate()) {
|
||||
_formKey.currentState!.save();
|
||||
|
||||
final Bal bal = widget.viewModel.bal!;
|
||||
final Bal bal = widget.viewModel.selectedBal!;
|
||||
|
||||
final result = await widget.viewModel.editBal(
|
||||
bal.id,
|
||||
|
|
|
|||
|
|
@ -18,18 +18,25 @@ class HomeViewModel extends ChangeNotifier {
|
|||
* =================
|
||||
*/
|
||||
|
||||
/// [List<Bal>] of all [Bal]
|
||||
List<Bal> _bals = [];
|
||||
|
||||
/// [List<Bal>] of all [Bal]
|
||||
List<Bal> get bals => _bals;
|
||||
|
||||
Bal? _currentBal;
|
||||
Bal? get currentBal => _currentBal;
|
||||
/// [Bal] currently [BalState.ongoing]
|
||||
Bal? _ongoingBal;
|
||||
|
||||
/// [Bal] currently [BalState.ongoing]
|
||||
Bal? get ongoingBal => _ongoingBal;
|
||||
|
||||
/// Creates a [Bal] from its [name], [startTime] and [endTime]
|
||||
Future<Result<void>> createBal(
|
||||
String name,
|
||||
DateTime start,
|
||||
DateTime end,
|
||||
DateTime startTime,
|
||||
DateTime endTime,
|
||||
) async {
|
||||
final result = await _balRepository.addBal(name, start, end);
|
||||
final result = await _balRepository.addBal(name, startTime, endTime);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
final result2 = await _balRepository.getBals();
|
||||
|
|
@ -54,9 +61,11 @@ class HomeViewModel extends ChangeNotifier {
|
|||
* =================================
|
||||
*/
|
||||
|
||||
/// Command to load all necessary data
|
||||
late final Command0 load;
|
||||
bool isLoaded = false;
|
||||
|
||||
/// Manages loaders
|
||||
Future<Result<void>> _load() async {
|
||||
final result2 = await _loadBal();
|
||||
switch (result2) {
|
||||
|
|
@ -70,12 +79,13 @@ class HomeViewModel extends ChangeNotifier {
|
|||
return result2;
|
||||
}
|
||||
|
||||
/// Loads data about [Bal]
|
||||
Future<Result<void>> _loadBal() async {
|
||||
final result = await _balRepository.getBals();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_bals = result.value..sort((a, b) => a.compareTo(b));
|
||||
_currentBal = _bals
|
||||
_ongoingBal = _bals
|
||||
.where((bal) => bal.state == BalState.ongoing)
|
||||
.firstOrNull;
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class _HomePageState extends State<HomePage> {
|
|||
: ListView(
|
||||
children: [
|
||||
for (Bal bal in widget.viewModel.bals.where(
|
||||
(el) => el.id != widget.viewModel.currentBal?.id,
|
||||
(el) => el.id != widget.viewModel.ongoingBal?.id,
|
||||
))
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
|
|
@ -81,20 +81,20 @@ class _HomePageState extends State<HomePage> {
|
|||
],
|
||||
),
|
||||
),
|
||||
switch (widget.viewModel.currentBal == null) {
|
||||
switch (widget.viewModel.ongoingBal == null) {
|
||||
true => SizedBox(),
|
||||
false => Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10.0),
|
||||
child: Card(
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.event_available),
|
||||
title: Text(widget.viewModel.currentBal!.name),
|
||||
title: Text(widget.viewModel.ongoingBal!.name),
|
||||
subtitle: Text("BAL en cours"),
|
||||
trailing: IconButton(
|
||||
onPressed: () {
|
||||
_moveToBal(
|
||||
context,
|
||||
widget.viewModel.currentBal!.id,
|
||||
widget.viewModel.ongoingBal!.id,
|
||||
);
|
||||
},
|
||||
icon: Icon(Icons.arrow_forward),
|
||||
|
|
|
|||
|
|
@ -31,10 +31,13 @@ class SellViewModel extends ChangeNotifier {
|
|||
final BookRepository _bookRepository;
|
||||
final OwnerRepository _ownerRepository;
|
||||
|
||||
bool _showScan = false;
|
||||
bool get showScan => _showScan;
|
||||
set showScan(bool newValue) {
|
||||
_showScan = newValue;
|
||||
/// Wether to show the scan screen
|
||||
bool _showScanScreen = false;
|
||||
|
||||
/// Wether to show the scan screen
|
||||
bool get showScanScreen => _showScanScreen;
|
||||
set showScanScreen(bool newValue) {
|
||||
_showScanScreen = newValue;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
|
@ -44,56 +47,66 @@ class SellViewModel extends ChangeNotifier {
|
|||
* ===============================
|
||||
*/
|
||||
|
||||
final List<BookStack> _soldBooks = [];
|
||||
List<BookStack> get soldBooks => _soldBooks;
|
||||
/// Books in the sell
|
||||
final List<BookStack> _booksInSell = [];
|
||||
|
||||
/// Books in the sell
|
||||
List<BookStack> get booksInSell => _booksInSell;
|
||||
|
||||
/// Books scanned on the scan screen
|
||||
final List<BookStack> _scannedBooks = [];
|
||||
|
||||
/// Books scanned on the scan screen
|
||||
List<BookStack> get scannedBooks => _scannedBooks;
|
||||
|
||||
bool isScanLoaded = false;
|
||||
bool isSendingSell = false;
|
||||
double minimumAmount = 0;
|
||||
double minimumAmountToPay = 0;
|
||||
|
||||
void sellBook(BookStack addedBook) {
|
||||
minimumAmount += addedBook.instance.price;
|
||||
_soldBooks.add(addedBook);
|
||||
/// Adds a book to the [_booksInSell]
|
||||
void addBookToSell(BookStack bookToAdd) {
|
||||
minimumAmountToPay += bookToAdd.instance.price;
|
||||
_booksInSell.add(bookToAdd);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void sendSell(double givenAmount) async {
|
||||
/// Sends the sell
|
||||
void sendSell(double givenMoney) async {
|
||||
isSendingSell = true;
|
||||
notifyListeners();
|
||||
List<BookInstance> toSend = [];
|
||||
int nbOfPl = 0;
|
||||
for (BookStack book in _soldBooks) {
|
||||
List<BookInstance> booksToSend = [];
|
||||
int numberOfPL = 0;
|
||||
for (BookStack book in _booksInSell) {
|
||||
if (book.instance.price != 0) {
|
||||
book.instance.soldPrice = book.instance.price;
|
||||
givenAmount -= book.instance.price;
|
||||
toSend.add(book.instance);
|
||||
givenMoney -= book.instance.price;
|
||||
booksToSend.add(book.instance);
|
||||
} else {
|
||||
nbOfPl++;
|
||||
numberOfPL++;
|
||||
}
|
||||
}
|
||||
if (nbOfPl != 0) {
|
||||
double amountPerPl = givenAmount / nbOfPl;
|
||||
for (BookStack book in _soldBooks) {
|
||||
if (numberOfPL != 0) {
|
||||
double moneyPerPL = givenMoney / numberOfPL;
|
||||
for (BookStack book in _booksInSell) {
|
||||
if (book.instance.price == 0) {
|
||||
book.instance.soldPrice = amountPerPl;
|
||||
toSend.add(book.instance);
|
||||
book.instance.soldPrice = moneyPerPL;
|
||||
booksToSend.add(book.instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
await _bookInstanceRepository.sellBooks(toSend);
|
||||
_soldBooks.clear();
|
||||
await _bookInstanceRepository.sellBooks(booksToSend);
|
||||
_booksInSell.clear();
|
||||
isSendingSell = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void deleteBook(int id) {
|
||||
_soldBooks.removeWhere((book) => book.instance.id == id);
|
||||
/// Removes a book from the sell
|
||||
void removeBookFromSell(int bookId) {
|
||||
_booksInSell.removeWhere((book) => book.instance.id == bookId);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Search a book by [title] or [author]
|
||||
Future<void> searchBook(String title, String author) async {
|
||||
Bal? bal = await _balRepository.ongoingBal();
|
||||
isScanLoaded = false;
|
||||
|
|
@ -106,15 +119,21 @@ class SellViewModel extends ChangeNotifier {
|
|||
);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
// For each result value, you need to complete some values
|
||||
for (SearchResult searchResult in result.value) {
|
||||
// In case you get a book that's actually not available
|
||||
if (searchResult.instance.available == false) {
|
||||
continue;
|
||||
}
|
||||
if (_soldBooks
|
||||
|
||||
// In case the instance is already in the sell
|
||||
if (_booksInSell
|
||||
.where((book) => book.instance.id == searchResult.instance.id)
|
||||
.isNotEmpty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Search for the owner
|
||||
Owner owner;
|
||||
final result2 = await _ownerRepository.getOwnerById(
|
||||
searchResult.instance.ownerId,
|
||||
|
|
@ -126,6 +145,7 @@ class SellViewModel extends ChangeNotifier {
|
|||
case Error():
|
||||
continue;
|
||||
}
|
||||
|
||||
_scannedBooks.add(
|
||||
BookStack(searchResult.book, searchResult.instance, owner),
|
||||
);
|
||||
|
|
@ -140,18 +160,19 @@ class SellViewModel extends ChangeNotifier {
|
|||
return;
|
||||
}
|
||||
|
||||
/// Gets [BookInstance]s from its ean in a [barcode]
|
||||
Future<void> scanBook(BarcodeCapture barcode) async {
|
||||
isScanLoaded = false;
|
||||
int ean = int.parse(barcode.barcodes.first.rawValue!);
|
||||
Bal? bal = await _balRepository.ongoingBal();
|
||||
Bal? ongoingBal = await _balRepository.ongoingBal();
|
||||
_scannedBooks.clear();
|
||||
|
||||
final result = await _bookInstanceRepository.getByEan(bal!.id, ean);
|
||||
switch (result) {
|
||||
final result1 = await _bookInstanceRepository.getByEan(ongoingBal!.id, ean);
|
||||
switch (result1) {
|
||||
case Ok():
|
||||
Book book;
|
||||
final result2 = await _bookRepository.getBookById(
|
||||
result.value.first.bookId,
|
||||
result1.value.first.bookId,
|
||||
);
|
||||
switch (result2) {
|
||||
case Ok():
|
||||
|
|
@ -160,15 +181,22 @@ class SellViewModel extends ChangeNotifier {
|
|||
case Error():
|
||||
return;
|
||||
}
|
||||
for (BookInstance instance in result.value) {
|
||||
|
||||
// For each result value, you need to complete some values
|
||||
for (BookInstance instance in result1.value) {
|
||||
// In case you get a book that's actually not available
|
||||
if (instance.available == false) {
|
||||
continue;
|
||||
}
|
||||
if (_soldBooks
|
||||
|
||||
// In case the instance is already in the sell
|
||||
if (_booksInSell
|
||||
.where((book) => book.instance.id == instance.id)
|
||||
.isNotEmpty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Search for the owner
|
||||
Owner owner;
|
||||
final result3 = await _ownerRepository.getOwnerById(instance.ownerId);
|
||||
switch (result3) {
|
||||
|
|
@ -178,6 +206,7 @@ class SellViewModel extends ChangeNotifier {
|
|||
case Error():
|
||||
continue;
|
||||
}
|
||||
|
||||
_scannedBooks.add(BookStack(book, instance, owner));
|
||||
}
|
||||
break;
|
||||
|
|
@ -196,8 +225,11 @@ class SellViewModel extends ChangeNotifier {
|
|||
* =================
|
||||
*/
|
||||
|
||||
Bal? _currentBal;
|
||||
get currentBal => _currentBal;
|
||||
/// The currently ongoing [Bal]
|
||||
Bal? _ongoingBal;
|
||||
|
||||
/// The currently ongoing [Bal]
|
||||
get ongoingBal => _ongoingBal;
|
||||
|
||||
/*
|
||||
* =================================
|
||||
|
|
@ -205,9 +237,11 @@ class SellViewModel extends ChangeNotifier {
|
|||
* =================================
|
||||
*/
|
||||
|
||||
/// Command to load necessary data
|
||||
late final Command0 load;
|
||||
bool isLoaded = false;
|
||||
|
||||
/// Manages loaders
|
||||
Future<Result<void>> _load() async {
|
||||
final result1 = await _loadBal();
|
||||
switch (result1) {
|
||||
|
|
@ -221,11 +255,12 @@ class SellViewModel extends ChangeNotifier {
|
|||
return result1;
|
||||
}
|
||||
|
||||
/// Loads information about [Bal]
|
||||
Future<Result<void>> _loadBal() async {
|
||||
final result = await _balRepository.getBals();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
_currentBal = result.value
|
||||
_ongoingBal = result.value
|
||||
.where((bal) => bal.state == BalState.ongoing)
|
||||
.firstOrNull;
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ class _ScanScreenState extends State<ScanScreen> {
|
|||
children: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
widget.viewModel.showScan = false;
|
||||
widget.viewModel.showScanScreen = false;
|
||||
},
|
||||
icon: Icon(Icons.arrow_back),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ class SellChoicePopup extends StatelessWidget {
|
|||
child: Card(
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
viewModel.sellBook(book);
|
||||
viewModel.addBookToSell(book);
|
||||
Navigator.of(context).pop();
|
||||
viewModel.showScan = false;
|
||||
viewModel.showScanScreen = false;
|
||||
},
|
||||
child: ListTile(
|
||||
leading: Text(
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class _SellPageState extends State<SellPage> {
|
|||
builder: (context, child) {
|
||||
return switch (widget.viewModel.isLoaded) {
|
||||
false => AwaitLoading(),
|
||||
true => switch (widget.viewModel.currentBal) {
|
||||
true => switch (widget.viewModel.ongoingBal) {
|
||||
null => Center(
|
||||
child: SizedBox(
|
||||
width: 300,
|
||||
|
|
@ -80,7 +80,7 @@ class _SellPageState extends State<SellPage> {
|
|||
? Center(child: Text("Aucun"))
|
||||
: SizedBox(),
|
||||
for (BookStack book
|
||||
in widget.viewModel.soldBooks)
|
||||
in widget.viewModel.booksInSell)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 15,
|
||||
|
|
@ -99,9 +99,10 @@ class _SellPageState extends State<SellPage> {
|
|||
),
|
||||
trailing: IconButton(
|
||||
onPressed: () {
|
||||
widget.viewModel.deleteBook(
|
||||
book.instance.id,
|
||||
);
|
||||
widget.viewModel
|
||||
.removeBookFromSell(
|
||||
book.instance.id,
|
||||
);
|
||||
},
|
||||
icon: Icon(Icons.delete),
|
||||
),
|
||||
|
|
@ -113,7 +114,7 @@ class _SellPageState extends State<SellPage> {
|
|||
),
|
||||
SizedBox(height: 40),
|
||||
Text(
|
||||
"Montant minimum à payer : ${widget.viewModel.minimumAmount.toString()}€",
|
||||
"Montant minimum à payer : ${widget.viewModel.minimumAmountToPay.toString()}€",
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 60.0),
|
||||
|
|
@ -167,7 +168,7 @@ class _SellPageState extends State<SellPage> {
|
|||
} else if (double.parse(
|
||||
price.text.replaceFirst(",", "."),
|
||||
) <
|
||||
widget.viewModel.minimumAmount) {
|
||||
widget.viewModel.minimumAmountToPay) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
|
|
@ -202,7 +203,7 @@ class _SellPageState extends State<SellPage> {
|
|||
SizedBox(width: 70),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
widget.viewModel.showScan = true;
|
||||
widget.viewModel.showScanScreen = true;
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
style: ButtonStyle(
|
||||
|
|
@ -216,7 +217,7 @@ class _SellPageState extends State<SellPage> {
|
|||
],
|
||||
),
|
||||
),
|
||||
(widget.viewModel.showScan)
|
||||
(widget.viewModel.showScanScreen)
|
||||
? ScanScreen(viewModel: widget.viewModel)
|
||||
: SizedBox(),
|
||||
],
|
||||
|
|
|
|||
Reference in a new issue