Skip to content

Commit

Permalink
add updateDisplayPinnedCourse fo the pinned view
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Jan 27, 2024
1 parent fb97ea8 commit 7068ee4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
15 changes: 14 additions & 1 deletion lib/models/user/user_state_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class UserState {
final Semester? current;
final String? currentAsString;
final List<Course>? displayedCourses;
final List<Course>? displayedPinnedCourses;

const UserState({
this.isLoading = false,
this.user,
Expand All @@ -33,6 +35,7 @@ class UserState {
this.current,
this.currentAsString,
this.displayedCourses,
this.displayedPinnedCourses,
});

UserState copyWith({
Expand All @@ -49,7 +52,8 @@ class UserState {
List<String>? semestersAsString,
Semester? current,
String? currentAsString,
List<Course>? displayedCourses,}) {
List<Course>? displayedCourses,
List<Course>? displayedPinnedCourses,}) {
return UserState(
isLoading: isLoading ?? this.isLoading,
user: user ?? this.user,
Expand All @@ -65,6 +69,7 @@ class UserState {
current: current ?? this.current,
currentAsString: currentAsString ?? this.currentAsString,
displayedCourses: displayedCourses ?? this.displayedCourses,
displayedPinnedCourses: displayedPinnedCourses ?? this.displayedPinnedCourses,
);
}

Expand All @@ -77,6 +82,8 @@ class UserState {
List<Course>? publicCourses,
AppError? error,
List<Course>? downloadedCourses,
List<Course>? displayedCourses,
List<Course>? displayedPinnedCourses,
List<Semester>? semesters,
}) {
return UserState(
Expand All @@ -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,
);
}
}
27 changes: 24 additions & 3 deletions lib/view_models/user_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class UserViewModel extends StateNotifier<UserState> {
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);
Expand Down Expand Up @@ -216,6 +216,15 @@ class UserViewModel extends StateNotifier<UserState> {
);
}

void updateSelectedPinnedSemester(String? semester, List<Course> allCourses) {
state = state.copyWith(selectedSemester: semester);
updatedDisplayedPinnedCourses(CourseUtils.filterCoursesBySemester(
allCourses,
state.selectedSemester ?? 'All',
),
);
}

void setSemestersAsString(List<Semester> semesters) {
state = state.copyWith(
semestersAsString: CourseUtils.convertAndSortSemesters(semesters, true),
Expand All @@ -226,14 +235,26 @@ class UserViewModel extends StateNotifier<UserState> {
state = state.copyWith(displayedCourses: displayedCourses);
}

void updatedDisplayedPinnedCourses(List<Course> displayedPinnedCourses) {
state = state.copyWith(displayedPinnedCourses: displayedPinnedCourses);
}


void setUpDisplayedCourses(List<Course> allCourses) {
CourseUtils.sortCourses(allCourses, 'Newest First');
updatedDisplayedCourses(CourseUtils.filterCoursesBySemester(
allCourses,
state.selectedSemester ?? 'All',
),
);

}


void setUpDisplayedPinnedCourses(List<Course> allCourses) {
CourseUtils.sortCourses(allCourses, 'Newest First');
updatedDisplayedPinnedCourses(CourseUtils.filterCoursesBySemester(
allCourses,
state.selectedSemester ?? 'All',
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,33 @@ class PinnedCoursesState extends ConsumerState<PinnedCourses> {
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<Course> liveCourses = userPinned.where((course) => liveCourseIds.contains(course.id)).toList();
Expand Down

0 comments on commit 7068ee4

Please sign in to comment.