Skip to content

Commit

Permalink
made import to work with service
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoCoderMatrix86 committed Aug 30, 2024
1 parent 9507924 commit 9c02366
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 212 deletions.
15 changes: 2 additions & 13 deletions AudioCuesheetEditor/Extensions/SessionStateContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,18 @@ public void ResetImport()
{
Importfile = null;
ImportAudiofile = null;
}

public void StartImportCuesheet(ApplicationOptions applicationOptions)
{
if (ImportCuesheet != null)
{
Cuesheet.Import(ImportCuesheet, applicationOptions, _traceChangeManager);
ImportCuesheet = null;
}
ResetImport();
ImportCuesheet = null;
}

private void SetCuesheetReference(Cuesheet value)
{
cuesheet.CuesheetImported -= Cuesheet_CuesheetImported;
cuesheet = value;
cuesheet.CuesheetImported += Cuesheet_CuesheetImported;
_traceChangeManager.Reset();
_traceChangeManager.TraceChanges(Cuesheet);
CuesheetChanged?.Invoke(this, EventArgs.Empty);
}

private void Cuesheet_CuesheetImported(object? sender, EventArgs e)
public void FireCuesheetImported()
{
CuesheetChanged?.Invoke(this, EventArgs.Empty);
}
Expand Down
80 changes: 1 addition & 79 deletions AudioCuesheetEditor/Model/AudioCuesheet/Cuesheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Model.AudioCuesheet.Import;
using AudioCuesheetEditor.Model.Entity;
using AudioCuesheetEditor.Model.IO;
using AudioCuesheetEditor.Model.IO.Audio;
using AudioCuesheetEditor.Model.IO.Export;
using AudioCuesheetEditor.Model.Options;
Expand Down Expand Up @@ -60,7 +58,6 @@ public class Cuesheet(TraceChangeManager? traceChangeManager = null) : Validatea
public event EventHandler<TrackAddRemoveEventArgs>? TrackRemoved;
public event EventHandler<CuesheetSectionAddRemoveEventArgs>? SectionAdded;
public event EventHandler<CuesheetSectionAddRemoveEventArgs>? SectionRemoved;
public event EventHandler? CuesheetImported;

[JsonInclude]
public IReadOnlyCollection<Track> Tracks
Expand Down Expand Up @@ -174,7 +171,7 @@ public TimeSpan? RecordingTime
}

[JsonIgnore]
public Boolean IsImporting { get; private set; }
public Boolean IsImporting { get; set; }

[JsonInclude]
public IReadOnlyCollection<CuesheetSection> Sections
Expand Down Expand Up @@ -391,27 +388,6 @@ public void MoveTrack(Track track, MoveDirection moveDirection)
}
}

public void Import(ICuesheet cuesheet, ApplicationOptions applicationOptions, TraceChangeManager? traceChangeManager = null)
{
//Since we use a stack for several changes we need to lock execution for everything else
lock (syncLock)
{
IsImporting = true;
//We are doing a bulk edit, so inform the TraceChangeManager
if (traceChangeManager != null)
{
traceChangeManager.BulkEdit = true;
}
CopyValues(cuesheet, applicationOptions);
if (traceChangeManager != null)
{
traceChangeManager.BulkEdit = false;
}
IsImporting = false;
}
CuesheetImported?.Invoke(this, EventArgs.Empty);
}

public void StartRecording()
{
recordingStart = DateTime.UtcNow;
Expand Down Expand Up @@ -552,60 +528,6 @@ private void ReCalculateTrackProperties(Track trackToCalculate)
}
}

/// <summary>
/// Copy values from import cuesheet to this cuesheet
/// </summary>
/// <param name="cuesheet">Reference to import cuesheet</param>
/// <param name="applicationOptions">Reference to application options</param>
private void CopyValues(ICuesheet cuesheet, ApplicationOptions applicationOptions)
{
Artist = cuesheet.Artist;
Title = cuesheet.Title;
IEnumerable<ITrack>? tracks = null;
if (cuesheet is Cuesheet originCuesheet)
{
tracks = originCuesheet.tracks;
// Copy sections
foreach (var section in originCuesheet.Sections)
{
var newSplitPoint = AddSection();
newSplitPoint.CopyValues(section);
}
Audiofile = originCuesheet.Audiofile;
CDTextfile = originCuesheet.CDTextfile;
Cataloguenumber = originCuesheet.Cataloguenumber;
}
if (cuesheet is ImportCuesheet importCuesheet)
{
tracks = importCuesheet.Tracks;
if (String.IsNullOrEmpty(importCuesheet.Audiofile) == false)
{
Audiofile = new Audiofile(importCuesheet.Audiofile);
}
if (String.IsNullOrEmpty(importCuesheet.CDTextfile) == false)
{
CDTextfile = new CDTextfile(importCuesheet.CDTextfile);
}
Cataloguenumber = new Cataloguenumber()
{
Value = importCuesheet.Cataloguenumber
};
}
if (tracks != null)
{
foreach (var importTrack in tracks)
{
//We don't want to copy the cuesheet reference since we are doing a copy and want to assign the track to this object
var track = new Track(importTrack, false);
AddTrack(track, applicationOptions);
}
}
else
{
throw new NullReferenceException();
}
}

