Skip to content

Commit

Permalink
fix(#58): Adiciona command ao view model
Browse files Browse the repository at this point in the history
  • Loading branch information
EngDann committed Dec 28, 2024
1 parent fc4872f commit 92a8317
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
19 changes: 12 additions & 7 deletions lib/ui/login/viewModel/login_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:aranduapp/core/log/Log.dart';
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/navbar/view/navBarView.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';
import 'package:aranduapp/ui/login/service/LoginService.dart';
import 'package:aranduapp/ui/login/model/LoginRequest.dart';

class LoginViewModel extends ChangeNotifier {
final BuildContext context;
late Command0<void> loginCommand;

bool isLoading;

Expand All @@ -18,26 +21,28 @@ class LoginViewModel extends ChangeNotifier {
: isLoading = false,
formKey = GlobalKey<FormState>(),
emailController = TextEditingController(),
passwordController = TextEditingController();
passwordController = TextEditingController() {
loginCommand = Command0<void>(loginWithEmailAndPassword);
}

Future<void> loginWithEmailAndPassword() async {
// TODO use mutex to make this
Future<Result<void>> loginWithEmailAndPassword() async {
if (isLoading) {
return;
return Result.value(null);
}

try {
isLoading = true;
super.notifyListeners();
notifyListeners();

if (!formKey.currentState!.validate()) {
throw Exception('Valores inválidos');
return Result.error(Exception('Valores inválidos'));
}

await LoginService.login(
LoginRequest(emailController.text, passwordController.text));
return Result.value(null);
} catch (e) {
rethrow;
return Result.error(e);
} finally {
isLoading = false;
notifyListeners();
Expand Down
12 changes: 8 additions & 4 deletions test/ui/login/view/login_view_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:async' as _i4;
import 'package:aranduapp/ui/login/viewModel/login_view_model.dart' as _i3;
import 'package:flutter/material.dart' as _i2;
import 'package:mockito/mockito.dart' as _i1;
import 'package:async/async.dart' as _i5;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
Expand Down Expand Up @@ -134,14 +135,17 @@ class MockLoginViewModel extends _i1.Mock implements _i3.LoginViewModel {
) as bool);

@override
_i4.Future<void> loginWithEmailAndPassword() => (super.noSuchMethod(
_i4.Future<_i5.Result<void>> loginWithEmailAndPassword() =>
(super.noSuchMethod(
Invocation.method(
#loginWithEmailAndPassword,
[],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);
returnValue:
_i4.Future<_i5.Result<void>>.value(_i5.Result<void>.value(null)),
returnValueForMissingStub:
_i4.Future<_i5.Result<void>>.value(_i5.Result<void>.value(null)),
) as _i4.Future<_i5.Result<void>>);

@override
_i4.Future<void> validateToken() => (super.noSuchMethod(
Expand Down

0 comments on commit 92a8317

Please sign in to comment.