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

Commit

Permalink
solved delete bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ge59dil committed Jan 27, 2024
1 parent ef7fd74 commit cc14c79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions lib/view_models/download_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,35 @@ class DownloadViewModel extends StateNotifier<DownloadState> {
_logger.e('Error fetching downloaded videos: $e');
}
}

Future<void> deleteDownload(int videoId) async {
_logger.i('Deleting downloaded video with ID: $videoId');
_logger.d('Current state before deletion: ${state.downloadedVideos}');

try {
String? filePath = state.downloadedVideos[videoId];
_logger.d('File path to delete: $filePath');

if (filePath != null && filePath.isNotEmpty) {
final file = File(filePath);
if (await file.exists()) {
await file.delete();
_logger.d('Deleted video file at: $filePath');

final prefs = await SharedPreferences.getInstance();
final updatedDownloads =
Map<int, String>.from(state.downloadedVideos);
updatedDownloads.remove(videoId);

// Save updated list to SharedPreferences
await prefs.setString(
'downloadedVideos',
json.encode(updatedDownloads
.map((key, value) => MapEntry(key.toString(), value)),),);
state = state.copyWith(downloadedVideos: updatedDownloads);
} else {
_logger.w('File not found: $filePath');
}

// Update the state and SharedPreferences after deletion
final updatedDownloads = Map<int, String>.from(state.downloadedVideos);
updatedDownloads.remove(videoId);

final prefs = await SharedPreferences.getInstance();
await prefs.setString(
'downloadedVideos',
json.encode(updatedDownloads.map((key, value) => MapEntry(key.toString(), value))),
);

state = state.copyWith(downloadedVideos: updatedDownloads);
_logger.d('Updated state after deletion: ${state.downloadedVideos}');
} else {
_logger.w('No file path found for video ID: $videoId');
}
Expand All @@ -98,6 +101,7 @@ class DownloadViewModel extends StateNotifier<DownloadState> {
}
}


Future<void> deleteAllDownloads() async {
_logger.i('Deleting all downloaded videos');

Expand Down
2 changes: 1 addition & 1 deletion lib/views/video_view/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
break;
}
}
//combinedDownloadUrl="https://file-examples.com/storage/fe5048eb7365a64ba96daa9/2017/04/file_example_MP4_480_1_5MG.mp4";
//downloadUrl="https://file-examples.com/storage/fed61549c865b2b5c9768b5/2017/04/file_example_MP4_480_1_5MG.mp4";
// Check if the Combined URL is found
if (downloadUrl == null) {
if (!mounted) return;
Expand Down

0 comments on commit cc14c79

Please sign in to comment.