Skip to content

Commit

Permalink
Merge pull request #310 from NeoCoderMatrix86/299-undo-doesnt-work-if…
Browse files Browse the repository at this point in the history
…-using-audiofile

Initial correction for setting track end after audio file analysis
  • Loading branch information
NeoCoderMatrix86 authored May 12, 2023
2 parents eb36dff + bf4cc61 commit 385c861
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Extensions/SessionStateContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SessionStateContainer
public SessionStateContainer(TraceChangeManager traceChangeManager)
{
_traceChangeManager = traceChangeManager;
cuesheet = new Cuesheet();
cuesheet = new Cuesheet(_traceChangeManager);
SetCuesheetReference(cuesheet);
}
public Cuesheet Cuesheet
Expand Down
9 changes: 7 additions & 2 deletions AudioCuesheetEditor/Model/AudioCuesheet/Cuesheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ public class Cuesheet : Validateable<Cuesheet>, ICuesheetEntity, ITraceable
public event EventHandler<SplitPointAddRemoveEventArgs>? SplitPointRemoved;
public event EventHandler? CuesheetImported;

public Cuesheet()
public Cuesheet(TraceChangeManager? traceChangeManager = null)
{
tracks = new();
catalogueNumber = new();
splitPoints = new();
TraceChangeManager = traceChangeManager;
}

[JsonInclude]
Expand Down Expand Up @@ -209,6 +210,9 @@ private set
}
}

[JsonIgnore]
public TraceChangeManager? TraceChangeManager { get; }

public SplitPoint AddSplitPoint()
{
var previousValue = new List<SplitPoint>(splitPoints);
Expand Down Expand Up @@ -527,6 +531,7 @@ private void ReCalculateTrackProperties(Track trackToCalculate)
if ((Audiofile != null) && (Audiofile.Duration.HasValue) && (trackToCalculate.End.HasValue == false))
{
trackToCalculate.End = Audiofile.Duration;
TraceChangeManager?.MergeLastEditWithEdit(x => x.Changes.All(y => y.TraceableObject == this && y.TraceableChange.PropertyName == nameof(Audiofile)));
}
if (Tracks.Count > 1)
{
Expand Down Expand Up @@ -568,7 +573,7 @@ private void ReCalculateTrackProperties(Track trackToCalculate)
{
trackToCalculate.Begin = TimeSpan.Zero;
}
}
}
}

/// <summary>
Expand Down
51 changes: 39 additions & 12 deletions AudioCuesheetEditor/Model/UI/TraceChangeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//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;
Expand Down Expand Up @@ -50,14 +52,12 @@ public ITraceable? TraceableObject

public class TracedChanges
{
private readonly Stack<TracedChange> _tracedChanges;

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

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

Expand Down Expand Up @@ -135,9 +135,9 @@ public void Undo()
if ((changes != null) && changes.HasTraceableObject)
{
var redoChanges = new List<TracedChange>();
do
for (int i = changes.Changes.Count - 1; i >= 0; i--)
{
var change = changes.Changes.Pop();
var change = changes.Changes.ElementAt(i);
var tracedObject = change?.TraceableObject;
var traceAbleChange = change?.TraceableChange;
_logger.LogDebug("tracedObject = {tracedObject}, traceAbleChange = {traceAbleChange}", tracedObject, traceAbleChange);
Expand All @@ -156,8 +156,11 @@ public void Undo()
throw new NullReferenceException(String.Format("Property {0} could not be found!", traceAbleChange.PropertyName));
}
}
if (change != null)
{
changes.Changes.Remove(change);
}
}
while (changes.Changes.Count > 0);
//Push the old value to redo stack
redoStack.Push(new TracedChanges(redoChanges));
}
Expand All @@ -184,9 +187,9 @@ public void Redo()
if ((changes != null) && changes.HasTraceableObject)
{
var undoChanges = new List<TracedChange>();
do
for (int i = changes.Changes.Count - 1;i >= 0; i--)
{
var change = changes.Changes.Pop();
var change = changes.Changes.ElementAt(i);
var tracedObject = change?.TraceableObject;
var traceAbleChange = change?.TraceableChange;
_logger.LogDebug("tracedObject = {tracedObject}, traceAbleChange = {traceAbleChange}", tracedObject, traceAbleChange);
Expand All @@ -205,8 +208,11 @@ public void Redo()
throw new NullReferenceException(String.Format("Property {0} could not be found!", traceAbleChange.PropertyName));
}
}
if (change != null)
{
changes.Changes.Remove(change);
}
}
while (changes.Changes.Count > 0);
//Push the old value to redo stack
undoStack.Push(new TracedChanges(undoChanges));
}
Expand Down Expand Up @@ -237,14 +243,35 @@ public Boolean BulkEdit
}
}

public TracedChanges? LastEdit
{
get
{
return undoStack.Peek();
}
}

public void MergeLastEditWithEdit(Func<TracedChanges, bool> targetEdit)
{
var edit = undoStack.FirstOrDefault(targetEdit);
if (edit != null)
{
if (undoStack.Count > 0)
{
var lastEdits = undoStack.Pop();
edit.Changes.AddRange(lastEdits.Changes);
UndoDone?.Invoke(this, EventArgs.Empty);
}
}
}

private void ResetStack(Stack<TracedChanges> stack)
{
while (stack.Count > 0)
{
var tracedChange = stack.Pop();
while (tracedChange.Changes.Count > 0)
foreach (var change in tracedChange.Changes)
{
var change = tracedChange.Changes.Pop();
if (change.TraceableObject != null)
{
change.TraceableObject.TraceablePropertyChanged -= Traceable_TraceablePropertyChanged;
Expand Down
15 changes: 13 additions & 2 deletions AudioCuesheetEditor/Shared/AudioPlayer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,20 @@ along with Foobar. If not, see
{
if (_sessionStateContainer.Cuesheet.Audiofile != null)
{
_sessionStateContainer.Cuesheet.Audiofile.ContentStreamLoaded += AudioFile_ContentStreamLoaded;
if (_sessionStateContainer.Cuesheet.Audiofile.IsContentStreamLoaded)
{
AnalyseAudioFile();
}
else
{
TotalTime = null;
_sessionStateContainer.Cuesheet.Audiofile.ContentStreamLoaded += AudioFile_ContentStreamLoaded;
}
}
else
{
TotalTime = null;
}
TotalTime = null;
}

void AudioFile_ContentStreamLoaded(object? sender, EventArgs args)
Expand Down
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ along with Foobar. If not, see
void newCuesheetDelegate(object? sender, EventArgs args)
{
_logger.LogInformation("newCuesheetDelegate");
_sessionStateContainer.Cuesheet = new Cuesheet();
_sessionStateContainer.Cuesheet = new Cuesheet(_traceChangeManager);
modalDialog.Confirmed -= newCuesheetDelegate;
StateHasChanged();
};
Expand Down

0 comments on commit 385c861

Please sign in to comment.