diff --git a/lib/models/user/user_state_model.dart b/lib/models/user/user_state_model.dart index f8b757bf..65d958b9 100644 --- a/lib/models/user/user_state_model.dart +++ b/lib/models/user/user_state_model.dart @@ -18,6 +18,8 @@ class UserState { final Semester? current; final String? currentAsString; final List? displayedCourses; + final List? displayedPinnedCourses; + const UserState({ this.isLoading = false, this.user, @@ -33,6 +35,7 @@ class UserState { this.current, this.currentAsString, this.displayedCourses, + this.displayedPinnedCourses, }); UserState copyWith({ @@ -49,7 +52,8 @@ class UserState { List? semestersAsString, Semester? current, String? currentAsString, - List? displayedCourses,}) { + List? displayedCourses, + List? displayedPinnedCourses,}) { return UserState( isLoading: isLoading ?? this.isLoading, user: user ?? this.user, @@ -65,6 +69,7 @@ class UserState { current: current ?? this.current, currentAsString: currentAsString ?? this.currentAsString, displayedCourses: displayedCourses ?? this.displayedCourses, + displayedPinnedCourses: displayedPinnedCourses ?? this.displayedPinnedCourses, ); } @@ -77,6 +82,8 @@ class UserState { List? publicCourses, AppError? error, List? downloadedCourses, + List? displayedCourses, + List? displayedPinnedCourses, List? semesters, }) { return UserState( @@ -89,6 +96,12 @@ class UserState { error: null, downloadedCourses: downloadedCourses ?? this.downloadedCourses, semesters: semesters ?? this.semesters, + selectedSemester: selectedSemester, + semestersAsString: semestersAsString, + current: current, + currentAsString: currentAsString, + displayedCourses: displayedCourses ?? this.displayedCourses, + displayedPinnedCourses: displayedPinnedCourses ?? this.displayedPinnedCourses, ); } } diff --git a/lib/view_models/user_view_model.dart b/lib/view_models/user_view_model.dart index 852d9ac2..24ed55b8 100644 --- a/lib/view_models/user_view_model.dart +++ b/lib/view_models/user_view_model.dart @@ -134,7 +134,7 @@ class UserViewModel extends StateNotifier { try { var courses = await PinnedHandler(_grpcHandler).fetchUserPinned(); state = state.copyWith(userPinned: courses, isLoading: false); - setUpDisplayedCourses(state.userPinned ?? []); + setUpDisplayedPinnedCourses(state.userPinned ?? []); } catch (e) { _logger.e(e); state = state.copyWith(error: e as AppError, isLoading: false); @@ -216,6 +216,15 @@ class UserViewModel extends StateNotifier { ); } + void updateSelectedPinnedSemester(String? semester, List allCourses) { + state = state.copyWith(selectedSemester: semester); + updatedDisplayedPinnedCourses(CourseUtils.filterCoursesBySemester( + allCourses, + state.selectedSemester ?? 'All', + ), + ); + } + void setSemestersAsString(List semesters) { state = state.copyWith( semestersAsString: CourseUtils.convertAndSortSemesters(semesters, true), @@ -226,6 +235,11 @@ class UserViewModel extends StateNotifier { state = state.copyWith(displayedCourses: displayedCourses); } + void updatedDisplayedPinnedCourses(List displayedPinnedCourses) { + state = state.copyWith(displayedPinnedCourses: displayedPinnedCourses); + } + + void setUpDisplayedCourses(List allCourses) { CourseUtils.sortCourses(allCourses, 'Newest First'); updatedDisplayedCourses(CourseUtils.filterCoursesBySemester( @@ -233,7 +247,14 @@ class UserViewModel extends StateNotifier { state.selectedSemester ?? 'All', ), ); - } - + + void setUpDisplayedPinnedCourses(List allCourses) { + CourseUtils.sortCourses(allCourses, 'Newest First'); + updatedDisplayedPinnedCourses(CourseUtils.filterCoursesBySemester( + allCourses, + state.selectedSemester ?? 'All', + ), + ); + } } diff --git a/lib/views/course_view/pinned_courses_view/pinned_courses_view.dart b/lib/views/course_view/pinned_courses_view/pinned_courses_view.dart index b7853988..5396b38a 100644 --- a/lib/views/course_view/pinned_courses_view/pinned_courses_view.dart +++ b/lib/views/course_view/pinned_courses_view/pinned_courses_view.dart @@ -42,33 +42,33 @@ class PinnedCoursesState extends ConsumerState { var userPinned = ref.watch(userViewModelProvider).userPinned ?? []; ref .read(userViewModelProvider.notifier) - .updateSelectedSemester(selectedSemester, userPinned); + .updateSelectedPinnedSemester(selectedSemester, userPinned); } void _searchCourses() { final userViewModelNotifier = ref.read(userViewModelProvider.notifier); final searchInput = searchController.text.toLowerCase(); var displayedCourses = - ref.watch(userViewModelProvider).displayedCourses ?? []; + ref.watch(userViewModelProvider).displayedPinnedCourses ?? []; if (!isSearchInitialized) { temp = List.from(displayedCourses); isSearchInitialized = true; } if (searchInput.isEmpty) { - userViewModelNotifier.updatedDisplayedCourses(temp); + userViewModelNotifier.updatedDisplayedPinnedCourses(temp); isSearchInitialized = false; } else { displayedCourses = displayedCourses.where((course) { return course.name.toLowerCase().contains(searchInput) || course.slug.toLowerCase().contains(searchInput); }).toList(); - userViewModelNotifier.updatedDisplayedCourses(displayedCourses); + userViewModelNotifier.updatedDisplayedPinnedCourses(displayedCourses); } } @override Widget build(BuildContext context) { - final userPinned = ref.watch(userViewModelProvider).displayedCourses ?? []; + final userPinned = ref.watch(userViewModelProvider).displayedPinnedCourses ?? []; final liveStreams = ref.watch(videoViewModelProvider).liveStreams ?? []; var liveCourseIds = liveStreams.map((stream) => stream.courseID).toSet(); List liveCourses = userPinned.where((course) => liveCourseIds.contains(course.id)).toList();