From 7b0396731068a16f68ade1332feb085f8125aa50 Mon Sep 17 00:00:00 2001 From: EngDann Date: Thu, 16 Jan 2025 15:05:03 -0300 Subject: [PATCH] feat(#119): Adiciona GetIt em edit_profile --- lib/core/di/locator.dart | 2 ++ lib/ui/edit_password/view/edit_password_view.dart | 7 ++++--- .../viewmodel/edit_password_viewmodel.dart | 4 ++-- lib/ui/edit_profile/di/di.dart | 11 +++++++++++ lib/ui/edit_profile/service/edit_profile_service.dart | 4 +--- lib/ui/edit_profile/view/edit_profile_view.dart | 6 ++++-- .../viewmodel/edit_profile_viewmodel.dart | 11 +++++------ 7 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 lib/ui/edit_profile/di/di.dart diff --git a/lib/core/di/locator.dart b/lib/core/di/locator.dart index 1e635ec..f0508ba 100644 --- a/lib/core/di/locator.dart +++ b/lib/core/di/locator.dart @@ -1,3 +1,4 @@ +import 'package:aranduapp/ui/edit_profile/di/di.dart'; import 'package:aranduapp/ui/login/di/di.dart'; import 'package:aranduapp/ui/register_account/di/di.dart'; import 'package:get_it/get_it.dart'; @@ -7,4 +8,5 @@ final GetIt locator = GetIt.instance; void setupLocator() { setupLoginDI(); setupRegisterDI(); + setupEditProfileDI(); } diff --git a/lib/ui/edit_password/view/edit_password_view.dart b/lib/ui/edit_password/view/edit_password_view.dart index 09c8975..11d1238 100644 --- a/lib/ui/edit_password/view/edit_password_view.dart +++ b/lib/ui/edit_password/view/edit_password_view.dart @@ -11,7 +11,7 @@ class EditPassword extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider( - create: (context) => EditPasswordViewMode(), + create: (context) => EditPasswordViewModel(), child: const EditPasswordScreen(), ); } @@ -59,7 +59,8 @@ class EditPasswordScreen extends StatelessWidget { } Widget _formSection(BuildContext context) { - EditPasswordViewMode viewModel = Provider.of(context); + EditPasswordViewModel viewModel = + Provider.of(context); return Form( key: viewModel.formKey, @@ -78,7 +79,7 @@ class EditPasswordScreen extends StatelessWidget { ); } - Widget _button(BuildContext context, EditPasswordViewMode viewModel) { + Widget _button(BuildContext context, EditPasswordViewModel viewModel) { return Requestbutton( command: viewModel.editCommand, nameButton: "Enviar", diff --git a/lib/ui/edit_password/viewmodel/edit_password_viewmodel.dart b/lib/ui/edit_password/viewmodel/edit_password_viewmodel.dart index c0ddfd0..b9a80a5 100644 --- a/lib/ui/edit_password/viewmodel/edit_password_viewmodel.dart +++ b/lib/ui/edit_password/viewmodel/edit_password_viewmodel.dart @@ -4,14 +4,14 @@ 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 { +class EditPasswordViewModel extends ChangeNotifier { final GlobalKey formKey; final TextEditingController newPasswordController; final TextEditingController oldPasswordController; late Command0 editCommand; - EditPasswordViewMode() + EditPasswordViewModel() : formKey = GlobalKey(), newPasswordController = TextEditingController(), oldPasswordController = TextEditingController() { diff --git a/lib/ui/edit_profile/di/di.dart b/lib/ui/edit_profile/di/di.dart new file mode 100644 index 0000000..5b17976 --- /dev/null +++ b/lib/ui/edit_profile/di/di.dart @@ -0,0 +1,11 @@ +import 'package:aranduapp/ui/edit_profile/service/edit_profile_service.dart'; +import 'package:aranduapp/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart'; +import 'package:get_it/get_it.dart'; + +final GetIt locator = GetIt.instance; + +void setupEditProfileDI() { + locator.registerLazySingleton(() => EditProfileService()); + + locator.registerFactory(() => EditProfileViewModel()); +} diff --git a/lib/ui/edit_profile/service/edit_profile_service.dart b/lib/ui/edit_profile/service/edit_profile_service.dart index 5a948f5..e008e43 100644 --- a/lib/ui/edit_profile/service/edit_profile_service.dart +++ b/lib/ui/edit_profile/service/edit_profile_service.dart @@ -2,10 +2,8 @@ import 'package:aranduapp/core/network/base_api.dart'; import 'package:aranduapp/ui/edit_profile/model/edit_profile_request.dart'; class EditProfileService { - static Future edit(EditProfileRequest editProfileRequest) async { - + Future edit(EditProfileRequest editProfileRequest) async { await BaseApi.getInstance(auth: true) .patch(path: '/users', data: editProfileRequest.toJson()); - } } diff --git a/lib/ui/edit_profile/view/edit_profile_view.dart b/lib/ui/edit_profile/view/edit_profile_view.dart index 45c5cab..4395a6c 100644 --- a/lib/ui/edit_profile/view/edit_profile_view.dart +++ b/lib/ui/edit_profile/view/edit_profile_view.dart @@ -1,8 +1,10 @@ import 'package:aranduapp/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart'; +import 'package:aranduapp/ui/login/viewmodel/login_viewmodel.dart'; import 'package:aranduapp/ui/shared/text_email.dart'; import 'package:aranduapp/ui/shared/text_name.dart'; import 'package:aranduapp/ui/shared/request_button.dart'; import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; import 'package:provider/provider.dart'; class EditProfile extends StatelessWidget { @@ -10,8 +12,8 @@ class EditProfile extends StatelessWidget { @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (context) => EditProfileViewModel(), + return ChangeNotifierProvider.value( + value: GetIt.instance(), child: const EditProfileScreen(), ); } diff --git a/lib/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart b/lib/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart index ceb77d3..8e588a3 100644 --- a/lib/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart +++ b/lib/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart @@ -3,6 +3,7 @@ import 'package:aranduapp/ui/edit_profile/model/edit_profile_request.dart'; import 'package:aranduapp/ui/edit_profile/service/edit_profile_service.dart'; import 'package:async/async.dart'; import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; class EditProfileViewModel extends ChangeNotifier { final GlobalKey formKey; @@ -17,14 +18,12 @@ class EditProfileViewModel extends ChangeNotifier { nameController = TextEditingController(), userNameController = TextEditingController(), emailController = TextEditingController() { - - editCommand = Command0(editprofile); - + editCommand = Command0(editProfile); } - Future> editprofile() async { + Future> editProfile() async { if (!formKey.currentState!.validate()) { - return Result.error(Exception('Valores inválidos' )); + return Result.error(Exception('Valores inválidos')); } EditProfileRequest request = EditProfileRequest( @@ -32,7 +31,7 @@ class EditProfileViewModel extends ChangeNotifier { userName: userNameController.text, email: emailController.text); - await EditProfileService.edit(request); + await GetIt.instance().edit(request); return Result.value(null); }