From c2f09a78c44ee8aa9aba4cd22672f83e52f7dd2e Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Tue, 14 Jan 2025 17:45:30 -0300 Subject: [PATCH] =?UTF-8?q?ref(#58):=20adicionar=20inje=C3=A7=C3=A3o=20de?= =?UTF-8?q?=20depend=C3=AAncia=20entre=20camadas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/di/locator.dart | 8 ++++++++ lib/main.dart | 4 ++++ lib/ui/login/di/di.dart | 12 ++++++++++++ .../{LoginService.dart => login_service.dart} | 4 ++-- lib/ui/login/view/login_view.dart | 8 ++++---- .../login_viewmodel.dart} | 8 ++++---- pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + test/ui/login/view/login_view_test.dart | 18 +++++++++--------- test/ui/login/view/login_view_test.mocks.dart | 2 +- 10 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 lib/core/di/locator.dart create mode 100644 lib/ui/login/di/di.dart rename lib/ui/login/service/{LoginService.dart => login_service.dart} (90%) rename lib/ui/login/{viewModel/login_view_model.dart => viewmodel/login_viewmodel.dart} (87%) diff --git a/lib/core/di/locator.dart b/lib/core/di/locator.dart new file mode 100644 index 0000000..61fe8dd --- /dev/null +++ b/lib/core/di/locator.dart @@ -0,0 +1,8 @@ +import 'package:aranduapp/ui/login/di/di.dart'; +import 'package:get_it/get_it.dart'; + +final GetIt locator = GetIt.instance; + +void setupLocator() { + setupLoginDI(); +} diff --git a/lib/main.dart b/lib/main.dart index e39bf39..623bad5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:aranduapp/config/ThemeApp.dart'; +import 'package:aranduapp/core/di/locator.dart'; import 'package:aranduapp/ui/welcome/view/WelcomeView.dart'; import 'package:flutter/material.dart'; @@ -11,6 +12,9 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { + + setupLocator(); + return MaterialApp( theme: ThemeApp.themeData(), darkTheme: ThemeApp.darkThemeData(), diff --git a/lib/ui/login/di/di.dart b/lib/ui/login/di/di.dart new file mode 100644 index 0000000..c9133a5 --- /dev/null +++ b/lib/ui/login/di/di.dart @@ -0,0 +1,12 @@ +import 'package:aranduapp/ui/login/service/login_service.dart'; +import 'package:aranduapp/ui/login/viewmodel/login_viewmodel.dart'; +import 'package:get_it/get_it.dart'; + +final GetIt locator = GetIt.instance; + +void setupLoginDI() { + + locator.registerLazySingleton(() => LoginService()); + + locator.registerFactory(() => LoginViewModel()); +} diff --git a/lib/ui/login/service/LoginService.dart b/lib/ui/login/service/login_service.dart similarity index 90% rename from lib/ui/login/service/LoginService.dart rename to lib/ui/login/service/login_service.dart index 0d85360..60303bb 100644 --- a/lib/ui/login/service/LoginService.dart +++ b/lib/ui/login/service/login_service.dart @@ -7,7 +7,7 @@ import 'package:aranduapp/ui/login/model/LoginResponse.dart'; import 'package:dio/dio.dart'; class LoginService { - static Future login(LoginRequest loginRequest) async { + Future login(LoginRequest loginRequest) async { Log.d('${loginRequest.email} ${loginRequest.password}'); Response response = await BaseApi.getInstance(auth: false).post( @@ -27,7 +27,7 @@ class LoginService { return response; } - static Future validateToken() async { + Future validateToken() async { await AuthService().refreshToken(); } } diff --git a/lib/ui/login/view/login_view.dart b/lib/ui/login/view/login_view.dart index 0481108..cde42d9 100644 --- a/lib/ui/login/view/login_view.dart +++ b/lib/ui/login/view/login_view.dart @@ -3,10 +3,11 @@ import 'package:aranduapp/ui/navbar/view/navBarView.dart'; import 'package:aranduapp/ui/shared/TextAndLink.dart'; import 'package:aranduapp/ui/shared/requestbutton.dart'; import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; import 'package:provider/provider.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; -import 'package:aranduapp/ui/login/viewModel/login_view_model.dart'; +import 'package:aranduapp/ui/login/viewmodel/login_viewmodel.dart'; import 'package:aranduapp/ui/recover_account/view/recover_account_view.dart'; import 'package:aranduapp/ui/register_account/view/register_account_view.dart'; @@ -19,11 +20,10 @@ import 'package:aranduapp/ui/shared/OrDivider.dart'; class Login extends StatelessWidget { const Login({super.key}); - @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (context) => LoginViewModel(), + return ChangeNotifierProvider.value( + value: GetIt.instance(), child: const LoginScreen(), ); } diff --git a/lib/ui/login/viewModel/login_view_model.dart b/lib/ui/login/viewmodel/login_viewmodel.dart similarity index 87% rename from lib/ui/login/viewModel/login_view_model.dart rename to lib/ui/login/viewmodel/login_viewmodel.dart index d8a755a..759a3d7 100644 --- a/lib/ui/login/viewModel/login_view_model.dart +++ b/lib/ui/login/viewmodel/login_viewmodel.dart @@ -1,10 +1,10 @@ import 'package:aranduapp/core/log/Log.dart'; import 'package:aranduapp/core/state/command.dart'; -import 'package:aranduapp/ui/navbar/view/navBarView.dart'; import 'package:async/async.dart'; import 'package:flutter/material.dart'; +import 'package:get_it/get_it.dart'; import 'package:local_auth/local_auth.dart'; -import 'package:aranduapp/ui/login/service/LoginService.dart'; +import 'package:aranduapp/ui/login/service/login_service.dart'; import 'package:aranduapp/ui/login/model/LoginRequest.dart'; class LoginViewModel extends ChangeNotifier { @@ -32,14 +32,14 @@ class LoginViewModel extends ChangeNotifier { return Result.error(Exception('Valores inválidos')); } - await LoginService.login( + await GetIt.instance().login( LoginRequest(emailController.text, passwordController.text)); return Result.value(null); } Future> validateToken() async { - await LoginService.validateToken(); + await GetIt.instance().validateToken(); return Result.value(null); } diff --git a/pubspec.lock b/pubspec.lock index cd7aa9a..8158b4c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -341,6 +341,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.0" + get_it: + dependency: "direct main" + description: + name: get_it + sha256: f126a3e286b7f5b578bf436d5592968706c4c1de28a228b870ce375d9f743103 + url: "https://pub.dev" + source: hosted + version: "8.0.3" glob: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 94dd8b2..8f02ee7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -47,6 +47,7 @@ dependencies: mockito: ^5.4.4 build_runner: ^2.4.13 async: ^2.11.0 + get_it: ^8.0.3 dev_dependencies: diff --git a/test/ui/login/view/login_view_test.dart b/test/ui/login/view/login_view_test.dart index cf2186a..995fd31 100644 --- a/test/ui/login/view/login_view_test.dart +++ b/test/ui/login/view/login_view_test.dart @@ -1,15 +1,15 @@ import 'package:aranduapp/core/state/command.dart'; import 'package:aranduapp/ui/login/view/login_view.dart'; -import 'package:aranduapp/ui/login/viewModel/login_view_model.dart'; +import 'package:aranduapp/ui/login/viewmodel/login_viewmodel.dart'; import 'package:aranduapp/ui/navbar/view/navBarView.dart'; import 'package:aranduapp/ui/shared/TextEmail.dart'; import 'package:aranduapp/ui/shared/TextPassword.dart'; import 'package:async/async.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:get_it/get_it.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; -import 'package:provider/provider.dart'; @GenerateNiceMocks([MockSpec(), MockSpec()]) import 'login_view_test.mocks.dart'; @@ -19,7 +19,7 @@ void main() { late MockCommand0 mockLoginCommand; late MockCommand0 mockValidadeTokenCommand; - setUp(() { + setUp(() async { mockViewModel = MockLoginViewModel(); when(mockViewModel.formKey).thenReturn(GlobalKey()); when(mockViewModel.emailController).thenReturn(TextEditingController()); @@ -38,14 +38,14 @@ void main() { when(mockValidadeTokenCommand.running).thenReturn(false); when(mockValidadeTokenCommand.isError).thenReturn(false); when(mockValidadeTokenCommand.isOk).thenReturn(false); + + await GetIt.instance.reset(); + GetIt.I.registerLazySingleton(() => mockViewModel); }); Widget createLoginScreen() { - return ChangeNotifierProvider.value( - value: mockViewModel, - child: const MaterialApp( - home: LoginScreen(), - ), + return const MaterialApp( + home: Login(), ); } @@ -125,6 +125,6 @@ void main() { }); testWidgets('Displays error when login fails', (WidgetTester tester) async { - //TODO: + //TODO: }); } diff --git a/test/ui/login/view/login_view_test.mocks.dart b/test/ui/login/view/login_view_test.mocks.dart index 21468ae..a58f803 100644 --- a/test/ui/login/view/login_view_test.mocks.dart +++ b/test/ui/login/view/login_view_test.mocks.dart @@ -6,7 +6,7 @@ import 'dart:async' as _i6; import 'package:aranduapp/core/state/command.dart' as _i2; -import 'package:aranduapp/ui/login/viewModel/login_view_model.dart' as _i5; +import 'package:aranduapp/ui/login/viewmodel/login_viewmodel.dart' as _i5; import 'package:async/async.dart' as _i4; import 'package:flutter/material.dart' as _i3; import 'package:mockito/mockito.dart' as _i1;