feat: compelete bal pending screen
This commit is contained in:
parent
b12809ba93
commit
618764f513
4 changed files with 72 additions and 3 deletions
|
|
@ -50,6 +50,22 @@ class BalRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isABalOngoing() {
|
||||||
|
return _bals?.where((bal) => bal.state == BalState.ongoing).isNotEmpty ??
|
||||||
|
false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Result<Bal>> startBal(int id) async {
|
||||||
|
if (isABalOngoing()) {
|
||||||
|
return Result.error(
|
||||||
|
Exception("Cannot have multiple BAL ongoing at the same time !"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
final result = await _apiClient.startBal(id);
|
||||||
|
_getBalsNoCache();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> editBal(
|
Future<Result<Bal>> editBal(
|
||||||
int id,
|
int id,
|
||||||
String name,
|
String name,
|
||||||
|
|
@ -57,13 +73,13 @@ class BalRepository {
|
||||||
DateTime end,
|
DateTime end,
|
||||||
) async {
|
) async {
|
||||||
final result = await _apiClient.editBal(id, name, start, end);
|
final result = await _apiClient.editBal(id, name, start, end);
|
||||||
_getBalsNoCache();
|
await _getBalsNoCache();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Result<void>> addBal(String name, DateTime start, DateTime end) async {
|
Future<Result<void>> addBal(String name, DateTime start, DateTime end) async {
|
||||||
final result = await _apiClient.addBal(name, start, end);
|
final result = await _apiClient.addBal(name, start, end);
|
||||||
_getBalsNoCache();
|
await _getBalsNoCache();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,33 @@ class ApiClient {
|
||||||
* =================
|
* =================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
Future<Result<Bal>> startBal(int id) async {
|
||||||
|
final client = Client();
|
||||||
|
try {
|
||||||
|
final headers = await _getHeaders();
|
||||||
|
final response = await client.post(
|
||||||
|
Uri.parse("https://$apiBasePath/bal/${id.toString()}/start"),
|
||||||
|
headers: headers,
|
||||||
|
);
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final json = jsonDecode(response.body);
|
||||||
|
return Result.ok(Bal.fromJSON(json));
|
||||||
|
} else if (response.statusCode == 403) {
|
||||||
|
throw "You don't own the specified BAL";
|
||||||
|
} else if (response.statusCode == 404) {
|
||||||
|
throw "No BAL with specified ID found";
|
||||||
|
} else if (response.statusCode == 409) {
|
||||||
|
throw "Cannot have multiple BAl ongoing at the same time!";
|
||||||
|
} else {
|
||||||
|
throw "Unknown error";
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return Result.error(Exception(e));
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<Result<Bal>> editBal(
|
Future<Result<Bal>> editBal(
|
||||||
int id,
|
int id,
|
||||||
String name,
|
String name,
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,22 @@ class BalViewModel extends ChangeNotifier {
|
||||||
Bal? _bal;
|
Bal? _bal;
|
||||||
int? id;
|
int? id;
|
||||||
Bal? get bal => _bal;
|
Bal? get bal => _bal;
|
||||||
|
bool isABalOngoing = false;
|
||||||
|
|
||||||
|
Future<Result<void>> startBal(int id) async {
|
||||||
|
if (isABalOngoing) {
|
||||||
|
return Result.error(Exception("Cannot have multiple BALs ongoing !"));
|
||||||
|
}
|
||||||
|
final result = await _balRepository.startBal(id);
|
||||||
|
switch (result) {
|
||||||
|
case Ok():
|
||||||
|
_bal = result.value;
|
||||||
|
notifyListeners();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Future<Result<void>> editBal(
|
Future<Result<void>> editBal(
|
||||||
int id,
|
int id,
|
||||||
|
|
@ -46,6 +62,7 @@ class BalViewModel extends ChangeNotifier {
|
||||||
bool isLoaded = false;
|
bool isLoaded = false;
|
||||||
|
|
||||||
Future<Result<void>> _load() async {
|
Future<Result<void>> _load() async {
|
||||||
|
isABalOngoing = _balRepository.isABalOngoing();
|
||||||
final result1 = await _loadBal();
|
final result1 = await _loadBal();
|
||||||
switch (result1) {
|
switch (result1) {
|
||||||
case Ok():
|
case Ok():
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ class BalPendingScreen extends StatelessWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final theme = Theme.of(context);
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
bottomNavigationBar: AppNavigationBar(startIndex: 0),
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
|
|
@ -31,7 +32,15 @@ class BalPendingScreen extends StatelessWidget {
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Center(
|
||||||
child: ElevatedButton(
|
child: ElevatedButton(
|
||||||
onPressed: () {},
|
onPressed: viewModel.isABalOngoing
|
||||||
|
? () {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text("Une BAL est déjà en cours.")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
: () {
|
||||||
|
viewModel.startBal(viewModel.bal!.id);
|
||||||
|
},
|
||||||
child: Text("Démarrer cette BAL"),
|
child: Text("Démarrer cette BAL"),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Reference in a new issue