Skip to content

Commit

Permalink
Merge branch 'dev' into 225-Add-course-name-on-downloads-card-UI
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab authored Jan 29, 2024
2 parents a905e2c + 24658e1 commit 8fbb8a6
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/base/networking/api/handler/auth_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AuthHandler {
_logger.i('JWT token saved successfully for user: $username');
} catch (e) {
_logger.e('Error saving JWT token for user: $username, Error: $e');
throw AppError.tokenSaveError(e);
throw AppError.userError();
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/base/networking/api/handler/settings_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SettingsHandler {
/// This method sends a `patchUserSettings` gRPC call to update the user's preferred name on the server.
/// * [newName] - The new preferred name.
/// Returns `true` if the update was successful.
Future<bool> updatePreferredName(String newName) async {
Future<void> updatePreferredName(String newName) async {
try {
_logger.i('Updating user settings...');
final request = PatchUserSettingsRequest()
Expand All @@ -77,10 +77,9 @@ class SettingsHandler {
_logger.i('User settings updated successfully');
},
);
return true;
} catch (e) {
_logger.e('Error updating user settings: $e');
return false;
rethrow;
}
}

Expand Down
15 changes: 11 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import 'firebase_options.dart';
final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();

Future<void> main() async {
Logger.level = Level.info;
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

Logger.level = Level.debug;
runApp(
const ProviderScope(
const ProviderScope(
child: App(),
),
);
Expand All @@ -29,12 +29,19 @@ Future<void> main() async {
bool isPushNotificationListenerSet = false;

class App extends ConsumerWidget {
const App({super.key});

const App({
super.key,
});


@override
Widget build(BuildContext context, WidgetRef ref) {
final userState = ref.watch(userViewModelProvider);

bool isLoggedIn = ref.watch(userViewModelProvider).user != null;


_handleErrors(ref, userState);
_setupNotifications(ref, userState);

Expand All @@ -45,7 +52,7 @@ class App extends ConsumerWidget {
ref.watch(themeModeProvider), // Use the theme mode from the provider
navigatorKey: navigatorKey,
scaffoldMessengerKey: scaffoldMessengerKey,
home: userState.user == null
home: !isLoggedIn
? const WelcomeScreen()
: const NavigationTab(),
routes: _buildRoutes(),
Expand Down
2 changes: 2 additions & 0 deletions lib/view_models/stream_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:gocast_mobile/base/networking/api/handler/stream_handler.dart';
import 'package:gocast_mobile/models/error/error_model.dart';
import 'package:gocast_mobile/models/video/stream_state_model.dart';
import 'package:gocast_mobile/utils/sort_utils.dart';
import 'package:logger/logger.dart';
import 'package:tuple/tuple.dart';

class StreamViewModel extends StateNotifier<StreamState> {
Expand Down Expand Up @@ -111,6 +112,7 @@ class StreamViewModel extends StateNotifier<StreamState> {
try {
return await StreamHandler(_grpcHandler).fetchThumbnailStreams(streamId);
} catch (e) {
Logger().e(e);
rethrow;
}
}
Expand Down
26 changes: 25 additions & 1 deletion lib/view_models/user_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ import 'package:gocast_mobile/models/error/error_model.dart';
import 'package:gocast_mobile/models/user/user_state_model.dart';
import 'package:gocast_mobile/utils/globals.dart';
import 'package:gocast_mobile/utils/sort_utils.dart';
import 'package:jwt_decode/jwt_decode.dart';
import 'package:logger/logger.dart';

class UserViewModel extends StateNotifier<UserState> {
final Logger _logger = Logger();

final GrpcHandler _grpcHandler;

UserViewModel(this._grpcHandler) : super(const UserState());
UserViewModel(this._grpcHandler) : super(const UserState()){
// Check if the user is already logged in
_checkToken();
}


/// Handles basic authentication.
/// If the authentication is successful, it navigates to the courses screen.
Expand Down Expand Up @@ -152,4 +157,23 @@ class UserViewModel extends StateNotifier<UserState> {
state = state.copyWith(selectedSemester: choice);
}

Future<void> _checkToken() async {
String token = await _getToken();
if(token.isNotEmpty && !Jwt.isExpired(token)) {
_logger.i('Token found, fetching user: $token');
fetchUser();
}else {
_logger.i('Token not found or expired');
}
}

Future<String> _getToken() async {
try {
return await TokenHandler.loadToken('jwt');
} catch(e){
Logger().w("Token not found");
return '';
}
}

}
13 changes: 9 additions & 4 deletions lib/views/course_view/components/course_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CourseCard extends StatelessWidget {
final Course? course;
final Function(Course)? onPinUnpin;
final bool? isPinned;
final bool isLoggedIn;

//for displaying livestreams
final String? subtitle;
Expand All @@ -30,6 +31,7 @@ class CourseCard extends StatelessWidget {
this.course,
this.onPinUnpin,
this.isPinned,
required this.isLoggedIn,
});

@override
Expand Down Expand Up @@ -63,8 +65,9 @@ class CourseCard extends StatelessWidget {
cardWidth,
context,
course!,
onPinUnpin!,
isPinned!,
onPinUnpin!,
isPinned!,
isLoggedIn,
),
),
),
Expand All @@ -78,11 +81,13 @@ class CourseCard extends StatelessWidget {
Course course,
Function(Course) onPinUnpin,
bool isPinned,
bool isLoggedIn,
) {

return Slidable(
key: ValueKey(course.id),
closeOnScroll: true,
endActionPane: ActionPane(
endActionPane: isLoggedIn ? ActionPane(
motion: const DrawerMotion(),
dragDismissible: true,
children: [
Expand All @@ -108,7 +113,7 @@ class CourseCard extends StatelessWidget {
label: 'Pin',
),
],
),
) : null,
child: IntrinsicHeight(
child: Row(
children: [
Expand Down
9 changes: 7 additions & 2 deletions lib/views/course_view/components/course_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:gocast_mobile/utils/section_kind.dart';
import 'package:gocast_mobile/views/components/view_all_button.dart';
import 'package:gocast_mobile/views/course_view/components/course_card.dart';
import 'package:gocast_mobile/views/course_view/course_detail_view/course_detail_view.dart';
import 'dart:math' as math;


/// CourseSection
///
Expand All @@ -26,7 +28,7 @@ import 'package:gocast_mobile/views/course_view/course_detail_view/course_detail
class CourseSection extends StatelessWidget {
final String sectionTitle;
final SectionKind
sectionKind; //0 for livestreams, 1 cor mycourses, 2 for puliccourses
sectionKind;
final List<Course> courses;
final List<Stream> streams;
final VoidCallback? onViewAll;
Expand Down Expand Up @@ -74,9 +76,11 @@ class CourseSection extends StatelessWidget {

Widget _buildCourseList(BuildContext context) {
bool isTablet = MediaQuery.of(context).size.width >= 600 ? true : false;
int displayCount = math.min(courses.length, 3);
double cardHeight = 75;

return ConstrainedBox(
constraints: BoxConstraints(maxHeight: isTablet ? 600 : 400),
constraints: BoxConstraints(maxHeight: isTablet ? double.infinity : cardHeight * displayCount,),
child: ListView.builder(
physics: const ClampingScrollPhysics(),
shrinkWrap: true,
Expand All @@ -89,6 +93,7 @@ class CourseSection extends StatelessWidget {

final isPinned = userPinned.contains(course);
return CourseCard(
isLoggedIn: ref.read(userViewModelProvider).user != null,
course: course,
isPinned: isPinned,
onPinUnpin: (course) => _togglePin(course, isPinned),
Expand Down
6 changes: 6 additions & 0 deletions lib/views/course_view/components/pin_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class PinButton extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
bool isLoggedIn = ref.read(userViewModelProvider).user != null;
if (!isLoggedIn) {
return const Icon(
Icons.push_pin_outlined,
color: Colors.transparent,);
}
return StatefulBuilder(
builder: (context, setState) {
return IconButton(
Expand Down
6 changes: 1 addition & 5 deletions lib/views/course_view/components/small_stream_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ class SmallStreamCard extends StatelessWidget {
}

Widget _buildCourseImage() {
// Assuming `path` is now a URL string
return Stack(
children: [
AspectRatio(
aspectRatio: 16 / 12, // Maintain the same aspect ratio
aspectRatio: 16 / 12,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
// Keep the rounded corners
child: path == null
? Image.asset(
AppImages.course1,
Expand All @@ -172,7 +170,6 @@ class SmallStreamCard extends StatelessWidget {
);
},
errorBuilder: (context, error, stackTrace) {
// Provide a fallback asset image in case of error
return Image.asset(
AppImages.course1,
fit: BoxFit.cover,
Expand All @@ -181,7 +178,6 @@ class SmallStreamCard extends StatelessWidget {
),
),
),
// If you have additional overlays like in the thumbnail widget, add them here
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class CoursesList extends ConsumerWidget {
final userPinned = ref.watch(pinnedCourseViewModelProvider).userPinned ?? [];
List<Course> liveCourses =
courses.where((course) => liveCourseIds.contains(course.id)).toList();
return ConstrainedBox(
constraints: BoxConstraints(maxHeight: isTablet ? 600 : 400),
child: ListView.builder(
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
Expand All @@ -61,6 +59,7 @@ class CoursesList extends ConsumerWidget {
final course = courses[index];
final isPinned = userPinned.contains(course);
return CourseCard(
isLoggedIn: ref.read(userViewModelProvider).user != null,
course: course,
isPinned: isPinned,
onPinUnpin: (course) {
Expand Down Expand Up @@ -88,8 +87,6 @@ class CoursesList extends ConsumerWidget {
);
},
);
},
),
);
},);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class PinnedCoursesState extends ConsumerState<PinnedCourses> {
final isPinned =
userPinned.any((pinnedCourse) => pinnedCourse.id == course.id);
return CourseCard(
isLoggedIn: true,
course: course,
isPinned: isPinned,
onPinUnpin: (course) => _togglePin(course, isPinned),
Expand Down
4 changes: 2 additions & 2 deletions lib/views/settings_view/custom_playback_speed_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void showAddCustomSpeedDialog(
errorMessage = '';
} else {
double? parsedValue = double.tryParse(value);
if (parsedValue != null && parsedValue >= 0.25 && parsedValue <= 4.0) {
if (parsedValue != null && parsedValue >= 0.25 && parsedValue <= 2.0) {
List<String> splitValue = value.split('.');
if ((splitValue[0].length > 1) ||
(splitValue.length > 1 && splitValue[1].length > 2)) {
Expand All @@ -24,7 +24,7 @@ void showAddCustomSpeedDialog(
customSpeed = parsedValue;
}
} else {
errorMessage = 'Please enter a number between\n0.25 and 4.0';
errorMessage = 'Please enter a number between\n0.25 and 2.0';
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/views/settings_view/playback_speed_picker_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void showPlaybackSpeedsPicker(
List<double> selectedSpeeds,
Function(double, bool) updateSelectedSpeeds,
) {
List<double> defaultSpeeds = List.generate(14, (index) => (index + 1) * 0.25);
List<double> defaultSpeeds = List.generate(8, (index) => (index + 1) * 0.25);

if (!selectedSpeeds.contains(1.0)) {
selectedSpeeds.add(1.0);
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
jwt_decode:
dependency: "direct main"
description:
name: jwt_decode
sha256: d2e9f68c052b2225130977429d30f187aa1981d789c76ad104a32243cfdebfbb
url: "https://pub.dev"
source: hosted
version: "0.3.1"
lints:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
chewie: ^1.7.4
url_launcher: ^6.2.3
path_provider: any
jwt_decode: ^0.3.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 8fbb8a6

Please sign in to comment.