Skip to content

Commit

Permalink
fixed completed notification
Browse files Browse the repository at this point in the history
  • Loading branch information
frostnova721 committed Feb 27, 2024
1 parent 93a9356 commit d5cbf62
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 170 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:label="animestream"
Expand Down
37 changes: 25 additions & 12 deletions lib/core/anime/downloader/downloader.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "dart:io";
import "dart:typed_data";
import "package:animestream/ui/models/notification.dart";
import "package:http/http.dart";
import "package:path_provider/path_provider.dart";
Expand Down Expand Up @@ -37,7 +38,8 @@ class Downloader {
finalPath = "${externalStorage?.path}/animestream/${fileName}.mp4";
}
final output = File(finalPath);
var out = output.openWrite();
final List<Uint8List> buffers = [];
// var out = output.openWrite();
final streamBaseLink = _makeBaseLink(streamLink);
try {
final List<String> segments = await _getSegments(streamLink);
Expand All @@ -52,35 +54,46 @@ class Downloader {
await NotificationService().pushBasicNotification(
"Download Cancelled", "The download has been cancelled.");
downloading = false;
await out.close();
await output.delete();
buffers.clear();
// await out.close();
// await output.delete();
return;
}
await Future.delayed(Duration(milliseconds: 50));
if (segment.length != 0) {
final uri =
segment.startsWith('http') ? segment : "$streamBaseLink/$segment";
final segmentNumber = segments.indexOf(segment) + 1;
print("fetching segment [$segmentNumber/${segments.length}]");
final res = await get(Uri.parse(uri));
NotificationService().updateNotificationProgressBar(
id: 69,
currentStep: segmentNumber,
maxStep: segments.length,
fileName: "$fileName.mp4",
);
id: 69,
currentStep: segmentNumber,
maxStep: segments.length - 1,
fileName: "$fileName.mp4",
path: finalPath);
if (res.statusCode == 200) {
out.add(res.bodyBytes);
}
buffers.add(res.bodyBytes);
} else
throw new Exception("ERR_REQ_FAILED");
}
}
//write the data after full download. (idk why)
//un comment the commented lines to make it write the data to file as soon as it is downloaded
final out = await output.openWrite();
for (final buffer in buffers) {
out.add(buffer);
}
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();
buffers.clear();
if (await output.exists()) output.delete();
// await out.close();
// await output.delete();
}
}

