-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from fga-eps-mds/feat#59/Criação-View-Cadastro
Página de Registro em MVVM
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:convert'; | ||
class RegisterRequest { | ||
final String email; | ||
final String firstName; | ||
final String lastName; | ||
final String password; | ||
final String confPassword; | ||
RegisterRequest({ | ||
required this.email, | ||
required this.firstName, | ||
required this.lastName, | ||
required this.password, | ||
required this.confPassword, | ||
}); | ||
Map<String, dynamic> toJson() { | ||
return { | ||
'email': email, | ||
'firstName': firstName, | ||
'lastName': lastName, | ||
'password': password, | ||
'confPassword': confPassword, | ||
}; | ||
} | ||
factory RegisterRequest.fromJsonString(String jsonString) { | ||
final json = jsonDecode(jsonString); | ||
return RegisterRequest( | ||
email: json['email'], | ||
firstName: json['firstName'], | ||
lastName: json['lastName'], | ||
password: json['password'], | ||
confPassword: json['confPassword'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'dart:convert'; | ||
class RegisterResponse { | ||
final String authToken; | ||
final String refreshToken; | ||
RegisterResponse({ | ||
required this.authToken, | ||
required this.refreshToken, | ||
}); | ||
factory RegisterResponse.fromJsonString(String jsonString) { | ||
final json = jsonDecode(jsonString); | ||
return RegisterResponse( | ||
authToken: json['auth_token'], | ||
refreshToken: json['refresh_token'], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:dio/dio.dart'; | ||
import '../model/RegisterRequest.dart'; | ||
class RegisterService { | ||
static final Dio _dio = Dio(); | ||
static Future<void> register(RegisterRequest request) async { | ||
final response = await _dio.post( | ||
'https://example.com/register', | ||
data: request.toJson(), | ||
); | ||
if (response.statusCode != 200) { | ||
throw Exception('Erro ao criar conta: ${response.data['message']}'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:flutter/material.dart'; | ||
import '../model/RegisterRequest.dart'; | ||
import '../service/RegisterService.dart'; | ||
class RegisterAccountViewModel extends ChangeNotifier { | ||
final GlobalKey<FormState> formKey = GlobalKey<FormState>(); | ||
final TextEditingController emailController = TextEditingController(); | ||
final TextEditingController firstNameController = TextEditingController(); | ||
final TextEditingController lastNameController = TextEditingController(); | ||
final TextEditingController passwordController = TextEditingController(); | ||
final TextEditingController confPasswordController = TextEditingController(); | ||
bool isLoading = false; | ||
bool isTermsAccepted = false; // Adicionado | ||
// Método para alternar a aceitação dos termos | ||
void toggleTermsAccepted(bool value) { | ||
isTermsAccepted = value; | ||
notifyListeners(); | ||
} | ||
Future<void> register() async { | ||
if (isLoading) return; | ||
// Valida se os termos foram aceitos | ||
if (!isTermsAccepted) { | ||
throw Exception('Você deve aceitar os termos de privacidade e políticas de uso.'); | ||
} | ||
try { | ||
isLoading = true; | ||
notifyListeners(); | ||
// Valida os campos do formulário | ||
if (!formKey.currentState!.validate()) { | ||
throw Exception('Por favor, preencha todos os campos corretamente'); | ||
} | ||
// Criação do objeto de requisição | ||
final request = RegisterRequest( | ||
email: emailController.text, | ||
firstName: firstNameController.text, | ||
lastName: lastNameController.text, | ||
password: passwordController.text, | ||
confPassword: confPasswordController.text, | ||
); | ||
// Chamada do serviço de registro | ||
await RegisterService.register(request); | ||
} finally { | ||
isLoading = false; | ||
notifyListeners(); | ||
} | ||
} | ||
} |