Skip to content

Commit

Permalink
Merge pull request #56 from fga-eps-mds/qa/register_account
Browse files Browse the repository at this point in the history
Qa/register account
  • Loading branch information
GabrielCostaDeOliveira authored Dec 27, 2024
2 parents 05ac25a + 2c2a76d commit 5e28b25
Show file tree
Hide file tree
Showing 12 changed files with 620 additions and 98 deletions.
47 changes: 47 additions & 0 deletions lib/core/state/command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:async/async.dart';
import 'package:flutter/material.dart';

abstract class Command<T> extends ChangeNotifier {
Result<T>? _result;

bool _running = false;

Command();

bool get isError => result?.asError != null;

bool get isOk => result?.asValue != null;

Result<T>? get result => _result;

bool get running => _running;

Future<void> _execute(action) async {
if (running) return;

_result = null;
_running = true;
notifyListeners();

try {
_result = await action();
} catch (e) {
_result = Result.error(e);
} finally {
_running = false;
notifyListeners();
}
}
}

class Command0<T> extends Command<T> {
final Future<Result<T>> Function() action;

Command0(this.action);

Future<Result<T>> execute() async {
await _execute(action);

return _result!;
}
}
2 changes: 1 addition & 1 deletion lib/ui/login/view/login_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:aranduapp/ui/login/viewModel/login_view_model.dart';

import 'package:aranduapp/ui/recover_account/view/recover_account_view.dart';
import 'package:aranduapp/ui/register_account/view/RegisterAccount.dart';
import 'package:aranduapp/ui/register_account/view/register_account_view.dart';

import 'package:aranduapp/ui/shared/TitleSlogan.dart';
import 'package:aranduapp/ui/shared/TextEmail.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:aranduapp/core/network/base_api.dart';
import '../model/RegisterRequest.dart';
import '../model/register_request.dart';

class RegisterService {
static Future<void> register(RegisterRequest registerRequest) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:provider/provider.dart';
import 'package:aranduapp/core/log/Log.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'package:aranduapp/ui/register_account/viewModel/RegisterViewModel.dart';
import 'package:aranduapp/ui/register_account/viewModel/register_view_model.dart';

import 'package:aranduapp/ui/shared/TitleSlogan.dart';
import 'package:aranduapp/ui/shared/TextEmail.dart';
Expand All @@ -21,37 +21,30 @@ class RegisterAccount extends StatelessWidget {
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => RegisterAccountViewModel(),
child: const _RegisterAccount(),
child: const RegisterAccountScreen(),
);
}
}

