Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
added error handing
Browse files Browse the repository at this point in the history
  • Loading branch information
ge59dil committed Jan 25, 2024
1 parent 0f1b95c commit cd55ec4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
4 changes: 1 addition & 3 deletions lib/view_models/download_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class DownloadViewModel extends StateNotifier<DownloadState> {
String videoUrl, Int64 streamId, String fileName,{required bool downloadOverWifiOnly,}) async {
try {
_logger.d('Download Over Wi-Fi Only setting is: $downloadOverWifiOnly');


// Check if 'Download Over Wi-Fi Only' is enabled and if not connected to Wi-Fi
if (downloadOverWifiOnly == !(await isConnectedToWifi())) {
if (downloadOverWifiOnly && !(await isConnectedToWifi())) {
_logger.w('Not connected to Wi-Fi. Download cancelled.');
throw Exception('Not connected to Wi-Fi');
}
Expand Down
61 changes: 46 additions & 15 deletions lib/views/video_view/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
onDownload: () => _downloadVideo(widget.stream),
),
Expanded(
child:
ChatView(isActive: _isChatVisible, streamID: widget.stream.id),),
child:
ChatView(isActive: _isChatVisible, streamID: widget.stream.id),),
],
);
}
Expand All @@ -62,7 +62,9 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
await ref
.read(courseViewModelProvider.notifier)
.getCourseWithID(widget.stream.courseID);
Course? course = ref.read(courseViewModelProvider).course;
Course? course = ref
.read(courseViewModelProvider)
.course;
if (course != null) {
if ((course.chatEnabled || course.vodChatEnabled) &&
widget.stream.chatEnabled) {
Expand Down Expand Up @@ -90,7 +92,9 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.stream.name)),
body: ref.read(videoViewModelProvider).isLoading
body: ref
.read(videoViewModelProvider)
.isLoading
? const Center(child: CircularProgressIndicator())
: _buildVideoLayout(),
);
Expand Down Expand Up @@ -132,10 +136,12 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
// Seek to the last progress.
Future<void> _seekToLastProgress() async {
Progress progress =
ref.read(videoViewModelProvider).progress ?? Progress(progress: 0.0);
ref
.read(videoViewModelProvider)
.progress ?? Progress(progress: 0.0);
final position = Duration(
seconds: (progress.progress *
_controllerManager.videoPlayerController.value.duration.inSeconds)
_controllerManager.videoPlayerController.value.duration.inSeconds)
.round(),
);
await _controllerManager.videoPlayerController.seekTo(position);
Expand Down Expand Up @@ -200,7 +206,9 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
}

void _switchPlaylist(String newPlaylistUrl) async {
if (ref.read(videoViewModelProvider).videoSource == newPlaylistUrl) {
if (ref
.read(videoViewModelProvider)
.videoSource == newPlaylistUrl) {
Logger().i("Already displaying $newPlaylistUrl");
return;
}
Expand Down Expand Up @@ -235,8 +243,11 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
_isChatVisible = !_isChatVisible;
});
}

void _downloadVideo(Stream stream) {
final isDownloadWithWifiOnly = ref.watch(settingViewModelProvider).isDownloadWithWifiOnly;
final isDownloadWithWifiOnly = ref
.watch(settingViewModelProvider)
.isDownloadWithWifiOnly;

// Extract the "Combined" download URL from the Stream object
String? combinedDownloadUrl;
Expand All @@ -261,32 +272,52 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
const SnackBar(content: Text('Starting download...')),
);

// Call the download function from the DownloadViewModel
// Call the download function from the DownloadViewModel
ref
.read(downloadViewModelProvider.notifier)
.downloadVideo(combinedDownloadUrl, stream.id, fileName, downloadOverWifiOnly: isDownloadWithWifiOnly)
.downloadVideo(combinedDownloadUrl, stream.id, fileName,
downloadOverWifiOnly: isDownloadWithWifiOnly,)
.then((localPath) {
if (localPath.isNotEmpty) {
// Download successful
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Video Downloaded')),
);
} else {
// Download failed, but not due to Wi-Fi (since it would throw an exception)
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Download failed')),
);
}
}).catchError((error) {
// Handle specific error from 'downloadVideo'
if (error.toString() == "Not connected to Wi-Fi. Download cancelled.") {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please connect to Wi-Fi to download videos')),
// _logger.d('Caught error: ${error.toString()}');
if (error.toString().contains("Not connected to Wi-Fi")) {
// Show a dialog for Wi-Fi error
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Download Failed"),
content: const Text(
"You are not connected to Wi-Fi. Please connect to Wi-Fi to download the video.",),
actions: <Widget>[
TextButton(
child: const Text("OK"),
onPressed: () {
Navigator.of(context).pop(); // Dismiss the dialog
},
),
],
);
},
);
} else {
// For other errors, show a snackbar
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: ${error.toString()}')),
);
}
});
}

}
}

0 comments on commit cd55ec4

Please sign in to comment.