feat: honestly forgot
This commit is contained in:
parent
48bcf0b1f8
commit
da953ba651
19 changed files with 1097 additions and 244 deletions
79
lib/ui/home_page/view_model/home_view_model.dart
Normal file
79
lib/ui/home_page/view_model/home_view_model.dart
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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();
|
||||
isLoaded = true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in a new issue