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

98 lines
2.3 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>] of all [Bal]
List<Bal> _bals = [];
/// [List<Bal>] of all [Bal]
List<Bal> get bals => _bals;
/// [Bal] currently [BalState.ongoing]
Bal? _ongoingBal;
/// [Bal] currently [BalState.ongoing]
Bal? get ongoingBal => _ongoingBal;
/// Creates a [Bal] from its [name], [startTime] and [endTime]
Future<Result<void>> createBal(
String name,
DateTime startTime,
DateTime endTime,
) async {
final result = await _balRepository.addBal(name, startTime, endTime);
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():
return result2;
}
break;
case Error():
return result;
}
notifyListeners();
return result;
}
/*
* =================================
* =====[ COMMAND AND LOADING ]=====
* =================================
*/
/// Command to load all necessary data
late final Command0 load;
bool isLoaded = false;
/// Manages loaders
Future<Result<void>> _load() async {
final result2 = await _loadBal();
switch (result2) {
case Ok():
isLoaded = true;
break;
default:
break;
}
notifyListeners();
return result2;
}
/// Loads data about [Bal]
Future<Result<void>> _loadBal() async {
final result = await _balRepository.getBals();
switch (result) {
case Ok():
_bals = result.value..sort((a, b) => a.compareTo(b));
_ongoingBal = _bals
.where((bal) => bal.state == BalState.ongoing)
.firstOrNull;
break;
case Error():
return result;
}
return result;
}
}