Skip to content

Commit

Permalink
fix: yt download bullshit
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Jul 10, 2024
1 parent 5fd9a90 commit e869894
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 211 deletions.
15 changes: 8 additions & 7 deletions lib/controller/notification_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import 'package:namida/controller/json_to_history_parser.dart';
import 'package:namida/core/extensions.dart';
import 'package:namida/youtube/class/download_task_base.dart';

class NotificationService {
//Hanle displaying of notifications.
Expand Down Expand Up @@ -93,7 +94,7 @@ class NotificationService {
}

void downloadYoutubeNotification({
required String notificationID,
required DownloadTaskFilename notificationID,
required String title,
required String Function(String progressText) subtitle,
String? imagePath,
Expand All @@ -112,23 +113,23 @@ class NotificationService {
payload: _youtubeDownloadPayload,
imagePath: imagePath,
isInBytes: true,
tag: notificationID,
tag: notificationID.filename,
displayTime: displayTime,
);
}

Future<void> removeDownloadingYoutubeNotification({required String notificationID}) async {
await _flutterLocalNotificationsPlugin.cancel(_youtubeDownloadID, tag: notificationID);
Future<void> removeDownloadingYoutubeNotification({required DownloadTaskFilename notificationID}) async {
await _flutterLocalNotificationsPlugin.cancel(_youtubeDownloadID, tag: notificationID.filename);
}

void doneDownloadingYoutubeNotification({
required String notificationID,
required DownloadTaskFilename notificationID,
required String videoTitle,
required String subtitle,
required bool failed,
String? imagePath,
}) async {
await _flutterLocalNotificationsPlugin.cancel(_youtubeDownloadID, tag: notificationID);
await _flutterLocalNotificationsPlugin.cancel(_youtubeDownloadID, tag: notificationID.filename);
_createNotification(
id: _youtubeDownloadID,
title: videoTitle,
Expand All @@ -139,7 +140,7 @@ class NotificationService {
payload: _youtubeDownloadPayload,
imagePath: imagePath,
isInBytes: true,
tag: notificationID,
tag: notificationID.filename,
displayTime: DateTime.now(),
);
}
Expand Down
68 changes: 68 additions & 0 deletions lib/youtube/class/download_task_base.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
abstract class DownloadTask<T> {
const DownloadTask();

@override
String toString();
}

class DownloadTaskFilename extends DownloadTask {
String filename;

DownloadTaskFilename({
required String initialFilename,
}) : filename = initialFilename;

@override
bool operator ==(Object other) {
if (other is DownloadTaskFilename) return filename == other.filename;
return false;
}

@override
int get hashCode => filename.hashCode;

@override
String toString() => filename;
}

class DownloadTaskVideoId {
final String videoId;

const DownloadTaskVideoId({
required this.videoId,
});

@override
bool operator ==(Object other) {
if (other is DownloadTaskVideoId) return videoId == other.videoId;
return false;
}

@override
int get hashCode => videoId.hashCode;

@override
String toString() => videoId;
}

class DownloadTaskGroupName {
final String groupName;

const DownloadTaskGroupName({
required this.groupName,
});

const DownloadTaskGroupName.defaulty() : groupName = '';

@override
bool operator ==(Object other) {
if (other is DownloadTaskGroupName) return groupName == other.groupName;
return false;
}

@override
int get hashCode => groupName.hashCode;

@override
String toString() => groupName;
}
40 changes: 29 additions & 11 deletions lib/youtube/class/youtube_item_download_config.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import 'package:flutter/foundation.dart';
import 'package:youtipie/class/streams/audio_stream.dart';
import 'package:youtipie/class/streams/video_stream.dart';

import 'package:namida/core/utils.dart';
import 'package:namida/youtube/class/download_task_base.dart';

class YoutubeItemDownloadConfig {
final String id;
String filename; // filename can be changed after deciding quality/codec, or manually.
DownloadTaskFilename get filename => _filename.value;
// ignore: avoid_rx_value_getter_outside_obx
DownloadTaskFilename get filenameR => _filename.valueR;

final DownloadTaskVideoId id;
final DownloadTaskGroupName groupName;
final Rx<DownloadTaskFilename> _filename; // filename can be changed after deciding quality/codec, or manually.
final Map<String, String?> ffmpegTags;
DateTime? fileDate;
VideoStream? videoStream;
Expand All @@ -15,7 +24,8 @@ class YoutubeItemDownloadConfig {

YoutubeItemDownloadConfig({
required this.id,
required this.filename,
required this.groupName,
required DownloadTaskFilename filename,
required this.ffmpegTags,
required this.fileDate,
required this.videoStream,
Expand All @@ -24,7 +34,13 @@ class YoutubeItemDownloadConfig {
required this.prefferedAudioQualityID,
required this.fetchMissingAudio,
required this.fetchMissingVideo,
});
}) : _filename = filename.obs;

/// Using this method is restricted only for the function that will rename all the other instances in other parts.
@protected
void rename(DownloadTaskFilename newName) {
_filename.value = newName;
}

factory YoutubeItemDownloadConfig.fromJson(Map<String, dynamic> map) {
VideoStream? vids;
Expand All @@ -36,8 +52,9 @@ class YoutubeItemDownloadConfig {
auds = AudioStream.fromMap(map['audioStream']);
} catch (_) {}
return YoutubeItemDownloadConfig(
id: map['id'] ?? 'UNKNOWN_ID',
filename: map['filename'] ?? 'UNKNOWN_FILENAME',
id: DownloadTaskVideoId(videoId: map['id'] ?? 'UNKNOWN_ID'),
filename: DownloadTaskFilename(initialFilename: map['filename'] ?? 'UNKNOWN_FILENAME'),
groupName: DownloadTaskGroupName(groupName: map['groupName'] ?? ''),
fileDate: DateTime.fromMillisecondsSinceEpoch(map['fileDate'] ?? 0),
ffmpegTags: (map['ffmpegTags'] as Map<String, dynamic>?)?.cast() ?? {},
videoStream: vids,
Expand All @@ -51,8 +68,9 @@ class YoutubeItemDownloadConfig {

Map<String, dynamic> toJson() {
return {
'id': id,
'filename': filename,
'id': id.videoId,
'groupName': groupName.groupName,
'filename': filename.filename,
'ffmpegTags': ffmpegTags,
'fileDate': fileDate?.millisecondsSinceEpoch,
'videoStream': videoStream?.toMap(),
Expand All @@ -67,13 +85,13 @@ class YoutubeItemDownloadConfig {
@override
bool operator ==(other) {
if (other is YoutubeItemDownloadConfig) {
return id == other.id && filename == other.filename;
return id == other.id && groupName == other.groupName && filename == other.filename;
}
return false;
}

/// only [id] && [filename] are matched, since map lookup will
/// only [id], [groupName] && [filename] are matched, since map lookup will
/// recognize this and update accordingly
@override
int get hashCode => "$id$filename".hashCode;
int get hashCode => "$id$groupName$filename".hashCode;
}
Loading

0 comments on commit e869894

Please sign in to comment.