Skip to content

Commit

Permalink
feat(#59): adiciona edit password
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Jan 10, 2025
1 parent 5dfd20c commit 6d45993
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 181 deletions.
10 changes: 10 additions & 0 deletions lib/core/network/base_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,14 @@ class BaseApi {
rethrow;
}
}


Future<Response> put({required String path, Object? data}) async {
try {
return await _dio.put(path, data: data);
} catch (e) {
Log.e(e);
rethrow;
}
}
}
28 changes: 28 additions & 0 deletions lib/ui/edit_password/model/edit_password_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:convert';

class EditPasswordRequest {
final String oldPassword;
final String newPassword;

EditPasswordRequest({
required this.oldPassword,
required this.newPassword,
});

Map<String, dynamic> toJson() {
return <String, dynamic>{
'oldPassword': oldPassword,
'newPassword': newPassword,
};
}

factory EditPasswordRequest.fromJsonString(String jsonString) {
final json = jsonDecode(jsonString);

return EditPasswordRequest(
oldPassword: json['oldPpassword']! as String,
newPassword: json['newPassword']! as String,
);
}
}

12 changes: 12 additions & 0 deletions lib/ui/edit_password/service/edit_password_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:aranduapp/core/network/base_api.dart';
import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart';

class EditPasswordService {

static Future<void> edit(EditPasswordRequest editPasswordRequest) async {
await BaseApi.getInstance(auth: true)
.put(path: '/outh-change-password', data: editPasswordRequest.toJson());
}


}
96 changes: 96 additions & 0 deletions lib/ui/edit_password/view/edit_password_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import 'package:aranduapp/ui/edit_password/viewmodel/edit_password_viewmode.dart';
import 'package:aranduapp/ui/shared/requestbutton.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:aranduapp/ui/shared/TextPassword.dart';

class EditPassword extends StatelessWidget {
const EditPassword({super.key});

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => EditPasswordViewMode(),
child: const EditPasswordScreen(),
);
}
}

class EditPasswordScreen extends StatelessWidget {
const EditPasswordScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.surface,
elevation: 0,
title: Center(
child: Text(
'Editar Senha',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontSize: 24,
),
),
),
leading: IconButton(
color: Theme.of(context).colorScheme.primary,
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();
},
),
),
body: _buildForm(context));
}

Widget _buildForm(BuildContext context) {
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const SizedBox(height: 10),
_formSection(context),
],
),
);
}

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

return Form(
key: viewModel.formKey,
child: Column(children: [
TextPassWord(
label: "Senha Antiga",
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
controller: viewModel.oldPasswordController),
TextPassWord(
label: "Senha Nova",
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
controller: viewModel.newPasswordController),
const SizedBox(height: 80),
_button(context, viewModel)
]),
);
}

Widget _button(BuildContext context, EditPasswordViewMode viewModel) {
return Requestbutton(
command: viewModel.editCommand,
nameButton: "Enviar",
onErrorCallback: (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e)),
);
},
onSuccessCallback: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Senha atualizada com sucesso')),
);
});
}
}
34 changes: 34 additions & 0 deletions lib/ui/edit_password/viewmodel/edit_password_viewmode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart';
import 'package:aranduapp/ui/edit_password/service/edit_password_service.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';

class EditPasswordViewMode extends ChangeNotifier {
final GlobalKey<FormState> formKey;
final TextEditingController newPasswordController;
final TextEditingController oldPasswordController;

late Command0<void> editCommand;

EditPasswordViewMode()
: formKey = GlobalKey<FormState>(),
newPasswordController = TextEditingController(),
oldPasswordController = TextEditingController() {
editCommand = Command0<void>(editPassword);
}

Future<Result<void>> editPassword() async {
if (!formKey.currentState!.validate()) {
return Result.error(Exception('Valores inválidos'));
}

EditPasswordRequest request = EditPasswordRequest(
oldPassword: oldPasswordController.text,
newPassword: newPasswordController.text);

await EditPasswordService.edit(request);

return Result.value(null);
}
}
4 changes: 2 additions & 2 deletions lib/ui/edit_profile/service/edit_profile_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:aranduapp/ui/edit_profile/model/edit_profile_request.dart';

