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 81593fd..5a41dd4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:aranduapp/config/theme_app.dart'; import 'package:aranduapp/ui/welcome/view/welcome_view.dart'; +import 'package:aranduapp/core/di/locator.dart'; import 'package:flutter/material.dart'; void main() { @@ -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/login_service.dart b/lib/ui/login/service/login_service.dart index b406dcc..b888c36 100644 --- a/lib/ui/login/service/login_service.dart +++ b/lib/ui/login/service/login_service.dart @@ -7,7 +7,7 @@ import 'package:aranduapp/ui/login/model/login_response.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 af22c2b..c5ea304 100644 --- a/lib/ui/login/view/login_view.dart +++ b/lib/ui/login/view/login_view.dart @@ -3,6 +3,7 @@ import 'package:aranduapp/ui/navbar/view/navbar_view.dart'; import 'package:aranduapp/ui/shared/text_and_link.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'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; @@ -19,11 +20,10 @@ import 'package:aranduapp/ui/shared/or_divider.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_viewmodel.dart b/lib/ui/login/viewmodel/login_viewmodel.dart index 7491ab7..b60cae1 100644 --- a/lib/ui/login/viewmodel/login_viewmodel.dart +++ b/lib/ui/login/viewmodel/login_viewmodel.dart @@ -1,8 +1,8 @@ import 'package:aranduapp/core/log/log.dart'; import 'package:aranduapp/core/state/command.dart'; -import 'package:aranduapp/ui/navbar/view/navbar_view.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/login_service.dart'; import 'package:aranduapp/ui/login/model/login_request.dart'; @@ -30,14 +30,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 8a92626..6113b52 100644 --- a/test/ui/login/view/login_view_test.dart +++ b/test/ui/login/view/login_view_test.dart @@ -7,9 +7,9 @@ import 'package:aranduapp/ui/shared/text_password.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(), ); }