Skip to content

Commit

Permalink
removed some logs, logic fix
Browse files Browse the repository at this point in the history
fixed logic in downloader and filter the array to only contain the non empty string
  • Loading branch information
frostnova721 committed Feb 26, 2024
1 parent c921171 commit 93a9356
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
33 changes: 29 additions & 4 deletions lib/core/anime/downloader/downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import "package:http/http.dart";
import "package:path_provider/path_provider.dart";
import "../../commons/utils.dart";

bool downloading = false;

class Downloader {
String _makeBaseLink(String uri) {
final split = uri.split('/');
split.removeLast();
return split.join('/');
}

void cancelDownload() {
downloading = false;
}

Future<void> download(String streamLink, String fileName) async {
final downPath = await Directory('/storage/emulated/0/Download');
String finalPath;
fileName = fileName.replaceAll(RegExp(r'[<>:"/\\|?*]'), '');
downloading = true;
if (downPath.existsSync()) {
final directory = Directory("${downPath.path}/animestream/");
if (!(await directory.exists())) {
Expand All @@ -33,16 +41,30 @@ class Downloader {
final streamBaseLink = _makeBaseLink(streamLink);
try {
final List<String> segments = await _getSegments(streamLink);
for (final segment in segments) {
List<String> segmentsFiltered = [];
segments.forEach(
(element) {
if (element.length != 0) segmentsFiltered.add(element);
},
);
for (final segment in segmentsFiltered) {
if (!downloading) {
await NotificationService().pushBasicNotification(
"Download Cancelled", "The download has been cancelled.");
downloading = false;
await out.close();
await output.delete();
return;
}
if (segment.length != 0) {
final uri =
segment.startsWith('http') ? segment : "$streamBaseLink/$segment";
print(
"fetching segment [${segments.indexOf(segment)}/${segments.length}]");
final segmentNumber = segments.indexOf(segment) + 1;
print("fetching segment [$segmentNumber/${segments.length}]");
final res = await get(Uri.parse(uri));
NotificationService().updateNotificationProgressBar(
id: 69,
currentStep: segments.indexOf(segment) + 1,
currentStep: segmentNumber,
maxStep: segments.length,
fileName: "$fileName.mp4",
);
Expand All @@ -54,6 +76,9 @@ class Downloader {
await out.close();
} catch (err) {
print(err);
await NotificationService().pushBasicNotification(
"Download failed", "The download has been cancelled.");
downloading = false;
await out.close();
await output.delete();
}
Expand Down
1 change: 0 additions & 1 deletion lib/ui/models/bottomSheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class BottomSheetContentState extends State<BottomSheetContent> {
if (mounted)
setState(() {
qualities = qualities + mainList;
print(qualities);
});
}

Expand Down
30 changes: 23 additions & 7 deletions lib/ui/models/notification.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:animestream/core/anime/downloader/downloader.dart';
import 'package:animestream/ui/theme/mainTheme.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';

class NotificationService {
Future<void> init() async {
Expand Down Expand Up @@ -29,7 +31,7 @@ class NotificationService {
AwesomeNotifications().requestPermissionToSendNotifications();
}

pushBasicNotification(String title, String content, current) {
pushBasicNotification(String title, String content) {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 69,
Expand All @@ -40,38 +42,45 @@ class NotificationService {
);
}

void updateNotificationProgressBar({
removeNotification() {
AwesomeNotifications().cancel(69);
}

Future<void> updateNotificationProgressBar({
required int id,
required int currentStep,
required int maxStep,
required String fileName,
}) {
}) async {
if (currentStep < maxStep) {
int progress = ((currentStep / maxStep) * 100).round();
AwesomeNotifications().createNotification(
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'animestream',
title: 'Downloading $fileName ($progress%)',
body: '$fileName',
body: 'The file is being downloaded',
category: NotificationCategory.Progress,
payload: {'file': '$fileName', 'path': ''},
notificationLayout: NotificationLayout.ProgressBar,
progress: progress.toDouble(),
locked: true,
),
actionButtons: [
NotificationActionButton(key: "cancel", label: "cancel")
]
);
} else {
AwesomeNotifications().createNotification(
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'animestream',
title: 'Download finished',
body: '$fileName has been downloaded succesfully!',
category: NotificationCategory.Progress,
payload: {'file': '$fileName', 'path': ''},
locked: false,
),
actionButtons: [NotificationActionButton(key: "open_file", label: "open")]
);
}
}
Expand All @@ -93,6 +102,13 @@ class NotificationController {
@pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
if(receivedAction.buttonKeyPressed == 'cancel') {
NotificationService().removeNotification();
Downloader().cancelDownload();
}
if(receivedAction.buttonKeyPressed == 'open_file') {
OpenFile.open("/storage/emulated/0/Download/animestream");
}
// MyApp.navigatorKey.currentState?.pushNamedAndRemoveUntil(
// '/notification-page',
// (route) => (route.settings.name != '/notification-page') || route.isFirst,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class _HomeState extends State<Home> {
Row(
children: [
IconButton(
onPressed: () {
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.8.2+1"
open_file:
dependency: "direct dev"
description:
name: open_file
sha256: a5a32d44acb7c899987d0999e1e3cbb0a0f1adebbf41ac813ec6d2d8faa0af20
url: "https://pub.dev"
source: hosted
version: "3.3.2"
page_transition:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dev_dependencies:
hive_flutter: ^1.1.0
path_provider: ^2.0.11
awesome_notifications: ^0.9.2
open_file: ^3.3.2

flutter_launcher_icons:
android: true
Expand Down

0 comments on commit 93a9356

Please sign in to comment.