fix: stats didn't display when stopping bal
This commit is contained in:
parent
609af329e3
commit
892cd03f79
2 changed files with 45 additions and 44 deletions
|
|
@ -327,13 +327,12 @@ class ApiClient {
|
|||
}
|
||||
|
||||
Future<Result<Bal?>> 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<Result<Book>> 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<Result<Book>> 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<dynamic>;
|
||||
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<dynamic>;
|
||||
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 {
|
||||
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<Result<Owner>> 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<Result<Owner>> 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<Result<List<Owner>>> 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<dynamic>;
|
||||
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),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -36,14 +36,24 @@ class BalViewModel extends ChangeNotifier {
|
|||
bool isABalOngoing = false;
|
||||
|
||||
Future<Result<void>> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue