This repository has been archived on 2025-08-25. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Seshat/lib/ui/home_page/view_model/home_view_model.dart
2025-08-12 23:59:13 +02:00

85 lines
2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:seshat/data/repositories/bal_repository.dart';
import 'package:seshat/domain/models/bal.dart';
import 'package:seshat/utils/command.dart';
import 'package:seshat/utils/result.dart';
class HomeViewModel extends ChangeNotifier {
HomeViewModel({required BalRepository balRepository})
: _balRepository = balRepository {
load = Command0(_load)..execute();
}
final BalRepository _balRepository;
/*
* =================
* =====[ BAL ]=====
* =================
*/
List<Bal> _bals = [];
List<Bal> get bals => _bals;
Bal? _currentBal;
Bal? get currentBal => _currentBal;
Future<Result<void>> createBal(String name) async {
final result = await _balRepository.addBal(name);
switch (result) {
case Ok():
final result2 = await _balRepository.getBals();
switch (result2) {
case Ok():
_bals = result2.value..sort((a, b) => a.compareTo(b));
break;
case Error():
debugPrint("\n\n\n\n${result2.error.toString()}\n\n\n\n");
return result2;
}
break;
case Error():
return result;
}
notifyListeners();
return result;
}
/*
* =================================
* =====[ COMMAND AND LOADING ]=====
* =================================
*/
late final Command0 load;
bool isLoaded = false;
Future<Result<void>> _load() async {
final result2 = await _loadBal();
switch (result2) {
case Ok():
isLoaded = true;
break;
default:
break;
}
notifyListeners();
return result2;
}
Future<Result<void>> _loadBal() async {
final result = await _balRepository.getBals();
switch (result) {
case Ok():
_bals = result.value..sort((a, b) => a.compareTo(b));
_currentBal = _bals
.where((bal) => bal.state == BalState.ongoing)
.firstOrNull;
break;
case Error():
return result;
}
return result;
}
}