Expand Down
120 changes: 7 additions & 113 deletions lib/ui/models/customControls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ class _ControlsState extends State<Controls> {
padding: EdgeInsets.only(right: 35),
child: InkWell(
onTap: () async {
if (currentEpIndex == 0)
return floatingSnackBar(context,
"Already on the first episode");
showModalBottomSheet(
showDragHandle: true,
backgroundColor: Color(0xff121212),
Expand All @@ -229,35 +232,6 @@ class _ControlsState extends State<Controls> {
);
});
},

// showModalBottomSheet(
// showDragHandle: true,
// backgroundColor: Color(0xff121212),
// context: context,
// builder: (BuildContext context) {
// return _streamButton(0, false);
// },
// );
// try {
// await getEpisodeSources(false);
// Navigator.of(context).pop();
// showModalBottomSheet(
// showDragHandle: true,
// backgroundColor:
// Color.fromARGB(255, 19, 19, 19),
// context: context,
// builder: (BuildContext context) {
// return _streamButton(
// currentEpIndex - 1, false);
// },
// );
// } on Exception catch (e) {
// Navigator.of(context).pop(context);
// print(e);
// floatingSnackBar(
// context, "Already on first episode!");
// }
// },
child: Icon(
Icons.skip_previous_rounded,
color: Colors.white,
Expand Down Expand Up @@ -322,6 +296,10 @@ class _ControlsState extends State<Controls> {
child: InkWell(
onTap: () async {
//get next episode sources!
if (currentEpIndex + 1 ==
widget.episode['epLinks'].length)
return floatingSnackBar(context,
"You are already in the final episode!");
if (preloadedSources.isNotEmpty) {
print("from preload");

Expand Down Expand Up @@ -422,90 +400,6 @@ class _ControlsState extends State<Controls> {
},
);
}

// Container _streamButton(int episode, bool next) {
// return Container(
// padding: EdgeInsets.only(top: 20, left: 25, right: 25, bottom: 30),
// child: currentSources.length > 0
// ? ListView.builder(
// shrinkWrap: true,
// itemCount: currentSources.length,
// itemBuilder: (context, i) {
// return Container(
// margin: EdgeInsets.only(top: 15),
// decoration: BoxDecoration(
// color: Color.fromARGB(97, 190, 175, 255),
// borderRadius: BorderRadius.circular(20),
// ),
// child: ElevatedButton(
// onPressed: () async {
// if (next && preloadedSources.isNotEmpty)
// await playVideo(preloadedSources[i].link);
// else
// await playVideo(currentSources[i].link);
// currentEpIndex =
// next ? currentEpIndex + 1 : currentEpIndex - 1;
// widget.refreshPage(currentEpIndex, currentSources[i]);
// Navigator.pop(context);
// },
// style: ElevatedButton.styleFrom(
// backgroundColor: Color.fromARGB(68, 190, 175, 255),
// padding: EdgeInsets.only(
// top: 10, bottom: 10, left: 20, right: 20),
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(20),
// ),
// ),
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Row(
// mainAxisAlignment: MainAxisAlignment.start,
// crossAxisAlignment: CrossAxisAlignment.center,
// children: [
// Text(
// currentSources[i].server,
// style: TextStyle(
// fontFamily: "NotoSans",
// fontSize: 17,
// color: themeColor,
// ),
// ),
// if (currentSources[i].backup)
// Text(
// " • backup",
// style: TextStyle(
// fontFamily: "NotoSans",
// fontSize: 14,
// color: Colors.grey[500],
// ),
// )
// ],
// ),
// Padding(
// padding: EdgeInsets.only(top: 5),
// child: Text(
// currentSources[i].quality,
// style: TextStyle(
// color: Colors.white, fontFamily: "Rubik"),
// ),
// ),
// ],
// ),
// ),
// );
// },
// )
// : Container(
// height: 100,
// child: Center(
// child: CircularProgressIndicator(
// color: themeColor,
// ),
// ),
// ),
// );
// }
}

class CustomControlsBottomSheet extends StatefulWidget {
Expand Down
86 changes: 44 additions & 42 deletions lib/ui/models/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class NotificationService {
pushBasicNotification(String title, String content) {
AwesomeNotifications().createNotification(
content: NotificationContent(
id: 69,
channelKey: "animestream",
title: title,
body: content,
),
id: 69,
channelKey: "animestream",
title: title,
body: content,
backgroundColor: themeColor),
);
}

Expand All @@ -51,37 +51,44 @@ class NotificationService {
required int currentStep,
required int maxStep,
required String fileName,
required String path,
}) async {
if (currentStep < maxStep) {
int progress = ((currentStep / maxStep) * 100).round();
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'animestream',
title: 'Downloading $fileName ($progress%)',
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")
]
);
content: NotificationContent(
id: id,
channelKey: 'animestream',
title: 'Downloading $fileName ($progress%)',
body: 'The file is being downloaded',
category: NotificationCategory.Progress,
payload: {
'path': path,
},
notificationLayout: NotificationLayout.ProgressBar,
progress: progress.toDouble(),
locked: true,
backgroundColor: themeColor,
),
actionButtons: [
NotificationActionButton(key: "cancel", label: "cancel")
]);
} else {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'animestream',
title: 'Download finished',
body: '$fileName has been downloaded succesfully!',
payload: {'file': '$fileName', 'path': ''},
locked: false,
),
actionButtons: [NotificationActionButton(key: "open_file", label: "open")]
);
content: NotificationContent(
id: id,
channelKey: 'animestream',
title: 'Download finished',
body: '$fileName has been downloaded succesfully!',
payload: {
'path': path,
},
locked: false,
backgroundColor: themeColor,
),
actionButtons: [
NotificationActionButton(key: "open_file", label: "open")
]);
}
}
}
Expand All @@ -102,17 +109,12 @@ 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,
// arguments: receivedAction,
// );
if (receivedAction.buttonKeyPressed == 'cancel') {
NotificationService().removeNotification();
Downloader().cancelDownload();
}
if (receivedAction.buttonKeyPressed == 'open_file') {
OpenFile.open(receivedAction.payload?['path'], type: "video/mp4");
}
}
}
4 changes: 1 addition & 3 deletions lib/ui/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter/services.dart';
import 'package:hive/hive.dart';


class Home extends StatefulWidget {
const Home({super.key});

Expand Down Expand Up @@ -54,8 +55,6 @@ class _HomeState extends State<Home> {
}
box.close();

// print(recentlyWatched[0].widget);

final List currentlyAiringResponse =
await Anilist().getCurrentlyAiringAnime();
if (currentlyAiringResponse.length == 0) return;
Expand All @@ -64,7 +63,6 @@ class _HomeState extends State<Home> {
currentlyAiringResponse.forEach((e) {
final title = e['title']['english'] ?? e['title']['romaji'];
final image = e['coverImage']['large'] ?? e['coverImage']['extraLarge'];
// final id = e['id'];
currentlyAiring
.add(ListElement(widget: animeCard(title, image), info: e));
});
Expand Down

0 comments on commit d5cbf62

Please sign in to comment.