Skip to content

Commit

Permalink
Improvements 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Dec 21, 2022
1 parent 4d8d5f7 commit fb6b6b6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
9 changes: 8 additions & 1 deletion AniStream/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
<application android:allowBackup="true" android:theme="@style/AppTheme" android:icon="@drawable/logo" android:label="@string/app_name" android:supportsRtl="true" android:usesCleartextTraffic="true">
<application
android:allowBackup="true"
android:theme="@style/AppTheme"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:largeHeap="true">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
Expand Down
29 changes: 20 additions & 9 deletions AniStream/Utils/Downloading/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
using System.Threading.Tasks;
using AniStream.Services;
using AnimeDl.Utils.Extensions;
using DotNetTools.JGrabber.Grabbed;
using System.Collections.Generic;

namespace AniStream.Utils.Downloading;

public class Downloader
{
private readonly Activity _activity;
private readonly AnimeClient _client = new(WeebUtils.AnimeSite);
private readonly int _notificationId;

public Downloader(Activity activity)
{
_activity = activity;
_notificationId = (int)DateTime.Now.Ticks;
}

public async void Download(
Expand All @@ -35,16 +35,17 @@ public async void Download(
bool hasStoragePermission = androidStoragePermission.HasStoragePermission();
if (!hasStoragePermission)
{
_activity.ShowToast("Please grant storage permission then retry");
//_activity.ShowToast("Please grant storage permission then retry");
hasStoragePermission = await androidStoragePermission.RequestStoragePermission();
}

if (!hasStoragePermission)
return;

var extension = System.IO.Path.GetExtension(fileName).Split('.').LastOrDefault();

var mime = MimeTypeMap.Singleton!;
var mimeType = mime.GetMimeTypeFromExtension("mp4");
var mimeTypem4a = mime.GetMimeTypeFromExtension("m4a");
var mimeType = mime.GetMimeTypeFromExtension(extension);

//string invalidCharRemoved = Episode.EpisodeName.Replace("[\\\\/:*?\"<>|]", "");

Expand All @@ -66,7 +67,7 @@ public async void Download(

//request.SetDestinationInExternalPublicDir(WeebUtils.AppFolderName, invalidCharsRemoved + ".mp4");
request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, invalidCharsRemoved);
var dm = (DownloadManager)Application.Context.GetSystemService(Android.Content.Context.DownloadService);
var dm = (DownloadManager)Application.Context.GetSystemService(Android.Content.Context.DownloadService)!;
long id = dm.Enqueue(request);

_activity.ShowToast("Download started");
Expand All @@ -78,8 +79,18 @@ public async Task DownloadHls(
NameValueCollection headers)
{
var loadingDialog = WeebUtils.SetProgressDialog(_activity, "Getting qualities. Please wait...", false);
var metadataResources = await _client.GetHlsStreamMetadatasAsync(url, headers);
loadingDialog.Dismiss();
var metadataResources = new List<GrabbedHlsStreamMetadata>();
try
{
metadataResources = await _client.GetHlsStreamMetadatasAsync(url, headers);
loadingDialog.Dismiss();
}
catch
{
loadingDialog.Dismiss();
_activity.ShowToast("Failed to get qualities. Try another source");
return;
}

var listener = new DialogClickListener();
listener.OnItemClick += async (s, which) =>
Expand All @@ -98,7 +109,7 @@ public async Task DownloadHls(
//await Download(fileName, stream, headers);
};

var builder = new AlertDialog.Builder(_activity);
var builder = new AlertDialog.Builder(_activity, Resource.Style.DialogTheme);
builder.SetTitle(fileName);

builder.SetNegativeButton("Cancel", (s, e) => { });
Expand Down
21 changes: 19 additions & 2 deletions AniStream/Utils/Downloading/DownloaderProgress.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using Android.App;
using Android.Content;
using AndroidX.Core.App;
Expand All @@ -12,6 +13,9 @@ public class DownloaderProgress : IProgress<double>, IDisposable
private readonly Service _service;
private readonly int _notificationId;
private readonly string _title;

private Timer? timer;
private double currentProgress = 0;

public DownloaderProgress(
Service service,
Expand All @@ -27,7 +31,15 @@ public DownloaderProgress(

public void Report(double progress)
{
ShowProgressNotification((int)(progress * 100));
//ShowProgressNotification((int)(progress * 100));

timer ??= new Timer(TimerHandler, null, 0, 1000);
currentProgress = progress;
}

private void TimerHandler(object? state)
{
ShowProgressNotification((int)(currentProgress * 100));
}

private void ShowProgressNotification(int progress)
Expand Down Expand Up @@ -55,5 +67,10 @@ private void ShowProgressNotification(int progress)
_service.StartForeground(_notificationId, builder.Build());
}

public void Dispose() => GC.SuppressFinalize(this);
public void Dispose()
{
timer?.Dispose();
timer = null;
GC.SuppressFinalize(this);
}
}
3 changes: 2 additions & 1 deletion AniStream/Utils/Downloading/HlsDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public async Task DownloadAsync(
{
try
{
await _client.DownloadTsAsync(stream, headers, filePath, progress, cancellationToken);
//await _client.DownloadTsAsync(stream, headers, filePath, progress, cancellationToken);
await _client.DownloadAllTsThenMergeAsync(stream, headers, filePath, progress, 15, cancellationToken);
}
catch
{
Expand Down

0 comments on commit fb6b6b6

Please sign in to comment.