class EditProfileService {
static Future<void> edit(EditProfileRequest editProfileRequest) async {
await BaseApi.getInstance(auth: false)
.post(path: '/users', data: editProfileRequest.toJson());
await BaseApi.getInstance(auth: true)
.patch(path: '/users', data: editProfileRequest.toJson());
}
}
84 changes: 31 additions & 53 deletions lib/ui/edit_profile/view/edit_profile_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:aranduapp/core/log/Log.dart';
import 'package:aranduapp/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart';
import 'package:aranduapp/ui/shared/ErrorPopUp.dart';
import 'package:aranduapp/ui/shared/TextEmail.dart';
import 'package:aranduapp/ui/shared/ProfileHeader.dart';
import 'package:aranduapp/ui/shared/TextName.dart';
Expand Down Expand Up @@ -28,43 +26,35 @@ class EditProfileScreen extends StatelessWidget {
EditProfileViewModel viewModel = Provider.of<EditProfileViewModel>(context);

return Scaffold(
// appBar: AppBar(
// backgroundColor: Theme.of(context).colorScheme.surface,
// elevation: 0,
// title: Center(
// child: Text(
// 'Editar perfil',
// style: TextStyle(
// color: Theme.of(context).colorScheme.onSurface,
// fontSize: 24,
// ),
// ),
// ),
// actions: [
// IconButton(
// color: Theme.of(context).colorScheme.primary,
// icon: const Icon(Icons.notifications),
// onPressed: () {},
// ),
// ],
// leading: IconButton(
// color: Theme.of(context).colorScheme.primary,
// icon: const Icon(Icons.arrow_back),
// onPressed: () {
// Navigator.of(context).pop();
// },
// ),
// ),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
children: [
_buildProfileHeader(context),
const SizedBox(height: 50),
_buildForm(context, viewModel),
],
),
));
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.surface,
elevation: 0,
title: Center(
child: Text(
'Editar perfil',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontSize: 24,
),
),
),
leading: IconButton(
color: Theme.of(context).colorScheme.primary,
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();
},
),
),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
children: [
const SizedBox(height: 10),
_buildForm(context, viewModel),
],
),
));
}

Widget _buildForm(BuildContext context, EditProfileViewModel viewModel) {
Expand Down Expand Up @@ -97,25 +87,13 @@ class EditProfileScreen extends StatelessWidget {
);
}

Widget _buildProfileHeader(BuildContext context) {
return const Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ProfileHeader(
name: "Stefani",
role: "Estudante",
),
]);
}

Widget _saveButton(BuildContext context, EditProfileViewModel viewModel) {
return Requestbutton(
command: viewModel.editCommand,
nameButton: "Salvar",
onErrorCallback: (e) {
showDialog<Object>(
context: context,
builder: (BuildContext context) => ErrorPopUp(content: Text(e)),
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e)),
);
},
onSuccessCallback: () {
Expand Down
38 changes: 38 additions & 0 deletions lib/ui/profile/view/ProfileView.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:aranduapp/ui/edit_password/view/edit_password_view.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -59,6 +60,43 @@ class Profile extends StatelessWidget {
children: [
_buildProfileHeader(context),
const SizedBox(height: 80),
SizedBox(
width: double.infinity,
height: 60,
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const EditPassword(),
),
);
},
child: Row(
children: [
Icon(
Icons.lock_reset,
color: Theme.of(context).colorScheme.primary,
size: 32,
),
const Text('Trocar senha'),
Icon(
Icons.chevron_right,
color: Theme.of(context).colorScheme.primary,
size: 32,
),
],
),
)
],
),
),
),
const SizedBox(height: 80),
_buildLogoutButton(context),
],
),
Expand Down
Loading

0 comments on commit 6d45993

Please sign in to comment.