Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed May 6, 2023
1 parent bd1d4ed commit 54ac1eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
25 changes: 9 additions & 16 deletions AniStream/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
using Juro.Providers.Anime;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Storage;
using static Android.Renderscripts.ScriptGroup;
using AlertDialog = AndroidX.AppCompat.App.AlertDialog;

namespace AniStream;
Expand Down Expand Up @@ -290,6 +289,9 @@ private void SetupViewPager()
case AnimeSites.AnimePahe:
bottomNavigationView.InflateMenu(Resource.Menu.bottommenu5);
break;
case AnimeSites.NineAnime:
bottomNavigationView.InflateMenu(Resource.Menu.bottommenu_nineanime);
break;
}

viewPager.CurrentItem = 0;
Expand All @@ -301,21 +303,6 @@ private void SetupViewPager()
{
switch (e.Item.ItemId)
{
//Server1
case Resource.Id.lastUpdated1:
viewPager.CurrentItem = 0;
break;
case Resource.Id.popular1:
viewPager.CurrentItem = 1;
break;
case Resource.Id.ongoing1:
viewPager.CurrentItem = 2;
break;
case Resource.Id.movies1:
viewPager.CurrentItem = 3;
break;

//Server2
case Resource.Id.Popular:
viewPager.CurrentItem = 0;
break;
Expand Down Expand Up @@ -410,6 +397,7 @@ private async void SetupSources(IMenu menu)
var gogoanime = menu.FindItem(Resource.Id.source_gogoanime);
var zoro = menu.FindItem(Resource.Id.source_zoro);
var animepahe = menu.FindItem(Resource.Id.source_animepahe);
var nineanime = menu.FindItem(Resource.Id.source_nineanime);

switch (WeebUtils.AnimeSite)
{
Expand All @@ -422,6 +410,9 @@ private async void SetupSources(IMenu menu)
case AnimeSites.AnimePahe:
animepahe?.SetChecked(true);
break;
case AnimeSites.NineAnime:
nineanime?.SetChecked(true);
break;
}
}

Expand Down Expand Up @@ -473,6 +464,8 @@ private async void SaveSelectedSource(int id)
WeebUtils.AnimeSite = AnimeSites.Zoro;
else if (id == Resource.Id.source_animepahe)
WeebUtils.AnimeSite = AnimeSites.AnimePahe;
else if (id == Resource.Id.source_nineanime)
WeebUtils.AnimeSite = AnimeSites.NineAnime;

if (lastAnimeSite != WeebUtils.AnimeSite)
{
Expand Down
11 changes: 8 additions & 3 deletions AniStream/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Android.Util;
using AndroidX.Core.App;
using AniStream.Utils.Downloading;
using JGrabber.Grabbed;
using Httpz.Hls;
using Newtonsoft.Json;

namespace AniStream.Services;
Expand All @@ -29,13 +29,18 @@ public override async void HandleMessage(Message msg)

var downloader = new HlsDownloader(_downloadService);

var stream = JsonConvert.DeserializeObject<GrabbedHlsStream>(msg.Data.GetString("stream")!)!;
var stream = JsonConvert.DeserializeObject<HlsStream>(msg.Data.GetString("stream")!)!;
var headers = JsonConvert.DeserializeObject<Dictionary<string, string>>(msg.Data.GetString("headers")!)!;
var fileName = msg.Data.GetString("fileName")!;

try
{
await downloader.DownloadAsync(fileName, stream, headers, CancellationTokenSource.Token);
await downloader.DownloadAsync(
fileName,
stream,
headers,
CancellationTokenSource.Token
);
}
catch (Exception e)
{
Expand Down
24 changes: 15 additions & 9 deletions AniStream/Utils/Downloading/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using AniStream.Services;
using AniStream.Utils.Extensions;
using AniStream.Utils.Listeners;
using JGrabber.Grabbed;
using Newtonsoft.Json;

namespace AniStream.Utils.Downloading;
Expand Down Expand Up @@ -172,6 +171,10 @@ public async Task DownloadHls(
string url,
Dictionary<string, string> headers)
{
// User-agent
if (!headers.ContainsKey("User-Agent"))
headers.Add("User-Agent", Httpz.Utils.Http.ChromeUserAgent());

var loadingDialog = WeebUtils.SetProgressDialog(
_activity,
"Getting qualities. Please wait...",
Expand All @@ -185,17 +188,20 @@ public async Task DownloadHls(
cancellationTokenSource.Cancel();
};

var metadataResources = new List<GrabbedHlsStreamMetadata>();
var qualities = new List<Httpz.Hls.HlsStreamMetadata>();

try
{
var downloader = new Httpz.HlsDownloader(Http.ClientProvider);

metadataResources = await downloader.GetHlsStreamMetadatasAsync(
url,
headers,
cancellationTokenSource.Token
);
await Task.Run(async () =>
{
qualities = await downloader.GetQualitiesAsync(
url,
headers,
cancellationTokenSource.Token
);
});

loadingDialog.Dismiss();
}
Expand All @@ -213,7 +219,7 @@ public async Task DownloadHls(
listener.OnItemClick += async (s, which) =>
{
loadingDialog = WeebUtils.SetProgressDialog(_activity, "Loading...", false);
var stream = await metadataResources[which].Stream;
var stream = qualities[which].Stream!;
loadingDialog.Dismiss();

//var intent = new Intent();
Expand All @@ -230,7 +236,7 @@ public async Task DownloadHls(
builder.SetTitle(fileName);
builder.SetNegativeButton("Cancel", (s, e) => { });

var items = metadataResources.Select(x => x.Resolution?.ToString()
var items = qualities.Select(x => x.Resolution?.ToString()
?? "Default quality").ToArray();

builder.SetItems(items, listener);
Expand Down
4 changes: 2 additions & 2 deletions AniStream/Utils/Downloading/HlsDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using AndroidX.Core.Content;
using AniStream.Services;
using AniStream.Utils.Extensions;
using JGrabber.Grabbed;
using Httpz.Hls;
using Laerdal.FFmpeg.Android;
using Microsoft.Maui.Storage;

Expand All @@ -28,7 +28,7 @@ public HlsDownloader(Service service)

public async Task DownloadAsync(
string fileName,
GrabbedHlsStream stream,
HlsStream stream,
Dictionary<string, string> headers,
CancellationToken cancellationToken = default)
{
Expand Down

0 comments on commit 54ac1eb

Please sign in to comment.