Converted to INotifyPropertyChanged for more targeted view update
This commit is contained in:
parent
88e892196f
commit
a7b7e3efea
@ -5,8 +5,10 @@ using LibationFileManager;
|
|||||||
using LibationWinForms.BookLiberation;
|
using LibationWinForms.BookLiberation;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
@ -32,35 +34,66 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
Failed
|
Failed
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ProcessBook
|
/// <summary>
|
||||||
|
/// This is the viewmodel for queued processables
|
||||||
|
/// </summary>
|
||||||
|
public class ProcessBook : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
public event EventHandler Completed;
|
public event EventHandler Completed;
|
||||||
public event EventHandler DataAvailable;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
public Processable CurrentProcessable => _currentProcessable ??= Processes.Dequeue().Invoke();
|
private ProcessBookResult _result = ProcessBookResult.None;
|
||||||
public ProcessBookResult Result { get; private set; } = ProcessBookResult.None;
|
private ProcessBookStatus _status = ProcessBookStatus.Queued;
|
||||||
public ProcessBookStatus Status { get; private set; } = ProcessBookStatus.Queued;
|
private string _bookText;
|
||||||
public string BookText { get; private set; }
|
private int _progress;
|
||||||
public Image Cover { get; private set; }
|
private TimeSpan _timeRemaining;
|
||||||
public int Progress { get; private set; }
|
private LibraryBook _libraryBook;
|
||||||
public TimeSpan TimeRemaining { get; private set; }
|
private Image _cover;
|
||||||
public LibraryBook LibraryBook { get; }
|
|
||||||
|
|
||||||
|
public ProcessBookResult Result { get => _result; private set { _result = value; NotifyPropertyChanged(); } }
|
||||||
|
public ProcessBookStatus Status { get => _status; private set { _status = value; NotifyPropertyChanged(); } }
|
||||||
|
public string BookText { get => _bookText; private set { _bookText = value; NotifyPropertyChanged(); } }
|
||||||
|
public int Progress { get => _progress; private set { _progress = value; NotifyPropertyChanged(); } }
|
||||||
|
public TimeSpan TimeRemaining { get => _timeRemaining; private set { _timeRemaining = value; NotifyPropertyChanged(); } }
|
||||||
|
public LibraryBook LibraryBook { get => _libraryBook; private set { _libraryBook = value; NotifyPropertyChanged(); } }
|
||||||
|
public Image Cover { get => _cover; private set { _cover = value; NotifyPropertyChanged(); } }
|
||||||
|
|
||||||
|
|
||||||
|
private Processable CurrentProcessable => _currentProcessable ??= Processes.Dequeue().Invoke();
|
||||||
|
private Processable NextProcessable() => _currentProcessable = null;
|
||||||
private Processable _currentProcessable;
|
private Processable _currentProcessable;
|
||||||
private Func<byte[]> GetCoverArtDelegate;
|
private Func<byte[]> GetCoverArtDelegate;
|
||||||
private readonly Queue<Func<Processable>> Processes = new();
|
private readonly Queue<Func<Processable>> Processes = new();
|
||||||
private readonly LogMe Logger;
|
private readonly LogMe Logger;
|
||||||
|
|
||||||
public ProcessBook(LibraryBook libraryBook, Image coverImage, LogMe logme)
|
public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
|
||||||
|
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
|
|
||||||
|
public ProcessBook(LibraryBook libraryBook, LogMe logme)
|
||||||
{
|
{
|
||||||
LibraryBook = libraryBook;
|
LibraryBook = libraryBook;
|
||||||
Cover = coverImage;
|
|
||||||
Logger = logme;
|
Logger = logme;
|
||||||
|
|
||||||
title = LibraryBook.Book.Title;
|
title = LibraryBook.Book.Title;
|
||||||
authorNames = LibraryBook.Book.AuthorNames();
|
authorNames = LibraryBook.Book.AuthorNames();
|
||||||
narratorNames = LibraryBook.Book.NarratorNames();
|
narratorNames = LibraryBook.Book.NarratorNames();
|
||||||
BookText = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}";
|
_bookText = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}";
|
||||||
|
|
||||||
|
(bool isDefault, byte[] picture) = PictureStorage.GetPicture(new PictureDefinition(LibraryBook.Book.PictureId, PictureSize._80x80));
|
||||||
|
|
||||||
|
if (isDefault)
|
||||||
|
PictureStorage.PictureCached += PictureStorage_PictureCached;
|
||||||
|
_cover = Dinah.Core.Drawing.ImageReader.ToImage(picture);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.Definition.PictureId == LibraryBook.Book.PictureId)
|
||||||
|
{
|
||||||
|
Cover = Dinah.Core.Drawing.ImageReader.ToImage(e.Picture);
|
||||||
|
PictureStorage.PictureCached -= PictureStorage_PictureCached;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ProcessBookResult> ProcessOneAsync()
|
public async Task<ProcessBookResult> ProcessOneAsync()
|
||||||
@ -103,8 +136,6 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
ProcessBookResult.FailedRetry => ProcessBookStatus.Queued,
|
ProcessBookResult.FailedRetry => ProcessBookStatus.Queued,
|
||||||
_ => ProcessBookStatus.Failed,
|
_ => ProcessBookStatus.Failed,
|
||||||
};
|
};
|
||||||
|
|
||||||
DataAvailable?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result;
|
return Result;
|
||||||
@ -118,7 +149,7 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
{
|
{
|
||||||
//There's some threadding bug that causes this to hang if executed synchronously.
|
//There's some threadding bug that causes this to hang if executed synchronously.
|
||||||
Task.Run(audioDecodable.Cancel);
|
Task.Run(audioDecodable.Cancel);
|
||||||
DataAvailable?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -135,18 +166,19 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
{
|
{
|
||||||
Processes.Enqueue(() => new T());
|
Processes.Enqueue(() => new T());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => LibraryBook.ToString();
|
public override string ToString() => LibraryBook.ToString();
|
||||||
|
|
||||||
#region Subscribers and Unsubscribers
|
#region Subscribers and Unsubscribers
|
||||||
|
|
||||||
private void LinkProcessable(Processable strProc)
|
private void LinkProcessable(Processable processable)
|
||||||
{
|
{
|
||||||
strProc.Begin += Processable_Begin;
|
processable.Begin += Processable_Begin;
|
||||||
strProc.Completed += Processable_Completed;
|
processable.Completed += Processable_Completed;
|
||||||
strProc.StreamingProgressChanged += Streamable_StreamingProgressChanged;
|
processable.StreamingProgressChanged += Streamable_StreamingProgressChanged;
|
||||||
strProc.StreamingTimeRemaining += Streamable_StreamingTimeRemaining;
|
processable.StreamingTimeRemaining += Streamable_StreamingTimeRemaining;
|
||||||
|
|
||||||
if (strProc is AudioDecodable audioDecodable)
|
if (processable is AudioDecodable audioDecodable)
|
||||||
{
|
{
|
||||||
audioDecodable.RequestCoverArt += AudioDecodable_RequestCoverArt;
|
audioDecodable.RequestCoverArt += AudioDecodable_RequestCoverArt;
|
||||||
audioDecodable.TitleDiscovered += AudioDecodable_TitleDiscovered;
|
audioDecodable.TitleDiscovered += AudioDecodable_TitleDiscovered;
|
||||||
@ -156,14 +188,14 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UnlinkProcessable(Processable strProc)
|
private void UnlinkProcessable(Processable processable)
|
||||||
{
|
{
|
||||||
strProc.Begin -= Processable_Begin;
|
processable.Begin -= Processable_Begin;
|
||||||
strProc.Completed -= Processable_Completed;
|
processable.Completed -= Processable_Completed;
|
||||||
strProc.StreamingProgressChanged -= Streamable_StreamingProgressChanged;
|
processable.StreamingProgressChanged -= Streamable_StreamingProgressChanged;
|
||||||
strProc.StreamingTimeRemaining -= Streamable_StreamingTimeRemaining;
|
processable.StreamingTimeRemaining -= Streamable_StreamingTimeRemaining;
|
||||||
|
|
||||||
if (strProc is AudioDecodable audioDecodable)
|
if (processable is AudioDecodable audioDecodable)
|
||||||
{
|
{
|
||||||
audioDecodable.RequestCoverArt -= AudioDecodable_RequestCoverArt;
|
audioDecodable.RequestCoverArt -= AudioDecodable_RequestCoverArt;
|
||||||
audioDecodable.TitleDiscovered -= AudioDecodable_TitleDiscovered;
|
audioDecodable.TitleDiscovered -= AudioDecodable_TitleDiscovered;
|
||||||
@ -201,7 +233,6 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
private void updateBookInfo()
|
private void updateBookInfo()
|
||||||
{
|
{
|
||||||
BookText = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}";
|
BookText = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}";
|
||||||
DataAvailable?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AudioDecodable_RequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate)
|
public void AudioDecodable_RequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate)
|
||||||
@ -214,7 +245,6 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
private void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt)
|
private void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt)
|
||||||
{
|
{
|
||||||
Cover = Dinah.Core.Drawing.ImageReader.ToImage(coverArt);
|
Cover = Dinah.Core.Drawing.ImageReader.ToImage(coverArt);
|
||||||
DataAvailable?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -223,7 +253,6 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
private void Streamable_StreamingTimeRemaining(object sender, TimeSpan timeRemaining)
|
private void Streamable_StreamingTimeRemaining(object sender, TimeSpan timeRemaining)
|
||||||
{
|
{
|
||||||
TimeRemaining = timeRemaining;
|
TimeRemaining = timeRemaining;
|
||||||
DataAvailable?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Streamable_StreamingProgressChanged(object sender, Dinah.Core.Net.Http.DownloadProgress downloadProgress)
|
private void Streamable_StreamingProgressChanged(object sender, Dinah.Core.Net.Http.DownloadProgress downloadProgress)
|
||||||
@ -235,8 +264,6 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
TimeRemaining = TimeSpan.Zero;
|
TimeRemaining = TimeSpan.Zero;
|
||||||
else
|
else
|
||||||
Progress = (int)downloadProgress.ProgressPercentage;
|
Progress = (int)downloadProgress.ProgressPercentage;
|
||||||
|
|
||||||
DataAvailable?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -273,7 +300,7 @@ namespace LibationWinForms.ProcessQueue
|
|||||||
|
|
||||||
if (Processes.Count > 0)
|
if (Processes.Count > 0)
|
||||||
{
|
{
|
||||||
_currentProcessable = null;
|
NextProcessable();
|
||||||
LinkProcessable(CurrentProcessable);
|
LinkProcessable(CurrentProcessable);
|
||||||
var result = await CurrentProcessable.ProcessSingleAsync(libraryBook, validate: true);
|
var result = await CurrentProcessable.ProcessSingleAsync(libraryBook, validate: true);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user