Compare commits

..

No commits in common. "main" and "0.2.4" have entirely different histories.
main ... 0.2.4

27 changed files with 422 additions and 753 deletions

View file

@ -1,6 +1,3 @@
> [!WARNING]
> This repo has been moved to [illes](https://git.illes.fr/UEAuvergne/Seshat).
# seshat # seshat
Client android/iOS/web, écrit en dart x flutter, pour Alexandria. Client android/iOS/web, écrit en dart x flutter, pour Alexandria.

View file

@ -2,7 +2,6 @@ import 'package:flutter/foundation.dart';
import 'package:seshat/data/services/auth_client.dart'; import 'package:seshat/data/services/auth_client.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
/// Repository to manage authentification
class AuthRepository extends ChangeNotifier { class AuthRepository extends ChangeNotifier {
AuthRepository({required AuthClient authClient}) : _authClient = authClient; AuthRepository({required AuthClient authClient}) : _authClient = authClient;
@ -10,7 +9,6 @@ class AuthRepository extends ChangeNotifier {
bool? _isAuthenticated; bool? _isAuthenticated;
/// Checks the validity of the token if not already checked
Future<bool> get isLoggedIn async { Future<bool> get isLoggedIn async {
if (_isAuthenticated != null) { if (_isAuthenticated != null) {
return _isAuthenticated!; return _isAuthenticated!;
@ -27,7 +25,6 @@ class AuthRepository extends ChangeNotifier {
} }
} }
/// Logs in the user
Future<Result<void>> login(String username, String password) async { Future<Result<void>> login(String username, String password) async {
try { try {
final result = await _authClient.login(username, password); final result = await _authClient.login(username, password);
@ -36,14 +33,13 @@ class AuthRepository extends ChangeNotifier {
_isAuthenticated = true; _isAuthenticated = true;
return Result.ok(()); return Result.ok(());
case Error(): case Error():
return result; return Result.error(result.error);
} }
} catch (e) { } catch (e) {
return Result.error(Exception(e)); return Result.error(Exception(e));
} }
} }
/// Gets the API's remote version
Future<Result<int>> getRemoteApiVersion() async { Future<Result<int>> getRemoteApiVersion() async {
return await _authClient.getRemoteApiVersion(); return await _authClient.getRemoteApiVersion();
} }

View file

@ -5,19 +5,13 @@ import 'package:seshat/domain/models/bal_stats.dart';
import 'package:seshat/domain/models/enums.dart'; import 'package:seshat/domain/models/enums.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
/// Repository to manage [Bal]
class BalRepository { class BalRepository {
BalRepository({required ApiClient apiClient}) : _apiClient = apiClient; BalRepository({required ApiClient apiClient}) : _apiClient = apiClient;
final ApiClient _apiClient; final ApiClient _apiClient;
/// [List<Bal>] of all the user's [Bal]
List<Bal>? _bals; List<Bal>? _bals;
Accounting? accounting;
/// [Accounting] of [Bal], mapped by [Bal] id
final Map<int, Accounting?> _accountingMap = {};
/// Gets a list of all [Bal] from cache or remote
Future<Result<List<Bal>>> getBals() async { Future<Result<List<Bal>>> getBals() async {
if (_bals != null) { if (_bals != null) {
return Result.ok(_bals!); return Result.ok(_bals!);
@ -32,7 +26,6 @@ class BalRepository {
} }
} }
/// Gets a list of all [Bal] from remote only
Future<Result<List<Bal>>> _getBalsNoCache() async { Future<Result<List<Bal>>> _getBalsNoCache() async {
final result = await _apiClient.getBals(); final result = await _apiClient.getBals();
switch (result) { switch (result) {
@ -44,16 +37,15 @@ class BalRepository {
} }
} }
/// Gets a [Bal] by [balId], either from cache or remote Future<Result<Bal>> balById(int id) async {
Future<Result<Bal>> balById(int balId) async {
if (_bals == null) { if (_bals == null) {
await getBals(); await getBals();
} }
Bal? bal = _bals!.where((bal) => bal.id == balId).firstOrNull; Bal? bal = _bals!.where((bal) => bal.id == id).firstOrNull;
if (bal != null) { if (bal != null) {
return Result.ok(bal); return Result.ok(bal);
} }
final result = await _apiClient.getBalById(balId); final result = await _apiClient.getBalById(id);
switch (result) { switch (result) {
case Ok(): case Ok():
return Result.ok(result.value); return Result.ok(result.value);
@ -62,13 +54,11 @@ class BalRepository {
} }
} }
/// Return wether or not a [Bal] is currently [BalState.ongoing]
bool isABalOngoing() { bool isABalOngoing() {
return _bals?.where((bal) => bal.state == BalState.ongoing).isNotEmpty ?? return _bals?.where((bal) => bal.state == BalState.ongoing).isNotEmpty ??
false; false;
} }
/// Gets the [Bal] that is [BalState.ongoing]
Future<Bal?> ongoingBal() async { Future<Bal?> ongoingBal() async {
if (_bals == null) { if (_bals == null) {
await _getBalsNoCache(); await _getBalsNoCache();
@ -76,14 +66,12 @@ class BalRepository {
return _bals!.where((bal) => bal.state == BalState.ongoing).firstOrNull; return _bals!.where((bal) => bal.state == BalState.ongoing).firstOrNull;
} }
/// Stops a [Bal] and refresh cache
Future<Result<Bal>> stopBal(int id) async { Future<Result<Bal>> stopBal(int id) async {
final result = await _apiClient.stopBal(id); final result = await _apiClient.stopBal(id);
_getBalsNoCache(); _getBalsNoCache();
return result; return result;
} }
/// Starts a [Bal] and refresh cache
Future<Result<Bal>> startBal(int id) async { Future<Result<Bal>> startBal(int id) async {
if (isABalOngoing()) { if (isABalOngoing()) {
return Result.error( return Result.error(
@ -95,62 +83,52 @@ class BalRepository {
return result; return result;
} }
/// Changes a [Bal]'s [name], [startTime] or [endTime]
Future<Result<Bal>> editBal( Future<Result<Bal>> editBal(
int id, int id,
String name, String name,
DateTime startTime, DateTime start,
DateTime endTime, DateTime end,
) async { ) async {
final result = await _apiClient.editBal(id, name, startTime, endTime); final result = await _apiClient.editBal(id, name, start, end);
await _getBalsNoCache(); await _getBalsNoCache();
return result; return result;
} }
/// Creates a [Bal] from its [name], [startTime] and [endTime] Future<Result<void>> addBal(String name, DateTime start, DateTime end) async {
Future<Result<void>> addBal( final result = await _apiClient.addBal(name, start, end);
String name,
DateTime startTime,
DateTime endTime,
) async {
final result = await _apiClient.addBal(name, startTime, endTime);
await _getBalsNoCache(); await _getBalsNoCache();
return result; return result;
} }
/// Gets a [BalStats] from its [balId] Future<Result<BalStats>> getBalStats(int id) async {
Future<Result<BalStats>> getBalStats(int balId) async { return _apiClient.getBalStats(id);
return _apiClient.getBalStats(balId);
} }
/// Get [Accounting] of a [Bal] from remote only
Future<Result<Accounting>> getAccountingNoCache(int balId) async { Future<Result<Accounting>> getAccountingNoCache(int balId) async {
final result = await _apiClient.getAccounting(balId); final result = await _apiClient.getAccounting(balId);
switch (result) { switch (result) {
case Ok(): case Ok():
_accountingMap[balId] = result.value; accounting = result.value;
break; break;
default: default:
} }
return result; return result;
} }
/// Get [Accounting] of a [Bal] from cache or remote
Future<Result<Accounting>> getAccounting(int balId) async { Future<Result<Accounting>> getAccounting(int balId) async {
if (_accountingMap[balId] != null) { if (accounting != null) {
return Result.ok(_accountingMap[balId]!); return Result.ok(accounting!);
} }
final result = await _apiClient.getAccounting(balId); final result = await _apiClient.getAccounting(balId);
switch (result) { switch (result) {
case Ok(): case Ok():
_accountingMap[balId] = result.value; accounting = result.value;
break; break;
default: default:
} }
return result; return result;
} }
/// Manages what returning (of type [ReturnType]) does to cache and notifies remote
Future<Result<void>> returnToId( Future<Result<void>> returnToId(
int balId, int balId,
int ownerId, int ownerId,
@ -161,32 +139,26 @@ class BalRepository {
case Ok(): case Ok():
switch (type) { switch (type) {
case ReturnType.books: case ReturnType.books:
final owner = _accountingMap[balId]?.owners final owner = accounting?.owners
.where((el) => el.ownerId == ownerId) .where((el) => el.ownerId == ownerId)
.firstOrNull; .firstOrNull;
if (owner?.owedMoney == 0) { if (owner?.owedMoney == 0) {
_accountingMap[balId]?.owners.removeWhere( accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
(el) => el.ownerId == ownerId,
);
} }
owner?.owed = []; owner?.owed = [];
owner?.owedInstances = []; owner?.owedInstances = [];
break; break;
case ReturnType.money: case ReturnType.money:
final owner = _accountingMap[balId]?.owners final owner = accounting?.owners
.where((el) => el.ownerId == ownerId) .where((el) => el.ownerId == ownerId)
.firstOrNull; .firstOrNull;
if (owner?.owed == null || owner!.owed.isEmpty) { if (owner?.owed == null || owner!.owed.isEmpty) {
_accountingMap[balId]?.owners.removeWhere( accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
(el) => el.ownerId == ownerId,
);
} }
owner?.owedMoney = 0; owner?.owedMoney = 0;
break; break;
case ReturnType.all: case ReturnType.all:
_accountingMap[balId]?.owners.removeWhere( accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
(el) => el.ownerId == ownerId,
);
break; break;
} }
break; break;

View file

@ -6,19 +6,16 @@ import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/domain/models/search_result.dart'; import 'package:seshat/domain/models/search_result.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
/// Repository to manage [BookInstance]
class BookInstanceRepository { class BookInstanceRepository {
BookInstanceRepository({required ApiClient apiClient}) BookInstanceRepository({required ApiClient apiClient})
: _apiClient = apiClient; : _apiClient = apiClient;
final ApiClient _apiClient; final ApiClient _apiClient;
/// Gets a [List<BookInstance>] from an [ean]
Future<Result<List<BookInstance>>> getByEan(int balId, int ean) async { Future<Result<List<BookInstance>>> getByEan(int balId, int ean) async {
return await _apiClient.getBookInstanceByEAN(balId, ean); return await _apiClient.getBookInstanceByEAN(balId, ean);
} }
/// Gets a [List<BookInstance>] from a [title] and [author]
Future<Result<List<SearchResult>>> getBySearch( Future<Result<List<SearchResult>>> getBySearch(
int balId, int balId,
String title, String title,
@ -27,17 +24,15 @@ class BookInstanceRepository {
return await _apiClient.getBookInstanceBySearch(balId, title, author); return await _apiClient.getBookInstanceBySearch(balId, title, author);
} }
/// Sends a new [BookInstance]'s [book], [owner], [bal] and [price] Future<Result<BookInstance>> sendBook(
Future<Result<BookInstance>> sendNewBookInstance(
Book book, Book book,
Owner owner, Owner owner,
Bal bal, Bal bal,
double price, double price,
) async { ) async {
return await _apiClient.sendNewBookInstance(book, owner, bal, price); return await _apiClient.sendBook(book, owner, bal, price);
} }
/// Sells a [List<BookInstance>]
Future<Result<void>> sellBooks(List<BookInstance> books) async { Future<Result<void>> sellBooks(List<BookInstance> books) async {
Map<String, double?> res = {}; Map<String, double?> res = {};
for (BookInstance instance in books) { for (BookInstance instance in books) {

View file

@ -2,19 +2,16 @@ import 'package:seshat/data/services/api_client.dart';
import 'package:seshat/domain/models/book.dart'; import 'package:seshat/domain/models/book.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
/// Repository to manage [Book]
class BookRepository { class BookRepository {
BookRepository({required ApiClient apiClient}) : _apiClient = apiClient; BookRepository({required ApiClient apiClient}) : _apiClient = apiClient;
final ApiClient _apiClient; final ApiClient _apiClient;
/// Gets a [Book] by its [ean]
Future<Result<Book>> getBookByEAN(String ean) async { Future<Result<Book>> getBookByEAN(String ean) async {
return await _apiClient.getBookByEAN(ean); return await _apiClient.getBookByEAN(ean);
} }
/// Gets a [Book] by its [bookId] Future<Result<Book>> getBookById(int id) async {
Future<Result<Book>> getBookById(int bookId) async { return await _apiClient.getBookById(id);
return await _apiClient.getBookById(bookId);
} }
} }

View file

@ -5,7 +5,6 @@ import 'package:seshat/data/services/websocket_client.dart';
import 'package:seshat/domain/models/owner.dart'; import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
/// Repository to manage [Owner]
class OwnerRepository { class OwnerRepository {
OwnerRepository({ OwnerRepository({
required ApiClient apiClient, required ApiClient apiClient,
@ -15,25 +14,18 @@ class OwnerRepository {
final ApiClient _apiClient; final ApiClient _apiClient;
final WebsocketClient _wsClient; final WebsocketClient _wsClient;
/// [StreamSubscription] to the [Stream<Owner>] for [_wsClient]
late final StreamSubscription sub; late final StreamSubscription sub;
/// [List<Owner>] of owners, updated by [_wsClient]
List<Owner>? _cachedOwners; List<Owner>? _cachedOwners;
Owner? _sectionOwner;
/// [Owner] of the current user Future<Result<Owner>> get sectionOwner async {
Owner? _ownerOfUser; if (_sectionOwner != null) {
return Result.ok(_sectionOwner!);
/// [Owner] of the current user
Future<Result<Owner>> get ownerOfUser async {
if (_ownerOfUser != null) {
return Result.ok(_ownerOfUser!);
} }
final result = await _apiClient.getOwnerOfUser(); final result = await _apiClient.getSectionOwner();
switch (result) { switch (result) {
case Ok(): case Ok():
_ownerOfUser = result.value; _sectionOwner = result.value;
break; break;
default: default:
break; break;
@ -41,17 +33,16 @@ class OwnerRepository {
return result; return result;
} }
/// Gets an [Owner] from its [ownerId] Future<Result<Owner>> getOwnerById(int id) async {
Future<Result<Owner>> getOwnerById(int ownerId) async {
if (_cachedOwners != null) { if (_cachedOwners != null) {
final result1 = _cachedOwners! final result1 = _cachedOwners!
.where((owner) => owner.id == ownerId) .where((owner) => owner.id == id)
.firstOrNull; .firstOrNull;
if (result1 != null) { if (result1 != null) {
return Result.ok(result1); return Result.ok(result1);
} }
} }
return await _apiClient.getOwnerById(ownerId); return await _apiClient.getOwnerById(id);
} }
/// Adds an [Owner] to the database, and gets the resulting [Owner]. /// Adds an [Owner] to the database, and gets the resulting [Owner].

View file

@ -1,5 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:logger/logger.dart'; import 'package:logger/logger.dart';
@ -9,9 +11,9 @@ import 'package:seshat/domain/models/bal.dart';
import 'package:seshat/domain/models/bal_stats.dart'; import 'package:seshat/domain/models/bal_stats.dart';
import 'package:seshat/domain/models/book.dart'; import 'package:seshat/domain/models/book.dart';
import 'package:seshat/domain/models/book_instance.dart'; import 'package:seshat/domain/models/book_instance.dart';
import 'package:seshat/domain/models/enums.dart';
import 'package:seshat/domain/models/owner.dart'; import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/domain/models/search_result.dart'; import 'package:seshat/domain/models/search_result.dart';
import 'package:seshat/utils/command.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
extension StringExtension on String { extension StringExtension on String {
@ -20,17 +22,12 @@ extension StringExtension on String {
} }
} }
/// API Client to manage all authenticated REST routes
class ApiClient { class ApiClient {
ApiClient(); ApiClient({String? host, int? port});
/// JWT for registration late final Command0 load;
String? token; String? token;
/// Readiness of the API Client
bool isReady = false; bool isReady = false;
/// Storage to access JWT
FlutterSecureStorage? _secureStorage; FlutterSecureStorage? _secureStorage;
Logger log = Logger( Logger log = Logger(
printer: PrettyPrinter( printer: PrettyPrinter(
@ -41,12 +38,10 @@ class ApiClient {
), ),
); );
/// Initializes connection to the [_secureStorage]
Future<void> _initStore() async { Future<void> _initStore() async {
_secureStorage ??= const FlutterSecureStorage(); _secureStorage ??= const FlutterSecureStorage(aOptions: AndroidOptions());
} }
/// Generates authorization headers and option [additionalHeaders]
Future<Map<String, String>> _getHeaders([ Future<Map<String, String>> _getHeaders([
Map<String, String>? additionalHeaders, Map<String, String>? additionalHeaders,
]) async { ]) async {
@ -62,7 +57,6 @@ class ApiClient {
* ======================== * ========================
*/ */
/// Gets data about a BAL's needed returns
Future<Result<Accounting>> getAccounting(int balId) async { Future<Result<Accounting>> getAccounting(int balId) async {
final url = "https://$apiBasePath/bal/$balId/accounting"; final url = "https://$apiBasePath/bal/$balId/accounting";
log.i("Fetching: getAccounting ($url)"); log.i("Fetching: getAccounting ($url)");
@ -89,8 +83,6 @@ class ApiClient {
} }
} }
/// Notifies the server that either Books, Money or All has been returned to the user
/// [type] is the stringified version of [ReturnType]
Future<Result<void>> returnToId(int balId, int ownerId, String type) async { Future<Result<void>> returnToId(int balId, int ownerId, String type) async {
final url = final url =
"https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}"; "https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}";
@ -130,9 +122,8 @@ class ApiClient {
* ================= * =================
*/ */
/// Get stats about a BAL's performance Future<Result<BalStats>> getBalStats(int id) async {
Future<Result<BalStats>> getBalStats(int balId) async { final url = "https://$apiBasePath/bal/${id.toString()}/stats";
final url = "https://$apiBasePath/bal/${balId.toString()}/stats";
log.i("Fetching: getBalStats ($url)"); log.i("Fetching: getBalStats ($url)");
final client = Client(); final client = Client();
try { try {
@ -159,9 +150,8 @@ class ApiClient {
} }
} }
/// Stops a BAL, putting it's [BalState] to [BalState.ended] Future<Result<Bal>> stopBal(int id) async {
Future<Result<Bal>> stopBal(int balId) async { final url = "https://$apiBasePath/bal/${id.toString()}/stop";
final url = "https://$apiBasePath/bal/${balId.toString()}/stop";
log.i("Fetching: stopBal ($url)"); log.i("Fetching: stopBal ($url)");
final client = Client(); final client = Client();
try { try {
@ -188,9 +178,8 @@ class ApiClient {
} }
} }
/// Starts a BAL, putting it's [BalState] to [BalState.ongoing] Future<Result<Bal>> startBal(int id) async {
Future<Result<Bal>> startBal(int balId) async { final url = "https://$apiBasePath/bal/${id.toString()}/start";
final url = "https://$apiBasePath/bal/${balId.toString()}/start";
log.i("Fetching: startBal ($url)"); log.i("Fetching: startBal ($url)");
final client = Client(); final client = Client();
try { try {
@ -217,22 +206,21 @@ class ApiClient {
} }
} }
/// Changes the information about a [Bal], such as its [name], [startTime] or [endTime]
Future<Result<Bal>> editBal( Future<Result<Bal>> editBal(
int balId, int id,
String name, String name,
DateTime startTime, DateTime start,
DateTime endTime, DateTime end,
) async { ) async {
final url = "https://$apiBasePath/bal/${balId.toString()}"; final url = "https://$apiBasePath/bal/${id.toString()}";
log.i("Fetching: editBal ($url)"); log.i("Fetching: editBal ($url)");
final client = Client(); final client = Client();
try { try {
final headers = await _getHeaders({"Content-Type": "application/json"}); final headers = await _getHeaders({"Content-Type": "application/json"});
final body = { final body = {
"name": name, "name": name,
"start_timestamp": (startTime.millisecondsSinceEpoch / 1000).round(), "start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
"end_timestamp": (endTime.millisecondsSinceEpoch / 1000).round(), "end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
}; };
final response = await client.patch( final response = await client.patch(
Uri.parse(url), Uri.parse(url),
@ -258,9 +246,8 @@ class ApiClient {
} }
} }
/// Gets a [Bal] from it's [balId] Future<Result<Bal>> getBalById(int id) async {
Future<Result<Bal>> getBalById(int balId) async { final url = "https://$apiBasePath/bal/${id.toString()}";
final url = "https://$apiBasePath/bal/${balId.toString()}";
log.i("Fetching: getBalById ($url)"); log.i("Fetching: getBalById ($url)");
final client = Client(); final client = Client();
try { try {
@ -285,12 +272,7 @@ class ApiClient {
} }
} }
/// Adds a [Bal] from it's [name], [startTime] and [endTime] Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) async {
Future<Result<Bal>> addBal(
String name,
DateTime startTime,
DateTime endTime,
) async {
final url = "https://$apiBasePath/bal"; final url = "https://$apiBasePath/bal";
log.i("Fetching: addBal ($url)"); log.i("Fetching: addBal ($url)");
final client = Client(); final client = Client();
@ -298,8 +280,8 @@ class ApiClient {
final headers = await _getHeaders({"Content-Type": "application/json"}); final headers = await _getHeaders({"Content-Type": "application/json"});
final body = { final body = {
"name": name, "name": name,
"start_timestamp": (startTime.millisecondsSinceEpoch / 1000).round(), "start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
"end_timestamp": (endTime.millisecondsSinceEpoch / 1000).round(), "end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
}; };
final response = await client.post( final response = await client.post(
Uri.parse(url), Uri.parse(url),
@ -323,7 +305,6 @@ class ApiClient {
} }
} }
/// Gets a [List<Bal>] of all [Bal]
Future<Result<List<Bal>>> getBals() async { Future<Result<List<Bal>>> getBals() async {
final url = "https://$apiBasePath/bals"; final url = "https://$apiBasePath/bals";
log.i("Fetching: getBals ($url)"); log.i("Fetching: getBals ($url)");
@ -331,26 +312,21 @@ class ApiClient {
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
switch (response.statusCode) { final json = jsonDecode(response.body) as List<dynamic>;
case 200: return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
final json = jsonDecode(response.body) as List<dynamic>; } else {
return Result.ok( throw Exception("Something wrong happened");
json.map((element) => Bal.fromJSON(element)).toList(),
);
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString()); debugPrint("ERROR: ${e.toString()}");
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Gets the ongoing BAL for the user Future<Result<Bal?>> getCurrentBal() async {
Future<Result<Bal?>> getOngoingBal() async {
final url = "https://$apiBasePath/bal/current"; final url = "https://$apiBasePath/bal/current";
log.i("Fetching: getCurrentBal ($url)"); log.i("Fetching: getCurrentBal ($url)");
final client = Client(); final client = Client();
@ -378,32 +354,26 @@ class ApiClient {
* =================== * ===================
*/ */
/// Gets a [Book] by its [bookId] Future<Result<Book>> getBookById(int id) async {
Future<Result<Book>> getBookById(int bookId) async { final url = "https://$apiBasePath/book/id/${id.toString()}";
final url = "https://$apiBasePath/book/id/${bookId.toString()}";
log.i("Fetching: getBookById ($url)"); log.i("Fetching: getBookById ($url)");
final client = Client(); final client = Client();
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body);
final json = jsonDecode(response.body); return Result.ok(Book.fromJSON(json));
return Result.ok(Book.fromJSON(json)); } else {
case 404: throw Exception("The book was not found");
throw "No book with this id exists in database";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception("API $e")); return Result.error(Exception("API $e"));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Gets a [Book] from its [ean]
Future<Result<Book>> getBookByEAN(String ean) async { Future<Result<Book>> getBookByEAN(String ean) async {
final url = "https://$apiBasePath/book/ean/$ean"; final url = "https://$apiBasePath/book/ean/$ean";
log.i("Fetching: getBookByEan ($url)"); log.i("Fetching: getBookByEan ($url)");
@ -411,17 +381,13 @@ class ApiClient {
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body);
final json = jsonDecode(response.body); return Result.ok(Book.fromJSON(json));
return Result.ok(Book.fromJSON(json)); } else {
case 404: throw Exception("The book was not found");
throw "No book with this EAN found in the database of BNF";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception("API $e")); return Result.error(Exception("API $e"));
} finally { } finally {
client.close(); client.close();
@ -434,7 +400,6 @@ class ApiClient {
* ============================= * =============================
*/ */
/// Gets a [BookInstance] from it's [title], [author] or both
Future<Result<List<SearchResult>>> getBookInstanceBySearch( Future<Result<List<SearchResult>>> getBookInstanceBySearch(
int balId, int balId,
String title, String title,
@ -451,26 +416,19 @@ class ApiClient {
headers: headers, headers: headers,
body: body, body: body,
); );
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body) as List<dynamic>;
final json = jsonDecode(response.body) as List<dynamic>; return Result.ok(json.map((el) => SearchResult.fromJSON(el)).toList());
return Result.ok( } else {
json.map((el) => SearchResult.fromJSON(el)).toList(), throw "Unknown Error";
);
case 403:
throw "You do not own the BAL";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception("API $e")); return Result.error(Exception("API $e"));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Gets a [BookInstance] from it's [ean]
Future<Result<List<BookInstance>>> getBookInstanceByEAN( Future<Result<List<BookInstance>>> getBookInstanceByEAN(
int balId, int balId,
int ean, int ean,
@ -482,29 +440,19 @@ class ApiClient {
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body) as List<dynamic>;
final json = jsonDecode(response.body) as List<dynamic>; return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList());
return Result.ok( } else {
json.map((el) => BookInstance.fromJSON(el)).toList(), throw "Unknown Error";
);
case 403:
throw "You do not own the BAL";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception("API $e")); return Result.error(Exception("API $e"));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Notifies the server of the sell of multiple [BookInstance]. [books] is in the form of
/// ```dart
/// final books = {"aBookInstanceId": 6.0} // and its price
/// ```
Future<Result<void>> sellBooks(Map<String, double?> books) async { Future<Result<void>> sellBooks(Map<String, double?> books) async {
final url = "https://$apiBasePath/book_instance/sell/bulk"; final url = "https://$apiBasePath/book_instance/sell/bulk";
log.i("Fetching: sellBooks ($url)"); log.i("Fetching: sellBooks ($url)");
@ -517,28 +465,19 @@ class ApiClient {
headers: headers, headers: headers,
body: body, body: body,
); );
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: return Result.ok(response.statusCode);
return Result.ok(response.statusCode); } else {
case 403: throw "Unknown error";
throw "You don't own one of the specified books";
case 404:
throw "One of the books was not found";
case 409:
throw "One of the books wasn't available";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Creates a new [BookInstance] from it's [book], it's [owner], it's [bal] and it's [price] Future<Result<BookInstance>> sendBook(
Future<Result<BookInstance>> sendNewBookInstance(
Book book, Book book,
Owner owner, Owner owner,
Bal bal, Bal bal,
@ -560,14 +499,13 @@ class ApiClient {
headers: headers, headers: headers,
body: body, body: body,
); );
switch (response.statusCode) { if (response.statusCode == 201) {
case 201: final json = jsonDecode(response.body);
final json = jsonDecode(response.body); return Result.ok(BookInstance.fromJSON(json));
return Result.ok(BookInstance.fromJSON(json)); } else if (response.statusCode == 403) {
case 403: throw Exception("You don't own that book instance");
throw "You don't own that book instance"; } else {
default: throw Exception("Something wrong happened");
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
return Result.error(Exception(e)); return Result.error(Exception(e));
@ -582,57 +520,47 @@ class ApiClient {
* ==================== * ====================
*/ */
/// Gets an [Owner] by it's [ownerId] Future<Result<Owner>> getOwnerById(int id) async {
Future<Result<Owner>> getOwnerById(int ownerId) async { final url = "https://$apiBasePath/owner/${id.toString()}";
final url = "https://$apiBasePath/owner/${ownerId.toString()}";
log.i("Fetching: getOwnerById ($url)"); log.i("Fetching: getOwnerById ($url)");
final client = Client(); final client = Client();
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body);
final json = jsonDecode(response.body); return Result.ok(Owner.fromJSON(json));
return Result.ok(Owner.fromJSON(json)); } else {
case 403: throw Exception("The owner was not found");
throw "You do not own specified owner";
case 404:
throw "No owner with this id exists";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception("API $e")); return Result.error(Exception("API $e"));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Get the owner of the current user Future<Result<Owner>> getSectionOwner() async {
Future<Result<Owner>> getOwnerOfUser() async {
final url = "https://$apiBasePath/owner/self"; final url = "https://$apiBasePath/owner/self";
log.i("Fetching: getSectionOwner ($url)"); log.i("Fetching: getSectionOwner ($url)");
final client = Client(); final client = Client();
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body);
final json = jsonDecode(response.body); return Result.ok(Owner.fromJSON(json));
return Result.ok(Owner.fromJSON(json)); } else {
default: throw "Unknown error";
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Get a [List<Owner>] of all [Owner] /// Call on `/owners` to get a list of all [Owner]s
Future<Result<List<Owner>>> getOwners() async { Future<Result<List<Owner>>> getOwners() async {
final url = "https://$apiBasePath/owners"; final url = "https://$apiBasePath/owners";
log.i("Fetching: getOwners ($url)"); log.i("Fetching: getOwners ($url)");
@ -640,14 +568,13 @@ class ApiClient {
try { try {
final headers = await _getHeaders(); final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers); final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: final json = jsonDecode(response.body) as List<dynamic>;
final json = jsonDecode(response.body) as List<dynamic>; return Result.ok(
return Result.ok( json.map((element) => Owner.fromJSON(element)).toList(),
json.map((element) => Owner.fromJSON(element)).toList(), );
); } else {
default: throw Exception("Invalid request");
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} on Exception catch (error) { } on Exception catch (error) {
return Result.error(error); return Result.error(error);
@ -656,7 +583,7 @@ class ApiClient {
} }
} }
/// Adds an [Owner] from its [firstName], [lastName] and [contact] /// Adds an owner to the database
Future<Result<Owner>> addOwner( Future<Result<Owner>> addOwner(
String firstName, String firstName,
String lastName, String lastName,
@ -677,16 +604,14 @@ class ApiClient {
headers: headers, headers: headers,
body: jsonEncode(body), body: jsonEncode(body),
); );
switch (response.statusCode) { if (response.statusCode == 201) {
case 201: final json = jsonDecode(response.body);
final json = jsonDecode(response.body); return Result.ok(Owner.fromJSON(json));
return Result.ok(Owner.fromJSON(json)); } else {
default: throw Exception("Invalid request");
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } on Exception catch (error) {
log.e(e.toString()); return Result.error(error);
return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();
} }

View file

@ -1,43 +1,29 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:logger/logger.dart';
import 'package:seshat/config/constants.dart'; import 'package:seshat/config/constants.dart';
import 'package:seshat/utils/result.dart'; import 'package:seshat/utils/result.dart';
import "package:http/http.dart"; import "package:http/http.dart" as http;
/// API Client to manage all unauthenticated REST routes
class AuthClient { class AuthClient {
AuthClient(); AuthClient();
/// Storage to access JWT
FlutterSecureStorage? _secureStorage; FlutterSecureStorage? _secureStorage;
Logger log = Logger(
printer: PrettyPrinter(
colors: true,
lineLength: 100,
methodCount: 0,
dateTimeFormat: DateTimeFormat.dateAndTime,
),
);
/// Initializes connection to the [_secureStorage]
Future<void> _initStore() async { Future<void> _initStore() async {
_secureStorage ??= const FlutterSecureStorage(); _secureStorage ??= const FlutterSecureStorage(
aOptions: AndroidOptions(encryptedSharedPreferences: true),
);
} }
/// Verifies the validity of the token stored in [_secureStorage]
Future<Result<bool>> hasValidToken() async { Future<Result<bool>> hasValidToken() async {
final url = "https://$apiBasePath/token-check";
log.i("Fetching: hasValidToken ($url)");
final client = Client();
try { try {
await _initStore(); await _initStore();
bool hasToken = await _secureStorage!.containsKey(key: "token"); bool hasToken = await _secureStorage!.containsKey(key: "token");
if (hasToken) { if (hasToken) {
var token = await _secureStorage!.read(key: "token"); var token = await _secureStorage!.read(key: "token");
var response = await client.post( var url = Uri.parse("https://$apiBasePath/token-check");
Uri.parse(url), var response = await http.post(
url,
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
body: jsonEncode({"token": token}), body: jsonEncode({"token": token}),
); );
@ -48,64 +34,52 @@ class AuthClient {
} }
return Result.ok(false); return Result.ok(false);
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally {
client.close();
} }
} }
/// Logs a user in from its [username] and [password]
Future<Result<String>> login(String username, String password) async { Future<Result<String>> login(String username, String password) async {
final url = "https://$apiBasePath/auth"; var client = http.Client();
log.i("Logging in: $url");
var client = Client();
try { try {
await _initStore(); await _initStore();
var url = Uri.parse("https://$apiBasePath/auth");
var response = await client.post( var response = await client.post(
Uri.parse(url), url,
headers: {"Content-Type": "application/json"}, headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
body: jsonEncode({"password": password, "username": username}), body: jsonEncode({"password": password, "username": username}),
); );
switch (response.statusCode) { if (response.statusCode == 200) {
case 200: var json = jsonDecode(response.body);
var json = jsonDecode(response.body); await _secureStorage!.write(key: "token", value: json["access_token"]);
await _secureStorage!.write( return Result.ok(json["access_token"]);
key: "token", } else if (response.statusCode == 401) {
value: json["access_token"], return Result.error(Exception("Wrong credentials"));
); } else {
return Result.ok(json["access_token"]); return Result.error(Exception("Token creation error"));
case 401:
throw "Wrong credentials";
case 500:
throw "Token creation error";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();
} }
} }
/// Gets the API version of the server
Future<Result<int>> getRemoteApiVersion() async { Future<Result<int>> getRemoteApiVersion() async {
final url = "https://$apiBasePath/version"; final client = http.Client();
log.i("Fetching: getRemoteApiVersion ($url)");
final client = Client();
try { try {
final response = await client.get(Uri.parse(url)); final response = await client.get(
switch (response.statusCode) { Uri.parse("https://$apiBasePath/version"),
case 200: );
final json = jsonDecode(response.body) as int; if (response.statusCode == 200) {
return Result.ok(json); final json = jsonDecode(response.body) as int;
default: return Result.ok(json);
throw "Unknown error with code ${response.statusCode.toString()}"; } else {
throw "Something wrong happened";
} }
} catch (e) { } catch (e) {
log.e(e.toString());
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();

View file

@ -2,55 +2,33 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:logger/logger.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:seshat/config/constants.dart'; import 'package:seshat/config/constants.dart';
import 'package:seshat/domain/models/owner.dart'; import 'package:seshat/domain/models/owner.dart';
import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/web_socket_channel.dart';
/// API Client to manages connections to WebSockets
class WebsocketClient { class WebsocketClient {
/// Storage to access JWT
FlutterSecureStorage? _secureStorage;
/// Raw channel of data from WebSocket
WebSocketChannel? _channel; WebSocketChannel? _channel;
FlutterSecureStorage? _secureStorage;
/// Global WebSocket Stream
final BehaviorSubject<dynamic> _baseController = BehaviorSubject(); final BehaviorSubject<dynamic> _baseController = BehaviorSubject();
/// WebSocket Stream dedicated to [Owner] entries
final BehaviorSubject<Owner> _ownersController = BehaviorSubject<Owner>( final BehaviorSubject<Owner> _ownersController = BehaviorSubject<Owner>(
sync: true, sync: true,
); );
/// Subscription to [_baseController]
late final StreamSubscription sub; late final StreamSubscription sub;
Logger log = Logger(
printer: PrettyPrinter(
colors: true,
lineLength: 100,
methodCount: 0,
dateTimeFormat: DateTimeFormat.dateAndTime,
),
);
/// Gets a stream of [Owner]
Stream<Owner> get owners => _ownersController.stream; Stream<Owner> get owners => _ownersController.stream;
/// Initializes connection to the [_secureStorage]
Future<void> _initStore() async { Future<void> _initStore() async {
_secureStorage ??= const FlutterSecureStorage(); _secureStorage ??= const FlutterSecureStorage(
aOptions: AndroidOptions(encryptedSharedPreferences: true),
);
} }
/// Connects to the websocket
Future<void> connect() async { Future<void> connect() async {
final url = "wss://$apiBasePath/ws";
log.i("Webocket: $url");
await _initStore(); await _initStore();
if (_channel != null) return; if (_channel != null) return;
_channel = WebSocketChannel.connect(Uri.parse(url)); _channel = WebSocketChannel.connect(Uri.parse("wss://$apiBasePath/ws"));
await _channel!.ready; await _channel!.ready;
var token = await _secureStorage!.read(key: "token"); var token = await _secureStorage!.read(key: "token");
@ -82,13 +60,11 @@ class WebsocketClient {
} }
} }
/// Disconnects from the websocket
void _handleDisconnect() { void _handleDisconnect() {
sub.cancel(); sub.cancel();
_channel = null; _channel = null;
} }
/// Closes all connections
void dispose() { void dispose() {
sub.cancel(); sub.cancel();
_channel?.sink.close(); _channel?.sink.close();

View file

@ -55,7 +55,7 @@ GoRouter router(AuthRepository authRepository) => GoRouter(
pageBuilder: (context, state) { pageBuilder: (context, state) {
final viewModel = BalViewModel( final viewModel = BalViewModel(
balRepository: context.read(), balRepository: context.read(),
selectedBalId: int.parse(state.pathParameters["id"] ?? ""), id: int.parse(state.pathParameters["id"] ?? ""),
ownerRepository: context.read(), ownerRepository: context.read(),
); );
return NoTransitionPage(child: BalPage(viewModel: viewModel)); return NoTransitionPage(child: BalPage(viewModel: viewModel));
@ -72,6 +72,11 @@ GoRouter router(AuthRepository authRepository) => GoRouter(
); );
return NoTransitionPage(child: AddPage(viewModel: viewModel)); return NoTransitionPage(child: AddPage(viewModel: viewModel));
}, },
// routes: [
// GoRoute(path: Routes.addForm),
// GoRoute(path: Routes.addOwner),
// GoRoute(path: Routes.addPrice),
// ],
), ),
GoRoute( GoRoute(
path: Routes.sell, path: Routes.sell,

View file

@ -38,29 +38,18 @@ class AddViewModel extends ChangeNotifier {
* ==================== * ====================
*/ */
/// Owner currently selected in the ui
Owner? _currentOwner; Owner? _currentOwner;
/// Owner currently selected in the ui
Owner? get currentOwner => _currentOwner; Owner? get currentOwner => _currentOwner;
set currentOwner(Owner? owner) { set currentOwner(Owner? owner) {
_currentOwner = owner; _currentOwner = owner;
notifyListeners(); notifyListeners();
} }
/// Owner of the current user Owner? _sectionOwner;
Owner? _ownerOfUser; Owner? get sectionOwner => _sectionOwner;
/// Owner of the current user
Owner? get ownerOfUser => _ownerOfUser;
/// All the [Owner]
List<Owner> _owners = []; List<Owner> _owners = [];
/// All the [Owner]
List<Owner>? get owners => _owners; List<Owner>? get owners => _owners;
/// Adds an owner from it's [firstName], [lastName] and [contact]
Future<Result<Owner>> addOwner( Future<Result<Owner>> addOwner(
String firstName, String firstName,
String lastName, String lastName,
@ -96,11 +85,8 @@ class AddViewModel extends ChangeNotifier {
* ================= * =================
*/ */
/// Ongoing [Bal] Bal? _currentBal;
Bal? _ongoingBal; Bal? get currentBal => _currentBal;
/// Ongoing [Bal]
Bal? get ongoingBal => _ongoingBal;
/* /*
* =================== * ===================
@ -108,10 +94,7 @@ class AddViewModel extends ChangeNotifier {
* =================== * ===================
*/ */
/// Wether to ask for a price
bool _askPrice = true; bool _askPrice = true;
/// Wether to ask for a price
bool get askPrice => _askPrice; bool get askPrice => _askPrice;
set askPrice(bool newValue) { set askPrice(bool newValue) {
_askPrice = newValue; _askPrice = newValue;
@ -124,25 +107,21 @@ class AddViewModel extends ChangeNotifier {
* ================================= * =================================
*/ */
/// Retrieves the book associated with an ean through a [barcode] /// Sends an api request with a [bacorde], then gets the [Book] that was
Future<Result<Book>> scanBook(String ean) async { /// either created or retrieved. Sens the [Book] back wrapped in a [Result].
Future<Result<Book>> scanBook(BarcodeCapture barcode) async {
var ean = barcode.barcodes.first.rawValue!;
var result = await _bookRepository.getBookByEAN(ean); var result = await _bookRepository.getBookByEAN(ean);
return result; return result;
} }
/// Creates a new Book Instance from its [book], [owner], [bal] and [price] Future<Result<BookInstance>> sendBook(
Future<Result<BookInstance>> sendNewBookInstance(
Book book, Book book,
Owner owner, Owner owner,
Bal bal, Bal bal,
double price, double price,
) async { ) async {
return await _bookInstanceRepository.sendNewBookInstance( return await _bookInstanceRepository.sendBook(book, owner, bal, price);
book,
owner,
bal,
price,
);
} }
/* /*
@ -151,11 +130,9 @@ class AddViewModel extends ChangeNotifier {
* ================================= * =================================
*/ */
/// Command to load the view model
late final Command0 load; late final Command0 load;
bool isLoaded = false; bool isLoaded = false;
/// Manages the loaders
Future<Result<void>> _load() async { Future<Result<void>> _load() async {
final result1 = await _loadOwners(); final result1 = await _loadOwners();
switch (result1) { switch (result1) {
@ -176,12 +153,11 @@ class AddViewModel extends ChangeNotifier {
return result2; return result2;
} }
/// Loads all necessary data about [Bal]s
Future<Result<void>> _loadBal() async { Future<Result<void>> _loadBal() async {
final result = await _balRepository.getBals(); final result = await _balRepository.getBals();
switch (result) { switch (result) {
case Ok(): case Ok():
_ongoingBal = result.value _currentBal = result.value
.where((bal) => bal.state == BalState.ongoing) .where((bal) => bal.state == BalState.ongoing)
.firstOrNull; .firstOrNull;
break; break;
@ -192,7 +168,6 @@ class AddViewModel extends ChangeNotifier {
return result; return result;
} }
/// Loads all the necessary data about [Owner]s
Future<Result<void>> _loadOwners() async { Future<Result<void>> _loadOwners() async {
final result = await _ownerRepository.getOwners(); final result = await _ownerRepository.getOwners();
switch (result) { switch (result) {
@ -207,10 +182,10 @@ class AddViewModel extends ChangeNotifier {
return result; return result;
} }
final result2 = await _ownerRepository.ownerOfUser; final result2 = await _ownerRepository.sectionOwner;
switch (result2) { switch (result2) {
case Ok(): case Ok():
_ownerOfUser = result2.value; _sectionOwner = result2.value;
break; break;
default: default:
} }

View file

@ -22,27 +22,30 @@ class AddPage extends StatefulWidget {
} }
class _AddPageState extends State<AddPage> { class _AddPageState extends State<AddPage> {
final MobileScannerController scannerController = MobileScannerController( num? price;
final MobileScannerController controller = MobileScannerController(
formats: [BarcodeFormat.ean13], formats: [BarcodeFormat.ean13],
detectionTimeoutMs: 1000, detectionTimeoutMs: 1000,
); );
@override @override
void dispose() { void dispose() {
scannerController.dispose(); controller.dispose();
super.dispose(); super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
// return Consumer<TabScreen>(
// builder: (context, screen, child) {
return Scaffold( return Scaffold(
bottomNavigationBar: AppNavigationBar(startIndex: 1), bottomNavigationBar: AppNavigationBar(startIndex: 1),
body: ListenableBuilder( body: ListenableBuilder(
listenable: widget.viewModel, listenable: widget.viewModel,
builder: (context, child) => switch (widget.viewModel.isLoaded) { builder: (context, child) => switch (widget.viewModel.isLoaded) {
false => AwaitLoading(), false => AwaitLoading(),
true => switch (widget.viewModel.ongoingBal) { true => switch (widget.viewModel.currentBal) {
null => Center( null => Center(
child: SizedBox( child: SizedBox(
width: 300, width: 300,
@ -75,37 +78,60 @@ class _AddPageState extends State<AddPage> {
children: [ children: [
ColoredBox(color: Colors.black), ColoredBox(color: Colors.black),
MobileScanner( MobileScanner(
controller: scannerController, controller: controller,
onDetect: (barcodes) async { onDetect: (barcodes) async {
if (barcodes.barcodes.isEmpty) { if (barcodes.barcodes.isEmpty) {
return; return;
} }
if (widget.viewModel.currentOwner == null) { if (widget.viewModel.currentOwner == null) {
_showMissingOwnerSnackBar( ScaffoldMessenger.of(context).showSnackBar(
context, SnackBar(
scannerController, content: Text(
widget.viewModel, "Attention : vous devez choisir un·e propriétaire",
),
behavior: SnackBarBehavior.floating,
),
); );
return; return;
} }
_scanEan( void setPrice(num newPrice) async {
context, setState(() {
widget.viewModel, price = newPrice;
barcodes.barcodes.first.rawValue!, });
scannerController, }
Result<Book> result = await widget.viewModel.scanBook(
barcodes,
); );
switch (result) {
case Ok():
await _confirmationDialogBuilder(
context,
setPrice,
controller,
widget.viewModel,
result.value,
);
break;
case Error():
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Erreur : ${result.error}"),
behavior: SnackBarBehavior.floating,
),
);
break;
}
}, },
), ),
Center( Center(
child: SvgPicture.asset( child: SvgPicture.asset(
'assets/scan-overlay.svg', 'assets/scan-overlay.svg',
height: (MediaQuery.sizeOf(context).height / 5) * 2, height: (MediaQuery.sizeOf(context).height / 5) * 2,
), ),
), ),
SafeArea( SafeArea(
child: SingleChildScrollView( child: SingleChildScrollView(
child: Column( child: Column(
@ -129,7 +155,7 @@ class _AddPageState extends State<AddPage> {
), ),
onPressed: () => _ownerDialogBuilder( onPressed: () => _ownerDialogBuilder(
context, context,
scannerController, controller,
widget.viewModel, widget.viewModel,
), ),
), ),
@ -158,7 +184,6 @@ class _AddPageState extends State<AddPage> {
), ),
), ),
), ),
SafeArea( SafeArea(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end,
@ -172,17 +197,19 @@ class _AddPageState extends State<AddPage> {
), ),
onPressed: () { onPressed: () {
if (widget.viewModel.currentOwner == null) { if (widget.viewModel.currentOwner == null) {
_showMissingOwnerSnackBar( ScaffoldMessenger.of(context).showSnackBar(
context, SnackBar(
scannerController, content: Text(
widget.viewModel, "Attention : vous devez choisir un·e propriétaire",
),
behavior: SnackBarBehavior.floating,
),
); );
return;
} }
_formDialogBuilder( _formDialogBuilder(
context, context,
scannerController, controller,
widget.viewModel, widget.viewModel,
); );
}, },
@ -204,71 +231,18 @@ class _AddPageState extends State<AddPage> {
} }
} }
void _scanEan(
BuildContext context,
AddViewModel viewModel,
String ean,
MobileScannerController scannerController, {
Function(BuildContext)? leaveLastPopup,
}) async {
Result<Book> result = await viewModel.scanBook(ean);
if (context.mounted) {
if (leaveLastPopup != null) {
leaveLastPopup(context);
}
switch (result) {
case Ok():
await _confirmationDialogBuilder(
context,
scannerController,
viewModel,
result.value,
);
break;
case Error():
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Erreur : ${result.error}"),
behavior: SnackBarBehavior.floating,
),
);
break;
}
}
}
void _showMissingOwnerSnackBar(
BuildContext context,
MobileScannerController scannerController,
AddViewModel viewModel,
) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Attention : vous devez choisir un·e propriétaire"),
duration: Duration(seconds: 4),
action: SnackBarAction(
label: "Choisir",
onPressed: () =>
_ownerDialogBuilder(context, scannerController, viewModel),
),
behavior: SnackBarBehavior.floating,
),
);
}
Future<void> _confirmationDialogBuilder( Future<void> _confirmationDialogBuilder(
BuildContext context, BuildContext context,
MobileScannerController scannerController, Function(num) setPrice,
MobileScannerController controller,
AddViewModel viewModel, AddViewModel viewModel,
Book book, Book book,
) { ) {
scannerController.stop(); controller.stop();
// Utility function to pass to downwards widgets
void exitPopup(BuildContext localContext) { void exitPopup(BuildContext localContext) {
Navigator.of(localContext).pop(); Navigator.of(localContext).pop();
scannerController.start(); controller.start();
} }
return showDialog( return showDialog(
@ -276,6 +250,7 @@ Future<void> _confirmationDialogBuilder(
barrierDismissible: false, barrierDismissible: false,
builder: (context) => ConfirmationPopup( builder: (context) => ConfirmationPopup(
exitPopup: exitPopup, exitPopup: exitPopup,
setPrice: setPrice,
viewModel: viewModel, viewModel: viewModel,
book: book, book: book,
), ),
@ -289,7 +264,6 @@ Future<void> _formDialogBuilder(
) { ) {
controller.stop(); controller.stop();
// Utility function to pass to downwards widgets
void exitPopup(BuildContext localContext) { void exitPopup(BuildContext localContext) {
Navigator.of(localContext).pop(); Navigator.of(localContext).pop();
controller.start(); controller.start();
@ -298,12 +272,7 @@ Future<void> _formDialogBuilder(
return showDialog( return showDialog(
context: context, context: context,
barrierDismissible: false, barrierDismissible: false,
builder: (context) => FormPopup( builder: (context) => FormPopup(viewModel: viewModel, exitPopup: exitPopup),
viewModel: viewModel,
exitPopup: exitPopup,
scannerController: controller,
scanEan: _scanEan,
),
); );
} }
@ -314,8 +283,7 @@ Future<void> _ownerDialogBuilder(
) { ) {
controller.stop(); controller.stop();
// Utility function to pass to downwards widgets void onPressAccept(BuildContext localContext) {
void exitPopup(BuildContext localContext) {
Navigator.of(localContext).pop(); Navigator.of(localContext).pop();
controller.start(); controller.start();
} }
@ -324,6 +292,6 @@ Future<void> _ownerDialogBuilder(
context: context, context: context,
barrierDismissible: false, barrierDismissible: false,
builder: (context) => builder: (context) =>
OwnerPopup(viewModel: viewModel, exitPopup: exitPopup), OwnerPopup(viewModel: viewModel, onPressAccept: onPressAccept),
); );
} }

View file

@ -7,11 +7,13 @@ class ConfirmationPopup extends StatefulWidget {
const ConfirmationPopup({ const ConfirmationPopup({
super.key, super.key,
required this.exitPopup, required this.exitPopup,
required this.setPrice,
required this.viewModel, required this.viewModel,
required this.book, required this.book,
}); });
final Function(BuildContext) exitPopup; final Function(BuildContext) exitPopup;
final Function(num) setPrice;
final AddViewModel viewModel; final AddViewModel viewModel;
final Book book; final Book book;
@ -22,11 +24,9 @@ class ConfirmationPopup extends StatefulWidget {
class _ConfirmationPopupState extends State<ConfirmationPopup> { class _ConfirmationPopupState extends State<ConfirmationPopup> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
double price = 0; double price = 0;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
return AlertDialog( return AlertDialog(
title: Text("Prix"), title: Text("Prix"),
content: Form( content: Form(
@ -72,7 +72,6 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
], ],
), ),
), ),
SizedBox(height: 10), SizedBox(height: 10),
(widget.viewModel.askPrice) (widget.viewModel.askPrice)
? TextFormField( ? TextFormField(
@ -88,21 +87,15 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return "Indiquez un prix"; return "Indiquez un prix";
} else if (double.tryParse( } else if (num.tryParse(value) == null) {
value.replaceAll(",", "."),
) ==
null) {
return "Le prix doit être un nombre"; return "Le prix doit être un nombre";
} else if (double.parse(value.replaceAll(",", ".")) < } else if (num.parse(value) < 0) {
0) {
return "Le prix doit être positif ou nul"; return "Le prix doit être positif ou nul";
} }
return null; return null;
}, },
onSaved: (newValue) { onSaved: (newValue) {
price = double.parse( price = double.parse(newValue!);
newValue?.replaceAll(",", ".") ?? "0",
);
}, },
) )
: SizedBox(), : SizedBox(),
@ -112,7 +105,6 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
), ),
actions: [ actions: [
TextButton( TextButton(
child: Text("Annuler"),
onPressed: () { onPressed: () {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
@ -122,21 +114,19 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
); );
widget.exitPopup(context); widget.exitPopup(context);
}, },
child: Text("Annuler"),
), ),
TextButton( TextButton(
child: Text("Valider"),
onPressed: () async { onPressed: () async {
if (widget.viewModel.askPrice && if (widget.viewModel.askPrice &&
_formKey.currentState!.validate()) { _formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();
} else {
return;
} }
var result = await widget.viewModel.sendNewBookInstance( var result = await widget.viewModel.sendBook(
widget.book, widget.book,
widget.viewModel.currentOwner!, widget.viewModel.currentOwner!,
widget.viewModel.ongoingBal!, widget.viewModel.currentBal!,
price, price,
); );
@ -146,61 +136,49 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
Navigator.of(context).pop(); Navigator.of(context).pop();
showDialog( showDialog(
context: context, context: context,
barrierDismissible: false, builder: (context) => AlertDialog(
builder: (context) => title: Text(
RegisteredBookPopup(widget: widget, price: price), "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; break;
case Error(): case Error():
if (context.mounted) { if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Erreur : ${result.error}")), SnackBar(
content: Text(
"Une erreur est survenue : ${result.error}",
),
),
); );
} }
} }
}, },
), child: Text("Valider"),
],
);
}
}
class RegisteredBookPopup extends StatelessWidget {
const RegisteredBookPopup({
super.key,
required this.widget,
required this.price,
});
final ConfirmationPopup widget;
final double price;
@override
Widget build(BuildContext context) {
return AlertDialog(
// This thing is the BookInstance's short ID
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.ownerOfUser!.id)
? "Pensez à la gomette ! Ce livre appartient au syndicat."
: "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(
child: Text("Ok"),
onPressed: () {
widget.exitPopup(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Livre enregistré: ${widget.book.title}"),
behavior: SnackBarBehavior.floating,
duration: Duration(seconds: 2),
),
);
},
), ),
], ],
); );

View file

@ -1,5 +1,4 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:seshat/ui/add_page/view_model/add_view_model.dart'; import 'package:seshat/ui/add_page/view_model/add_view_model.dart';
class FormPopup extends StatelessWidget { class FormPopup extends StatelessWidget {
@ -7,14 +6,10 @@ class FormPopup extends StatelessWidget {
super.key, super.key,
required this.viewModel, required this.viewModel,
required this.exitPopup, required this.exitPopup,
required this.scannerController,
required this.scanEan,
}); });
final AddViewModel viewModel; final AddViewModel viewModel;
final Function(BuildContext) exitPopup; final Function(BuildContext) exitPopup;
final MobileScannerController scannerController;
final Function scanEan;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -36,8 +31,6 @@ class FormPopup extends StatelessWidget {
return _ManualEANPopup( return _ManualEANPopup(
exitPopup: exitPopup, exitPopup: exitPopup,
viewModel: viewModel, viewModel: viewModel,
scannerController: scannerController,
scanEan: scanEan,
); );
}, },
); );
@ -58,7 +51,6 @@ class FormPopup extends StatelessWidget {
), ),
), ),
), ),
Card( Card(
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
child: InkWell( child: InkWell(
@ -101,24 +93,11 @@ class FormPopup extends StatelessWidget {
} }
} }
/*
* ======================
* ====< MANUAL EAN >====
* ======================
*/
class _ManualEANPopup extends StatefulWidget { class _ManualEANPopup extends StatefulWidget {
const _ManualEANPopup({ const _ManualEANPopup({required this.exitPopup, required this.viewModel});
required this.exitPopup,
required this.viewModel,
required this.scannerController,
required this.scanEan,
});
final Function(BuildContext) exitPopup; final Function(BuildContext) exitPopup;
final AddViewModel viewModel; final AddViewModel viewModel;
final MobileScannerController scannerController;
final Function scanEan;
@override @override
State<_ManualEANPopup> createState() => _ManualEANPopupState(); State<_ManualEANPopup> createState() => _ManualEANPopupState();
@ -127,11 +106,11 @@ class _ManualEANPopup extends StatefulWidget {
class _ManualEANPopupState extends State<_ManualEANPopup> { class _ManualEANPopupState extends State<_ManualEANPopup> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
String? ean; String? ean;
num? price;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return AlertDialog( return AlertDialog(
title: Text("Entrée manuelle par EAN"), title: Text("Recherche par EAN"),
content: Form( content: Form(
key: _formKey, key: _formKey,
child: Column( child: Column(
@ -149,39 +128,61 @@ class _ManualEANPopupState extends State<_ManualEANPopup> {
validator: (value) { validator: (value) {
if (value == null || if (value == null ||
value.length != 13 || value.length != 13 ||
int.tryParse(value) == null || int.tryParse(value) == null) {
int.parse(value) < 0) {
return "L'entrée n'est pas un code EAN-13 valide"; return "L'entrée n'est pas un code EAN-13 valide";
} }
return null; return null;
}, },
), ),
SizedBox(height: 10),
ListenableBuilder(
listenable: widget.viewModel,
builder: (context, child) {
return (widget.viewModel.askPrice)
? TextFormField(
decoration: InputDecoration(
labelText: "Prix",
border: OutlineInputBorder(),
suffixText: "",
),
keyboardType: TextInputType.numberWithOptions(
decimal: true,
),
validator: (value) {
if (value == null || value.isEmpty) {
return "Indiquez un prix";
} else if (num.tryParse(value) == null) {
return "Le prix doit être un nombre";
} else if (num.parse(value) < 0) {
return "Le prix doit être positif ou nul";
}
return null;
},
onSaved: (newValue) {
price = num.parse(newValue!);
},
)
: SizedBox();
},
),
], ],
), ),
), ),
actions: [ actions: [
TextButton( TextButton(
child: Text("Annuler"),
onPressed: () { onPressed: () {
widget.exitPopup(context); widget.exitPopup(context);
}, },
child: Text("Annuler"),
), ),
TextButton( TextButton(
child: Text("Valider"),
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();
widget.scanEan( widget.exitPopup(context);
context,
widget.viewModel,
ean!,
widget.scannerController,
leaveLastPopup: (context) {
Navigator.of(context).pop();
},
);
} }
}, },
child: Text("Valider"),
), ),
], ],
); );

View file

@ -6,11 +6,11 @@ class OwnerPopup extends StatefulWidget {
const OwnerPopup({ const OwnerPopup({
super.key, super.key,
required this.viewModel, required this.viewModel,
required this.exitPopup, required this.onPressAccept,
}); });
final AddViewModel viewModel; final AddViewModel viewModel;
final Function(BuildContext) exitPopup; final Function(BuildContext) onPressAccept;
@override @override
State<OwnerPopup> createState() => _OwnerPopupState(); State<OwnerPopup> createState() => _OwnerPopupState();
@ -166,7 +166,7 @@ class _OwnerPopupState extends State<OwnerPopup> {
}); });
} }
} }
widget.exitPopup(context); widget.onPressAccept(context);
}, },
child: Text( child: Text(
(!showNewOwner && searchController.text == "") (!showNewOwner && searchController.text == "")

View file

@ -13,10 +13,8 @@ class LoginViewModel extends ChangeNotifier {
final AuthRepository _authRepository; final AuthRepository _authRepository;
/// Command to login with added capabilities
late Command1 login; late Command1 login;
/// Logins the user with credentials [(String username, String password)]
Future<Result<void>> _login((String, String) credentials) async { Future<Result<void>> _login((String, String) credentials) async {
final (username, password) = credentials; final (username, password) = credentials;
final result = await _authRepository.login(username, password); final result = await _authRepository.login(username, password);
@ -29,12 +27,10 @@ class LoginViewModel extends ChangeNotifier {
* ================================= * =================================
*/ */
/// Loads all necessary data
late final Command0 load; late final Command0 load;
bool isLoaded = false; bool isLoaded = false;
bool isUpToDate = false; bool isUpToDate = false;
/// Manages loaders
Future<Result<void>> _load() async { Future<Result<void>> _load() async {
final result1 = await _loadApiVersion(); final result1 = await _loadApiVersion();
switch (result1) { switch (result1) {
@ -48,7 +44,6 @@ class LoginViewModel extends ChangeNotifier {
return result1; return result1;
} }
/// Loads the current remote api version and compares to local hardcoded [apiVersion]
Future<Result<void>> _loadApiVersion() async { Future<Result<void>> _loadApiVersion() async {
final result = await _authRepository.getRemoteApiVersion(); final result = await _authRepository.getRemoteApiVersion();
switch (result) { switch (result) {

View file

@ -14,7 +14,7 @@ import 'package:seshat/utils/result.dart';
class BalViewModel extends ChangeNotifier { class BalViewModel extends ChangeNotifier {
BalViewModel({ BalViewModel({
required BalRepository balRepository, required BalRepository balRepository,
required this.selectedBalId, required this.id,
required OwnerRepository ownerRepository, required OwnerRepository ownerRepository,
}) : _balRepository = balRepository, }) : _balRepository = balRepository,
_ownerRepository = ownerRepository { _ownerRepository = ownerRepository {
@ -30,26 +30,18 @@ class BalViewModel extends ChangeNotifier {
* ===================== * =====================
*/ */
/// Selected [Bal] Bal? _bal;
Bal? _selectedBal; int id;
Bal? get bal => _bal;
/// Selected [Bal]
Bal? get selectedBal => _selectedBal;
/// Selected [Bal.id] from path parameters
int selectedBalId;
/// Is one of the [Bal] [BalState.ongoing]
bool isABalOngoing = false; bool isABalOngoing = false;
/// Stops a [Bal] Future<Result<void>> stopBal(int id) async {
Future<Result<void>> stopBal(int balId) async {
isLoaded = false; isLoaded = false;
notifyListeners(); notifyListeners();
final result = await _balRepository.stopBal(balId); final result = await _balRepository.stopBal(id);
switch (result) { switch (result) {
case Ok(): case Ok():
_selectedBal = result.value; _bal = result.value;
break; break;
default: default:
} }
@ -65,15 +57,14 @@ class BalViewModel extends ChangeNotifier {
return result; return result;
} }
/// Starts a [Bal] Future<Result<void>> startBal(int id) async {
Future<Result<void>> startBal(int balId) async {
if (isABalOngoing) { if (isABalOngoing) {
return Result.error(Exception("Cannot have multiple BALs ongoing !")); return Result.error(Exception("Cannot have multiple BALs ongoing !"));
} }
final result = await _balRepository.startBal(balId); final result = await _balRepository.startBal(id);
switch (result) { switch (result) {
case Ok(): case Ok():
_selectedBal = result.value; _bal = result.value;
notifyListeners(); notifyListeners();
break; break;
default: default:
@ -81,20 +72,21 @@ class BalViewModel extends ChangeNotifier {
return result; return result;
} }
/// Edits a [Bal]'s [name], [startTime] or [endTime]
Future<Result<void>> editBal( Future<Result<void>> editBal(
int id, int id,
String name, String name,
DateTime startTime, DateTime start,
DateTime endTime, DateTime end,
) async { ) async {
final result = await _balRepository.editBal(id, name, startTime, endTime); final result = await _balRepository.editBal(id, name, start, end);
switch (result) { switch (result) {
case Ok(): case Ok():
_selectedBal = result.value; debugPrint("\n\n\n\nDID EDIT\n\n\n\n");
_bal = result.value;
notifyListeners(); notifyListeners();
break; break;
case Error(): case Error():
debugPrint("\n\n\n\nERROR: ${result.error}");
break; break;
} }
return result; return result;
@ -107,16 +99,11 @@ class BalViewModel extends ChangeNotifier {
*/ */
// Specific to ended state // Specific to ended state
/// Owners a book or money is owed to
List<ReturnOwner>? owedToOwners; List<ReturnOwner>? owedToOwners;
double? totalOwed;
/// Statistics about the [_selectedBal]
BalStats? stats; BalStats? stats;
/// Froms [books], updates [owners] to include all the necessary information Future<void> applyAccountingOwners(
/// See [the api doc](https://bal.ueauvergne.fr/docs/#/bal-api/get_bal_accounting) for more details
Future<void> _updateOwedToOwnersWithBooks(
List<ReturnOwner> owners, List<ReturnOwner> owners,
Map<String, Book> books, Map<String, Book> books,
) async { ) async {
@ -137,20 +124,15 @@ class BalViewModel extends ChangeNotifier {
} }
} }
/// Returns either Books, Money or All ([ReturnType]) to an [Owner]
Future<Result<void>> returnById(ReturnType type, int ownerId) async { Future<Result<void>> returnById(ReturnType type, int ownerId) async {
final result = await _balRepository.returnToId( final result = await _balRepository.returnToId(id, ownerId, type);
selectedBalId, final result2 = await _balRepository.getAccounting(id);
ownerId,
type,
);
final result2 = await _balRepository.getAccounting(selectedBalId);
switch (result2) { switch (result2) {
case Ok(): case Ok():
_updateOwedToOwnersWithBooks(result2.value.owners, result2.value.books); applyAccountingOwners(result2.value.owners, result2.value.books);
break; break;
case Error(): case Error():
break; debugPrint(result2.error.toString());
} }
notifyListeners(); notifyListeners();
return result; return result;
@ -162,25 +144,22 @@ class BalViewModel extends ChangeNotifier {
* ================================= * =================================
*/ */
/// Loads all the necessary information
late final Command0 load; late final Command0 load;
bool isLoaded = false; bool isLoaded = false;
/// Manages loaders
Future<Result<void>> _load() async { Future<Result<void>> _load() async {
isABalOngoing = _balRepository.isABalOngoing(); isABalOngoing = _balRepository.isABalOngoing();
final result1 = await _loadBal(); final result1 = await _loadBal();
switch (result1) { switch (result1) {
case Ok(): case Ok():
isLoaded = isLoaded = (_bal == null || _bal?.state != BalState.ended)
(_selectedBal == null || _selectedBal?.state != BalState.ended)
? true ? true
: false; : false;
break; break;
default: default:
break; break;
} }
if (_selectedBal?.state == BalState.ended) { if (_bal?.state == BalState.ended) {
final result2 = await _loadEnded(); final result2 = await _loadEnded();
switch (result2) { switch (result2) {
case Ok(): case Ok():
@ -194,12 +173,11 @@ class BalViewModel extends ChangeNotifier {
return result1; return result1;
} }
/// Loads all common [Bal] information
Future<Result<void>> _loadBal() async { Future<Result<void>> _loadBal() async {
final result = await _balRepository.balById(selectedBalId); final result = await _balRepository.balById(id);
switch (result) { switch (result) {
case Ok(): case Ok():
_selectedBal = result.value; _bal = result.value;
break; break;
case Error(): case Error():
break; break;
@ -208,16 +186,15 @@ class BalViewModel extends ChangeNotifier {
return result; return result;
} }
/// Loads [Bal] information when it is [BalState.ended]
Future<Result<void>> _loadEnded() async { Future<Result<void>> _loadEnded() async {
final result = await _balRepository.getAccountingNoCache(selectedBalId); final result = await _balRepository.getAccountingNoCache(id);
switch (result) { switch (result) {
case Ok(): case Ok():
_updateOwedToOwnersWithBooks(result.value.owners, result.value.books); applyAccountingOwners(result.value.owners, result.value.books);
break; break;
default: default:
} }
final result2 = await _balRepository.getBalStats(selectedBalId); final result2 = await _balRepository.getBalStats(id);
switch (result2) { switch (result2) {
case Ok(): case Ok():
stats = result2.value; stats = result2.value;

View file

@ -27,14 +27,14 @@ class _BalPageState extends State<BalPage> {
bottomNavigationBar: AppNavigationBar(startIndex: 0), bottomNavigationBar: AppNavigationBar(startIndex: 0),
body: AwaitLoading(), body: AwaitLoading(),
), ),
true => switch (widget.viewModel.selectedBal == null) { true => switch (widget.viewModel.bal == null) {
true => Scaffold( true => Scaffold(
bottomNavigationBar: AppNavigationBar(startIndex: 0), bottomNavigationBar: AppNavigationBar(startIndex: 0),
body: Center( body: Center(
child: Text("La BAL référencée n'est pas accessible"), child: Text("La BAL référencée n'est pas accessible"),
), ),
), ),
false => switch (widget.viewModel.selectedBal!.state) { false => switch (widget.viewModel.bal!.state) {
BalState.pending => BalPendingScreen(viewModel: widget.viewModel), BalState.pending => BalPendingScreen(viewModel: widget.viewModel),
BalState.ongoing => BalOngoingScreen(viewModel: widget.viewModel), BalState.ongoing => BalOngoingScreen(viewModel: widget.viewModel),
BalState.ended => BalEndedScreen(viewModel: widget.viewModel), BalState.ended => BalEndedScreen(viewModel: widget.viewModel),

View file

@ -30,15 +30,12 @@ class _BalEndedScreenState extends State<BalEndedScreen>
return Scaffold( return Scaffold(
bottomNavigationBar: AppNavigationBar(startIndex: 0), bottomNavigationBar: AppNavigationBar(startIndex: 0),
appBar: AppBar( appBar: AppBar(
title: Text(widget.viewModel.selectedBal!.name), title: Text(widget.viewModel.bal!.name),
bottom: TabBar( bottom: TabBar(
controller: tabController, controller: tabController,
tabs: [ tabs: [
Tab(text: "Statistiques"), Tab(text: "Statistiques"),
Tab( Tab(text: "À rendre"),
text:
"À rendre (${widget.viewModel.owedToOwners?.length.toString() ?? "0"})",
),
], ],
), ),
), ),

View file

@ -11,7 +11,7 @@ class BalOngoingScreen extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
bottomNavigationBar: AppNavigationBar(startIndex: 0), bottomNavigationBar: AppNavigationBar(startIndex: 0),
appBar: AppBar(title: Text(viewModel.selectedBal!.name)), appBar: AppBar(title: Text(viewModel.bal!.name)),
body: Padding( body: Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: Column( child: Column(
@ -38,9 +38,7 @@ class BalOngoingScreen extends StatelessWidget {
), ),
TextButton( TextButton(
onPressed: () async { onPressed: () async {
await viewModel.stopBal( await viewModel.stopBal(viewModel.bal!.id);
viewModel.selectedBal!.id,
);
if (context.mounted) { if (context.mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }

View file

@ -15,7 +15,7 @@ class BalPendingScreen extends StatelessWidget {
return Scaffold( return Scaffold(
bottomNavigationBar: AppNavigationBar(startIndex: 0), bottomNavigationBar: AppNavigationBar(startIndex: 0),
appBar: AppBar( appBar: AppBar(
title: Text(viewModel.selectedBal!.name), title: Text(viewModel.bal!.name),
actions: [ actions: [
IconButton( IconButton(
onPressed: () { onPressed: () {
@ -57,9 +57,7 @@ class BalPendingScreen extends StatelessWidget {
), ),
TextButton( TextButton(
onPressed: () async { onPressed: () async {
await viewModel.startBal( await viewModel.startBal(viewModel.bal!.id);
viewModel.selectedBal!.id,
);
if (context.mounted) { if (context.mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
@ -98,8 +96,8 @@ class _EditPopup extends State<EditPopup> {
firstDate: DateTime(DateTime.now().year - 1), firstDate: DateTime(DateTime.now().year - 1),
lastDate: DateTime(DateTime.now().year + 2), lastDate: DateTime(DateTime.now().year + 2),
initialDateRange: DateTimeRange( initialDateRange: DateTimeRange(
start: start ?? widget.viewModel.selectedBal!.startTime, start: start ?? widget.viewModel.bal!.startTime,
end: end ?? widget.viewModel.selectedBal!.endTime, end: end ?? widget.viewModel.bal!.endTime,
), ),
); );
@ -128,7 +126,7 @@ class _EditPopup extends State<EditPopup> {
labelText: "Nom de la BAL", labelText: "Nom de la BAL",
border: OutlineInputBorder(), border: OutlineInputBorder(),
), ),
initialValue: widget.viewModel.selectedBal!.name, initialValue: widget.viewModel.bal!.name,
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return "Veuillez entrer un nom"; return "Veuillez entrer un nom";
@ -171,7 +169,7 @@ class _EditPopup extends State<EditPopup> {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();
final Bal bal = widget.viewModel.selectedBal!; final Bal bal = widget.viewModel.bal!;
final result = await widget.viewModel.editBal( final result = await widget.viewModel.editBal(
bal.id, bal.id,

View file

@ -18,25 +18,18 @@ class HomeViewModel extends ChangeNotifier {
* ================= * =================
*/ */
/// [List<Bal>] of all [Bal]
List<Bal> _bals = []; List<Bal> _bals = [];
/// [List<Bal>] of all [Bal]
List<Bal> get bals => _bals; List<Bal> get bals => _bals;
/// [Bal] currently [BalState.ongoing] Bal? _currentBal;
Bal? _ongoingBal; Bal? get currentBal => _currentBal;
/// [Bal] currently [BalState.ongoing]
Bal? get ongoingBal => _ongoingBal;
/// Creates a [Bal] from its [name], [startTime] and [endTime]
Future<Result<void>> createBal( Future<Result<void>> createBal(
String name, String name,
DateTime startTime, DateTime start,
DateTime endTime, DateTime end,
) async { ) async {
final result = await _balRepository.addBal(name, startTime, endTime); final result = await _balRepository.addBal(name, start, end);
switch (result) { switch (result) {
case Ok(): case Ok():
final result2 = await _balRepository.getBals(); final result2 = await _balRepository.getBals();
@ -61,11 +54,9 @@ class HomeViewModel extends ChangeNotifier {
* ================================= * =================================
*/ */
/// Command to load all necessary data
late final Command0 load; late final Command0 load;
bool isLoaded = false; bool isLoaded = false;
/// Manages loaders
Future<Result<void>> _load() async { Future<Result<void>> _load() async {
final result2 = await _loadBal(); final result2 = await _loadBal();
switch (result2) { switch (result2) {
@ -79,13 +70,12 @@ class HomeViewModel extends ChangeNotifier {
return result2; return result2;
} }
/// Loads data about [Bal]
Future<Result<void>> _loadBal() async { Future<Result<void>> _loadBal() async {
final result = await _balRepository.getBals(); final result = await _balRepository.getBals();
switch (result) { switch (result) {
case Ok(): case Ok():
_bals = result.value..sort((a, b) => a.compareTo(b)); _bals = result.value..sort((a, b) => a.compareTo(b));
_ongoingBal = _bals _currentBal = _bals
.where((bal) => bal.state == BalState.ongoing) .where((bal) => bal.state == BalState.ongoing)
.firstOrNull; .firstOrNull;
break; break;

View file

@ -38,7 +38,7 @@ class _HomePageState extends State<HomePage> {
: ListView( : ListView(
children: [ children: [
for (Bal bal in widget.viewModel.bals.where( for (Bal bal in widget.viewModel.bals.where(
(el) => el.id != widget.viewModel.ongoingBal?.id, (el) => el.id != widget.viewModel.currentBal?.id,
)) ))
Padding( Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
@ -81,20 +81,20 @@ class _HomePageState extends State<HomePage> {
], ],
), ),
), ),
switch (widget.viewModel.ongoingBal == null) { switch (widget.viewModel.currentBal == null) {
true => SizedBox(), true => SizedBox(),
false => Padding( false => Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0), padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Card( child: Card(
child: ListTile( child: ListTile(
leading: Icon(Icons.event_available), leading: Icon(Icons.event_available),
title: Text(widget.viewModel.ongoingBal!.name), title: Text(widget.viewModel.currentBal!.name),
subtitle: Text("BAL en cours"), subtitle: Text("BAL en cours"),
trailing: IconButton( trailing: IconButton(
onPressed: () { onPressed: () {
_moveToBal( _moveToBal(
context, context,
widget.viewModel.ongoingBal!.id, widget.viewModel.currentBal!.id,
); );
}, },
icon: Icon(Icons.arrow_forward), icon: Icon(Icons.arrow_forward),

View file

@ -31,13 +31,10 @@ class SellViewModel extends ChangeNotifier {
final BookRepository _bookRepository; final BookRepository _bookRepository;
final OwnerRepository _ownerRepository; final OwnerRepository _ownerRepository;
/// Wether to show the scan screen bool _showScan = false;
bool _showScanScreen = false; bool get showScan => _showScan;
set showScan(bool newValue) {
/// Wether to show the scan screen _showScan = newValue;
bool get showScanScreen => _showScanScreen;
set showScanScreen(bool newValue) {
_showScanScreen = newValue;
notifyListeners(); notifyListeners();
} }
@ -47,66 +44,56 @@ class SellViewModel extends ChangeNotifier {
* =============================== * ===============================
*/ */
/// Books in the sell final List<BookStack> _soldBooks = [];
final List<BookStack> _booksInSell = []; List<BookStack> get soldBooks => _soldBooks;
/// Books in the sell
List<BookStack> get booksInSell => _booksInSell;
/// Books scanned on the scan screen
final List<BookStack> _scannedBooks = []; final List<BookStack> _scannedBooks = [];
/// Books scanned on the scan screen
List<BookStack> get scannedBooks => _scannedBooks; List<BookStack> get scannedBooks => _scannedBooks;
bool isScanLoaded = false; bool isScanLoaded = false;
bool isSendingSell = false; bool isSendingSell = false;
double minimumAmountToPay = 0; double minimumAmount = 0;
/// Adds a book to the [_booksInSell] void sellBook(BookStack addedBook) {
void addBookToSell(BookStack bookToAdd) { minimumAmount += addedBook.instance.price;
minimumAmountToPay += bookToAdd.instance.price; _soldBooks.add(addedBook);
_booksInSell.add(bookToAdd);
notifyListeners(); notifyListeners();
} }
/// Sends the sell void sendSell(double givenAmount) async {
void sendSell(double givenMoney) async {
isSendingSell = true; isSendingSell = true;
notifyListeners(); notifyListeners();
List<BookInstance> booksToSend = []; List<BookInstance> toSend = [];
int numberOfPL = 0; int nbOfPl = 0;
for (BookStack book in _booksInSell) { for (BookStack book in _soldBooks) {
if (book.instance.price != 0) { if (book.instance.price != 0) {
book.instance.soldPrice = book.instance.price; book.instance.soldPrice = book.instance.price;
givenMoney -= book.instance.price; givenAmount -= book.instance.price;
booksToSend.add(book.instance); toSend.add(book.instance);
} else { } else {
numberOfPL++; nbOfPl++;
} }
} }
if (numberOfPL != 0) { if (nbOfPl != 0) {
double moneyPerPL = givenMoney / numberOfPL; double amountPerPl = givenAmount / nbOfPl;
for (BookStack book in _booksInSell) { for (BookStack book in _soldBooks) {
if (book.instance.price == 0) { if (book.instance.price == 0) {
book.instance.soldPrice = moneyPerPL; book.instance.soldPrice = amountPerPl;
booksToSend.add(book.instance); toSend.add(book.instance);
} }
} }
} }
await _bookInstanceRepository.sellBooks(booksToSend); await _bookInstanceRepository.sellBooks(toSend);
_booksInSell.clear(); _soldBooks.clear();
isSendingSell = false; isSendingSell = false;
notifyListeners(); notifyListeners();
} }
/// Removes a book from the sell void deleteBook(int id) {
void removeBookFromSell(int bookId) { _soldBooks.removeWhere((book) => book.instance.id == id);
_booksInSell.removeWhere((book) => book.instance.id == bookId);
notifyListeners(); notifyListeners();
} }
/// Search a book by [title] or [author]
Future<void> searchBook(String title, String author) async { Future<void> searchBook(String title, String author) async {
Bal? bal = await _balRepository.ongoingBal(); Bal? bal = await _balRepository.ongoingBal();
isScanLoaded = false; isScanLoaded = false;
@ -119,21 +106,15 @@ class SellViewModel extends ChangeNotifier {
); );
switch (result) { switch (result) {
case Ok(): case Ok():
// For each result value, you need to complete some values
for (SearchResult searchResult in result.value) { for (SearchResult searchResult in result.value) {
// In case you get a book that's actually not available
if (searchResult.instance.available == false) { if (searchResult.instance.available == false) {
continue; continue;
} }
if (_soldBooks
// In case the instance is already in the sell
if (_booksInSell
.where((book) => book.instance.id == searchResult.instance.id) .where((book) => book.instance.id == searchResult.instance.id)
.isNotEmpty) { .isNotEmpty) {
continue; continue;
} }
// Search for the owner
Owner owner; Owner owner;
final result2 = await _ownerRepository.getOwnerById( final result2 = await _ownerRepository.getOwnerById(
searchResult.instance.ownerId, searchResult.instance.ownerId,
@ -145,7 +126,6 @@ class SellViewModel extends ChangeNotifier {
case Error(): case Error():
continue; continue;
} }
_scannedBooks.add( _scannedBooks.add(
BookStack(searchResult.book, searchResult.instance, owner), BookStack(searchResult.book, searchResult.instance, owner),
); );
@ -160,19 +140,18 @@ class SellViewModel extends ChangeNotifier {
return; return;
} }
/// Gets [BookInstance]s from its ean in a [barcode]
Future<void> scanBook(BarcodeCapture barcode) async { Future<void> scanBook(BarcodeCapture barcode) async {
isScanLoaded = false; isScanLoaded = false;
int ean = int.parse(barcode.barcodes.first.rawValue!); int ean = int.parse(barcode.barcodes.first.rawValue!);
Bal? ongoingBal = await _balRepository.ongoingBal(); Bal? bal = await _balRepository.ongoingBal();
_scannedBooks.clear(); _scannedBooks.clear();
final result1 = await _bookInstanceRepository.getByEan(ongoingBal!.id, ean); final result = await _bookInstanceRepository.getByEan(bal!.id, ean);
switch (result1) { switch (result) {
case Ok(): case Ok():
Book book; Book book;
final result2 = await _bookRepository.getBookById( final result2 = await _bookRepository.getBookById(
result1.value.first.bookId, result.value.first.bookId,
); );
switch (result2) { switch (result2) {
case Ok(): case Ok():
@ -181,22 +160,15 @@ class SellViewModel extends ChangeNotifier {
case Error(): case Error():
return; 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) { if (instance.available == false) {
continue; continue;
} }
if (_soldBooks
// In case the instance is already in the sell
if (_booksInSell
.where((book) => book.instance.id == instance.id) .where((book) => book.instance.id == instance.id)
.isNotEmpty) { .isNotEmpty) {
continue; continue;
} }
// Search for the owner
Owner owner; Owner owner;
final result3 = await _ownerRepository.getOwnerById(instance.ownerId); final result3 = await _ownerRepository.getOwnerById(instance.ownerId);
switch (result3) { switch (result3) {
@ -206,7 +178,6 @@ class SellViewModel extends ChangeNotifier {
case Error(): case Error():
continue; continue;
} }
_scannedBooks.add(BookStack(book, instance, owner)); _scannedBooks.add(BookStack(book, instance, owner));
} }
break; break;
@ -225,11 +196,8 @@ class SellViewModel extends ChangeNotifier {
* ================= * =================
*/ */
/// The currently ongoing [Bal] Bal? _currentBal;
Bal? _ongoingBal; get currentBal => _currentBal;
/// The currently ongoing [Bal]
get ongoingBal => _ongoingBal;
/* /*
* ================================= * =================================
@ -237,11 +205,9 @@ class SellViewModel extends ChangeNotifier {
* ================================= * =================================
*/ */
/// Command to load necessary data
late final Command0 load; late final Command0 load;
bool isLoaded = false; bool isLoaded = false;
/// Manages loaders
Future<Result<void>> _load() async { Future<Result<void>> _load() async {
final result1 = await _loadBal(); final result1 = await _loadBal();
switch (result1) { switch (result1) {
@ -255,12 +221,11 @@ class SellViewModel extends ChangeNotifier {
return result1; return result1;
} }
/// Loads information about [Bal]
Future<Result<void>> _loadBal() async { Future<Result<void>> _loadBal() async {
final result = await _balRepository.getBals(); final result = await _balRepository.getBals();
switch (result) { switch (result) {
case Ok(): case Ok():
_ongoingBal = result.value _currentBal = result.value
.where((bal) => bal.state == BalState.ongoing) .where((bal) => bal.state == BalState.ongoing)
.firstOrNull; .firstOrNull;
break; break;

View file

@ -65,7 +65,7 @@ class _ScanScreenState extends State<ScanScreen> {
children: [ children: [
IconButton( IconButton(
onPressed: () { onPressed: () {
widget.viewModel.showScanScreen = false; widget.viewModel.showScan = false;
}, },
icon: Icon(Icons.arrow_back), icon: Icon(Icons.arrow_back),
), ),

View file

@ -34,9 +34,9 @@ class SellChoicePopup extends StatelessWidget {
child: Card( child: Card(
child: InkWell( child: InkWell(
onTap: () { onTap: () {
viewModel.addBookToSell(book); viewModel.sellBook(book);
Navigator.of(context).pop(); Navigator.of(context).pop();
viewModel.showScanScreen = false; viewModel.showScan = false;
}, },
child: ListTile( child: ListTile(
leading: Text( leading: Text(

View file

@ -28,7 +28,7 @@ class _SellPageState extends State<SellPage> {
builder: (context, child) { builder: (context, child) {
return switch (widget.viewModel.isLoaded) { return switch (widget.viewModel.isLoaded) {
false => AwaitLoading(), false => AwaitLoading(),
true => switch (widget.viewModel.ongoingBal) { true => switch (widget.viewModel.currentBal) {
null => Center( null => Center(
child: SizedBox( child: SizedBox(
width: 300, width: 300,
@ -80,7 +80,7 @@ class _SellPageState extends State<SellPage> {
? Center(child: Text("Aucun")) ? Center(child: Text("Aucun"))
: SizedBox(), : SizedBox(),
for (BookStack book for (BookStack book
in widget.viewModel.booksInSell) in widget.viewModel.soldBooks)
Padding( Padding(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
horizontal: 15, horizontal: 15,
@ -99,10 +99,9 @@ class _SellPageState extends State<SellPage> {
), ),
trailing: IconButton( trailing: IconButton(
onPressed: () { onPressed: () {
widget.viewModel widget.viewModel.deleteBook(
.removeBookFromSell( book.instance.id,
book.instance.id, );
);
}, },
icon: Icon(Icons.delete), icon: Icon(Icons.delete),
), ),
@ -114,7 +113,7 @@ class _SellPageState extends State<SellPage> {
), ),
SizedBox(height: 40), SizedBox(height: 40),
Text( Text(
"Montant minimum à payer : ${widget.viewModel.minimumAmountToPay.toString()}", "Montant minimum à payer : ${widget.viewModel.minimumAmount.toString()}",
), ),
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 60.0), padding: const EdgeInsets.symmetric(horizontal: 60.0),
@ -168,7 +167,7 @@ class _SellPageState extends State<SellPage> {
} else if (double.parse( } else if (double.parse(
price.text.replaceFirst(",", "."), price.text.replaceFirst(",", "."),
) < ) <
widget.viewModel.minimumAmountToPay) { widget.viewModel.minimumAmount) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text( content: Text(
@ -203,7 +202,7 @@ class _SellPageState extends State<SellPage> {
SizedBox(width: 70), SizedBox(width: 70),
IconButton( IconButton(
onPressed: () { onPressed: () {
widget.viewModel.showScanScreen = true; widget.viewModel.showScan = true;
}, },
icon: Icon(Icons.add), icon: Icon(Icons.add),
style: ButtonStyle( style: ButtonStyle(
@ -217,7 +216,7 @@ class _SellPageState extends State<SellPage> {
], ],
), ),
), ),
(widget.viewModel.showScanScreen) (widget.viewModel.showScan)
? ScanScreen(viewModel: widget.viewModel) ? ScanScreen(viewModel: widget.viewModel)
: SizedBox(), : SizedBox(),
], ],