Skip to content

Commit

Permalink
Merge pull request #51 from fga-eps-mds/feature/194-Telefone-Produtor
Browse files Browse the repository at this point in the history
#194 Adição do numero de telefone
  • Loading branch information
brenno-silva authored May 6, 2021
2 parents 3df24ce + 8099bc3 commit e408e44
Show file tree
Hide file tree
Showing 18 changed files with 346 additions and 85 deletions.
13 changes: 13 additions & 0 deletions hortum_mobile/lib/components/form_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ class FormValidation {
return null;
}

static String validatePhone(String value) {
String patttern = r'(^[0-9]*$)';
RegExp regExp = new RegExp(patttern);
if (value.length == 0) {
return "Informe o celular";
} else if (value.length != 11) {
return "O celular deve ter 11 dígitos";
} else if (!regExp.hasMatch(value)) {
return "O número do celular só deve conter dígitos";
}
return null;
}

static String validateDescription(String value) {
if (value.length == 0) {
return "Informe a descrição";
Expand Down
1 change: 1 addition & 0 deletions hortum_mobile/lib/data/login_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class LoginApi {
actualUser.writeSecureData('email', actualUser.email);
actualUser.writeSecureData('token_refresh', actualUser.tokenRefresh);
actualUser.writeSecureData('token_access', actualUser.tokenAccess);
actualUser.writeSecureData('phone_number', actualUser.phone_number);
} else {
actualUser = null;
}
Expand Down
30 changes: 18 additions & 12 deletions hortum_mobile/lib/data/register_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:hortum_mobile/globals.dart';
import 'package:hortum_mobile/views/login/login_page.dart';

class RegisterApi {
Dio dio;
Expand All @@ -14,7 +13,7 @@ class RegisterApi {
}

Future register(String username, String email, String password,
bool isProductor, BuildContext context) async {
String telefone, bool isProductor, BuildContext context) async {
String urlCustomer = 'http://$ip:8000/signup/customer/';
String urlProductor = 'http://$ip:8000/signup/productor/';
Response response;
Expand All @@ -26,23 +25,30 @@ class RegisterApi {
"username": username,
"email": email,
"password": password,
"phone_number": telefone
},
};

String _body = json.encode(params);
if (isProductor == false) {
response = await this
.dio
.post(urlCustomer, data: _body, options: Options(headers: header));
response = await this.dio.post(urlCustomer,
data: _body,
options: Options(
headers: header,
validateStatus: (status) {
return status <= 500;
},
));
} else {
response = await dio.post(urlProductor,
data: _body, options: Options(headers: header));
data: _body,
options: Options(
headers: header,
validateStatus: (status) {
return status <= 500;
},
));
}
if (response.statusCode == 201) {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LoginPage();
}));
}
// else {}
return response;
}
}
6 changes: 4 additions & 2 deletions hortum_mobile/lib/data/user_data_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class UserAPI {
}
}

Future updateUser({String username, String email}) async {
Future updateUser(
{String username, String email, String phone_number}) async {
//Trocar o IPLOCAL pelo ip de sua máquina
String url = 'http://$ip:8000/users/update/';

Expand All @@ -26,6 +27,7 @@ class UserAPI {
Map params = {
"username": username,
"email": email,
"phone_number": phone_number
};
params.removeWhere((key, value) => value == null);

Expand All @@ -38,7 +40,7 @@ class UserAPI {
return status <= 500;
},
));
return response.statusCode;
return response;
}

Future changePassword(String actualPassword, String newPassword) async {
Expand Down
12 changes: 9 additions & 3 deletions hortum_mobile/lib/model/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User {
String username;
String password;
bool isProductor;
String phone_number;
final _storage = new FlutterSecureStorage();

User(
Expand All @@ -15,7 +16,8 @@ class User {
this.email,
this.username,
this.password,
this.isProductor});
this.isProductor,
this.phone_number});

factory User.fromJson(Map<String, dynamic> json) {
return User(
Expand All @@ -24,7 +26,8 @@ class User {
email: json['email'],
username: json['username'],
password: json['password'],
isProductor: json['is_productor']);
isProductor: json['is_productor'],
phone_number: json['phone_number']);
}

