fix: no decimal on prices on ios
Some checks failed
/ test (release) Failing after 2m27s

This commit is contained in:
alzalia1 2025-08-20 12:13:33 +02:00
parent e5d383548d
commit ebeab2db94
6 changed files with 161 additions and 103 deletions

View file

@ -1,10 +1,10 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:math';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.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';
@ -22,8 +22,6 @@ extension StringExtension on String {
} }
} }
typedef AuthHeaderProvider = String? Function();
class ApiClient { class ApiClient {
ApiClient({String? host, int? port}); ApiClient({String? host, int? port});
@ -31,11 +29,17 @@ class ApiClient {
String? token; String? token;
bool isReady = false; bool isReady = false;
FlutterSecureStorage? _secureStorage; FlutterSecureStorage? _secureStorage;
Logger log = Logger(
printer: PrettyPrinter(
colors: true,
lineLength: 100,
methodCount: 0,
dateTimeFormat: DateTimeFormat.dateAndTime,
),
);
Future<void> _initStore() async { Future<void> _initStore() async {
_secureStorage ??= const FlutterSecureStorage( _secureStorage ??= const FlutterSecureStorage(aOptions: AndroidOptions());
aOptions: AndroidOptions(encryptedSharedPreferences: true),
);
} }
Future<Map<String, String>> _getHeaders([ Future<Map<String, String>> _getHeaders([
@ -54,20 +58,25 @@ class ApiClient {
*/ */
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();
@ -75,25 +84,32 @@ class ApiClient {
} }
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();
@ -107,20 +123,27 @@ class ApiClient {
*/ */
Future<Result<BalStats>> getBalStats(int id) async { Future<Result<BalStats>> getBalStats(int id) async {
final url = "https://$apiBasePath/bal/${id.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();
@ -128,26 +151,27 @@ class ApiClient {
} }
Future<Result<Bal>> stopBal(int id) async { Future<Result<Bal>> stopBal(int id) async {
final url = "https://$apiBasePath/bal/${id.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();
@ -155,26 +179,27 @@ class ApiClient {
} }
Future<Result<Bal>> startBal(int id) async { Future<Result<Bal>> startBal(int id) async {
final url = "https://$apiBasePath/bal/${id.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();
@ -187,6 +212,8 @@ class ApiClient {
DateTime start, DateTime start,
DateTime end, DateTime end,
) async { ) async {
final url = "https://$apiBasePath/bal/${id.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"});
@ -196,17 +223,23 @@ class ApiClient {
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(), "end_timestamp": (end.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();
@ -214,24 +247,25 @@ class ApiClient {
} }
Future<Result<Bal>> getBalById(int id) async { Future<Result<Bal>> getBalById(int id) async {
final url = "https://$apiBasePath/bal/${id.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();
@ -239,6 +273,8 @@ class ApiClient {
} }
Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) async { Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) 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"});
@ -248,17 +284,21 @@ class ApiClient {
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(), "end_timestamp": (end.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();
@ -266,20 +306,14 @@ class ApiClient {
} }
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) { if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>; 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()); return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
} else { } else {
throw Exception("Something wrong happened"); throw Exception("Something wrong happened");

View file

@ -1,3 +1,5 @@
import 'dart:nativewrappers/_internal/vm/lib/ffi_patch.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:seshat/domain/models/book.dart'; import 'package:seshat/domain/models/book.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';
@ -80,12 +82,16 @@ class _ConfirmationPopupState extends State<ConfirmationPopup> {
border: OutlineInputBorder(), border: OutlineInputBorder(),
suffixText: "", suffixText: "",
), ),
keyboardType: TextInputType.number, keyboardType: TextInputType.numberWithOptions(
decimal: 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;
}, },

View file

@ -145,12 +145,16 @@ class _ManualEANPopupState extends State<_ManualEANPopup> {
border: OutlineInputBorder(), border: OutlineInputBorder(),
suffixText: "", suffixText: "",
), ),
keyboardType: TextInputType.number, keyboardType: TextInputType.numberWithOptions(
decimal: 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;
}, },
@ -273,12 +277,16 @@ class _FullyManualState extends State<_FullyManual> {
border: OutlineInputBorder(), border: OutlineInputBorder(),
suffixText: "", suffixText: "",
), ),
keyboardType: TextInputType.number, keyboardType: TextInputType.numberWithOptions(
decimal: 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;
}, },

View file

@ -129,7 +129,9 @@ class _SellPageState extends State<SellPage> {
suffixText: "", suffixText: "",
border: OutlineInputBorder(), border: OutlineInputBorder(),
), ),
keyboardType: TextInputType.number, keyboardType: TextInputType.numberWithOptions(
decimal: true,
),
), ),
), ),
), ),

View file

@ -293,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

View file

@ -41,7 +41,6 @@ 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
@ -51,6 +50,7 @@ dependencies:
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: