From 892cd03f796de705c162ed988f717983647b2ebd Mon Sep 17 00:00:00 2001 From: alzalia1 Date: Wed, 20 Aug 2025 12:43:35 +0200 Subject: [PATCH] fix: stats didn't display when stopping bal --- lib/data/services/api_client.dart | 74 +++++++++---------- .../bal_page/view_model/bal_view_model.dart | 15 +++- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/data/services/api_client.dart b/lib/data/services/api_client.dart index 2d9e273..3bc766a 100644 --- a/lib/data/services/api_client.dart +++ b/lib/data/services/api_client.dart @@ -327,13 +327,12 @@ class ApiClient { } Future> getCurrentBal() async { + final url = "https://$apiBasePath/bal/current"; + log.i("Fetching: getCurrentBal ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse("https://$apiBasePath/bal/current"), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body); return Result.ok(Bal.fromJSON(json)); @@ -356,13 +355,12 @@ class ApiClient { */ Future> getBookById(int id) async { + final url = "https://$apiBasePath/book/id/${id.toString()}"; + log.i("Fetching: getBookById ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse("https://$apiBasePath/book/id/${id.toString()}"), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body); return Result.ok(Book.fromJSON(json)); @@ -377,13 +375,12 @@ class ApiClient { } Future> getBookByEAN(String ean) async { + final url = "https://$apiBasePath/book/ean/$ean"; + log.i("Fetching: getBookByEan ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse("https://$apiBasePath/book/ean/$ean"), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body); return Result.ok(Book.fromJSON(json)); @@ -408,25 +405,24 @@ class ApiClient { String title, String author, ) async { + final url = "https://$apiBasePath/bal/${balId.toString()}/search"; + log.i("Fetching: getBookInstancesBySearch ($url)"); final client = Client(); try { final headers = await _getHeaders({"Content-Type": "application/json"}); final body = jsonEncode({"title": title, "author": author}); - debugPrint("\n\n\n\n$body\n\n\n\n"); final response = await client.post( - Uri.parse("https://$apiBasePath/bal/${balId.toString()}/search"), + Uri.parse(url), headers: headers, body: body, ); if (response.statusCode == 200) { final json = jsonDecode(response.body) as List; - debugPrint("\n\n\n\nJSON : $json\n\n\n\n"); return Result.ok(json.map((el) => SearchResult.fromJSON(el)).toList()); } else { throw "Unknown Error"; } } catch (e) { - debugPrint("\n\n\n\nERROR: ${e.toString()}\n\n\n\n"); return Result.error(Exception("API $e")); } finally { client.close(); @@ -437,15 +433,13 @@ class ApiClient { int balId, int ean, ) async { + final url = + "https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances"; + log.i("Fetching: getBookInstancesByEan ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse( - "https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances", - ), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body) as List; return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList()); @@ -460,14 +454,14 @@ class ApiClient { } Future> sellBooks(Map books) async { + final url = "https://$apiBasePath/book_instance/sell/bulk"; + log.i("Fetching: sellBooks ($url)"); final client = Client(); try { final headers = await _getHeaders({"Content-Type": "application/json"}); - debugPrint("\n\n\n\nMAP: $books\n\n\n\n"); final body = jsonEncode(books); - debugPrint("\n\n\n\nSENT: $body\n\n\n\n"); final response = await client.post( - Uri.parse("https://$apiBasePath/book_instance/sell/bulk"), + Uri.parse(url), headers: headers, body: body, ); @@ -477,7 +471,6 @@ class ApiClient { throw "Unknown error"; } } catch (e) { - debugPrint("\n\n\n\nERROR : ${e.toString()}\n\n\n\n"); return Result.error(Exception(e)); } finally { client.close(); @@ -490,6 +483,8 @@ class ApiClient { Bal bal, double price, ) async { + final url = "https://$apiBasePath/book_instance"; + log.i("Fetching: sendBook ($url)"); final client = Client(); try { final headers = await _getHeaders({"Content-Type": "application/json"}); @@ -500,7 +495,7 @@ class ApiClient { "price": price, }); final response = await client.post( - Uri.parse("https://$apiBasePath/book_instance"), + Uri.parse(url), headers: headers, body: body, ); @@ -526,13 +521,12 @@ class ApiClient { */ Future> getOwnerById(int id) async { + final url = "https://$apiBasePath/owner/${id.toString()}"; + log.i("Fetching: getOwnerById ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse("https://$apiBasePath/owner/${id.toString()}"), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body); return Result.ok(Owner.fromJSON(json)); @@ -547,13 +541,12 @@ class ApiClient { } Future> getSectionOwner() async { + final url = "https://$apiBasePath/owner/self"; + log.i("Fetching: getSectionOwner ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse("https://$apiBasePath/owner/self"), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body); return Result.ok(Owner.fromJSON(json)); @@ -569,13 +562,12 @@ class ApiClient { /// Call on `/owners` to get a list of all [Owner]s Future>> getOwners() async { + final url = "https://$apiBasePath/owners"; + log.i("Fetching: getOwners ($url)"); final client = Client(); try { final headers = await _getHeaders(); - final response = await client.get( - Uri.parse("https://$apiBasePath/owners"), - headers: headers, - ); + final response = await client.get(Uri.parse(url), headers: headers); if (response.statusCode == 200) { final json = jsonDecode(response.body) as List; return Result.ok( @@ -597,6 +589,8 @@ class ApiClient { String lastName, String contact, ) async { + final url = "https://$apiBasePath/owner"; + log.i("Fetching: addOwner ($url)"); final client = Client(); try { final headers = await _getHeaders({"Content-Type": "application/json"}); @@ -606,7 +600,7 @@ class ApiClient { "contact": contact, }; final response = await client.post( - Uri.parse("https://$apiBasePath/owner"), + Uri.parse(url), headers: headers, body: jsonEncode(body), ); diff --git a/lib/ui/bal_page/view_model/bal_view_model.dart b/lib/ui/bal_page/view_model/bal_view_model.dart index f9947f1..ad5e47d 100644 --- a/lib/ui/bal_page/view_model/bal_view_model.dart +++ b/lib/ui/bal_page/view_model/bal_view_model.dart @@ -36,14 +36,24 @@ class BalViewModel extends ChangeNotifier { bool isABalOngoing = false; Future> stopBal(int id) async { + isLoaded = false; + notifyListeners(); final result = await _balRepository.stopBal(id); switch (result) { case Ok(): _bal = result.value; - notifyListeners(); break; default: } + final result2 = await _loadEnded(); + switch (result2) { + case Ok(): + isLoaded = true; + break; + case Error(): + break; + } + notifyListeners(); return result; } @@ -149,16 +159,13 @@ class BalViewModel extends ChangeNotifier { default: break; } - debugPrint("$isLoaded"); if (_bal?.state == BalState.ended) { final result2 = await _loadEnded(); - debugPrint("Hello"); switch (result2) { case Ok(): isLoaded = true; break; case Error(): - debugPrint("No ${result2.error}"); break; } }