class _RegisterAccount extends StatefulWidget {
const _RegisterAccount({Key? key}) : super(key: key);
class RegisterAccountScreen extends StatelessWidget {
const RegisterAccountScreen({super.key});

@override
State<StatefulWidget> createState() {
return _RegisterAccountState();
}
}

class _RegisterAccountState extends State<_RegisterAccount> {
@override
Widget build(BuildContext context) {
return Scaffold(body: _buildForm());
return Scaffold(body: _buildForm(context));
}

Widget _buildForm() {
Widget _buildForm(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const SizedBox(height: 80),
const TitleSlogan(),
const SizedBox(height: 10),
_formSection(),
_formSection(context),
const OrDivider(),
_buildGoogleLoginButton(),
_buildGoogleLoginButton(context),
TextAndLink(
text: 'Já tem uma conta?',
link: 'faça login',
Expand All @@ -63,18 +56,20 @@ class _RegisterAccountState extends State<_RegisterAccount> {
);
}

Widget _formSection() {
Widget _formSection(BuildContext context) {
RegisterAccountViewModel viewModel =
Provider.of<RegisterAccountViewModel>(context);

return Form(
key: viewModel.formKey,
child: Column(children: [
TextName(
key: const Key('nameField'),
label: 'Nome',
controller: viewModel.nameController,
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20)),
TextName(
key: const Key('userNameField'),
label: 'Nome de Usuário',
controller: viewModel.userNameController,
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20)),
Expand All @@ -84,14 +79,14 @@ class _RegisterAccountState extends State<_RegisterAccount> {
TextPassWord(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
controller: viewModel.passwordController),
_buildTermsCheckbox(),
_buildTermsCheckbox(context),
const SizedBox(height: 20),
_buildRegisterButton(),
_buildRegisterButton(context),
]),
);
}

Widget _buildTermsCheckbox() {
Widget _buildTermsCheckbox(BuildContext context) {
final viewModel = Provider.of<RegisterAccountViewModel>(context);
return Row(
children: [
Expand All @@ -114,34 +109,49 @@ class _RegisterAccountState extends State<_RegisterAccount> {
);
}

Widget _buildRegisterButton() {
Widget _buildRegisterButton(BuildContext context) {
final viewModel = Provider.of<RegisterAccountViewModel>(context);
return SizedBox(
width: 291,
height: 64,
child: ElevatedButton(
onPressed: () async {
try {
await viewModel.register(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Cadastro concluído com sucesso!")));
} catch(e) {
showDialog<Object>(
context: context,
builder: (BuildContext context) => ErrorPopUp(content: Text('$e')),
//Ação ao clicar no botão de cadastro
);
}
},
child: Consumer<RegisterAccountViewModel>(
builder: (context, value, child) => value.isLoading

return ListenableBuilder(
listenable: viewModel.registerCommand,
builder: (context, child) {
if (viewModel.registerCommand.isError) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog<Object>(
context: context,
builder: (BuildContext context) => ErrorPopUp(
content: Text(viewModel.registerCommand.result!.asError!.error
.toString()),
),
);
});
}

if (viewModel.registerCommand.isOk) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('conta criada com sucesso!!!')),
);
});
}

return SizedBox(
width: 291,
height: 64,
child: ElevatedButton(
onPressed: () async {
viewModel.registerCommand.execute();
},
child: viewModel.registerCommand.running
? const CircularProgressIndicator(value: null)
: const Text('Registrar'),
)),
),
);
},
);
}

Widget _buildGoogleLoginButton() {
Widget _buildGoogleLoginButton(BuildContext context) {
return GestureDetector(
onTap: () => Log.d(""),
child: Container(
Expand Down
53 changes: 0 additions & 53 deletions lib/ui/register_account/viewModel/RegisterViewModel.dart

This file was deleted.

47 changes: 47 additions & 0 deletions lib/ui/register_account/viewModel/register_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import '../model/register_request.dart';
import '../service/register_service.dart';

class RegisterAccountViewModel extends ChangeNotifier {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController nameController = TextEditingController();
final TextEditingController emailController = TextEditingController();
final TextEditingController userNameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();

bool isTermsAccepted = false;

late Command0<void> registerCommand;

RegisterAccountViewModel() {
registerCommand = Command0<void>(_register);
}

Future<Result<void>> _register() async {

if (!isTermsAccepted) {
return Result.error(
'Você deve aceitar os termos de privacidade e políticas de uso.');
}

if (!formKey.currentState!.validate()) {
Result.error('Por favor, preencha todos os campos corretamente');
}

await RegisterService.register(RegisterRequest(
email: emailController.text,
name: nameController.text,
userName: userNameController.text,
password: passwordController.text,
));

return Result.value(null);
}

void toggleTermsAccepted(bool value) {
isTermsAccepted = value;
notifyListeners();
}
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ packages:
source: hosted
version: "2.6.0"
async:
dependency: transitive
dependency: "direct main"
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
Expand Down Expand Up @@ -577,7 +577,7 @@ packages:
dependency: transitive
description:
name: path
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies:
flutter_secure_storage: ^9.2.2
mockito: ^5.4.4
build_runner: ^2.4.13
async: ^2.11.0


dev_dependencies:
Expand Down
Loading

0 comments on commit 5e28b25

Please sign in to comment.