Skip to content

Commit

Permalink
Report progress from some item factories.
Browse files Browse the repository at this point in the history
  • Loading branch information
pudding committed Jan 28, 2024
1 parent d8c9f1b commit c8aa43b
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 23 deletions.
3 changes: 0 additions & 3 deletions FoxTunes.Core/Tasks/LibraryTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
using FoxTunes.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using static FoxTunes.LibraryHierarchyNode;

namespace FoxTunes
{
Expand Down
16 changes: 12 additions & 4 deletions FoxTunes.Output.Bass.Archive/BassArchiveStreamProviderBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public override void InitializeComponent(ICore core)
password.Reset();
}
this.PlaylistBrowser = core.Components.PlaylistBrowser;
this.Factory = new ArchivePlaylistItemFactory();
this.Factory = new ArchivePlaylistItemFactory(this.Visible);
this.Factory.InitializeComponent(core);
base.InitializeComponent(core);
}
Expand Down Expand Up @@ -346,7 +346,8 @@ protected override async Task AddPaths(IEnumerable<string> paths)

protected override async Task AddPlaylistItems(IEnumerable<string> paths, CancellationToken cancellationToken)
{
var playlistItems = await this.Factory.Create(paths).ConfigureAwait(false);
var playlistItems = default(PlaylistItem[]);
await this.WithSubTask(this.Factory, async () => playlistItems = await this.Factory.Create(paths).ConfigureAwait(false)).ConfigureAwait(false);
using (var transaction = this.Database.BeginTransaction(this.Database.PreferredIsolationLevel))
{
var set = this.Database.Set<PlaylistItem>(transaction);
Expand Down Expand Up @@ -401,7 +402,13 @@ public IEnumerable<string> Roots

public override void InitializeComponent(ICore core)
{
this.Factory = new ArchiveLibraryItemFactory();
//Clear any cached passwords if possible.
var password = ComponentRegistry.Instance.GetComponent<BassArchiveStreamPasswordBehaviour>();
if (password != null)
{
password.Reset();
}
this.Factory = new ArchiveLibraryItemFactory(this.Visible);
this.Factory.InitializeComponent(core);
base.InitializeComponent(core);
}
Expand All @@ -420,7 +427,8 @@ protected override async Task OnRun()

protected override async Task AddPaths(IEnumerable<string> paths)
{
var libraryItems = await this.Factory.Create(paths).ConfigureAwait(false);
var libraryItems = default(LibraryItem[]);
await this.WithSubTask(this.Factory, async () => libraryItems = await this.Factory.Create(paths).ConfigureAwait(false)).ConfigureAwait(false);
using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_HIGH, async cancellationToken =>
{
await this.RemoveLibraryItems().ConfigureAwait(false);
Expand Down
9 changes: 9 additions & 0 deletions FoxTunes.Output.Bass.Archive/Properties/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions FoxTunes.Output.Bass.Archive/Properties/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ArchiveItemFactory.Name" xml:space="preserve">
<value>Reading archive</value>
</data>
<data name="BassArchiveStreamProviderBehaviour.Open" xml:space="preserve">
<value>Open Archive</value>
</data>
Expand Down
20 changes: 17 additions & 3 deletions FoxTunes.Output.Bass.Archive/Utilities/ArchiveItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

namespace FoxTunes
{
public abstract class ArchiveItemFactory<T> : BaseComponent where T : IFileData, new()
public abstract class ArchiveItemFactory<T> : PopulatorBase where T : IFileData, new()
{
public ArchiveItemFactory(bool reportProgress) : base(reportProgress)
{

}

public IOutput Output { get; private set; }

public IMetaDataSourceFactory MetaDataSourceFactory { get; private set; }
Expand All @@ -35,6 +40,11 @@ public override void InitializeComponent(ICore core)

public async Task<T[]> Create(IEnumerable<string> paths)
{
if (this.ReportProgress)
{
this.Name = Strings.ArchiveItemFactory_Name;
}

var items = new List<T>();
foreach (var path in paths)
{
Expand Down Expand Up @@ -80,6 +90,10 @@ protected virtual async Task Create(IntPtr archive, string path, List<T> items)
{
continue;
}
if (this.ReportProgress)
{
this.Description = Path.GetFileName(entry.path);
}
var fileName = ArchiveUtils.CreateUrl(path, entry.path);
var item = new T()
{
Expand Down Expand Up @@ -123,13 +137,13 @@ await metaDataSource.GetMetaData(fileAbstraction).ConfigureAwait(false)
}
else
{
//TODO: Warn.
Logger.Write(this, LogLevel.Warn, "Failed to read archive entry at position {0}.", a);
}
}
}
else
{
//TODO: Warn.
Logger.Write(this, LogLevel.Warn, "Failed to read archive entries.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
{
public class ArchiveLibraryItemFactory : ArchiveItemFactory<LibraryItem>
{
public ArchiveLibraryItemFactory(bool reportProgress) : base(reportProgress)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{
public class ArchivePlaylistItemFactory : ArchiveItemFactory<PlaylistItem>
{
public ArchivePlaylistItemFactory(bool reportProgress) : base(reportProgress)
{

}
}
}
5 changes: 3 additions & 2 deletions FoxTunes.Output.Bass.Cd/BassCdBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public override void InitializeComponent(ICore core)
{
this.PlaylistManager = core.Managers.Playlist;
this.PlaylistBrowser = core.Components.PlaylistBrowser;
this.Factory = new CdPlaylistItemFactory(this.Drive, this.CdLookup, this.CdLookupHosts);
this.Factory = new CdPlaylistItemFactory(this.Drive, this.CdLookup, this.CdLookupHosts, this.Visible);
this.Factory.InitializeComponent(core);
base.InitializeComponent(core);
}
Expand All @@ -314,7 +314,8 @@ protected override async Task OnRun()
throw new InvalidOperationException("Drive is not ready.");
}
var playlist = this.PlaylistManager.SelectedPlaylist;
var playlistItems = await this.Factory.Create().ConfigureAwait(false);
var playlistItems = default(PlaylistItem[]);
await this.WithSubTask(this.Factory, async () => playlistItems = await this.Factory.Create(CancellationToken.None).ConfigureAwait(false)).ConfigureAwait(false);
using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_HIGH, async cancellationToken =>
{
//Always append for now.
Expand Down
9 changes: 9 additions & 0 deletions FoxTunes.Output.Bass.Cd/Properties/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions FoxTunes.Output.Bass.Cd/Properties/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@
<data name="BassCdStreamProviderBehaviourConfiguration.Section" xml:space="preserve">
<value>CD</value>
</data>
<data name="CdPlaylistItemFactory.Name" xml:space="preserve">
<value>Reading CD</value>
</data>
</root>
17 changes: 14 additions & 3 deletions FoxTunes.Output.Bass.Cd/Utilities/CdPlaylistItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace FoxTunes
{
public class CdPlaylistItemFactory : BaseComponent
public class CdPlaylistItemFactory : PopulatorBase
{
public CdPlaylistItemFactory(int drive, bool cdLookup, IEnumerable<string> cdLookupHosts)
public CdPlaylistItemFactory(int drive, bool cdLookup, IEnumerable<string> cdLookupHosts, bool reportProgress) : base(reportProgress)
{
this.Drive = drive;
this.CdLookup = cdLookup;
Expand All @@ -35,8 +37,13 @@ public override void InitializeComponent(ICore core)
base.InitializeComponent(core);
}

public async Task<PlaylistItem[]> Create()
public async Task<PlaylistItem[]> Create(CancellationToken cancellationToken)
{
if (this.ReportProgress)
{
this.Name = Strings.CdPlaylistItemFactory_Name;
}

var info = default(CDInfo);
BassUtils.OK(BassCd.GetInfo(this.Drive, out info));
var id = BassCd.GetID(this.Drive, CDID.CDPlayer);
Expand All @@ -50,6 +57,10 @@ public async Task<PlaylistItem[]> Create()
continue;
}
var fileName = CdUtils.CreateUrl(this.Drive, id, a);
if (this.ReportProgress)
{
this.Description = Path.GetFileName(fileName);
}
var metaData = default(MetaDataItem[]);
try
{
Expand Down
13 changes: 11 additions & 2 deletions FoxTunes.Output.Bass.Cue/BassCueStreamAdvisorBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ public AddCueToPlaylistTask(Playlist playlist, int sequence, string fileName) :
this.FileName = fileName;
}

public override bool Visible
{
get
{
return true;
}
}

public string FileName { get; private set; }

public CueSheetParser Parser { get; private set; }
Expand All @@ -258,15 +266,16 @@ public override void InitializeComponent(ICore core)
{
this.Parser = new CueSheetParser();
this.Parser.InitializeComponent(core);
this.Factory = new CueSheetPlaylistItemFactory();
this.Factory = new CueSheetPlaylistItemFactory(this.Visible);
this.Factory.InitializeComponent(core);
base.InitializeComponent(core);
}

protected override async Task OnRun()
{
var cueSheet = this.Parser.Parse(this.FileName);
var playlistItems = await this.Factory.Create(cueSheet).ConfigureAwait(false);
var playlistItems = default(PlaylistItem[]);
await this.WithSubTask(this.Factory, async () => playlistItems = await this.Factory.Create(cueSheet).ConfigureAwait(false)).ConfigureAwait(false);
using (var task = new SingletonReentrantTask(this, ComponentSlots.Database, SingletonReentrantTask.PRIORITY_HIGH, async cancellationToken =>
{
await this.AddPlaylistItems(playlistItems).ConfigureAwait(false);
Expand Down
9 changes: 9 additions & 0 deletions FoxTunes.Output.Bass.Cue/Properties/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions FoxTunes.Output.Bass.Cue/Properties/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,7 @@
<data name="BassSkipSilenceStreamComponent.None" xml:space="preserve">
<value>none</value>
</data>
<data name="CueSheetPlaylistItemFactory.Name" xml:space="preserve">
<value>Reading cue sheet</value>
</data>
</root>
18 changes: 17 additions & 1 deletion FoxTunes.Output.Bass.Cue/Utility/CueSheetPlaylistItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace FoxTunes
{
public class CueSheetPlaylistItemFactory : BaseComponent
public class CueSheetPlaylistItemFactory : PopulatorBase
{
private static IDictionary<string, string> FILE_NAMES = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
Expand All @@ -22,6 +24,11 @@ public class CueSheetPlaylistItemFactory : BaseComponent
{ "DATE", CommonMetaData.Year }
};

public CueSheetPlaylistItemFactory(bool reportProgress) : base(reportProgress)
{

}

public IOutput Output { get; private set; }

public IMetaDataSourceFactory MetaDataSourceFactory { get; private set; }
Expand All @@ -35,6 +42,11 @@ public override void InitializeComponent(ICore core)

public async Task<PlaylistItem[]> Create(CueSheet cueSheet)
{
if (this.ReportProgress)
{
this.Name = Strings.CueSheetPlaylistItemFactory_Name;
}

var playlistItems = new List<PlaylistItem>();
var directoryName = Path.GetDirectoryName(cueSheet.FileName);
var metaDataSource = this.MetaDataSourceFactory.Create();
Expand Down Expand Up @@ -72,6 +84,10 @@ await metaDataSource.GetMetaData(target).ConfigureAwait(false)
file.Tracks[b].Index.Time
);
}
if (this.ReportProgress)
{
this.Description = Path.GetFileName(fileName);
}
var playlistItem = new PlaylistItem()
{
DirectoryName = directoryName,
Expand Down
15 changes: 10 additions & 5 deletions FoxTunes.UI.Windows/ViewModel/PlaylistManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ protected virtual Task OnPlaylistUpdated(PlaylistUpdatedSignalState state)
{
if (state != null && state.Playlists != null && state.Playlists.Any())
{
#if NET40
return TaskEx.FromResult(false);
#else
return Task.CompletedTask;
#endif
return this.Refresh(state.Playlists);
}
else
{
Expand Down Expand Up @@ -133,6 +129,15 @@ protected virtual Task Refresh()
return Windows.Invoke(() => this.Playlists.ItemsSource = new ObservableCollection<Playlist>(playlists));
}

protected virtual Task Refresh(IEnumerable<Playlist> playlists)
{
#if NET40
return TaskEx.FromResult(false);
#else
return Task.CompletedTask;
#endif
}

public bool PlaylistManagerVisible
{
get
Expand Down

0 comments on commit c8aa43b

Please sign in to comment.