Skip to content

Commit

Permalink
fix download card
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Feb 2, 2024
1 parent bff615d commit 32e0e66
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
11 changes: 8 additions & 3 deletions lib/models/download/download_state_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,31 @@ class DownloadState {
class VideoDetails {
final String filePath;
final String name;
final int duration; // Duration in seconds or your preferred unit
final String duration; // Duration in seconds or your preferred unit
final String description;
final String date;

const VideoDetails({
required this.filePath,
required this.name,
required this.duration,
required this.description,
required this.date,
});

VideoDetails copyWith({
String? filePath,
String? name,
int? duration,
String? duration,
String? description,
String? date,
}) {
return VideoDetails(
filePath: filePath ?? this.filePath,
name: name ?? this.name,
duration: duration ?? this.duration,
description: description,
description: description ?? this.description,
date: date ?? this.date,
);
}
}
28 changes: 22 additions & 6 deletions lib/view_models/download_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:path_provider/path_provider.dart';
import 'package:dio/dio.dart';
import 'package:logger/logger.dart';
import 'dart:io';
import 'package:gocast_mobile/base/networking/api/gocast/api_v2.pb.dart';


class DownloadViewModel extends StateNotifier<DownloadState> {
final Logger _logger = Logger();
Expand Down Expand Up @@ -34,31 +36,32 @@ class DownloadViewModel extends StateNotifier<DownloadState> {
name: videoDetailsMap['name'],
duration: videoDetailsMap['duration'],
description: videoDetailsMap['description'],
date: videoDetailsMap['date'],
);
return MapEntry(int.parse(key), videoDetails);
}).cast<int, VideoDetails>(); // Ensure the map has the correct type
state = state.copyWith(downloadedVideos: downloadedVideos);
}
}

Future<String> downloadVideo(String videoUrl, int streamId, String fileName,
String streamName, int streamDuration, String description,) async {
Future<String> downloadVideo(String videoUrl, Stream stream, String streamName, String streamDate) async {
try {
final directory = await getApplicationDocumentsDirectory();
final filePath = '${directory.path}/$fileName';
final filePath = '${directory.path}/${streamName.replaceAll(' ', '_')}.mp4';
Dio dio = Dio();
await dio.download(videoUrl, filePath);
_logger.d('Downloaded video to: $filePath');

final prefs = await SharedPreferences.getInstance();
final int streamIdInt = streamId.toInt();
final int streamIdInt = stream.id.toInt();

// Create a map for the video details
final videoDetailsMap = {
'filePath': filePath,
'name': streamName,
'duration': streamDuration,
'description': description,
'duration': _formatDuration(stream.end.toDateTime().difference(stream.start.toDateTime()).inMinutes),
'description': stream.description,
'date': streamDate,
};

// Convert video details map to JSON string
Expand All @@ -82,6 +85,7 @@ class DownloadViewModel extends StateNotifier<DownloadState> {
name: videoDetailsMap['name'],
duration: videoDetailsMap['duration'],
description: videoDetailsMap['description'],
date: videoDetailsMap['date'],
);
return MapEntry(key, videoDetails);
}).cast<int, VideoDetails>();
Expand Down Expand Up @@ -166,6 +170,18 @@ class DownloadViewModel extends StateNotifier<DownloadState> {
}
}

String _formatDuration(int durationInMinutes) {
int hours = durationInMinutes ~/ 60;
int minutes = durationInMinutes % 60;
int seconds = 0;

String formattedHours = hours < 10 ? '0$hours' : '$hours';
String formattedMinutes = minutes < 10 ? '0$minutes' : '$minutes';
String formattedSeconds = seconds < 10 ? '0$seconds' : '$seconds';

return '$formattedHours:$formattedMinutes:$formattedSeconds';
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,18 @@ class DownloadedCoursesState extends ConsumerState<DownloadedCourses> {
videoCards: downloadedVideos.entries.map((entry) {
final int videoId = entry.key;
final VideoDetails videoDetails = entry.value;
final String localPath = videoDetails.filePath;
final String videoName = videoDetails.name;
final int durationSeconds = videoDetails.duration;
final String formattedDuration = "${(durationSeconds ~/ 3600).toString().padLeft(2, '0')}:${((durationSeconds % 3600) ~/ 60).toString().padLeft(2, '0')}:${(durationSeconds % 60).toString().padLeft(2, '0')}";
return SmallStreamCard(
isDownloaded: true,
courseId: videoId,
title: videoName,
subtitle: formattedDuration,
tumID: "TUMID",
title: videoDetails.name,
subtitle: videoDetails.duration,
tumID: videoDetails.date,
showDeleteConfirmationDialog: _showDeleteConfirmationDialog,
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
OfflineVideoPlayerPage(localPath: localPath),
OfflineVideoPlayerPage(localPath: videoDetails.filePath),
),
);
},
Expand Down
8 changes: 4 additions & 4 deletions lib/views/video_view/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:gocast_mobile/views/chat_view/poll_view.dart';
import 'package:gocast_mobile/views/video_view/utils/custom_video_control_bar.dart';
import 'package:gocast_mobile/views/video_view/utils/video_player_handler.dart';
import 'package:gocast_mobile/views/video_view/video_player_controller.dart';
import 'package:intl/intl.dart';
import 'package:logger/logger.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Expand Down Expand Up @@ -288,18 +289,17 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
);
return;
}

// Use the extracted URL for downloading
String fileName = "${stream.id}.mp4";
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(AppLocalizations.of(context)!.starting_download)),
);
// Call the download function from the StreamViewModel

String streamName = stream.name != '' ? stream.name : 'Lecture: ${DateFormat('EEEE. dd', Localizations.localeOf(context).toString()).format(stream.start.toDateTime())}';
String streamDate = DateFormat('dd MMMM yyyy', Localizations.localeOf(context).toString()).format(stream.start.toDateTime());
ref
.read(downloadViewModelProvider.notifier)
.downloadVideo(downloadUrl, stream.id, fileName,stream.name,stream.duration,stream.description,)
.downloadVideo(downloadUrl, stream, streamName, streamDate)
.then((localPath) {
if (localPath.isNotEmpty) {
// Download successful
Expand Down

0 comments on commit 32e0e66

Please sign in to comment.