fix: continuing error managment and documentation
This commit is contained in:
parent
59e1c2558c
commit
dad000a1b9
24 changed files with 389 additions and 182 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Reference in a new issue