fix: stats didn't display when stopping bal

This commit is contained in:
alzalia1 2025-08-20 12:43:35 +02:00
parent 609af329e3
commit 892cd03f79
2 changed files with 45 additions and 44 deletions

View file

@ -327,13 +327,12 @@ class ApiClient {
} }
Future<Result<Bal?>> getCurrentBal() async { Future<Result<Bal?>> getCurrentBal() 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));
@ -356,13 +355,12 @@ class ApiClient {
*/ */
Future<Result<Book>> getBookById(int id) async { Future<Result<Book>> getBookById(int id) async {
final url = "https://$apiBasePath/book/id/${id.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()}"),
headers: headers,
);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
return Result.ok(Book.fromJSON(json)); return Result.ok(Book.fromJSON(json));
@ -377,13 +375,12 @@ class ApiClient {
} }
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"),
headers: headers,
);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
return Result.ok(Book.fromJSON(json)); return Result.ok(Book.fromJSON(json));
@ -408,25 +405,24 @@ class ApiClient {
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) { if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>; final json = jsonDecode(response.body) as List<dynamic>;
debugPrint("\n\n\n\nJSON : $json\n\n\n\n");
return Result.ok(json.map((el) => SearchResult.fromJSON(el)).toList()); return Result.ok(json.map((el) => SearchResult.fromJSON(el)).toList());
} else { } else {
throw "Unknown Error"; throw "Unknown Error";
} }
} catch (e) { } catch (e) {
debugPrint("\n\n\n\nERROR: ${e.toString()}\n\n\n\n");
return Result.error(Exception("API $e")); return Result.error(Exception("API $e"));
} finally { } finally {
client.close(); client.close();
@ -437,15 +433,13 @@ class ApiClient {
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(
"https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances",
),
headers: headers,
);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>; final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList()); return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList());
@ -460,14 +454,14 @@ class ApiClient {
} }
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,
); );
@ -477,7 +471,6 @@ class ApiClient {
throw "Unknown error"; throw "Unknown error";
} }
} catch (e) { } catch (e) {
debugPrint("\n\n\n\nERROR : ${e.toString()}\n\n\n\n");
return Result.error(Exception(e)); return Result.error(Exception(e));
} finally { } finally {
client.close(); client.close();
@ -490,6 +483,8 @@ class ApiClient {
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"});
@ -500,7 +495,7 @@ 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,
); );
@ -526,13 +521,12 @@ class ApiClient {
*/ */
Future<Result<Owner>> getOwnerById(int id) async { Future<Result<Owner>> getOwnerById(int id) async {
final url = "https://$apiBasePath/owner/${id.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()}"),
headers: headers,
);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json)); return Result.ok(Owner.fromJSON(json));
@ -547,13 +541,12 @@ class ApiClient {
} }
Future<Result<Owner>> getSectionOwner() async { Future<Result<Owner>> getSectionOwner() 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"),
headers: headers,
);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json)); return Result.ok(Owner.fromJSON(json));
@ -569,13 +562,12 @@ class ApiClient {
/// Call on `/owners` to get a list of all [Owner]s /// Call on `/owners` to get a list of all [Owner]s
Future<Result<List<Owner>>> getOwners() async { Future<Result<List<Owner>>> getOwners() async {
final url = "https://$apiBasePath/owners";
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"),
headers: headers,
);
if (response.statusCode == 200) { if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>; final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok( return Result.ok(
@ -597,6 +589,8 @@ class ApiClient {
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"});
@ -606,7 +600,7 @@ 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),
); );

View file

@ -36,14 +36,24 @@ class BalViewModel extends ChangeNotifier {
bool isABalOngoing = false; bool isABalOngoing = false;
Future<Result<void>> stopBal(int id) async { Future<Result<void>> stopBal(int id) async {
isLoaded = false;
notifyListeners();
final result = await _balRepository.stopBal(id); final result = await _balRepository.stopBal(id);
switch (result) { switch (result) {
case Ok(): case Ok():
_bal = result.value; _bal = 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;
} }
@ -149,16 +159,13 @@ class BalViewModel extends ChangeNotifier {
default: default:
break; break;
} }
debugPrint("$isLoaded");
if (_bal?.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;
} }
} }