Skip to content

Commit

Permalink
Made PinState seperated
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Jan 28, 2024
1 parent 5ce7f09 commit 5812f6e
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 136 deletions.
67 changes: 67 additions & 0 deletions lib/models/course/pinned_course_state_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:flutter/material.dart';
import 'package:gocast_mobile/base/networking/api/gocast/api_v2.pbgrpc.dart';
import 'package:gocast_mobile/models/error/error_model.dart';

@immutable
class PinnedCourseState {
final bool? isLoading;
final List<Course>? userPinned;
final AppError? error;
final List<Course>? displayedPinnedCourses;
final List<Semester>? semesters;
final String? selectedSemester;
final List<String>? semestersAsString;
final Semester? current;
final String? currentAsString;

const PinnedCourseState({
this.isLoading = false,
this.userPinned,
this.error,
this.displayedPinnedCourses,
this.semesters,
this.selectedSemester = 'All',
this.semestersAsString,
this.current,
this.currentAsString,
});

PinnedCourseState copyWith({
bool? isLoading,
List<Course>? userPinned,
AppError? error,
List<Course>? displayedPinnedCourses,
List<Semester>? semesters,
String? selectedSemester,
List<String>? semestersAsString,
Semester? current,
String? currentAsString,
}) {
return PinnedCourseState(
isLoading: isLoading ?? this.isLoading,
userPinned: userPinned ?? this.userPinned,
error: error ?? this.error,
displayedPinnedCourses: displayedPinnedCourses ?? this.displayedPinnedCourses,
semesters: semesters ?? this.semesters,
selectedSemester: selectedSemester ?? this.selectedSemester,
semestersAsString: semestersAsString ?? this.semestersAsString,
current: current ?? this.current,
currentAsString: currentAsString ?? this.currentAsString,
);
}

PinnedCourseState clearError() {
return PinnedCourseState(
isLoading: isLoading,
userPinned: userPinned,
error: null,
displayedPinnedCourses: displayedPinnedCourses,
semesters: semesters,
selectedSemester: selectedSemester,
semestersAsString: semestersAsString,
current: current,
currentAsString: currentAsString,
);
}

}
16 changes: 0 additions & 16 deletions lib/models/user/user_state_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class UserState {
final bool isLoading;
final User? user;
final List<Course>? userCourses;
final List<Course>? userPinned;
final List<Bookmark>? userBookmarks;
final List<Course>? publicCourses;
final AppError? error;
Expand All @@ -18,13 +17,11 @@ class UserState {
final Semester? current;
final String? currentAsString;
final List<Course>? displayedCourses;
final List<Course>? displayedPinnedCourses;

const UserState({
this.isLoading = false,
this.user,
this.userCourses,
this.userPinned,
this.userBookmarks,
this.publicCourses,
this.error,
Expand All @@ -35,14 +32,12 @@ class UserState {
this.current,
this.currentAsString,
this.displayedCourses,
this.displayedPinnedCourses,
});

UserState copyWith({
bool? isLoading,
User? user,
List<Course>? userCourses,
List<Course>? userPinned,
List<Bookmark>? userBookmarks,
List<Course>? publicCourses,
AppError? error,
Expand All @@ -53,13 +48,11 @@ class UserState {
Semester? current,
String? currentAsString,
List<Course>? displayedCourses,
List<Course>? displayedPinnedCourses,
}) {
return UserState(
isLoading: isLoading ?? this.isLoading,
user: user ?? this.user,
userCourses: userCourses ?? this.userCourses,
userPinned: userPinned ?? this.userPinned,
userBookmarks: userBookmarks ?? this.userBookmarks,
publicCourses: publicCourses ?? this.publicCourses,
error: error ?? this.error,
Expand All @@ -70,29 +63,24 @@ class UserState {
current: current ?? this.current,
currentAsString: currentAsString ?? this.currentAsString,
displayedCourses: displayedCourses ?? this.displayedCourses,
displayedPinnedCourses:
displayedPinnedCourses ?? this.displayedPinnedCourses,
);
}

UserState clearError({
bool? isLoading,
User? user,
List<Course>? userCourses,
List<Course>? userPinned,
List<Bookmark>? userBookmarks,
List<Course>? publicCourses,
AppError? error,
List<Course>? downloadedCourses,
List<Course>? displayedCourses,
List<Course>? displayedPinnedCourses,
List<Semester>? semesters,
}) {
return UserState(
isLoading: isLoading ?? this.isLoading,
user: user ?? this.user,
userCourses: userCourses ?? this.userCourses,
userPinned: userPinned ?? this.userPinned,
userBookmarks: userBookmarks ?? this.userBookmarks,
publicCourses: publicCourses ?? this.publicCourses,
error: null,
Expand All @@ -103,8 +91,6 @@ class UserState {
current: current,
currentAsString: currentAsString,
displayedCourses: displayedCourses ?? this.displayedCourses,
displayedPinnedCourses:
displayedPinnedCourses ?? this.displayedPinnedCourses,
);
}

Expand All @@ -113,7 +99,6 @@ class UserState {
isLoading: false,
user: null,
userCourses: null,
userPinned: null,
userBookmarks: null,
publicCourses: null,
error: null,
Expand All @@ -124,7 +109,6 @@ class UserState {
current: null,
currentAsString: null,
displayedCourses: null,
displayedPinnedCourses: null,
);
}
}
7 changes: 7 additions & 0 deletions lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gocast_mobile/config/app_config.dart';
import 'package:gocast_mobile/models/chat/chat_state_model.dart';
import 'package:gocast_mobile/models/course/course_state_model.dart';
import 'package:gocast_mobile/models/course/pinned_course_state_model.dart';
import 'package:gocast_mobile/models/notifications/notification_state_model.dart';
import 'package:gocast_mobile/models/poll/poll_state_model.dart';
import 'package:gocast_mobile/models/settings/setting_state_model.dart';
Expand All @@ -11,6 +12,7 @@ import 'package:gocast_mobile/view_models/chat_view_model.dart';
import 'package:gocast_mobile/view_models/course_view_model.dart';
import 'package:gocast_mobile/view_models/notification_view_model.dart';
import 'package:gocast_mobile/view_models/download_view_model.dart';
import 'package:gocast_mobile/view_models/pinned_view_model.dart';
import 'package:gocast_mobile/view_models/poll_view_model.dart';
import 'package:gocast_mobile/view_models/setting_view_model.dart';
import 'package:gocast_mobile/view_models/stream_view_model.dart';
Expand Down Expand Up @@ -52,6 +54,11 @@ final courseViewModelProvider =
(ref) => CourseViewModel(ref.watch(grpcHandlerProvider)),
);

final pinnedCourseViewModelProvider =
StateNotifierProvider<PinnedViewModel, PinnedCourseState>(
(ref) => PinnedViewModel(ref.watch(grpcHandlerProvider)),
);

final downloadViewModelProvider =
StateNotifierProvider<DownloadViewModel, DownloadState>((ref) {
return DownloadViewModel();
Expand Down
118 changes: 118 additions & 0 deletions lib/view_models/pinned_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gocast_mobile/base/networking/api/gocast/api_v2.pb.dart';
import 'package:gocast_mobile/base/networking/api/handler/grpc_handler.dart';
import 'package:gocast_mobile/base/networking/api/handler/pinned_handler.dart';
import 'package:gocast_mobile/models/course/pinned_course_state_model.dart';
import 'package:gocast_mobile/models/error/error_model.dart';
import 'package:gocast_mobile/utils/sort_utils.dart';
import 'package:logger/logger.dart';

class PinnedViewModel extends StateNotifier<PinnedCourseState> {
final Logger _logger = Logger();

final GrpcHandler _grpcHandler;

PinnedViewModel(this._grpcHandler) : super(const PinnedCourseState());

Future<void> fetchUserPinned() async {
state = state.copyWith(isLoading: true);
try {
var courses = await PinnedHandler(_grpcHandler).fetchUserPinned();
state = state.copyWith(userPinned: courses, isLoading: false);
setUpDisplayedPinnedCourses(state.userPinned ?? []);
} catch (e) {
_logger.e(e);
state = state.copyWith(error: e as AppError, isLoading: false);
}
}


Future<bool> pinCourse(int courseID) async {
state = state.copyWith(isLoading: true);
try {
bool success = await PinnedHandler(_grpcHandler).pinCourse(courseID);
if (success) {
await fetchUserPinned();
} else {
_logger.e('Failed to pin course');
}
state = state.copyWith(isLoading: false);
return success;
} catch (e) {
_logger.e('Error pinning course: $e');
state = state.copyWith(error: e as AppError, isLoading: false);
return false;
}
}


Future<bool> unpinCourse(int courseID) async {
state = state.copyWith(isLoading: true);
try {
bool success = await PinnedHandler(_grpcHandler).unpinCourse(courseID);
if (success) {
await fetchUserPinned();
_logger.i('Course unpinned successfully');
} else {
_logger.e('Failed to unpin course');
}
state = state.copyWith(isLoading: false);
return success;
} catch (e) {
state = state.copyWith(error: e as AppError, isLoading: false);
return false;
}
}

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

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

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

bool isCoursePinned(int id) {
if (state.userPinned == null) {
return false;
}
for (var course in state.userPinned!) {
if (course.id == id) {
return true;
}
}
return false;
}

void setLoading(bool loading) {
state = state.copyWith(isLoading: loading);
}

void setSemestersAsString(List<Semester> semesters) {
state = state.copyWith(
semestersAsString: CourseUtils.convertAndSortSemesters(semesters, true),
);
}

void setSelectedSemester(String choice) {
state = state.copyWith(selectedSemester: choice);
}


}
Loading

0 comments on commit 5812f6e

Please sign in to comment.