private void Track_RankPropertyValueChanged(object? sender, string e)
{
if (sender is Track trackRaisedEvent)
Expand Down
46 changes: 11 additions & 35 deletions AudioCuesheetEditor/Model/UI/TraceChangeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,15 @@
//You should have received a copy of the GNU General Public License
//along with Foobar. If not, see
//<http: //www.gnu.org/licenses />.
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Pages;
using Markdig.Extensions.Yaml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AudioCuesheetEditor.Model.UI
{
/// <summary>
/// Class for tracing changes on an object
/// </summary>
public class TracedChange
public class TracedChange(ITraceable traceableObject, TraceableChange traceableChange)
{
private readonly WeakReference<ITraceable> _tracedObject;
public TracedChange(ITraceable traceableObject, TraceableChange traceableChange)
{
_tracedObject = new WeakReference<ITraceable>(traceableObject, false);
TraceableChange = traceableChange;
}
private readonly WeakReference<ITraceable> _tracedObject = new(traceableObject, false);

public ITraceable? TraceableObject
{
get
Expand All @@ -47,26 +34,21 @@ public ITraceable? TraceableObject
}
}

public TraceableChange TraceableChange { get; }
public TraceableChange TraceableChange { get; } = traceableChange;
}

public class TracedChanges
public class TracedChanges(IEnumerable<TracedChange> changes)
{
public TracedChanges(IEnumerable<TracedChange> changes)
{
Changes = new(changes);
}

public List<TracedChange> Changes { get; }
public List<TracedChange> Changes { get; } = new(changes);
public Boolean HasTraceableObject { get { return Changes.Any(x => x.TraceableObject != null); } }
}

/// <summary>
/// Manager for Undo and Redo operations on objects.
/// </summary>
public class TraceChangeManager
public class TraceChangeManager(ILogger<TraceChangeManager> logger)
{
private readonly ILogger<TraceChangeManager> _logger;
private readonly ILogger<TraceChangeManager> _logger = logger;

private readonly Stack<TracedChanges> undoStack = new();
private readonly Stack<TracedChanges> redoStack = new();
Expand All @@ -77,7 +59,7 @@ public class TraceChangeManager
public event EventHandler? UndoDone;
public event EventHandler? RedoDone;

public Boolean CurrentlyHandlingRedoOrUndoChanges { get; private set; }
public Boolean CurrentlyHandlingRedoOrUndoChanges { get; private set; } = false;
/// <summary>
/// Is Undo() currently possible (are there any changes)?
/// </summary>
Expand All @@ -100,12 +82,6 @@ public Boolean CanRedo
}
}

public TraceChangeManager(ILogger<TraceChangeManager> logger)
{
CurrentlyHandlingRedoOrUndoChanges = false;
_logger = logger;
}