void deleteUser() {
Expand All @@ -37,6 +40,7 @@ class User {
username = null;
password = null;
isProductor = null;
phone_number = null;
}

void updateToken(String newToken) {
Expand All @@ -49,6 +53,7 @@ class User {
this.username = username;
this.email = await this.readSecureData('email');
this.tokenAccess = await this.readSecureData('token_access');
this.phone_number = await this.readSecureData('phone_number');
}

Future writeSecureData(String key, String value) async {
Expand All @@ -74,6 +79,7 @@ class User {
other.email == email &&
other.username == username &&
other.password == password &&
other.isProductor == isProductor;
other.isProductor == isProductor &&
other.phone_number == phone_number;
}
}
38 changes: 28 additions & 10 deletions hortum_mobile/lib/views/profile/components/profile_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@ class ProfileForm extends StatefulWidget {
final Dio dio;
final TextEditingController email;
final TextEditingController username;
final TextEditingController phone_number;

const ProfileForm({this.dio, this.email, this.username, key})
const ProfileForm(
{this.dio, this.email, this.username, this.phone_number, key})
: super(key: key);

@override
_ProfileFormState createState() =>
_ProfileFormState(email: email, username: username);
_ProfileFormState createState() => _ProfileFormState(
email: email, username: username, phone_number: phone_number);
}

class _ProfileFormState extends State<ProfileForm> {
final picker = ImagePicker();
final _formKey = GlobalKey<FormState>();
final TextEditingController email;
final TextEditingController username;
_ProfileFormState({this.email, this.username});

final TextEditingController phone_number;
final _formKey = GlobalKey<FormState>();
_ProfileFormState({
this.email,
this.username,
this.phone_number,
});
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
child: Column(
children: <Widget>[
Container(
height: size.height * 0.33,
height: size.height * 0.4,
child: Form(
key: _formKey,
child: Column(
Expand Down Expand Up @@ -63,6 +69,17 @@ class _ProfileFormState extends State<ProfileForm> {
validator: FormValidation.validateEmail,
),
),
Padding(
padding: EdgeInsets.only(bottom: size.height * 0.025),
child: CustomFormField(
suffixIcon: false,
obscureText: false,
labelText: 'Telefone',
controller: phone_number,
icon: Icon(Icons.phone, color: Colors.black),
validator: FormValidation.validatePhone,
),
),
],
),
),
Expand All @@ -71,9 +88,10 @@ class _ProfileFormState extends State<ProfileForm> {
onPressed: () {
if (_formKey.currentState.validate()) {
if (actualUser.username != username.text ||
actualUser.email != email.text) {
ProfileServices.updateUser(
widget.dio, username.text, email.text, context);
actualUser.email != email.text ||
actualUser.phone_number != phone_number.text) {
ProfileServices.updateUser(widget.dio, username.text,
email.text, phone_number.text, context);
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion hortum_mobile/lib/views/profile/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class _UserProfileState extends State<UserProfile> {
TextEditingController email = TextEditingController(text: actualUser.email);
TextEditingController username =
TextEditingController(text: actualUser.username);
TextEditingController phone_number =
TextEditingController(text: actualUser.phone_number);
Size size = MediaQuery.of(context).size;
return Scaffold(
body: SingleChildScrollView(
Expand All @@ -39,7 +41,11 @@ class _UserProfileState extends State<UserProfile> {
radius: 50,
bottomMargin: size.height * 0.06,
),
ProfileForm(email: email, username: username),
ProfileForm(
email: email,
username: username,
phone_number: phone_number,
),
],
),
),
Expand Down
33 changes: 24 additions & 9 deletions hortum_mobile/lib/views/profile/services/profile_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@ import 'package:hortum_mobile/views/home_customer/home_customer_page.dart';
import 'package:hortum_mobile/views/home_productor/home_productor_page.dart';

class ProfileServices {
static Future updateUser(
Dio dio, String nameForm, String emailForm, BuildContext context) async {
static Future updateUser(Dio dio, String nameForm, String emailForm,
String phone_number_form, BuildContext context) async {
var response;
UserAPI updateData = new UserAPI(dio);

if (actualUser.email == emailForm) {
response = await updateData.updateUser(username: nameForm);
} else if (actualUser.username == nameForm) {
if (actualUser.email != emailForm) {
response = await updateData.updateUser(email: emailForm);
} else if (actualUser.username != nameForm) {
response = await updateData.updateUser(username: nameForm);
} else if (actualUser.phone_number != phone_number_form) {
response = await updateData.updateUser(phone_number: phone_number_form);
} else {
response =
await updateData.updateUser(username: nameForm, email: emailForm);
response = await updateData.updateUser(
username: nameForm,
email: emailForm,
phone_number: phone_number_form);
}

if (response == 400) {
if (response.statusCode == 400) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
key: Key('emailJaExistente'),
title: Text("Erro!"),
content: Text(
"Email já registrado!",
announErrorFormart(response.data.toString()),
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 16,
Expand All @@ -56,6 +60,7 @@ class ProfileServices {
} else {
actualUser.email = emailForm;
actualUser.username = nameForm;
actualUser.phone_number = phone_number_form;
if (actualUser.isProductor) {
return Navigator.push(context, MaterialPageRoute(builder: (context) {
return ProductorHomePage();
Expand All @@ -68,3 +73,13 @@ class ProfileServices {
}
}
}

String announErrorFormart(String responseMsg) {
if (responseMsg ==
("{phone_number: [user with this phone number already exists.]}"))
return 'Telefone já cadastrado!';
else if (responseMsg == ("{email: [Email ja registrado!]}"))
return 'Email já cadastrado!';
else
return 'Email e telefone já cadastrados!';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:hortum_mobile/views/login/login_page.dart';

Future<void> dialogEmailEnviado(BuildContext context) async {
return showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
key: Key('dialogEmailEnviado'),
title: Text("Email enviado"),
content: Text(
"Verifique sua caixa de email com o link para ativar sua conta",
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 16,
),
),
actions: [
TextButton(
onPressed: () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
(route) => false);
},
child: Text(
"OK",
key: Key('okButton'),
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 22,
color: Color.fromARGB(0xFF, 244, 156, 0),
),
),
)
],
);
},
);
}
Loading

0 comments on commit e408e44

Please sign in to comment.