Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: adiciona o command ao login (#58) #61

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions lib/ui/login/view/login_view.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -185,25 +186,22 @@ class _LoginScreenState extends State<LoginScreen> {
Widget _loginButtonSection() {
LoginViewModel viewModel = Provider.of<LoginViewModel>(context);

return SizedBox(
width: 291,
height: 64,
child: ElevatedButton(
onPressed: () {
viewModel.loginWithEmailAndPassword().then((_) {
viewModel.goToHome();
}).catchError((e) => showDialog<Object>(
context: context,
builder: (BuildContext context) =>
ErrorPopUp(content: Text('$e')),
));
},
child: Consumer<LoginViewModel>(
builder: (context, value, child) => value.isLoading
? const CircularProgressIndicator(value: null)
: const Text('Entrar'),
)),
);
return Requestbutton(
command: viewModel.loginCommand,
onErrorCallback: (String e) {
showDialog<Object>(
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() {
Expand Down
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
Loading