public void TraceChanges(ITraceable traceable)
{
traceable.TraceablePropertyChanged += Traceable_TraceablePropertyChanged;
Expand Down Expand Up @@ -229,7 +205,7 @@ public Boolean BulkEdit
_logger.LogDebug("Set BulkEdit called with {value}", value);
if (value)
{
bulkEditTracedChanges = new();
bulkEditTracedChanges = [];
}
else
{
Expand Down Expand Up @@ -293,7 +269,7 @@ private void Traceable_TraceablePropertyChanged(object? sender, TraceablePropert
if (BulkEdit == false)
{
//Single change
var changes = new TracedChanges(new List<TracedChange>() { new((ITraceable)sender, e.TraceableChange) });
var changes = new TracedChanges([new((ITraceable)sender, e.TraceableChange)]);
undoStack.Push(changes);
redoStack.Clear();
TracedObjectHistoryChanged?.Invoke(this, EventArgs.Empty);
Expand Down
3 changes: 1 addition & 2 deletions AudioCuesheetEditor/Pages/ViewModeImport.razor
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ along with Foobar. If not, see

private async Task ImportData()
{
var options = await _localStorageOptionsProvider.GetOptions<ApplicationOptions>();
_sessionStateContainer.StartImportCuesheet(options);
await _importManager.ImportCuesheetAsync();
_sessionStateContainer.CurrentViewMode = ViewMode.ViewModeFull;
StateHasChanged();
}
Expand Down
76 changes: 71 additions & 5 deletions AudioCuesheetEditor/Services/IO/ImportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
using AudioCuesheetEditor.Data.Options;
using AudioCuesheetEditor.Extensions;
using AudioCuesheetEditor.Model.AudioCuesheet;
using AudioCuesheetEditor.Model.AudioCuesheet.Import;
using AudioCuesheetEditor.Model.IO;
using AudioCuesheetEditor.Model.IO.Audio;
using AudioCuesheetEditor.Model.Options;
using AudioCuesheetEditor.Model.UI;
using AudioCuesheetEditor.Model.Utility;
using Blazorise;

Expand All @@ -31,11 +34,12 @@ public enum ImportFileType
Textfile,
Audiofile
}
public class ImportManager(SessionStateContainer sessionStateContainer, ILocalStorageOptionsProvider localStorageOptionsProvider, TextImportService textImportService)
public class ImportManager(SessionStateContainer sessionStateContainer, ILocalStorageOptionsProvider localStorageOptionsProvider, TextImportService textImportService, TraceChangeManager traceChangeManager)
{
private readonly SessionStateContainer _sessionStateContainer = sessionStateContainer;
private readonly ILocalStorageOptionsProvider _localStorageOptionsProvider = localStorageOptionsProvider;
private readonly TextImportService _textImportService = textImportService;
private readonly TraceChangeManager _traceChangeManager = traceChangeManager;

public async Task<Dictionary<IFileEntry, ImportFileType>> ImportFilesAsync(IEnumerable<IFileEntry> files)
{
Expand Down Expand Up @@ -88,21 +92,28 @@ public async Task ImportTextAsync(IEnumerable<String?> fileContent)
_sessionStateContainer.Importfile = _textImportService.Analyse(options, fileContent);
if (_sessionStateContainer.Importfile.AnalysedCuesheet != null)
{
var applicationOptions = await _localStorageOptionsProvider.GetOptions<ApplicationOptions>();
var importCuesheet = new Cuesheet();
importCuesheet.Import(_sessionStateContainer.Importfile.AnalysedCuesheet, applicationOptions);
await CopyCuesheetAsync(importCuesheet, _sessionStateContainer.Importfile.AnalysedCuesheet);
_sessionStateContainer.ImportCuesheet = importCuesheet;
}
}

public async Task ImportCuesheetAsync()
{
if (_sessionStateContainer.ImportCuesheet != null)
{
await CopyCuesheetAsync(_sessionStateContainer.Cuesheet, _sessionStateContainer.ImportCuesheet);
}
_sessionStateContainer.ResetImport();
}

private async Task ImportCuesheetAsync(IEnumerable<String?> fileContent)
{
_sessionStateContainer.Importfile = CuesheetImportService.Analyse(fileContent);
if (_sessionStateContainer.Importfile.AnalysedCuesheet != null)
{
var applicationOptions = await _localStorageOptionsProvider.GetOptions<ApplicationOptions>();
var importCuesheet = new Cuesheet();
importCuesheet.Import(_sessionStateContainer.Importfile.AnalysedCuesheet, applicationOptions);
await CopyCuesheetAsync(importCuesheet, _sessionStateContainer.Importfile.AnalysedCuesheet);
_sessionStateContainer.ImportCuesheet = importCuesheet;
}
}
Expand All @@ -115,5 +126,60 @@ private static async Task<MemoryStream> ReadFileContentAsync(IFileEntry file)
stream.Close();
return fileContent;
}

private async Task CopyCuesheetAsync(Cuesheet target, ICuesheet cuesheetToCopy)
{
_traceChangeManager.BulkEdit = true;
target.IsImporting = true;
target.Artist = cuesheetToCopy.Artist;
target.Title = cuesheetToCopy.Title;
IEnumerable<ITrack>? tracks = null;
if (cuesheetToCopy is Cuesheet originCuesheet)
{
tracks = originCuesheet.Tracks;
// Copy sections
foreach (var section in originCuesheet.Sections)
{
var newSplitPoint = target.AddSection();
newSplitPoint.CopyValues(section);
}
target.Audiofile = originCuesheet.Audiofile;
target.CDTextfile = originCuesheet.CDTextfile;
target.Cataloguenumber = originCuesheet.Cataloguenumber;
}
if (cuesheetToCopy is ImportCuesheet importCuesheet)
{
tracks = importCuesheet.Tracks;
if (String.IsNullOrEmpty(importCuesheet.Audiofile) == false)
{
target.Audiofile = new Audiofile(importCuesheet.Audiofile);
}
if (String.IsNullOrEmpty(importCuesheet.CDTextfile) == false)
{
target.CDTextfile = new CDTextfile(importCuesheet.CDTextfile);
}
target.Cataloguenumber = new Cataloguenumber()
{
Value = importCuesheet.Cataloguenumber
};
}
if (tracks != null)
{
var applicationOptions = await _localStorageOptionsProvider.GetOptions<ApplicationOptions>();
foreach (var importTrack in tracks)
{
//We don't want to copy the cuesheet reference since we are doing a copy and want to assign the track to this object
var track = new Track(importTrack, false);
target.AddTrack(track, applicationOptions);
}
}
else
{
throw new NullReferenceException();
}
target.IsImporting = false;
_traceChangeManager.BulkEdit = false;
_sessionStateContainer.FireCuesheetImported();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SessionStateContainerFireCuesheetChangedTest()
cuesheetChangedFired = true;
};
Assert.IsFalse(cuesheetChangedFired);
container.Cuesheet.Import(new Cuesheet(), helper.ApplicationOptions);
container.FireCuesheetImported();
Assert.IsTrue(cuesheetChangedFired);
}
}
Expand Down
Loading

0 comments on commit 9c02366

Please sign in to comment.