63 lines
1.7 KiB
Dart
63 lines
1.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:seshat/config/constants.dart';
|
|
import 'package:seshat/data/repositories/auth_repository.dart';
|
|
import 'package:seshat/utils/command.dart';
|
|
import 'package:seshat/utils/result.dart';
|
|
|
|
class LoginViewModel extends ChangeNotifier {
|
|
LoginViewModel({required AuthRepository authRepository})
|
|
: _authRepository = authRepository {
|
|
login = Command1<void, (String username, String password)>(_login);
|
|
load = Command0(_load)..execute();
|
|
}
|
|
|
|
final AuthRepository _authRepository;
|
|
|
|
/// Command to login with added capabilities
|
|
late Command1 login;
|
|
|
|
/// Logins the user with credentials [(String username, String password)]
|
|
Future<Result<void>> _login((String, String) credentials) async {
|
|
final (username, password) = credentials;
|
|
final result = await _authRepository.login(username, password);
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* =================================
|
|
* =====[ COMMAND AND LOADING ]=====
|
|
* =================================
|
|
*/
|
|
|
|
/// Loads all necessary data
|
|
late final Command0 load;
|
|
bool isLoaded = false;
|
|
bool isUpToDate = false;
|
|
|
|
/// Manages loaders
|
|
Future<Result<void>> _load() async {
|
|
final result1 = await _loadApiVersion();
|
|
switch (result1) {
|
|
case Ok():
|
|
isLoaded = true;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
notifyListeners();
|
|
return result1;
|
|
}
|
|
|
|
/// Loads the current remote api version and compares to local hardcoded [apiVersion]
|
|
Future<Result<void>> _loadApiVersion() async {
|
|
final result = await _authRepository.getRemoteApiVersion();
|
|
switch (result) {
|
|
case Ok():
|
|
isUpToDate = result.value == apiVersion;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
}
|