Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT Adiciona GetIt ao recover account e seu teste #80

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
15 changes: 12 additions & 3 deletions lib/core/di/locator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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:aranduapp/ui/edit_password/di/di_edit_password.dart';
import 'package:aranduapp/ui/edit_profile/di/di_edit_profile.dart';
import 'package:aranduapp/ui/login/di/di_login.dart';
import 'package:aranduapp/ui/navbar/di/di_navbar.dart';
import 'package:aranduapp/ui/onboarding/di/di_onboarding.dart';
import 'package:aranduapp/ui/recover_account/di/di_recover_account.dart';
import 'package:aranduapp/ui/register_account/di/di_register_account.dart';
import 'package:get_it/get_it.dart';

final GetIt locator = GetIt.instance;
Expand All @@ -9,4 +13,9 @@ void setupLocator() {
setupLoginDI();
setupRegisterDI();
setupEditProfileDI();
setupRecoverAccountDI();
setupEditPasswordDI();
setupNavBarDI();
setupOnboardingDI();
setupLoginDI();
}
9 changes: 9 additions & 0 deletions lib/ui/edit_password/di/di_edit_password.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:aranduapp/ui/edit_password/service/edit_password_service.dart';
import 'package:aranduapp/ui/edit_password/viewmodel/edit_password_viewmodel.dart';
import 'package:get_it/get_it.dart';

void setupEditPasswordDI() {
GetIt.I.registerLazySingleton(() => EditPasswordService());

GetIt.I.registerFactory(() => EditPasswordViewModel());
}
5 changes: 1 addition & 4 deletions lib/ui/edit_password/service/edit_password_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ 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 {
Future<void> edit(EditPasswordRequest editPasswordRequest) async {
await BaseApi.getInstance(auth: true)
.put(path: '/auth/change-password', data: editPasswordRequest.toJson());
}


}
6 changes: 3 additions & 3 deletions lib/ui/edit_password/view/edit_password_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import 'package:aranduapp/ui/edit_password/viewmodel/edit_password_viewmodel.dar
import 'package:aranduapp/ui/shared/request_button.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:get_it/get_it.dart';
import 'package:aranduapp/ui/shared/text_password.dart';

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

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => EditPasswordViewModel(),
return ChangeNotifierProvider<EditPasswordViewModel>.value(
value: GetIt.instance<EditPasswordViewModel>(),
child: const EditPasswordScreen(),
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/edit_password/viewmodel/edit_password_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';
import 'package:get_it/get_it.dart';

class EditPasswordViewModel extends ChangeNotifier {
final GlobalKey<FormState> formKey;
Expand All @@ -27,7 +28,7 @@ class EditPasswordViewModel extends ChangeNotifier {
oldPassword: oldPasswordController.text,
newPassword: newPasswordController.text);

await EditPasswordService.edit(request);
await GetIt.instance<EditPasswordService>().edit(request);

return Result.value(null);
}
Expand Down
File renamed without changes.
15 changes: 5 additions & 10 deletions lib/ui/navbar/view/navbar_view.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import 'package:aranduapp/ui/home/view/home_view.dart';
import 'package:aranduapp/ui/profile/view/profile_view.dart';
//import 'package:aranduapp/ui/profile/view/profileView.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:get_it/get_it.dart';
import 'package:aranduapp/ui/navbar/viewmodel/navbar_viewmodel.dart';

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

@override
Widget build(BuildContext context) {
final NavbarViewModel viewModel = GetIt.I<NavbarViewModel>();

return Scaffold(
body: ChangeNotifierProvider(
create: (context) => NavbarViewModel(),
builder: (context, child) {
return page(context);
}),
body: page(context, viewModel),
);
}
}

