From 92a831784b0c3f8de20843aaf4d6c0e881ce9548 Mon Sep 17 00:00:00 2001 From: EngDann Date: Sat, 28 Dec 2024 16:48:19 -0300 Subject: [PATCH 1/2] fix(#58): Adiciona command ao view model --- lib/ui/login/viewModel/login_view_model.dart | 19 ++++++++++++------- test/ui/login/view/login_view_test.mocks.dart | 12 ++++++++---- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/ui/login/viewModel/login_view_model.dart b/lib/ui/login/viewModel/login_view_model.dart index e941a46..8576e0b 100644 --- a/lib/ui/login/viewModel/login_view_model.dart +++ b/lib/ui/login/viewModel/login_view_model.dart @@ -1,5 +1,7 @@ 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'; @@ -7,6 +9,7 @@ import 'package:aranduapp/ui/login/model/LoginRequest.dart'; class LoginViewModel extends ChangeNotifier { final BuildContext context; + late Command0 loginCommand; bool isLoading; @@ -18,26 +21,28 @@ class LoginViewModel extends ChangeNotifier { : isLoading = false, formKey = GlobalKey(), emailController = TextEditingController(), - passwordController = TextEditingController(); + passwordController = TextEditingController() { + loginCommand = Command0(loginWithEmailAndPassword); + } - Future loginWithEmailAndPassword() async { - // TODO use mutex to make this + Future> 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(); diff --git a/test/ui/login/view/login_view_test.mocks.dart b/test/ui/login/view/login_view_test.mocks.dart index fa695eb..d19d5f8 100644 --- a/test/ui/login/view/login_view_test.mocks.dart +++ b/test/ui/login/view/login_view_test.mocks.dart @@ -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 @@ -134,14 +135,17 @@ class MockLoginViewModel extends _i1.Mock implements _i3.LoginViewModel { ) as bool); @override - _i4.Future loginWithEmailAndPassword() => (super.noSuchMethod( + _i4.Future<_i5.Result> loginWithEmailAndPassword() => + (super.noSuchMethod( Invocation.method( #loginWithEmailAndPassword, [], ), - returnValue: _i4.Future.value(), - returnValueForMissingStub: _i4.Future.value(), - ) as _i4.Future); + returnValue: + _i4.Future<_i5.Result>.value(_i5.Result.value(null)), + returnValueForMissingStub: + _i4.Future<_i5.Result>.value(_i5.Result.value(null)), + ) as _i4.Future<_i5.Result>); @override _i4.Future validateToken() => (super.noSuchMethod( From 61ad8376f4e8d05023404ae8de2a5dbced85ecb4 Mon Sep 17 00:00:00 2001 From: EngDann Date: Sat, 28 Dec 2024 16:48:49 -0300 Subject: [PATCH 2/2] =?UTF-8?q?fix(#58):=20Adiciona=20bot=C3=A3o=20de=20re?= =?UTF-8?q?quisi=C3=A7=C3=A3o=20ao=20inv=C3=A9s=20do=20elevatedButton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ui/login/view/login_view.dart | 36 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/ui/login/view/login_view.dart b/lib/ui/login/view/login_view.dart index 1066cc7..3e4a91a 100644 --- a/lib/ui/login/view/login_view.dart +++ b/lib/ui/login/view/login_view.dart @@ -1,5 +1,6 @@ import 'package:aranduapp/core/log/Log.dart'; import 'package:aranduapp/ui/shared/TextAndLink.dart'; +import 'package:aranduapp/ui/shared/requestbutton.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; @@ -185,25 +186,22 @@ class _LoginScreenState extends State { Widget _loginButtonSection() { LoginViewModel viewModel = Provider.of(context); - return SizedBox( - width: 291, - height: 64, - child: ElevatedButton( - onPressed: () { - viewModel.loginWithEmailAndPassword().then((_) { - viewModel.goToHome(); - }).catchError((e) => showDialog( - context: context, - builder: (BuildContext context) => - ErrorPopUp(content: Text('$e')), - )); - }, - child: Consumer( - builder: (context, value, child) => value.isLoading - ? const CircularProgressIndicator(value: null) - : const Text('Entrar'), - )), - ); + return Requestbutton( + command: viewModel.loginCommand, + onErrorCallback: (String e) { + showDialog( + context: context, + builder: (BuildContext context) => ErrorPopUp(content: Text(e)), + ); + }, + onSuccessCallback: () { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text( + 'Bem-vindo(a) a bordo! Seu login foi feito com sucesso!')), + ); + }, + nameButton: 'Entrar'); } Widget _loggingInWithOther() {