notube-export/lib/videoList/cubit/videos_cubit.dart

122 lines
3.6 KiB
Dart

import 'dart:math';
import 'dart:async';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:notube/models/Video.dart';
import 'package:notube/services/converter.dart';
import 'package:notube/services/download.dart';
part 'videos_state.dart';
class VideosCubit extends Cubit<VideosState> {
VideosCubit() : super(VideosState());
late DLServices dlService;
late ConverterService converterService;
final List<Video> _videoList = [];
List<Video> get videoList => _videoList;
Iterable<Video> downloadingVid = [];
int runningTasks = 0;
int maxConcurrentTasks = 3;
void addVideo(Video video) {
_videoList.add(video);
emit(VideosState(videoList: _videoList));
}
void clearVideos() {
_videoList.clear();
emit(VideosState(videoList: _videoList));
}
void removeVideo(Video video) {
_videoList.remove(video);
emit(VideosState(videoList: _videoList));
}
Future<void> startDownloader() async {
dlService = await DLServices.init();
downloadingVid =
_videoList.where((video) => video.status != 'downloaded').take(3);
debugPrint('Videos to download: ${downloadingVid.length}');
while (downloadingVid.isNotEmpty && runningTasks < maxConcurrentTasks) {
runningTasks++;
debugPrint('Concurrent workers: $runningTasks');
await downloadVideo(downloadingVid.first);
downloadingVid =
_videoList.where((video) => video.status != 'downloaded');
}
/*
for (Video video in videosLeft) {
final shellStream = await dlService.downloadFile(video.url, video.format);
shellStream.listen((line) {
if (line.contains('[download]')) {
debugPrint('___Download___');
debugPrint('%=${line.split('[download]')[1].split(' of')[0].trim()}');
debugPrint('______');
}
debugPrint(line);
});
}
*/
}
Future convertVideo(Video video) async {
converterService = await ConverterService.init();
debugPrint('Converting ${video.title} to $video.format ');
final shellStream =
await converterService.convertFile(video.url, video.format);
shellStream.listen((line) {
debugPrint(line);
});
}
Future downloadVideo(Video video) async {
debugPrint(
'Downloading ${video.title} in ${video.format} format ${video.url}');
final shellStream = await dlService.downloadFile(video);
var completer = Completer<void>();
shellStream.listen(
(line) {
if (line.contains('[download]') &&
line.contains('of') &&
line.contains('%')) {
var percentString =
line.split('[download]')[1].split(' of')[0].split('%')[0].trim();
var percent = percentString == null ? 0 : double.parse(percentString);
video.status = 'downloading $percent%';
emit(VideosState(videoList: _videoList));
}
if (line.contains('[EmbedThumbnail]')) {
video.status = 'downloaded';
video.filename =
line.split('Adding thumbnail to "')[1].split('"')[0].trim() ?? '';
emit(VideosState(videoList: _videoList));
if (!completer.isCompleted) {
debugPrint('Download completed');
runningTasks--;
completer.complete();
}
}
debugPrint(line);
},
onError: (error) {
if (!completer.isCompleted) {
completer
.completeError(error); // Complete with error if an error occurs
}
},
);
return completer.future;
}
}