Widget page(BuildContext context) {
NavbarViewModel viewModel = Provider.of<NavbarViewModel>(context);

Widget page(BuildContext context, NavbarViewModel viewModel) {
final List<Widget> pages = [
const HomeView(),
const Center(child: Text('Friends', style: TextStyle(fontSize: 20))),
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/navbar/viewmodel/navbar_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:flutter/material.dart';

class NavbarViewModel extends ChangeNotifier {
late Command0<void> navbarCommand;
int _selectedIndex = 0;

int get selectedIndex => _selectedIndex;
Expand Down
50 changes: 23 additions & 27 deletions lib/ui/onboarding/view/onboarding_view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:get_it/get_it.dart';
import '../viewmodel/onboarding_viewmodel.dart';

class OnboardingView extends StatelessWidget {
Expand All @@ -9,48 +9,44 @@ class OnboardingView extends StatelessWidget {
{
'title': 'Bem-Vindo(a)!',
'description':
'Seja bem vindo ao Arandu!!! Sou um ambiente voltado para aprendizado de forma gratuita',
'Seja bem-vindo ao Arandu!!! Sou um ambiente voltado para aprendizado de forma gratuita',
'imageAsset': 'assets/images/Component1.png',
},
{
'title': 'o que você encontra?',
'title': 'O que você encontra?',
'description':
'Aqui você encontrará livros interativos de diversas disciplinas com objetivo de entregar praticidade e qualidade no aprendizado.',
'Aqui você encontrará livros interativos de diversas disciplinas com o objetivo de entregar praticidade e qualidade no aprendizado.',
'imageAsset': 'assets/images/Component2.png',
},
{
'title': 'Como funciona?',
'description':
'Para acessar uma trilha basta se inscrever na disciplina de sua preferência e ter acesso aos materiais. \n Bons estudos !!!',
'Para acessar uma trilha basta se inscrever na disciplina de sua preferência e ter acesso aos materiais. \n Bons estudos!!!',
'imageAsset': 'assets/images/Component3.png',
},
];

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => OnboardingViewModel(),
child: Consumer<OnboardingViewModel>(
builder: (context, viewModel, _) {
return Scaffold(
appBar: null,
body: LayoutBuilder(
builder: (context, constraints) {
final double imageHeight = constraints.maxHeight * 0.60;
final double contentHeight = constraints.maxHeight * 0.55;
final double buttonHeight = constraints.maxHeight * 0.15;
// Pegando a instância do OnboardingViewModel do GetIt
final onboardingViewModel = GetIt.I<OnboardingViewModel>();

return Stack(
children: [
_buildSemicirclePage(context),
_buildImageSection(viewModel, steps, imageHeight),
_buildContentSection(viewModel, steps, contentHeight),
_buildNavigationButtons(
context, viewModel, steps, buttonHeight),
],
);
},
),
return Scaffold(
appBar: null,
body: LayoutBuilder(
builder: (context, constraints) {
final double imageHeight = constraints.maxHeight * 0.60;
final double contentHeight = constraints.maxHeight * 0.55;
final double buttonHeight = constraints.maxHeight * 0.15;

return Stack(
children: [
_buildSemicirclePage(context),
_buildImageSection(onboardingViewModel, steps, imageHeight),
_buildContentSection(onboardingViewModel, steps, contentHeight),
_buildNavigationButtons(
context, onboardingViewModel, steps, buttonHeight),
],
);
},
),
Expand Down
13 changes: 8 additions & 5 deletions lib/ui/profile/view/profile_view.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import 'package:aranduapp/ui/edit_password/view/edit_password_view.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';

import 'package:provider/provider.dart';
import 'package:get_it/get_it.dart';
import 'package:aranduapp/ui/shared/profile_header.dart';
import 'package:aranduapp/ui/profile/viewmodel/profile_viewmodel.dart';
import 'package:aranduapp/ui/edit_profile/view/edit_profile_view.dart';
import 'package:aranduapp/ui/edit_password/view/edit_password_view.dart';

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

@override
Widget build(BuildContext context) {
// Usando GetIt para acessar a instância da ProfileViewModel
final profileViewModel = GetIt.I<ProfileViewModel>();

return Scaffold(
appBar: _buildAppBar(context),
body: ChangeNotifierProvider(
create: (context) => ProfileViewModel(context),
body: ChangeNotifierProvider.value(
value: profileViewModel,
builder: (context, child) {
return _buildPage(context);
},
Expand Down
5 changes: 2 additions & 3 deletions lib/ui/profile/viewmodel/profile_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:flutter/material.dart';

class ProfileViewModel extends ChangeNotifier {
final BuildContext context;

// Controllers e Key para o formulário
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController emailController = TextEditingController();
Expand All @@ -11,7 +9,8 @@ class ProfileViewModel extends ChangeNotifier {
bool _isLoading = false;
bool get isLoading => _isLoading;

ProfileViewModel(this.context);
// ProfileViewModel não precisa mais do BuildContext
ProfileViewModel();

// Método para simular o envio do e-mail de recuperação de senha
Future<void> forgetPassword() async {
Expand Down
9 changes: 9 additions & 0 deletions lib/ui/recover_account/di/di_recover_account.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:aranduapp/ui/recover_account/service/recover_account_service.dart';
import 'package:aranduapp/ui/recover_account/viewmodel/recover_account_viewmodel.dart';
import 'package:get_it/get_it.dart';

void setupRecoverAccountDI() {
GetIt.I.registerLazySingleton(() => RecoverAccountService());

GetIt.I.registerFactory(() => RecoverAccountViewModel());
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:aranduapp/core/network/base_api.dart';
import 'package:aranduapp/ui/recover_account/model/recover_account_request.dart';

class RecoverAccountService {
static Future<void> forgetPassword(
Future<void> forgetPassword(
RecoverAccountRequest recoverAccountRequest) async {
await BaseApi.getInstance(auth: false)
.post(path: '/auth/forgot-password', data: <String, dynamic>{
Expand Down
11 changes: 5 additions & 6 deletions lib/ui/recover_account/view/recover_account_view.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import 'package:aranduapp/core/log/log.dart';
import 'package:aranduapp/ui/recover_account/viewmodel/recover_account_viewmodel.dart';
import 'package:aranduapp/ui/shared/error_popup.dart';
import 'package:aranduapp/ui/shared/text_and_link.dart';
import 'package:aranduapp/ui/shared/text_email.dart';
import 'package:aranduapp/ui/shared/title_slogan.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 RecoverAccount extends StatelessWidget {
const RecoverAccount({super.key});

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => RecoverAccountViewModel(),
builder: (context, child) {
return const RecoverAccountScreen();
});
return ChangeNotifierProvider<RecoverAccountViewModel>.value(
value: GetIt.instance<RecoverAccountViewModel>(),
child: const RecoverAccountScreen(),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:aranduapp/core/log/log.dart';
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/recover_account/model/recover_account_request.dart';
import 'package:aranduapp/ui/recover_account/service/recover_account_service.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

class RecoverAccountViewModel extends ChangeNotifier {
GlobalKey<FormState> formKey;
Expand All @@ -22,9 +22,8 @@ class RecoverAccountViewModel extends ChangeNotifier {
if (!formKey.currentState!.validate()) {
Result.error('Valores inválidos');
}

await RecoverAccountService.forgetPassword(
RecoverAccountRequest(emailController.text));
await GetIt.instance<RecoverAccountService>()
.forgetPassword(RecoverAccountRequest(emailController.text));

return Result.value(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:aranduapp/ui/register_account/viewmodel/register_viewmodel.dart'
import 'package:get_it/get_it.dart';

void setupRegisterDI() {

GetIt.I.registerLazySingleton(() => RegisterService());

GetIt.I.registerFactory(() => RegisterAccountViewModel());
Expand Down
49 changes: 49 additions & 0 deletions test/ui/navbar/view/navbar_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:aranduapp/ui/navbar/view/navbar_view.dart';
import 'package:aranduapp/ui/navbar/viewmodel/navbar_viewmodel.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 'navbar_test.mocks.dart';

@GenerateNiceMocks([MockSpec<NavbarViewModel>()])
void main() {
late MockNavbarViewModel mockNavbarViewModel;

setUp(() {
mockNavbarViewModel = MockNavbarViewModel();

// Define os comportamentos padrão para o mock
when(mockNavbarViewModel.selectedIndex).thenReturn(0);
when(mockNavbarViewModel.hasListeners).thenReturn(false);

// Registra o mock como a instância do ViewModel usada pelo GetIt
GetIt.I.registerSingleton<NavbarViewModel>(mockNavbarViewModel);
});

Widget createScreen() {
return const MaterialApp(
home: NavbarView(),
);
}

//testWidgets('Navbar screen displays', (WidgetTester tester) async {
// await tester.pumpWidget(createScreen());

// Verifique a presença do BottomNavigationBar
//expect(find.byType(BottomNavigationBar), findsOneWidget);

// Verifique se os itens de navegação (exceto Home) estão presentes
//expect(find.byKey(Key('amigosTab')), findsOneWidget);
//expect(find.byKey(Key('dashboardTab')), findsOneWidget);
//expect(find.byKey(Key('perfilTab')), findsOneWidget);

// Verifique se o índice selecionado foi chamado
verify(mockNavbarViewModel.selectedIndex).called(1);

// Verifique se o mock não foi chamado mais de uma vez
// verifyNoMoreInteractions(mockNavbarViewModel);
// });
}
Loading