From 3e6c61f67fbbf4cf4ee88b209a781f21daba4951 Mon Sep 17 00:00:00 2001 From: Lukas <35173609+PHPLukaas@users.noreply.github.com> Date: Wed, 13 Mar 2024 18:51:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B:=20fix=20with=20redirection=20betw?= =?UTF-8?q?een=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/api/users/entities/user_entity.dart | 6 ++--- .../lib/api/users/models/user_model.dart | 2 +- .../users/repositories/user_repository.dart | 11 +++++++-- .../week_schedule_repository.dart | 2 +- .../class_groups/cubit/class_group_cubit.dart | 2 +- .../lib/login/cubit/login_cubit.dart | 7 +++--- frontend/app_student/lib/routes.dart | 24 +++++++++++++++++++ .../cubit/week_schedule_cubit.dart | 15 +++++++++++- .../week_schedule/views/week_schedule.dart | 11 +++++---- 9 files changed, 63 insertions(+), 17 deletions(-) diff --git a/frontend/app_student/lib/api/users/entities/user_entity.dart b/frontend/app_student/lib/api/users/entities/user_entity.dart index 5acce5b..75b3923 100644 --- a/frontend/app_student/lib/api/users/entities/user_entity.dart +++ b/frontend/app_student/lib/api/users/entities/user_entity.dart @@ -1,14 +1,12 @@ -import 'package:app_student/api/class_groups/entities/class_group_entity.dart'; - class UserEntity { final String ine; final String firstName; final DateTime birthDate; - final ClassGroupEntity? classGroup; + final String? promotionName; UserEntity( {required this.ine, required this.firstName, required this.birthDate, - this.classGroup}); + this.promotionName = ''}); } diff --git a/frontend/app_student/lib/api/users/models/user_model.dart b/frontend/app_student/lib/api/users/models/user_model.dart index 1bb562d..6d3c6b3 100644 --- a/frontend/app_student/lib/api/users/models/user_model.dart +++ b/frontend/app_student/lib/api/users/models/user_model.dart @@ -8,5 +8,5 @@ class UserModel { String get file => entity.ine; String get name => entity.firstName; DateTime get birthDate => entity.birthDate; - String get className => entity.classGroup?.name ?? ''; + String? get promotionName => entity.promotionName ?? ''; } diff --git a/frontend/app_student/lib/api/users/repositories/user_repository.dart b/frontend/app_student/lib/api/users/repositories/user_repository.dart index da4596a..28bfffb 100644 --- a/frontend/app_student/lib/api/users/repositories/user_repository.dart +++ b/frontend/app_student/lib/api/users/repositories/user_repository.dart @@ -11,14 +11,13 @@ class UserRepository { String? name = prefs.getString('name'); String? birthDate = prefs.getString('birthDate'); String? className = prefs.getString('className'); - if (ine != null && name != null && birthDate != null) { return UserModel( entity: UserEntity( ine: ine, firstName: name, birthDate: DateTime.parse(birthDate), - classGroup: null, + promotionName: className, ), ); } else { @@ -29,6 +28,14 @@ class UserRepository { Future saveUserDetails( String ine, String name, String birthDate, String className) async { SharedPreferences prefs = await SharedPreferences.getInstance(); + UserModel( + entity: UserEntity( + ine: ine, + firstName: name, + birthDate: DateTime.parse(birthDate), + promotionName: className, + ), + ); await prefs.setString('ine', ine); await prefs.setString('name', name); await prefs.setString('birthDate', birthDate); diff --git a/frontend/app_student/lib/api/week_schedule/repositories/week_schedule_repository.dart b/frontend/app_student/lib/api/week_schedule/repositories/week_schedule_repository.dart index 8eaf13e..0f303cc 100644 --- a/frontend/app_student/lib/api/week_schedule/repositories/week_schedule_repository.dart +++ b/frontend/app_student/lib/api/week_schedule/repositories/week_schedule_repository.dart @@ -5,7 +5,7 @@ import 'package:app_student/api/week_schedule/models/week_schedule_model.dart'; class WeekScheduleRepository { final ApiService apiService; - WeekScheduleRepository({required this.apiService, required String className}); + WeekScheduleRepository({required this.apiService}); Future> getWeeksSchedule(className) { return apiService.getData('/api/timetable?class_param=$className', (item) { diff --git a/frontend/app_student/lib/class_groups/cubit/class_group_cubit.dart b/frontend/app_student/lib/class_groups/cubit/class_group_cubit.dart index 3dcb97d..1622392 100644 --- a/frontend/app_student/lib/class_groups/cubit/class_group_cubit.dart +++ b/frontend/app_student/lib/class_groups/cubit/class_group_cubit.dart @@ -31,7 +31,7 @@ class ClassGroupCubit extends Cubit { } Future saveClass(ClassGroupModel classGroup) async { - await userRepository.saveUserClass(classGroup.toString()); + await userRepository.saveUserClass(classGroup.name.toString()); emit(ClassGroupSelected()); } } diff --git a/frontend/app_student/lib/login/cubit/login_cubit.dart b/frontend/app_student/lib/login/cubit/login_cubit.dart index fec1fa9..ba67eff 100644 --- a/frontend/app_student/lib/login/cubit/login_cubit.dart +++ b/frontend/app_student/lib/login/cubit/login_cubit.dart @@ -29,13 +29,14 @@ class LoginCubit extends Cubit { emit(RedirectToClassSelection()); } - Future checkUserAuthentication() async { + Future checkUserAuthentication() async { try { await userRepository.getUser(); emit(LoginAuthenticated()); + return true; } catch (e) { - emit(LoginFieldError()); + emit(LoginInitial()); + return false; } - emit(LoginInitial()); } } diff --git a/frontend/app_student/lib/routes.dart b/frontend/app_student/lib/routes.dart index e936b6f..f0417d9 100644 --- a/frontend/app_student/lib/routes.dart +++ b/frontend/app_student/lib/routes.dart @@ -2,8 +2,11 @@ import 'package:app_student/api/api_service.dart'; import 'package:app_student/api/class_groups/repositories/class_group_repository.dart'; import 'package:app_student/api/users/repositories/user_repository.dart'; +import 'package:app_student/api/week_schedule/repositories/week_schedule_repository.dart'; import 'package:app_student/class_groups/cubit/class_group_cubit.dart'; import 'package:app_student/config/config.dart'; +import 'package:app_student/week_schedule/cubit/week_schedule_cubit.dart'; +import 'package:app_student/week_schedule/views/week_schedule.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; @@ -15,6 +18,7 @@ import 'login/views/login_page.dart'; class AppRoutes { static const classListPage = '/classList'; static const loginPage = '/login'; + static const schedulePage = '/schedule'; static final routes = [ GoRoute( @@ -47,5 +51,25 @@ class AppRoutes { ), ), ), + GoRoute( + path: schedulePage, + pageBuilder: (context, state) => MaterialPage( + key: state.pageKey, + child: MultiRepositoryProvider( + providers: [ + RepositoryProvider(create: (context) { + return WeekScheduleRepository( + apiService: + ApiService(apiUrl: context.read().apiUrl)); + }), + RepositoryProvider(create: (context) => UserRepository()), + ], + child: BlocProvider( + create: (context) => WeekScheduleCubit( + weekScheduleRepository: + context.read(), + userRepository: context.read(), + ), + child: const WeekSchedulePage())))), ]; } diff --git a/frontend/app_student/lib/week_schedule/cubit/week_schedule_cubit.dart b/frontend/app_student/lib/week_schedule/cubit/week_schedule_cubit.dart index fa507ff..9777f6e 100644 --- a/frontend/app_student/lib/week_schedule/cubit/week_schedule_cubit.dart +++ b/frontend/app_student/lib/week_schedule/cubit/week_schedule_cubit.dart @@ -2,14 +2,22 @@ import 'package:app_student/api/week_schedule/repositories/week_schedule_reposit import 'package:bloc/bloc.dart'; import 'package:meta/meta.dart'; +import '../../api/users/repositories/user_repository.dart'; + part 'week_schedule_state.dart'; class WeekScheduleCubit extends Cubit { final WeekScheduleRepository weekScheduleRepository; + final UserRepository userRepository; - WeekScheduleCubit({required this.weekScheduleRepository}) + WeekScheduleCubit( + {required this.weekScheduleRepository, required this.userRepository}) : super(WeekScheduleInitial()); + Future getUser() async { + return await userRepository.getUser(); + } + Future fetchWeekSchedule(String className) async { try { emit(WeekScheduleLoading()); @@ -20,4 +28,9 @@ class WeekScheduleCubit extends Cubit { emit(WeekScheduleError(e.toString())); } } + + void fetchUserAndSchedule() async { + final user = await getUser(); + fetchWeekSchedule(user.promotionName); + } } diff --git a/frontend/app_student/lib/week_schedule/views/week_schedule.dart b/frontend/app_student/lib/week_schedule/views/week_schedule.dart index 7c06259..b8d71eb 100644 --- a/frontend/app_student/lib/week_schedule/views/week_schedule.dart +++ b/frontend/app_student/lib/week_schedule/views/week_schedule.dart @@ -4,6 +4,8 @@ import 'package:app_student/week_schedule/views/widgets/day_schedule_widget.dart import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import '../../api/users/repositories/user_repository.dart'; + class WeekSchedulePage extends StatelessWidget { const WeekSchedulePage({super.key}); @@ -11,12 +13,13 @@ class WeekSchedulePage extends StatelessWidget { Widget build(BuildContext context) { final weekScheduleRepository = RepositoryProvider.of(context); - final weekScheduleCubit = - WeekScheduleCubit(weekScheduleRepository: weekScheduleRepository); + final userRepository = RepositoryProvider.of(context); + final weekScheduleCubit = WeekScheduleCubit( + weekScheduleRepository: weekScheduleRepository, + userRepository: userRepository); return BlocProvider( - create: (context) => - weekScheduleCubit..fetchWeekSchedule('B3 Groupe 3 DLW-FA'), + create: (context) => weekScheduleCubit..fetchUserAndSchedule(), child: Scaffold( appBar: AppBar( title: const Text('Week Schedule'),