Compare commits
29 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b62d9781a | |||
| 6bcc3a7e88 | |||
|
|
dad000a1b9 | ||
|
|
59e1c2558c | ||
|
|
6359efa0c3 | ||
|
|
0d1b5ce68e | ||
|
|
8188fc61c9 | ||
|
|
ce7338db2a | ||
|
|
50074890c0 | ||
|
|
ec26aa873c | ||
|
|
892cd03f79 | ||
|
|
609af329e3 | ||
|
|
025afd3bed | ||
|
|
ebeab2db94 | ||
|
|
e5d383548d | ||
|
|
b18339e441 | ||
|
|
9144ef74d7 | ||
|
|
cbe51a8566 | ||
|
|
7ca31c676c | ||
|
|
594c54930e | ||
|
|
82236a83ad | ||
|
|
7695f4d563 | ||
|
|
08e986f55e | ||
|
|
77a2ce129a | ||
|
|
aa3fda9c88 | ||
|
|
587cc0689a | ||
|
|
8a464cc665 | ||
|
|
73e907ef7e | ||
|
|
f53f6dd697 |
34 changed files with 1066 additions and 618 deletions
18
.forgejo/workflows/deploy.yml
Normal file
18
.forgejo/workflows/deploy.yml
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [published]
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: docker
|
||||||
|
steps:
|
||||||
|
- run: apt update && apt install -y sshpass jq
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Set up Flutter
|
||||||
|
uses: https://github.com/subosito/flutter-action@v2
|
||||||
|
with:
|
||||||
|
channel: stable
|
||||||
|
flutter-version: 3.35.1
|
||||||
|
cache: true
|
||||||
|
- run: git config --global --add safe.directory /opt/hostedtoolcache/flutter/stable-3.35.1-x64
|
||||||
|
- run: flutter build web --release --wasm --base-href /app/
|
||||||
|
- run: sshpass -p "${{ secrets.DEPLOY_PASSWORD }}" scp -o StrictHostKeyChecking=accept-new -rp build/web/* ${{ secrets.DEPLOY_USERNAME }}@${{ secrets.DEPLOY_ADDRESS }}:${{ secrets.DEPLOY_PATH }}
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
> [!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.
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ android {
|
||||||
applicationId = "fr.ueauvergne.seshat"
|
applicationId = "fr.ueauvergne.seshat"
|
||||||
// You can update the following values to match your application needs.
|
// You can update the following values to match your application needs.
|
||||||
// For more information, see: https://flutter.dev/to/review-gradle-config.
|
// For more information, see: https://flutter.dev/to/review-gradle-config.
|
||||||
minSdk = flutter.minSdkVersion
|
minSdkVersion(24)
|
||||||
targetSdk = flutter.targetSdkVersion
|
targetSdk = flutter.targetSdkVersion
|
||||||
versionCode = flutter.versionCode
|
versionCode = flutter.versionCode
|
||||||
versionName = flutter.versionName
|
versionName = flutter.versionName
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -2,6 +2,7 @@ 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;
|
||||||
|
|
||||||
|
|
@ -9,6 +10,7 @@ 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!;
|
||||||
|
|
@ -25,6 +27,7 @@ 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);
|
||||||
|
|
@ -33,13 +36,14 @@ class AuthRepository extends ChangeNotifier {
|
||||||
_isAuthenticated = true;
|
_isAuthenticated = true;
|
||||||
return Result.ok(());
|
return Result.ok(());
|
||||||
case Error():
|
case Error():
|
||||||
return Result.error(result.error);
|
return result;
|
||||||
}
|
}
|
||||||
} 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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,19 @@ 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>? _bals;
|
|
||||||
Accounting? accounting;
|
|
||||||
|
|
||||||
|
/// [List<Bal>] of all the user's [Bal]
|
||||||
|
List<Bal>? _bals;
|
||||||
|
|
||||||
|
/// [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!);
|
||||||
|
|
@ -26,6 +32,7 @@ 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) {
|
||||||
|
|
@ -37,15 +44,16 @@ class BalRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> balById(int id) async {
|
/// Gets a [Bal] by [balId], either from cache or remote
|
||||||
|
Future<Result<Bal>> balById(int balId) async {
|
||||||
if (_bals == null) {
|
if (_bals == null) {
|
||||||
await getBals();
|
await getBals();
|
||||||
}
|
}
|
||||||
Bal? bal = _bals!.where((bal) => bal.id == id).firstOrNull;
|
Bal? bal = _bals!.where((bal) => bal.id == balId).firstOrNull;
|
||||||
if (bal != null) {
|
if (bal != null) {
|
||||||
return Result.ok(bal);
|
return Result.ok(bal);
|
||||||
}
|
}
|
||||||
final result = await _apiClient.getBalById(id);
|
final result = await _apiClient.getBalById(balId);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
return Result.ok(result.value);
|
return Result.ok(result.value);
|
||||||
|
|
@ -54,11 +62,13 @@ 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();
|
||||||
|
|
@ -66,12 +76,14 @@ 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(
|
||||||
|
|
@ -83,52 +95,62 @@ 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 start,
|
DateTime startTime,
|
||||||
DateTime end,
|
DateTime endTime,
|
||||||
) async {
|
) async {
|
||||||
final result = await _apiClient.editBal(id, name, start, end);
|
final result = await _apiClient.editBal(id, name, startTime, endTime);
|
||||||
await _getBalsNoCache();
|
await _getBalsNoCache();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<void>> addBal(String name, DateTime start, DateTime end) async {
|
/// Creates a [Bal] from its [name], [startTime] and [endTime]
|
||||||
final result = await _apiClient.addBal(name, start, end);
|
Future<Result<void>> addBal(
|
||||||
|
String name,
|
||||||
|
DateTime startTime,
|
||||||
|
DateTime endTime,
|
||||||
|
) async {
|
||||||
|
final result = await _apiClient.addBal(name, startTime, endTime);
|
||||||
await _getBalsNoCache();
|
await _getBalsNoCache();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<BalStats>> getBalStats(int id) async {
|
/// Gets a [BalStats] from its [balId]
|
||||||
return _apiClient.getBalStats(id);
|
Future<Result<BalStats>> getBalStats(int balId) async {
|
||||||
|
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():
|
||||||
accounting = result.value;
|
_accountingMap[balId] = 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 (accounting != null) {
|
if (_accountingMap[balId] != null) {
|
||||||
return Result.ok(accounting!);
|
return Result.ok(_accountingMap[balId]!);
|
||||||
}
|
}
|
||||||
final result = await _apiClient.getAccounting(balId);
|
final result = await _apiClient.getAccounting(balId);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
accounting = result.value;
|
_accountingMap[balId] = 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,
|
||||||
|
|
@ -139,26 +161,32 @@ class BalRepository {
|
||||||
case Ok():
|
case Ok():
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ReturnType.books:
|
case ReturnType.books:
|
||||||
final owner = accounting?.owners
|
final owner = _accountingMap[balId]?.owners
|
||||||
.where((el) => el.ownerId == ownerId)
|
.where((el) => el.ownerId == ownerId)
|
||||||
.firstOrNull;
|
.firstOrNull;
|
||||||
if (owner?.owedMoney == 0) {
|
if (owner?.owedMoney == 0) {
|
||||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
_accountingMap[balId]?.owners.removeWhere(
|
||||||
|
(el) => el.ownerId == ownerId,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
owner?.owed = [];
|
owner?.owed = [];
|
||||||
owner?.owedInstances = [];
|
owner?.owedInstances = [];
|
||||||
break;
|
break;
|
||||||
case ReturnType.money:
|
case ReturnType.money:
|
||||||
final owner = accounting?.owners
|
final owner = _accountingMap[balId]?.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) {
|
||||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
_accountingMap[balId]?.owners.removeWhere(
|
||||||
|
(el) => el.ownerId == ownerId,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
owner?.owedMoney = 0;
|
owner?.owedMoney = 0;
|
||||||
break;
|
break;
|
||||||
case ReturnType.all:
|
case ReturnType.all:
|
||||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
_accountingMap[balId]?.owners.removeWhere(
|
||||||
|
(el) => el.ownerId == ownerId,
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,19 @@ 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,
|
||||||
|
|
@ -24,15 +27,17 @@ class BookInstanceRepository {
|
||||||
return await _apiClient.getBookInstanceBySearch(balId, title, author);
|
return await _apiClient.getBookInstanceBySearch(balId, title, author);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<BookInstance>> sendBook(
|
/// Sends a new [BookInstance]'s [book], [owner], [bal] and [price]
|
||||||
|
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.sendBook(book, owner, bal, price);
|
return await _apiClient.sendNewBookInstance(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) {
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,19 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Book>> getBookById(int id) async {
|
/// Gets a [Book] by its [bookId]
|
||||||
return await _apiClient.getBookById(id);
|
Future<Result<Book>> getBookById(int bookId) async {
|
||||||
|
return await _apiClient.getBookById(bookId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ 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,
|
||||||
|
|
@ -14,18 +15,25 @@ class OwnerRepository {
|
||||||
|
|
||||||
final ApiClient _apiClient;
|
final ApiClient _apiClient;
|
||||||
final WebsocketClient _wsClient;
|
final WebsocketClient _wsClient;
|
||||||
late final StreamSubscription sub;
|
|
||||||
List<Owner>? _cachedOwners;
|
|
||||||
Owner? _sectionOwner;
|
|
||||||
|
|
||||||
Future<Result<Owner>> get sectionOwner async {
|
/// [StreamSubscription] to the [Stream<Owner>] for [_wsClient]
|
||||||
if (_sectionOwner != null) {
|
late final StreamSubscription sub;
|
||||||
return Result.ok(_sectionOwner!);
|
|
||||||
|
/// [List<Owner>] of owners, updated by [_wsClient]
|
||||||
|
List<Owner>? _cachedOwners;
|
||||||
|
|
||||||
|
/// [Owner] of the current user
|
||||||
|
Owner? _ownerOfUser;
|
||||||
|
|
||||||
|
/// [Owner] of the current user
|
||||||
|
Future<Result<Owner>> get ownerOfUser async {
|
||||||
|
if (_ownerOfUser != null) {
|
||||||
|
return Result.ok(_ownerOfUser!);
|
||||||
}
|
}
|
||||||
final result = await _apiClient.getSectionOwner();
|
final result = await _apiClient.getOwnerOfUser();
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
_sectionOwner = result.value;
|
_ownerOfUser = result.value;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
@ -33,16 +41,17 @@ class OwnerRepository {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Owner>> getOwnerById(int id) async {
|
/// Gets an [Owner] from its [ownerId]
|
||||||
|
Future<Result<Owner>> getOwnerById(int ownerId) async {
|
||||||
if (_cachedOwners != null) {
|
if (_cachedOwners != null) {
|
||||||
final result1 = _cachedOwners!
|
final result1 = _cachedOwners!
|
||||||
.where((owner) => owner.id == id)
|
.where((owner) => owner.id == ownerId)
|
||||||
.firstOrNull;
|
.firstOrNull;
|
||||||
if (result1 != null) {
|
if (result1 != null) {
|
||||||
return Result.ok(result1);
|
return Result.ok(result1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return await _apiClient.getOwnerById(id);
|
return await _apiClient.getOwnerById(ownerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds an [Owner] to the database, and gets the resulting [Owner].
|
/// Adds an [Owner] to the database, and gets the resulting [Owner].
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,17 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
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:seshat/config/constants.dart';
|
import 'package:seshat/config/constants.dart';
|
||||||
import 'package:seshat/domain/models/accounting.dart';
|
import 'package:seshat/domain/models/accounting.dart';
|
||||||
import 'package:seshat/domain/models/bal.dart';
|
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 {
|
||||||
|
|
@ -22,22 +20,33 @@ extension StringExtension on String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef AuthHeaderProvider = String? Function();
|
/// API Client to manage all authenticated REST routes
|
||||||
|
|
||||||
class ApiClient {
|
class ApiClient {
|
||||||
ApiClient({String? host, int? port});
|
ApiClient();
|
||||||
|
|
||||||
late final Command0 load;
|
/// JWT for registration
|
||||||
String? token;
|
String? token;
|
||||||
bool isReady = false;
|
|
||||||
FlutterSecureStorage? _secureStorage;
|
|
||||||
|
|
||||||
|
/// Readiness of the API Client
|
||||||
|
bool isReady = false;
|
||||||
|
|
||||||
|
/// Storage to access JWT
|
||||||
|
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),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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 {
|
||||||
|
|
@ -53,47 +62,62 @@ 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";
|
||||||
|
log.i("Fetching: getAccounting ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bal/$balId/accounting"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Accounting.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 403:
|
||||||
return Result.ok(Accounting.fromJSON(json));
|
throw "You don't own the specified BAL";
|
||||||
} else {
|
case 404:
|
||||||
throw "Unknown error";
|
throw "No BAL with this is exists in database";
|
||||||
|
default:
|
||||||
|
throw "Unknown error of 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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 =
|
||||||
|
"https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}";
|
||||||
|
log.i("Fetching: returnToId ($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 = jsonEncode({"return_type": type.capitalize()});
|
final body = jsonEncode({"return_type": type.capitalize()});
|
||||||
debugPrint(body);
|
|
||||||
final response = await client.post(
|
final response = await client.post(
|
||||||
Uri.parse(
|
Uri.parse(url),
|
||||||
"https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}",
|
|
||||||
),
|
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: body,
|
body: body,
|
||||||
);
|
);
|
||||||
if (response.statusCode == 200) {
|
switch (response.statusCode) {
|
||||||
return Result.ok(response);
|
case 200:
|
||||||
} else {
|
return Result.ok(response);
|
||||||
throw "Unknown error ${response.statusCode.toString()}";
|
case 403:
|
||||||
|
throw "You don't own the specified BAL or owner";
|
||||||
|
case 404:
|
||||||
|
throw "No BAL or owner with this id exists in database";
|
||||||
|
case 409:
|
||||||
|
throw "Books and money have already been returned, or there was nothing to return";
|
||||||
|
default:
|
||||||
|
throw "Unknown error of code ${response.statusCode.toString()}";
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint(e.toString());
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
|
|
@ -106,200 +130,233 @@ class ApiClient {
|
||||||
* =================
|
* =================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Future<Result<BalStats>> getBalStats(int id) async {
|
/// Get stats about a BAL's performance
|
||||||
|
Future<Result<BalStats>> getBalStats(int balId) async {
|
||||||
|
final url = "https://$apiBasePath/bal/${balId.toString()}/stats";
|
||||||
|
log.i("Fetching: getBalStats ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}/stats"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(BalStats.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 403:
|
||||||
return Result.ok(BalStats.fromJSON(json));
|
throw "You don't own the specified BAL";
|
||||||
} else {
|
case 404:
|
||||||
throw "Unknown error";
|
throw "No BAL with this id exists in the database";
|
||||||
|
case 409:
|
||||||
|
throw "The specified BAL is not ended yet";
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> stopBal(int id) async {
|
/// Stops a BAL, putting it's [BalState] to [BalState.ended]
|
||||||
|
Future<Result<Bal>> stopBal(int balId) async {
|
||||||
|
final url = "https://$apiBasePath/bal/${balId.toString()}/stop";
|
||||||
|
log.i("Fetching: stopBal ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.post(
|
final response = await client.post(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}/stop"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Bal.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 403:
|
||||||
return Result.ok(Bal.fromJSON(json));
|
throw "You don't own the specified BAL";
|
||||||
} else if (response.statusCode == 403) {
|
case 404:
|
||||||
throw "You don't own the specified BAL";
|
throw "No BAL with specified ID found";
|
||||||
} else if (response.statusCode == 404) {
|
case 409:
|
||||||
throw "No BAL with specified ID found";
|
throw "Selected BAL was not on ongoing state";
|
||||||
} else if (response.statusCode == 409) {
|
default:
|
||||||
throw "Selected BAL was not on ongoing state";
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
} else {
|
|
||||||
throw "Unknown error";
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> startBal(int id) async {
|
/// Starts a BAL, putting it's [BalState] to [BalState.ongoing]
|
||||||
|
Future<Result<Bal>> startBal(int balId) async {
|
||||||
|
final url = "https://$apiBasePath/bal/${balId.toString()}/start";
|
||||||
|
log.i("Fetching: startBal ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.post(
|
final response = await client.post(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}/start"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Bal.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 403:
|
||||||
return Result.ok(Bal.fromJSON(json));
|
throw "You don't own the specified BAL";
|
||||||
} else if (response.statusCode == 403) {
|
case 404:
|
||||||
throw "You don't own the specified BAL";
|
throw "No BAL with specified ID found";
|
||||||
} else if (response.statusCode == 404) {
|
case 409:
|
||||||
throw "No BAL with specified ID found";
|
throw "Cannot have multiple BAl ongoing at the same time!";
|
||||||
} else if (response.statusCode == 409) {
|
default:
|
||||||
throw "Cannot have multiple BAl ongoing at the same time!";
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
} else {
|
|
||||||
throw "Unknown error";
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Changes the information about a [Bal], such as its [name], [startTime] or [endTime]
|
||||||
Future<Result<Bal>> editBal(
|
Future<Result<Bal>> editBal(
|
||||||
int id,
|
int balId,
|
||||||
String name,
|
String name,
|
||||||
DateTime start,
|
DateTime startTime,
|
||||||
DateTime end,
|
DateTime endTime,
|
||||||
) async {
|
) async {
|
||||||
|
final url = "https://$apiBasePath/bal/${balId.toString()}";
|
||||||
|
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": (start.millisecondsSinceEpoch / 1000).round(),
|
"start_timestamp": (startTime.millisecondsSinceEpoch / 1000).round(),
|
||||||
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
|
"end_timestamp": (endTime.millisecondsSinceEpoch / 1000).round(),
|
||||||
};
|
};
|
||||||
final response = await client.patch(
|
final response = await client.patch(
|
||||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}"),
|
Uri.parse(url),
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: jsonEncode(body),
|
body: jsonEncode(body),
|
||||||
);
|
);
|
||||||
if (response.statusCode == 200) {
|
switch (response.statusCode) {
|
||||||
final json = jsonDecode(response.body);
|
case 200:
|
||||||
return Result.ok(Bal.fromJSON(json));
|
final json = jsonDecode(response.body);
|
||||||
} else {
|
return Result.ok(Bal.fromJSON(json));
|
||||||
throw Exception("Something went wrong");
|
case 403:
|
||||||
|
throw "You don't own the specified BAL";
|
||||||
|
case 404:
|
||||||
|
throw "No bal with specified id";
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> getBalById(int id) async {
|
/// Gets a [Bal] from it's [balId]
|
||||||
|
Future<Result<Bal>> getBalById(int balId) async {
|
||||||
|
final url = "https://$apiBasePath/bal/${balId.toString()}";
|
||||||
|
log.i("Fetching: getBalById ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Bal.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 403:
|
||||||
return Result.ok(Bal.fromJSON(json));
|
throw "You don't own the specified BAL";
|
||||||
} else if (response.statusCode == 403) {
|
case 404:
|
||||||
throw Exception("You don't own the specified bal");
|
throw "No BAL with this id found in database";
|
||||||
} else {
|
default:
|
||||||
return Result.error(
|
throw "Unknown error";
|
||||||
Exception("No bal wirth this id exists the database"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) async {
|
/// Adds a [Bal] from it's [name], [startTime] and [endTime]
|
||||||
|
Future<Result<Bal>> addBal(
|
||||||
|
String name,
|
||||||
|
DateTime startTime,
|
||||||
|
DateTime endTime,
|
||||||
|
) async {
|
||||||
|
final url = "https://$apiBasePath/bal";
|
||||||
|
log.i("Fetching: addBal ($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": (start.millisecondsSinceEpoch / 1000).round(),
|
"start_timestamp": (startTime.millisecondsSinceEpoch / 1000).round(),
|
||||||
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
|
"end_timestamp": (endTime.millisecondsSinceEpoch / 1000).round(),
|
||||||
};
|
};
|
||||||
final response = await client.post(
|
final response = await client.post(
|
||||||
Uri.parse("https://$apiBasePath/bal"),
|
Uri.parse(url),
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: jsonEncode(body),
|
body: jsonEncode(body),
|
||||||
);
|
);
|
||||||
if (response.statusCode == 201) {
|
switch (response.statusCode) {
|
||||||
final json = jsonDecode(response.body);
|
case 201:
|
||||||
return Result.ok(Bal.fromJSON(json));
|
final json = jsonDecode(response.body);
|
||||||
} else {
|
return Result.ok(Bal.fromJSON(json));
|
||||||
throw Exception("Something went wrong");
|
case 400:
|
||||||
|
throw "Time cannot go backwards";
|
||||||
|
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 a [List<Bal>] of all [Bal]
|
||||||
Future<Result<List<Bal>>> getBals() async {
|
Future<Result<List<Bal>>> getBals() async {
|
||||||
|
final url = "https://$apiBasePath/bals";
|
||||||
|
log.i("Fetching: getBals ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bals"),
|
|
||||||
headers: headers,
|
|
||||||
);
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
final json = jsonDecode(response.body) as List<dynamic>;
|
|
||||||
debugPrint("\n\n\n\nRECEIVED : $json\n\n\n\n");
|
|
||||||
debugPrint(
|
|
||||||
"\n\n\n\nFORMATTED : ${json.map((element) => Bal.fromJSON(element)).toList()}\n\n\n\n",
|
|
||||||
);
|
|
||||||
|
|
||||||
return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
|
switch (response.statusCode) {
|
||||||
} else {
|
case 200:
|
||||||
throw Exception("Something wrong happened");
|
final json = jsonDecode(response.body) as List<dynamic>;
|
||||||
|
return Result.ok(
|
||||||
|
json.map((element) => Bal.fromJSON(element)).toList(),
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint("ERROR: ${e.toString()}");
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Bal?>> getCurrentBal() async {
|
/// Gets the ongoing BAL for the user
|
||||||
|
Future<Result<Bal?>> getOngoingBal() async {
|
||||||
|
final url = "https://$apiBasePath/bal/current";
|
||||||
|
log.i("Fetching: getCurrentBal ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/bal/current"),
|
|
||||||
headers: headers,
|
|
||||||
);
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final json = jsonDecode(response.body);
|
final json = jsonDecode(response.body);
|
||||||
return Result.ok(Bal.fromJSON(json));
|
return Result.ok(Bal.fromJSON(json));
|
||||||
|
|
@ -321,42 +378,50 @@ class ApiClient {
|
||||||
* ===================
|
* ===================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Future<Result<Book>> getBookById(int id) async {
|
/// Gets a [Book] by its [bookId]
|
||||||
|
Future<Result<Book>> getBookById(int bookId) async {
|
||||||
|
final url = "https://$apiBasePath/book/id/${bookId.toString()}";
|
||||||
|
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(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/book/id/${id.toString()}"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Book.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 404:
|
||||||
return Result.ok(Book.fromJSON(json));
|
throw "No book with this id exists in database";
|
||||||
} else {
|
default:
|
||||||
throw Exception("The book was not found");
|
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";
|
||||||
|
log.i("Fetching: getBookByEan ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/book/ean/$ean"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Book.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 404:
|
||||||
return Result.ok(Book.fromJSON(json));
|
throw "No book with this EAN found in the database of BNF";
|
||||||
} else {
|
default:
|
||||||
throw Exception("The book was not found");
|
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();
|
||||||
|
|
@ -369,93 +434,118 @@ 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,
|
||||||
String author,
|
String author,
|
||||||
) async {
|
) async {
|
||||||
|
final url = "https://$apiBasePath/bal/${balId.toString()}/search";
|
||||||
|
log.i("Fetching: getBookInstancesBySearch ($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 = jsonEncode({"title": title, "author": author});
|
final body = jsonEncode({"title": title, "author": author});
|
||||||
debugPrint("\n\n\n\n$body\n\n\n\n");
|
|
||||||
final response = await client.post(
|
final response = await client.post(
|
||||||
Uri.parse("https://$apiBasePath/bal/${balId.toString()}/search"),
|
Uri.parse(url),
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: body,
|
body: body,
|
||||||
);
|
);
|
||||||
if (response.statusCode == 200) {
|
switch (response.statusCode) {
|
||||||
final json = jsonDecode(response.body) as List<dynamic>;
|
case 200:
|
||||||
debugPrint("\n\n\n\nJSON : $json\n\n\n\n");
|
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) {
|
||||||
debugPrint("\n\n\n\nERROR: ${e.toString()}\n\n\n\n");
|
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,
|
||||||
) async {
|
) async {
|
||||||
|
final url =
|
||||||
|
"https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances";
|
||||||
|
log.i("Fetching: getBookInstancesByEan ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse(
|
switch (response.statusCode) {
|
||||||
"https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances",
|
case 200:
|
||||||
),
|
final json = jsonDecode(response.body) as List<dynamic>;
|
||||||
headers: headers,
|
return Result.ok(
|
||||||
);
|
json.map((el) => BookInstance.fromJSON(el)).toList(),
|
||||||
if (response.statusCode == 200) {
|
);
|
||||||
final json = jsonDecode(response.body) as List<dynamic>;
|
case 403:
|
||||||
return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList());
|
throw "You do not own the BAL";
|
||||||
} 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("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";
|
||||||
|
log.i("Fetching: sellBooks ($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"});
|
||||||
debugPrint("\n\n\n\nMAP: $books\n\n\n\n");
|
|
||||||
final body = jsonEncode(books);
|
final body = jsonEncode(books);
|
||||||
debugPrint("\n\n\n\nSENT: $body\n\n\n\n");
|
|
||||||
final response = await client.post(
|
final response = await client.post(
|
||||||
Uri.parse("https://$apiBasePath/book_instance/sell/bulk"),
|
Uri.parse(url),
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: body,
|
body: body,
|
||||||
);
|
);
|
||||||
if (response.statusCode == 200) {
|
switch (response.statusCode) {
|
||||||
return Result.ok(response.statusCode);
|
case 200:
|
||||||
} else {
|
return Result.ok(response.statusCode);
|
||||||
throw "Unknown error";
|
case 403:
|
||||||
|
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) {
|
||||||
debugPrint("\n\n\n\nERROR : ${e.toString()}\n\n\n\n");
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<BookInstance>> sendBook(
|
/// Creates a new [BookInstance] from it's [book], it's [owner], it's [bal] and it's [price]
|
||||||
|
Future<Result<BookInstance>> sendNewBookInstance(
|
||||||
Book book,
|
Book book,
|
||||||
Owner owner,
|
Owner owner,
|
||||||
Bal bal,
|
Bal bal,
|
||||||
double price,
|
double price,
|
||||||
) async {
|
) async {
|
||||||
|
final url = "https://$apiBasePath/book_instance";
|
||||||
|
log.i("Fetching: sendBook ($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"});
|
||||||
|
|
@ -466,17 +556,18 @@ class ApiClient {
|
||||||
"price": price,
|
"price": price,
|
||||||
});
|
});
|
||||||
final response = await client.post(
|
final response = await client.post(
|
||||||
Uri.parse("https://$apiBasePath/book_instance"),
|
Uri.parse(url),
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: body,
|
body: body,
|
||||||
);
|
);
|
||||||
if (response.statusCode == 201) {
|
switch (response.statusCode) {
|
||||||
final json = jsonDecode(response.body);
|
case 201:
|
||||||
return Result.ok(BookInstance.fromJSON(json));
|
final json = jsonDecode(response.body);
|
||||||
} else if (response.statusCode == 403) {
|
return Result.ok(BookInstance.fromJSON(json));
|
||||||
throw Exception("You don't own that book instance");
|
case 403:
|
||||||
} else {
|
throw "You don't own that book instance";
|
||||||
throw Exception("Something wrong happened");
|
default:
|
||||||
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
|
|
@ -491,64 +582,72 @@ class ApiClient {
|
||||||
* ====================
|
* ====================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Future<Result<Owner>> getOwnerById(int id) async {
|
/// Gets an [Owner] by it's [ownerId]
|
||||||
|
Future<Result<Owner>> getOwnerById(int ownerId) async {
|
||||||
|
final url = "https://$apiBasePath/owner/${ownerId.toString()}";
|
||||||
|
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(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/owner/${id.toString()}"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Owner.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
case 403:
|
||||||
return Result.ok(Owner.fromJSON(json));
|
throw "You do not own specified owner";
|
||||||
} else {
|
case 404:
|
||||||
throw Exception("The owner was not found");
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<Owner>> getSectionOwner() async {
|
/// Get the owner of the current user
|
||||||
|
Future<Result<Owner>> getOwnerOfUser() async {
|
||||||
|
final url = "https://$apiBasePath/owner/self";
|
||||||
|
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(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/owner/self"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body);
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(Owner.fromJSON(json));
|
||||||
final json = jsonDecode(response.body);
|
default:
|
||||||
return Result.ok(Owner.fromJSON(json));
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
} else {
|
|
||||||
throw "Unknown error";
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Call on `/owners` to get a list of all [Owner]s
|
/// Get a [List<Owner>] of all [Owner]
|
||||||
Future<Result<List<Owner>>> getOwners() async {
|
Future<Result<List<Owner>>> getOwners() async {
|
||||||
|
final url = "https://$apiBasePath/owners";
|
||||||
|
log.i("Fetching: getOwners ($url)");
|
||||||
final client = Client();
|
final client = Client();
|
||||||
try {
|
try {
|
||||||
final headers = await _getHeaders();
|
final headers = await _getHeaders();
|
||||||
final response = await client.get(
|
final response = await client.get(Uri.parse(url), headers: headers);
|
||||||
Uri.parse("https://$apiBasePath/owners"),
|
switch (response.statusCode) {
|
||||||
headers: headers,
|
case 200:
|
||||||
);
|
final json = jsonDecode(response.body) as List<dynamic>;
|
||||||
if (response.statusCode == 200) {
|
return Result.ok(
|
||||||
final json = jsonDecode(response.body) as List<dynamic>;
|
json.map((element) => Owner.fromJSON(element)).toList(),
|
||||||
return Result.ok(
|
);
|
||||||
json.map((element) => Owner.fromJSON(element)).toList(),
|
default:
|
||||||
);
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
} else {
|
|
||||||
throw Exception("Invalid request");
|
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
|
|
@ -557,12 +656,14 @@ class ApiClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds an owner to the database
|
/// Adds an [Owner] from its [firstName], [lastName] and [contact]
|
||||||
Future<Result<Owner>> addOwner(
|
Future<Result<Owner>> addOwner(
|
||||||
String firstName,
|
String firstName,
|
||||||
String lastName,
|
String lastName,
|
||||||
String contact,
|
String contact,
|
||||||
) async {
|
) async {
|
||||||
|
final url = "https://$apiBasePath/owner";
|
||||||
|
log.i("Fetching: addOwner ($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"});
|
||||||
|
|
@ -572,18 +673,20 @@ class ApiClient {
|
||||||
"contact": contact,
|
"contact": contact,
|
||||||
};
|
};
|
||||||
final response = await client.post(
|
final response = await client.post(
|
||||||
Uri.parse("https://$apiBasePath/owner"),
|
Uri.parse(url),
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: jsonEncode(body),
|
body: jsonEncode(body),
|
||||||
);
|
);
|
||||||
if (response.statusCode == 201) {
|
switch (response.statusCode) {
|
||||||
final json = jsonDecode(response.body);
|
case 201:
|
||||||
return Result.ok(Owner.fromJSON(json));
|
final json = jsonDecode(response.body);
|
||||||
} else {
|
return Result.ok(Owner.fromJSON(json));
|
||||||
throw Exception("Invalid request");
|
default:
|
||||||
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} catch (e) {
|
||||||
return Result.error(error);
|
log.e(e.toString());
|
||||||
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,43 @@
|
||||||
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" as http;
|
import "package:http/http.dart";
|
||||||
|
|
||||||
|
/// API Client to manage all unauthenticated REST routes
|
||||||
class AuthClient {
|
class AuthClient {
|
||||||
AuthClient();
|
AuthClient();
|
||||||
FlutterSecureStorage? _secureStorage;
|
|
||||||
|
|
||||||
|
/// Storage to access JWT
|
||||||
|
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 url = Uri.parse("https://$apiBasePath/token-check");
|
var response = await client.post(
|
||||||
var response = await http.post(
|
Uri.parse(url),
|
||||||
url,
|
|
||||||
headers: {"Content-Type": "application/json"},
|
headers: {"Content-Type": "application/json"},
|
||||||
body: jsonEncode({"token": token}),
|
body: jsonEncode({"token": token}),
|
||||||
);
|
);
|
||||||
|
|
@ -34,52 +48,64 @@ class AuthClient {
|
||||||
}
|
}
|
||||||
return Result.ok(false);
|
return Result.ok(false);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return Result.error(Exception(e));
|
log.e(e.toString());
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<Result<String>> login(String username, String password) async {
|
|
||||||
var client = http.Client();
|
|
||||||
try {
|
|
||||||
await _initStore();
|
|
||||||
var url = Uri.parse("https://$apiBasePath/auth");
|
|
||||||
var response = await client.post(
|
|
||||||
url,
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Accept": "application/json",
|
|
||||||
},
|
|
||||||
body: jsonEncode({"password": password, "username": username}),
|
|
||||||
);
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
var json = jsonDecode(response.body);
|
|
||||||
await _secureStorage!.write(key: "token", value: json["access_token"]);
|
|
||||||
return Result.ok(json["access_token"]);
|
|
||||||
} else if (response.statusCode == 401) {
|
|
||||||
return Result.error(Exception("Wrong credentials"));
|
|
||||||
} else {
|
|
||||||
return Result.error(Exception("Token creation error"));
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<int>> getRemoteApiVersion() async {
|
/// Logs a user in from its [username] and [password]
|
||||||
final client = http.Client();
|
Future<Result<String>> login(String username, String password) async {
|
||||||
|
final url = "https://$apiBasePath/auth";
|
||||||
|
log.i("Logging in: $url");
|
||||||
|
var client = Client();
|
||||||
try {
|
try {
|
||||||
final response = await client.get(
|
await _initStore();
|
||||||
Uri.parse("https://$apiBasePath/version"),
|
var response = await client.post(
|
||||||
|
Uri.parse(url),
|
||||||
|
headers: {"Content-Type": "application/json"},
|
||||||
|
body: jsonEncode({"password": password, "username": username}),
|
||||||
);
|
);
|
||||||
if (response.statusCode == 200) {
|
switch (response.statusCode) {
|
||||||
final json = jsonDecode(response.body) as int;
|
case 200:
|
||||||
return Result.ok(json);
|
var json = jsonDecode(response.body);
|
||||||
} else {
|
await _secureStorage!.write(
|
||||||
throw "Something wrong happened";
|
key: "token",
|
||||||
|
value: json["access_token"],
|
||||||
|
);
|
||||||
|
return Result.ok(json["access_token"]);
|
||||||
|
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));
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the API version of the server
|
||||||
|
Future<Result<int>> getRemoteApiVersion() async {
|
||||||
|
final url = "https://$apiBasePath/version";
|
||||||
|
log.i("Fetching: getRemoteApiVersion ($url)");
|
||||||
|
final client = Client();
|
||||||
|
try {
|
||||||
|
final response = await client.get(Uri.parse(url));
|
||||||
|
switch (response.statusCode) {
|
||||||
|
case 200:
|
||||||
|
final json = jsonDecode(response.body) as int;
|
||||||
|
return Result.ok(json);
|
||||||
|
default:
|
||||||
|
throw "Unknown error with code ${response.statusCode.toString()}";
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.e(e.toString());
|
||||||
return Result.error(Exception(e));
|
return Result.error(Exception(e));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
|
|
|
||||||
|
|
@ -2,33 +2,55 @@ 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 {
|
||||||
WebSocketChannel? _channel;
|
/// Storage to access JWT
|
||||||
FlutterSecureStorage? _secureStorage;
|
FlutterSecureStorage? _secureStorage;
|
||||||
|
|
||||||
|
/// Raw channel of data from WebSocket
|
||||||
|
WebSocketChannel? _channel;
|
||||||
|
|
||||||
|
/// 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,
|
||||||
);
|
);
|
||||||
late final StreamSubscription sub;
|
|
||||||
|
|
||||||
|
/// Subscription to [_baseController]
|
||||||
|
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("wss://$apiBasePath/ws"));
|
_channel = WebSocketChannel.connect(Uri.parse(url));
|
||||||
|
|
||||||
await _channel!.ready;
|
await _channel!.ready;
|
||||||
var token = await _secureStorage!.read(key: "token");
|
var token = await _secureStorage!.read(key: "token");
|
||||||
|
|
@ -60,11 +82,13 @@ 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();
|
||||||
|
|
|
||||||
|
|
@ -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(),
|
||||||
id: int.parse(state.pathParameters["id"] ?? ""),
|
selectedBalId: int.parse(state.pathParameters["id"] ?? ""),
|
||||||
ownerRepository: context.read(),
|
ownerRepository: context.read(),
|
||||||
);
|
);
|
||||||
return NoTransitionPage(child: BalPage(viewModel: viewModel));
|
return NoTransitionPage(child: BalPage(viewModel: viewModel));
|
||||||
|
|
@ -72,11 +72,6 @@ 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,
|
||||||
|
|
|
||||||
|
|
@ -38,18 +38,29 @@ 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? _sectionOwner;
|
/// Owner of the current user
|
||||||
Owner? get sectionOwner => _sectionOwner;
|
Owner? _ownerOfUser;
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
|
@ -85,8 +96,11 @@ class AddViewModel extends ChangeNotifier {
|
||||||
* =================
|
* =================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Bal? _currentBal;
|
/// Ongoing [Bal]
|
||||||
Bal? get currentBal => _currentBal;
|
Bal? _ongoingBal;
|
||||||
|
|
||||||
|
/// Ongoing [Bal]
|
||||||
|
Bal? get ongoingBal => _ongoingBal;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ===================
|
* ===================
|
||||||
|
|
@ -94,7 +108,10 @@ class AddViewModel extends ChangeNotifier {
|
||||||
* ===================
|
* ===================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/// Wether to ask for a price
|
||||||
bool _askPrice = true;
|
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;
|
||||||
|
|
@ -107,21 +124,25 @@ class AddViewModel extends ChangeNotifier {
|
||||||
* =================================
|
* =================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// Sends an api request with a [bacorde], then gets the [Book] that was
|
/// Retrieves the book associated with an ean through a [barcode]
|
||||||
/// either created or retrieved. Sens the [Book] back wrapped in a [Result].
|
Future<Result<Book>> scanBook(String ean) async {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<BookInstance>> sendBook(
|
/// Creates a new Book Instance from its [book], [owner], [bal] and [price]
|
||||||
|
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.sendBook(book, owner, bal, price);
|
return await _bookInstanceRepository.sendNewBookInstance(
|
||||||
|
book,
|
||||||
|
owner,
|
||||||
|
bal,
|
||||||
|
price,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -130,9 +151,11 @@ 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) {
|
||||||
|
|
@ -153,11 +176,12 @@ 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():
|
||||||
_currentBal = result.value
|
_ongoingBal = result.value
|
||||||
.where((bal) => bal.state == BalState.ongoing)
|
.where((bal) => bal.state == BalState.ongoing)
|
||||||
.firstOrNull;
|
.firstOrNull;
|
||||||
break;
|
break;
|
||||||
|
|
@ -168,6 +192,7 @@ 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) {
|
||||||
|
|
@ -182,10 +207,10 @@ class AddViewModel extends ChangeNotifier {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
final result2 = await _ownerRepository.sectionOwner;
|
final result2 = await _ownerRepository.ownerOfUser;
|
||||||
switch (result2) {
|
switch (result2) {
|
||||||
case Ok():
|
case Ok():
|
||||||
_sectionOwner = result2.value;
|
_ownerOfUser = result2.value;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,30 +22,27 @@ class AddPage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _AddPageState extends State<AddPage> {
|
class _AddPageState extends State<AddPage> {
|
||||||
num? price;
|
final MobileScannerController scannerController = MobileScannerController(
|
||||||
final MobileScannerController controller = MobileScannerController(
|
|
||||||
formats: [BarcodeFormat.ean13],
|
formats: [BarcodeFormat.ean13],
|
||||||
detectionTimeoutMs: 1000,
|
detectionTimeoutMs: 1000,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
controller.dispose();
|
scannerController.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.currentBal) {
|
true => switch (widget.viewModel.ongoingBal) {
|
||||||
null => Center(
|
null => Center(
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 300,
|
width: 300,
|
||||||
|
|
@ -78,51 +75,37 @@ class _AddPageState extends State<AddPage> {
|
||||||
children: [
|
children: [
|
||||||
ColoredBox(color: Colors.black),
|
ColoredBox(color: Colors.black),
|
||||||
MobileScanner(
|
MobileScanner(
|
||||||
controller: controller,
|
controller: scannerController,
|
||||||
onDetect: (barcodes) async {
|
onDetect: (barcodes) async {
|
||||||
|
if (barcodes.barcodes.isEmpty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (widget.viewModel.currentOwner == null) {
|
if (widget.viewModel.currentOwner == null) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
_showMissingOwnerSnackBar(
|
||||||
SnackBar(
|
context,
|
||||||
content: Text(
|
scannerController,
|
||||||
"Attention : vous devez choisir un·e propriétaire",
|
widget.viewModel,
|
||||||
),
|
|
||||||
behavior: SnackBarBehavior.floating,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPrice(num newPrice) async {
|
_scanEan(
|
||||||
setState(() {
|
context,
|
||||||
price = newPrice;
|
widget.viewModel,
|
||||||
});
|
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(
|
||||||
|
child: SvgPicture.asset(
|
||||||
|
'assets/scan-overlay.svg',
|
||||||
|
height: (MediaQuery.sizeOf(context).height / 5) * 2,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
SafeArea(
|
SafeArea(
|
||||||
child: SingleChildScrollView(
|
child: SingleChildScrollView(
|
||||||
child: Column(
|
child: Column(
|
||||||
|
|
@ -146,7 +129,7 @@ class _AddPageState extends State<AddPage> {
|
||||||
),
|
),
|
||||||
onPressed: () => _ownerDialogBuilder(
|
onPressed: () => _ownerDialogBuilder(
|
||||||
context,
|
context,
|
||||||
controller,
|
scannerController,
|
||||||
widget.viewModel,
|
widget.viewModel,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
@ -175,7 +158,7 @@ class _AddPageState extends State<AddPage> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Center(child: SvgPicture.asset('assets/scan-overlay.svg')),
|
|
||||||
SafeArea(
|
SafeArea(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
|
|
@ -187,11 +170,22 @@ class _AddPageState extends State<AddPage> {
|
||||||
theme.cardColor,
|
theme.cardColor,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onPressed: () => _formDialogBuilder(
|
onPressed: () {
|
||||||
context,
|
if (widget.viewModel.currentOwner == null) {
|
||||||
controller,
|
_showMissingOwnerSnackBar(
|
||||||
widget.viewModel,
|
context,
|
||||||
),
|
scannerController,
|
||||||
|
widget.viewModel,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_formDialogBuilder(
|
||||||
|
context,
|
||||||
|
scannerController,
|
||||||
|
widget.viewModel,
|
||||||
|
);
|
||||||
|
},
|
||||||
child: Text("Enregistrer manuellement"),
|
child: Text("Enregistrer manuellement"),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
@ -210,18 +204,71 @@ 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,
|
||||||
Function(num) setPrice,
|
MobileScannerController scannerController,
|
||||||
MobileScannerController controller,
|
|
||||||
AddViewModel viewModel,
|
AddViewModel viewModel,
|
||||||
Book book,
|
Book book,
|
||||||
) {
|
) {
|
||||||
controller.stop();
|
scannerController.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();
|
scannerController.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
return showDialog(
|
return showDialog(
|
||||||
|
|
@ -229,7 +276,6 @@ 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,
|
||||||
),
|
),
|
||||||
|
|
@ -243,6 +289,7 @@ 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();
|
||||||
|
|
@ -251,7 +298,12 @@ Future<void> _formDialogBuilder(
|
||||||
return showDialog(
|
return showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: false,
|
barrierDismissible: false,
|
||||||
builder: (context) => FormPopup(viewModel: viewModel, exitPopup: exitPopup),
|
builder: (context) => FormPopup(
|
||||||
|
viewModel: viewModel,
|
||||||
|
exitPopup: exitPopup,
|
||||||
|
scannerController: controller,
|
||||||
|
scanEan: _scanEan,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -262,7 +314,8 @@ Future<void> _ownerDialogBuilder(
|
||||||
) {
|
) {
|
||||||
controller.stop();
|
controller.stop();
|
||||||
|
|
||||||
void onPressAccept(BuildContext localContext) {
|
// Utility function to pass to downwards widgets
|
||||||
|
void exitPopup(BuildContext localContext) {
|
||||||
Navigator.of(localContext).pop();
|
Navigator.of(localContext).pop();
|
||||||
controller.start();
|
controller.start();
|
||||||
}
|
}
|
||||||
|
|
@ -271,6 +324,6 @@ Future<void> _ownerDialogBuilder(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: false,
|
barrierDismissible: false,
|
||||||
builder: (context) =>
|
builder: (context) =>
|
||||||
OwnerPopup(viewModel: viewModel, onPressAccept: onPressAccept),
|
OwnerPopup(viewModel: viewModel, exitPopup: exitPopup),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,11 @@ 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;
|
||||||
|
|
||||||
|
|
@ -24,9 +22,11 @@ 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,6 +72,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
SizedBox(height: 10),
|
SizedBox(height: 10),
|
||||||
(widget.viewModel.askPrice)
|
(widget.viewModel.askPrice)
|
||||||
? TextFormField(
|
? TextFormField(
|
||||||
|
|
@ -80,17 +81,28 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
suffixText: "€",
|
suffixText: "€",
|
||||||
),
|
),
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.numberWithOptions(
|
||||||
|
decimal: true,
|
||||||
|
signed: true,
|
||||||
|
),
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value == null || value.isEmpty) {
|
if (value == null || value.isEmpty) {
|
||||||
return "Indiquez un prix";
|
return "Indiquez un prix";
|
||||||
} else if (num.tryParse(value) == null) {
|
} else if (double.tryParse(
|
||||||
|
value.replaceAll(",", "."),
|
||||||
|
) ==
|
||||||
|
null) {
|
||||||
return "Le prix doit être un nombre";
|
return "Le prix doit être un nombre";
|
||||||
|
} else if (double.parse(value.replaceAll(",", ".")) <
|
||||||
|
0) {
|
||||||
|
return "Le prix doit être positif ou nul";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onSaved: (newValue) {
|
onSaved: (newValue) {
|
||||||
price = double.parse(newValue!);
|
price = double.parse(
|
||||||
|
newValue?.replaceAll(",", ".") ?? "0",
|
||||||
|
);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
: SizedBox(),
|
: SizedBox(),
|
||||||
|
|
@ -100,6 +112,7 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
TextButton(
|
TextButton(
|
||||||
|
child: Text("Annuler"),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
|
|
@ -109,19 +122,21 @@ 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.sendBook(
|
var result = await widget.viewModel.sendNewBookInstance(
|
||||||
widget.book,
|
widget.book,
|
||||||
widget.viewModel.currentOwner!,
|
widget.viewModel.currentOwner!,
|
||||||
widget.viewModel.currentBal!,
|
widget.viewModel.ongoingBal!,
|
||||||
price,
|
price,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -131,49 +146,61 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => AlertDialog(
|
barrierDismissible: false,
|
||||||
title: Text(
|
builder: (context) =>
|
||||||
"ID : ${widget.viewModel.currentOwner!.firstName[0].toUpperCase()}${widget.viewModel.currentOwner!.lastName[0].toUpperCase()}${(price == 0) ? "PL" : price.toString()}",
|
RegisteredBookPopup(widget: widget, price: price),
|
||||||
),
|
|
||||||
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(
|
SnackBar(content: Text("Erreur : ${result.error}")),
|
||||||
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),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
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 {
|
||||||
|
|
@ -6,10 +7,14 @@ 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) {
|
||||||
|
|
@ -31,6 +36,8 @@ class FormPopup extends StatelessWidget {
|
||||||
return _ManualEANPopup(
|
return _ManualEANPopup(
|
||||||
exitPopup: exitPopup,
|
exitPopup: exitPopup,
|
||||||
viewModel: viewModel,
|
viewModel: viewModel,
|
||||||
|
scannerController: scannerController,
|
||||||
|
scanEan: scanEan,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
@ -51,6 +58,7 @@ class FormPopup extends StatelessWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
Card(
|
Card(
|
||||||
clipBehavior: Clip.hardEdge,
|
clipBehavior: Clip.hardEdge,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
|
|
@ -93,11 +101,24 @@ class FormPopup extends StatelessWidget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ======================
|
||||||
|
* ====< MANUAL EAN >====
|
||||||
|
* ======================
|
||||||
|
*/
|
||||||
|
|
||||||
class _ManualEANPopup extends StatefulWidget {
|
class _ManualEANPopup extends StatefulWidget {
|
||||||
const _ManualEANPopup({required this.exitPopup, required this.viewModel});
|
const _ManualEANPopup({
|
||||||
|
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();
|
||||||
|
|
@ -106,11 +127,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("Recherche par EAN"),
|
title: Text("Entrée manuelle par EAN"),
|
||||||
content: Form(
|
content: Form(
|
||||||
key: _formKey,
|
key: _formKey,
|
||||||
child: Column(
|
child: Column(
|
||||||
|
|
@ -128,57 +149,39 @@ 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.number,
|
|
||||||
validator: (value) {
|
|
||||||
if (value == null || value.isEmpty) {
|
|
||||||
return "Indiquez un prix";
|
|
||||||
} else if (num.tryParse(value) == null) {
|
|
||||||
return "Le prix doit être un nombre";
|
|
||||||
}
|
|
||||||
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.exitPopup(context);
|
widget.scanEan(
|
||||||
|
context,
|
||||||
|
widget.viewModel,
|
||||||
|
ean!,
|
||||||
|
widget.scannerController,
|
||||||
|
leaveLastPopup: (context) {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: Text("Valider"),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
@ -273,12 +276,17 @@ class _FullyManualState extends State<_FullyManual> {
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
suffixText: "€",
|
suffixText: "€",
|
||||||
),
|
),
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.numberWithOptions(
|
||||||
|
decimal: true,
|
||||||
|
signed: true,
|
||||||
|
),
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value == null || value.isEmpty) {
|
if (value == null || value.isEmpty) {
|
||||||
return "Indiquez un prix";
|
return "Indiquez un prix";
|
||||||
} else if (num.tryParse(value) == null) {
|
} else if (num.tryParse(value) == null) {
|
||||||
return "Le prix doit être un nombre";
|
return "Le prix doit être un nombre";
|
||||||
|
} else if (num.parse(value) < 0) {
|
||||||
|
return "Le prix doit être positif ou nul";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,11 @@ class OwnerPopup extends StatefulWidget {
|
||||||
const OwnerPopup({
|
const OwnerPopup({
|
||||||
super.key,
|
super.key,
|
||||||
required this.viewModel,
|
required this.viewModel,
|
||||||
required this.onPressAccept,
|
required this.exitPopup,
|
||||||
});
|
});
|
||||||
|
|
||||||
final AddViewModel viewModel;
|
final AddViewModel viewModel;
|
||||||
final Function(BuildContext) onPressAccept;
|
final Function(BuildContext) exitPopup;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<OwnerPopup> createState() => _OwnerPopupState();
|
State<OwnerPopup> createState() => _OwnerPopupState();
|
||||||
|
|
@ -166,7 +166,7 @@ class _OwnerPopupState extends State<OwnerPopup> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
widget.onPressAccept(context);
|
widget.exitPopup(context);
|
||||||
},
|
},
|
||||||
child: Text(
|
child: Text(
|
||||||
(!showNewOwner && searchController.text == "")
|
(!showNewOwner && searchController.text == "")
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,10 @@ 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);
|
||||||
|
|
@ -27,10 +29,12 @@ 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) {
|
||||||
|
|
@ -44,6 +48,7 @@ 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) {
|
||||||
|
|
|
||||||
|
|
@ -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.id,
|
required this.selectedBalId,
|
||||||
required OwnerRepository ownerRepository,
|
required OwnerRepository ownerRepository,
|
||||||
}) : _balRepository = balRepository,
|
}) : _balRepository = balRepository,
|
||||||
_ownerRepository = ownerRepository {
|
_ownerRepository = ownerRepository {
|
||||||
|
|
@ -30,31 +30,50 @@ class BalViewModel extends ChangeNotifier {
|
||||||
* =====================
|
* =====================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Bal? _bal;
|
/// Selected [Bal]
|
||||||
int id;
|
Bal? _selectedBal;
|
||||||
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;
|
||||||
|
|
||||||
Future<Result<void>> stopBal(int id) async {
|
/// Stops a [Bal]
|
||||||
final result = await _balRepository.stopBal(id);
|
Future<Result<void>> stopBal(int balId) async {
|
||||||
|
isLoaded = false;
|
||||||
|
notifyListeners();
|
||||||
|
final result = await _balRepository.stopBal(balId);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
_bal = result.value;
|
_selectedBal = result.value;
|
||||||
notifyListeners();
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
final result2 = await _loadEnded();
|
||||||
|
switch (result2) {
|
||||||
|
case Ok():
|
||||||
|
isLoaded = true;
|
||||||
|
break;
|
||||||
|
case Error():
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
notifyListeners();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<void>> startBal(int id) async {
|
/// Starts a [Bal]
|
||||||
|
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(id);
|
final result = await _balRepository.startBal(balId);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
_bal = result.value;
|
_selectedBal = result.value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -62,21 +81,20 @@ 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 start,
|
DateTime startTime,
|
||||||
DateTime end,
|
DateTime endTime,
|
||||||
) async {
|
) async {
|
||||||
final result = await _balRepository.editBal(id, name, start, end);
|
final result = await _balRepository.editBal(id, name, startTime, endTime);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
debugPrint("\n\n\n\nDID EDIT\n\n\n\n");
|
_selectedBal = result.value;
|
||||||
_bal = result.value;
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
break;
|
break;
|
||||||
case Error():
|
case Error():
|
||||||
debugPrint("\n\n\n\nERROR: ${result.error}");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -89,11 +107,16 @@ 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;
|
||||||
|
|
||||||
Future<void> applyAccountingOwners(
|
/// Froms [books], updates [owners] to include all the necessary information
|
||||||
|
/// See [the api doc](https://bal.ueauvergne.fr/docs/#/bal-api/get_bal_accounting) for more details
|
||||||
|
Future<void> _updateOwedToOwnersWithBooks(
|
||||||
List<ReturnOwner> owners,
|
List<ReturnOwner> owners,
|
||||||
Map<String, Book> books,
|
Map<String, Book> books,
|
||||||
) async {
|
) async {
|
||||||
|
|
@ -114,15 +137,20 @@ class BalViewModel extends ChangeNotifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns either Books, Money or All ([ReturnType]) to an [Owner]
|
||||||
Future<Result<void>> returnById(ReturnType type, int ownerId) async {
|
Future<Result<void>> returnById(ReturnType type, int ownerId) async {
|
||||||
final result = await _balRepository.returnToId(id, ownerId, type);
|
final result = await _balRepository.returnToId(
|
||||||
final result2 = await _balRepository.getAccounting(id);
|
selectedBalId,
|
||||||
|
ownerId,
|
||||||
|
type,
|
||||||
|
);
|
||||||
|
final result2 = await _balRepository.getAccounting(selectedBalId);
|
||||||
switch (result2) {
|
switch (result2) {
|
||||||
case Ok():
|
case Ok():
|
||||||
applyAccountingOwners(result2.value.owners, result2.value.books);
|
_updateOwedToOwnersWithBooks(result2.value.owners, result2.value.books);
|
||||||
break;
|
break;
|
||||||
case Error():
|
case Error():
|
||||||
debugPrint(result2.error.toString());
|
break;
|
||||||
}
|
}
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -134,31 +162,31 @@ 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 = (_bal == null || _bal?.state != BalState.ended)
|
isLoaded =
|
||||||
|
(_selectedBal == null || _selectedBal?.state != BalState.ended)
|
||||||
? true
|
? true
|
||||||
: false;
|
: false;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
debugPrint("$isLoaded");
|
if (_selectedBal?.state == BalState.ended) {
|
||||||
if (_bal?.state == BalState.ended) {
|
|
||||||
final result2 = await _loadEnded();
|
final result2 = await _loadEnded();
|
||||||
debugPrint("Hello");
|
|
||||||
switch (result2) {
|
switch (result2) {
|
||||||
case Ok():
|
case Ok():
|
||||||
isLoaded = true;
|
isLoaded = true;
|
||||||
break;
|
break;
|
||||||
case Error():
|
case Error():
|
||||||
debugPrint("No ${result2.error}");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -166,11 +194,12 @@ 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(id);
|
final result = await _balRepository.balById(selectedBalId);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
_bal = result.value;
|
_selectedBal = result.value;
|
||||||
break;
|
break;
|
||||||
case Error():
|
case Error():
|
||||||
break;
|
break;
|
||||||
|
|
@ -179,15 +208,16 @@ 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(id);
|
final result = await _balRepository.getAccountingNoCache(selectedBalId);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
applyAccountingOwners(result.value.owners, result.value.books);
|
_updateOwedToOwnersWithBooks(result.value.owners, result.value.books);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
final result2 = await _balRepository.getBalStats(id);
|
final result2 = await _balRepository.getBalStats(selectedBalId);
|
||||||
switch (result2) {
|
switch (result2) {
|
||||||
case Ok():
|
case Ok():
|
||||||
stats = result2.value;
|
stats = result2.value;
|
||||||
|
|
|
||||||
|
|
@ -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.bal == null) {
|
true => switch (widget.viewModel.selectedBal == 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.bal!.state) {
|
false => switch (widget.viewModel.selectedBal!.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),
|
||||||
|
|
|
||||||
|
|
@ -30,12 +30,15 @@ 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.bal!.name),
|
title: Text(widget.viewModel.selectedBal!.name),
|
||||||
bottom: TabBar(
|
bottom: TabBar(
|
||||||
controller: tabController,
|
controller: tabController,
|
||||||
tabs: [
|
tabs: [
|
||||||
Tab(text: "Statistiques"),
|
Tab(text: "Statistiques"),
|
||||||
Tab(text: "À rendre"),
|
Tab(
|
||||||
|
text:
|
||||||
|
"À rendre (${widget.viewModel.owedToOwners?.length.toString() ?? "0"})",
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -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.bal!.name)),
|
appBar: AppBar(title: Text(viewModel.selectedBal!.name)),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(10.0),
|
padding: const EdgeInsets.all(10.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
|
|
@ -38,7 +38,9 @@ class BalOngoingScreen extends StatelessWidget {
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
await viewModel.stopBal(viewModel.bal!.id);
|
await viewModel.stopBal(
|
||||||
|
viewModel.selectedBal!.id,
|
||||||
|
);
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.bal!.name),
|
title: Text(viewModel.selectedBal!.name),
|
||||||
actions: [
|
actions: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
|
|
@ -57,7 +57,9 @@ class BalPendingScreen extends StatelessWidget {
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
await viewModel.startBal(viewModel.bal!.id);
|
await viewModel.startBal(
|
||||||
|
viewModel.selectedBal!.id,
|
||||||
|
);
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
|
|
@ -96,8 +98,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.bal!.startTime,
|
start: start ?? widget.viewModel.selectedBal!.startTime,
|
||||||
end: end ?? widget.viewModel.bal!.endTime,
|
end: end ?? widget.viewModel.selectedBal!.endTime,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -126,7 +128,7 @@ class _EditPopup extends State<EditPopup> {
|
||||||
labelText: "Nom de la BAL",
|
labelText: "Nom de la BAL",
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
),
|
),
|
||||||
initialValue: widget.viewModel.bal!.name,
|
initialValue: widget.viewModel.selectedBal!.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";
|
||||||
|
|
@ -169,7 +171,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.bal!;
|
final Bal bal = widget.viewModel.selectedBal!;
|
||||||
|
|
||||||
final result = await widget.viewModel.editBal(
|
final result = await widget.viewModel.editBal(
|
||||||
bal.id,
|
bal.id,
|
||||||
|
|
|
||||||
|
|
@ -18,18 +18,25 @@ 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? _currentBal;
|
/// [Bal] currently [BalState.ongoing]
|
||||||
Bal? get currentBal => _currentBal;
|
Bal? _ongoingBal;
|
||||||
|
|
||||||
|
/// [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 start,
|
DateTime startTime,
|
||||||
DateTime end,
|
DateTime endTime,
|
||||||
) async {
|
) async {
|
||||||
final result = await _balRepository.addBal(name, start, end);
|
final result = await _balRepository.addBal(name, startTime, endTime);
|
||||||
switch (result) {
|
switch (result) {
|
||||||
case Ok():
|
case Ok():
|
||||||
final result2 = await _balRepository.getBals();
|
final result2 = await _balRepository.getBals();
|
||||||
|
|
@ -54,9 +61,11 @@ 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) {
|
||||||
|
|
@ -70,12 +79,13 @@ 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));
|
||||||
_currentBal = _bals
|
_ongoingBal = _bals
|
||||||
.where((bal) => bal.state == BalState.ongoing)
|
.where((bal) => bal.state == BalState.ongoing)
|
||||||
.firstOrNull;
|
.firstOrNull;
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -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.currentBal?.id,
|
(el) => el.id != widget.viewModel.ongoingBal?.id,
|
||||||
))
|
))
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
|
|
@ -81,20 +81,20 @@ class _HomePageState extends State<HomePage> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
switch (widget.viewModel.currentBal == null) {
|
switch (widget.viewModel.ongoingBal == null) {
|
||||||
true => SizedBox(),
|
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.currentBal!.name),
|
title: Text(widget.viewModel.ongoingBal!.name),
|
||||||
subtitle: Text("BAL en cours"),
|
subtitle: Text("BAL en cours"),
|
||||||
trailing: IconButton(
|
trailing: IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
_moveToBal(
|
_moveToBal(
|
||||||
context,
|
context,
|
||||||
widget.viewModel.currentBal!.id,
|
widget.viewModel.ongoingBal!.id,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.arrow_forward),
|
icon: Icon(Icons.arrow_forward),
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,13 @@ class SellViewModel extends ChangeNotifier {
|
||||||
final BookRepository _bookRepository;
|
final BookRepository _bookRepository;
|
||||||
final OwnerRepository _ownerRepository;
|
final OwnerRepository _ownerRepository;
|
||||||
|
|
||||||
bool _showScan = false;
|
/// Wether to show the scan screen
|
||||||
bool get showScan => _showScan;
|
bool _showScanScreen = false;
|
||||||
set showScan(bool newValue) {
|
|
||||||
_showScan = newValue;
|
/// Wether to show the scan screen
|
||||||
|
bool get showScanScreen => _showScanScreen;
|
||||||
|
set showScanScreen(bool newValue) {
|
||||||
|
_showScanScreen = newValue;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,56 +47,66 @@ class SellViewModel extends ChangeNotifier {
|
||||||
* ===============================
|
* ===============================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
final List<BookStack> _soldBooks = [];
|
/// Books in the sell
|
||||||
List<BookStack> get soldBooks => _soldBooks;
|
final List<BookStack> _booksInSell = [];
|
||||||
|
|
||||||
|
/// 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 minimumAmount = 0;
|
double minimumAmountToPay = 0;
|
||||||
|
|
||||||
void sellBook(BookStack addedBook) {
|
/// Adds a book to the [_booksInSell]
|
||||||
minimumAmount += addedBook.instance.price;
|
void addBookToSell(BookStack bookToAdd) {
|
||||||
_soldBooks.add(addedBook);
|
minimumAmountToPay += bookToAdd.instance.price;
|
||||||
|
_booksInSell.add(bookToAdd);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendSell(double givenAmount) async {
|
/// Sends the sell
|
||||||
|
void sendSell(double givenMoney) async {
|
||||||
isSendingSell = true;
|
isSendingSell = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
List<BookInstance> toSend = [];
|
List<BookInstance> booksToSend = [];
|
||||||
int nbOfPl = 0;
|
int numberOfPL = 0;
|
||||||
for (BookStack book in _soldBooks) {
|
for (BookStack book in _booksInSell) {
|
||||||
if (book.instance.price != 0) {
|
if (book.instance.price != 0) {
|
||||||
book.instance.soldPrice = book.instance.price;
|
book.instance.soldPrice = book.instance.price;
|
||||||
givenAmount -= book.instance.price;
|
givenMoney -= book.instance.price;
|
||||||
toSend.add(book.instance);
|
booksToSend.add(book.instance);
|
||||||
} else {
|
} else {
|
||||||
nbOfPl++;
|
numberOfPL++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nbOfPl != 0) {
|
if (numberOfPL != 0) {
|
||||||
double amountPerPl = givenAmount / nbOfPl;
|
double moneyPerPL = givenMoney / numberOfPL;
|
||||||
for (BookStack book in _soldBooks) {
|
for (BookStack book in _booksInSell) {
|
||||||
if (book.instance.price == 0) {
|
if (book.instance.price == 0) {
|
||||||
book.instance.soldPrice = amountPerPl;
|
book.instance.soldPrice = moneyPerPL;
|
||||||
toSend.add(book.instance);
|
booksToSend.add(book.instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await _bookInstanceRepository.sellBooks(toSend);
|
await _bookInstanceRepository.sellBooks(booksToSend);
|
||||||
_soldBooks.clear();
|
_booksInSell.clear();
|
||||||
isSendingSell = false;
|
isSendingSell = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void deleteBook(int id) {
|
/// Removes a book from the sell
|
||||||
_soldBooks.removeWhere((book) => book.instance.id == id);
|
void removeBookFromSell(int bookId) {
|
||||||
|
_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;
|
||||||
|
|
@ -106,15 +119,21 @@ 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,
|
||||||
|
|
@ -126,6 +145,7 @@ 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),
|
||||||
);
|
);
|
||||||
|
|
@ -140,18 +160,19 @@ 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? bal = await _balRepository.ongoingBal();
|
Bal? ongoingBal = await _balRepository.ongoingBal();
|
||||||
_scannedBooks.clear();
|
_scannedBooks.clear();
|
||||||
|
|
||||||
final result = await _bookInstanceRepository.getByEan(bal!.id, ean);
|
final result1 = await _bookInstanceRepository.getByEan(ongoingBal!.id, ean);
|
||||||
switch (result) {
|
switch (result1) {
|
||||||
case Ok():
|
case Ok():
|
||||||
Book book;
|
Book book;
|
||||||
final result2 = await _bookRepository.getBookById(
|
final result2 = await _bookRepository.getBookById(
|
||||||
result.value.first.bookId,
|
result1.value.first.bookId,
|
||||||
);
|
);
|
||||||
switch (result2) {
|
switch (result2) {
|
||||||
case Ok():
|
case Ok():
|
||||||
|
|
@ -160,15 +181,22 @@ 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) {
|
||||||
|
|
@ -178,6 +206,7 @@ class SellViewModel extends ChangeNotifier {
|
||||||
case Error():
|
case Error():
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
_scannedBooks.add(BookStack(book, instance, owner));
|
_scannedBooks.add(BookStack(book, instance, owner));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -196,8 +225,11 @@ class SellViewModel extends ChangeNotifier {
|
||||||
* =================
|
* =================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Bal? _currentBal;
|
/// The currently ongoing [Bal]
|
||||||
get currentBal => _currentBal;
|
Bal? _ongoingBal;
|
||||||
|
|
||||||
|
/// The currently ongoing [Bal]
|
||||||
|
get ongoingBal => _ongoingBal;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =================================
|
* =================================
|
||||||
|
|
@ -205,9 +237,11 @@ class SellViewModel extends ChangeNotifier {
|
||||||
* =================================
|
* =================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/// Command to load necessary data
|
||||||
late final Command0 load;
|
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) {
|
||||||
|
|
@ -221,11 +255,12 @@ 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():
|
||||||
_currentBal = result.value
|
_ongoingBal = result.value
|
||||||
.where((bal) => bal.state == BalState.ongoing)
|
.where((bal) => bal.state == BalState.ongoing)
|
||||||
.firstOrNull;
|
.firstOrNull;
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,9 @@ class _ScanScreenState extends State<ScanScreen> {
|
||||||
MobileScanner(
|
MobileScanner(
|
||||||
controller: controller,
|
controller: controller,
|
||||||
onDetect: (barcodes) async {
|
onDetect: (barcodes) async {
|
||||||
|
if (barcodes.barcodes.isEmpty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
controller.stop();
|
controller.stop();
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
|
|
@ -51,19 +54,24 @@ class _ScanScreenState extends State<ScanScreen> {
|
||||||
await widget.viewModel.scanBook(barcodes);
|
await widget.viewModel.scanBook(barcodes);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
Center(
|
||||||
|
child: SvgPicture.asset(
|
||||||
|
'assets/scan-overlay.svg',
|
||||||
|
height: (MediaQuery.sizeOf(context).height / 5) * 2,
|
||||||
|
),
|
||||||
|
),
|
||||||
SafeArea(
|
SafeArea(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
widget.viewModel.showScan = false;
|
widget.viewModel.showScanScreen = false;
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.arrow_back),
|
icon: Icon(Icons.arrow_back),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Center(child: SvgPicture.asset('assets/scan-overlay.svg')),
|
|
||||||
Column(
|
Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
|
|
||||||
|
|
@ -34,9 +34,9 @@ class SellChoicePopup extends StatelessWidget {
|
||||||
child: Card(
|
child: Card(
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
viewModel.sellBook(book);
|
viewModel.addBookToSell(book);
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
viewModel.showScan = false;
|
viewModel.showScanScreen = false;
|
||||||
},
|
},
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: Text(
|
leading: Text(
|
||||||
|
|
|
||||||
|
|
@ -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.currentBal) {
|
true => switch (widget.viewModel.ongoingBal) {
|
||||||
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.soldBooks)
|
in widget.viewModel.booksInSell)
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
horizontal: 15,
|
horizontal: 15,
|
||||||
|
|
@ -99,9 +99,10 @@ class _SellPageState extends State<SellPage> {
|
||||||
),
|
),
|
||||||
trailing: IconButton(
|
trailing: IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
widget.viewModel.deleteBook(
|
widget.viewModel
|
||||||
book.instance.id,
|
.removeBookFromSell(
|
||||||
);
|
book.instance.id,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.delete),
|
icon: Icon(Icons.delete),
|
||||||
),
|
),
|
||||||
|
|
@ -113,7 +114,7 @@ class _SellPageState extends State<SellPage> {
|
||||||
),
|
),
|
||||||
SizedBox(height: 40),
|
SizedBox(height: 40),
|
||||||
Text(
|
Text(
|
||||||
"Montant minimum à payer : ${widget.viewModel.minimumAmount.toString()}€",
|
"Montant minimum à payer : ${widget.viewModel.minimumAmountToPay.toString()}€",
|
||||||
),
|
),
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 60.0),
|
padding: const EdgeInsets.symmetric(horizontal: 60.0),
|
||||||
|
|
@ -122,14 +123,15 @@ class _SellPageState extends State<SellPage> {
|
||||||
controller: price,
|
controller: price,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "Argent reçu",
|
labelText: "Argent reçu",
|
||||||
hintText:
|
|
||||||
"Utilisez un point (.) pour les virgules (,)",
|
|
||||||
helperText:
|
helperText:
|
||||||
"L'argent reçu sera réparti automatiquement.",
|
"L'argent reçu sera réparti automatiquement.",
|
||||||
suffixText: "€",
|
suffixText: "€",
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
),
|
),
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.numberWithOptions(
|
||||||
|
decimal: true,
|
||||||
|
signed: true,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
@ -139,7 +141,21 @@ class _SellPageState extends State<SellPage> {
|
||||||
children: [
|
children: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
if (double.tryParse(price.text) == null) {
|
if (widget.viewModel.scannedBooks.isEmpty) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"La vente doit comporter au moins un livre",
|
||||||
|
),
|
||||||
|
behavior: SnackBarBehavior.floating,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (double.tryParse(
|
||||||
|
price.text.replaceFirst(",", "."),
|
||||||
|
) ==
|
||||||
|
null) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text(
|
content: Text(
|
||||||
|
|
@ -149,8 +165,10 @@ class _SellPageState extends State<SellPage> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
} else if (double.parse(price.text) <
|
} else if (double.parse(
|
||||||
widget.viewModel.minimumAmount) {
|
price.text.replaceFirst(",", "."),
|
||||||
|
) <
|
||||||
|
widget.viewModel.minimumAmountToPay) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text(
|
content: Text(
|
||||||
|
|
@ -162,7 +180,9 @@ class _SellPageState extends State<SellPage> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
widget.viewModel.sendSell(
|
widget.viewModel.sendSell(
|
||||||
double.parse(price.text),
|
double.parse(
|
||||||
|
price.text.replaceFirst(",", "."),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
|
|
@ -183,7 +203,7 @@ class _SellPageState extends State<SellPage> {
|
||||||
SizedBox(width: 70),
|
SizedBox(width: 70),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
widget.viewModel.showScan = true;
|
widget.viewModel.showScanScreen = true;
|
||||||
},
|
},
|
||||||
icon: Icon(Icons.add),
|
icon: Icon(Icons.add),
|
||||||
style: ButtonStyle(
|
style: ButtonStyle(
|
||||||
|
|
@ -197,7 +217,7 @@ class _SellPageState extends State<SellPage> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
(widget.viewModel.showScan)
|
(widget.viewModel.showScanScreen)
|
||||||
? ScanScreen(viewModel: widget.viewModel)
|
? ScanScreen(viewModel: widget.viewModel)
|
||||||
: SizedBox(),
|
: SizedBox(),
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,12 @@
|
||||||
import FlutterMacOS
|
import FlutterMacOS
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
import flutter_secure_storage_macos
|
import flutter_secure_storage_darwin
|
||||||
import mobile_scanner
|
import mobile_scanner
|
||||||
import path_provider_foundation
|
import path_provider_foundation
|
||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
|
FlutterSecureStorageDarwinPlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStorageDarwinPlugin"))
|
||||||
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
|
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
74
pubspec.lock
74
pubspec.lock
|
|
@ -151,50 +151,50 @@ packages:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: flutter_secure_storage
|
name: flutter_secure_storage
|
||||||
sha256: "9cad52d75ebc511adfae3d447d5d13da15a55a92c9410e50f67335b6d21d16ea"
|
sha256: f7eceb0bc6f4fd0441e29d43cab9ac2a1c5ffd7ea7b64075136b718c46954874
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "9.2.4"
|
version: "10.0.0-beta.4"
|
||||||
|
flutter_secure_storage_darwin:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_secure_storage_darwin
|
||||||
|
sha256: f226f2a572bed96bc6542198ebaec227150786e34311d455a7e2d3d06d951845
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.1.0"
|
||||||
flutter_secure_storage_linux:
|
flutter_secure_storage_linux:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: flutter_secure_storage_linux
|
name: flutter_secure_storage_linux
|
||||||
sha256: be76c1d24a97d0b98f8b54bce6b481a380a6590df992d0098f868ad54dc8f688
|
sha256: "9b4b73127e857cd3117d43a70fa3dddadb6e0b253be62e6a6ab85caa0742182c"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.2.3"
|
version: "2.0.1"
|
||||||
flutter_secure_storage_macos:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: flutter_secure_storage_macos
|
|
||||||
sha256: "6c0a2795a2d1de26ae202a0d78527d163f4acbb11cde4c75c670f3a0fc064247"
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "3.1.3"
|
|
||||||
flutter_secure_storage_platform_interface:
|
flutter_secure_storage_platform_interface:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: flutter_secure_storage_platform_interface
|
name: flutter_secure_storage_platform_interface
|
||||||
sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8
|
sha256: "8ceea1223bee3c6ac1a22dabd8feefc550e4729b3675de4b5900f55afcb435d6"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.2"
|
version: "2.0.1"
|
||||||
flutter_secure_storage_web:
|
flutter_secure_storage_web:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: flutter_secure_storage_web
|
name: flutter_secure_storage_web
|
||||||
sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9
|
sha256: "4c3f233e739545c6cb09286eeec1cc4744138372b985113acc904f7263bef517"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.2.1"
|
version: "2.0.0"
|
||||||
flutter_secure_storage_windows:
|
flutter_secure_storage_windows:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: flutter_secure_storage_windows
|
name: flutter_secure_storage_windows
|
||||||
sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709
|
sha256: ff32af20f70a8d0e59b2938fc92de35b54a74671041c814275afd80e27df9f21
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.2"
|
version: "4.0.0"
|
||||||
flutter_svg:
|
flutter_svg:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
|
@ -253,14 +253,6 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.20.2"
|
version: "0.20.2"
|
||||||
js:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: js
|
|
||||||
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "0.6.7"
|
|
||||||
json_annotation:
|
json_annotation:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
@ -273,26 +265,26 @@ packages:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: leak_tracker
|
name: leak_tracker
|
||||||
sha256: "6bb818ecbdffe216e81182c2f0714a2e62b593f4a4f13098713ff1685dfb6ab0"
|
sha256: "8dcda04c3fc16c14f48a7bb586d4be1f0d1572731b6d81d51772ef47c02081e0"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "10.0.9"
|
version: "11.0.1"
|
||||||
leak_tracker_flutter_testing:
|
leak_tracker_flutter_testing:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: leak_tracker_flutter_testing
|
name: leak_tracker_flutter_testing
|
||||||
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
|
sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.9"
|
version: "3.0.10"
|
||||||
leak_tracker_testing:
|
leak_tracker_testing:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: leak_tracker_testing
|
name: leak_tracker_testing
|
||||||
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
|
sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.1"
|
version: "3.0.2"
|
||||||
lints:
|
lints:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
@ -301,8 +293,16 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "5.1.1"
|
version: "5.1.1"
|
||||||
logging:
|
logger:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: logger
|
||||||
|
sha256: "55d6c23a6c15db14920e037fe7e0dc32e7cdaf3b64b4b25df2d541b5b6b81c0c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.6.1"
|
||||||
|
logging:
|
||||||
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: logging
|
name: logging
|
||||||
sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61
|
sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61
|
||||||
|
|
@ -510,10 +510,10 @@ packages:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: test_api
|
name: test_api
|
||||||
sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
|
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.7.4"
|
version: "0.7.6"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
@ -550,10 +550,10 @@ packages:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: vector_math
|
name: vector_math
|
||||||
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
|
sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.4"
|
version: "2.2.0"
|
||||||
vm_service:
|
vm_service:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
||||||
|
|
@ -41,15 +41,16 @@ dependencies:
|
||||||
provider: ^6.1.5
|
provider: ^6.1.5
|
||||||
flutter_svg: ^2.2.0
|
flutter_svg: ^2.2.0
|
||||||
go_router: ^16.0.0
|
go_router: ^16.0.0
|
||||||
logging: ^1.3.0
|
|
||||||
http: ^1.4.0
|
http: ^1.4.0
|
||||||
web_socket_channel: ^3.0.3
|
web_socket_channel: ^3.0.3
|
||||||
nested: ^1.0.0
|
nested: ^1.0.0
|
||||||
flutter_secure_storage: ^9.2.4
|
#flutter_secure_storage: ^9.2.4
|
||||||
|
flutter_secure_storage: ^10.0.0-beta.4
|
||||||
rxdart: ^0.28.0
|
rxdart: ^0.28.0
|
||||||
intl: ^0.20.2
|
intl: ^0.20.2
|
||||||
flutter_launcher_icons: ^0.14.4
|
flutter_launcher_icons: ^0.14.4
|
||||||
fl_chart: ^1.0.0
|
fl_chart: ^1.0.0
|
||||||
|
logger: ^2.6.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
1
scripts/build-web.sh
Executable file
1
scripts/build-web.sh
Executable file
|
|
@ -0,0 +1 @@
|
||||||
|
flutter build web --release --wasm --base-href /app/
|
||||||
Reference in a new issue