Skip to content

Commit

Permalink
fix: link download event to chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Feb 5, 2024
1 parent f779c54 commit caae44f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Mangarr.Stack/Downloading/PageDownloaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Mangarr.Stack.Downloading;

public class PageDownloaderService
{
public delegate Task ProcessCallbackDelegate(int progress);
public delegate Task ProcessCallbackDelegate(string chapterId, int progress);

private readonly ConversionService _conversionService;

public PageDownloaderService(ConversionService conversionService) => _conversionService = conversionService;

public event ProcessCallbackDelegate? Progress;

public async Task<Result<DownloadedPages>> DownloadPages(ISource source, PageList pageList)
public async Task<Result<DownloadedPages>> DownloadPages(string chapterId, ISource source, PageList pageList)
{
List<DownloadedPage> pages = new();

Expand Down Expand Up @@ -50,11 +50,11 @@ public async Task<Result<DownloadedPages>> DownloadPages(ISource source, PageLis
conversionResult.Reasons));
}

await Task.Delay(100);
await Task.Delay(Random.Shared.Next(10, 1000));

Progress:
int progress = (int)Math.Round(i / total * 100);
Progress?.Invoke(progress);
Progress?.Invoke(chapterId, progress);
}

return Result.Ok(new DownloadedPages(pages));
Expand Down
17 changes: 13 additions & 4 deletions src/Mangarr.Stack/Jobs/DownloadChapterJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task Execute(IJobExecutionContext context)

try
{
await NewMethod(id!);
await DownloadChapter(id!);
}
catch (Exception e)
{
Expand All @@ -99,7 +99,7 @@ public async Task Execute(IJobExecutionContext context)
}
}

private async Task NewMethod(string id)
private async Task DownloadChapter(string id)
{
if (string.IsNullOrEmpty(id))
{
Expand Down Expand Up @@ -210,7 +210,8 @@ private async Task<bool> TryGetPageList()

private async Task<bool> TryDownloadPages()
{
Result<DownloadedPages> downloadPages = await _pageDownloaderService.DownloadPages(_source!, _pageList);
Result<DownloadedPages> downloadPages =
await _pageDownloaderService.DownloadPages(_chapter!.Id, _source!, _pageList);

if (downloadPages.IsFailed)
{
Expand Down Expand Up @@ -279,7 +280,15 @@ private async Task<bool> TryExportArchive()
return true;
}

private Task OnPageDownloaderProgress(int progress) => SetProgress(10 + (int)Math.Round(progress * 0.8f));
private Task OnPageDownloaderProgress(string chapterId, int progress)
{
if (chapterId != _chapter!.Id)
{
return Task.CompletedTask;
}

return SetProgress(10 + (int)Math.Round(progress * 0.8f));
}

private async Task SetProgress(int progress) =>
await _chapterProgressRepository.UpdateAsync(
Expand Down

0 comments on commit caae44f

Please sign in to comment.