Skip to content

Commit

Permalink
Changed events to use list of tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoCoderMatrix86 committed Nov 12, 2024
1 parent e1a60e8 commit 6526742
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
19 changes: 8 additions & 11 deletions AudioCuesheetEditor.Tests/Model/AudioCuesheet/CuesheetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ public void AddTrackTest()
{
var testHelper = new TestHelper();
var cuesheet = new Cuesheet();
var trackAddedFired = false;
cuesheet.TrackAdded += delegate
{
trackAddedFired = true;
};
AutoResetEvent tracksAddedEvent = new(false);
cuesheet.TracksAdded += (object? sender, TracksAddedRemovedEventArgs args) => tracksAddedEvent.Set();
Assert.AreEqual(cuesheet.Tracks.Count, 0);
cuesheet.AddTrack(new Track(), testHelper.ApplicationOptions);
Assert.AreEqual(cuesheet.Tracks.Count, 1);
Assert.IsTrue(trackAddedFired);
Assert.IsTrue(tracksAddedEvent.WaitOne(1000));
}

[TestMethod()]
Expand Down Expand Up @@ -429,9 +426,9 @@ public void RemoveTrackTest()
{
var testHelper = new TestHelper();
testHelper.ApplicationOptions.LinkTracksWithPreviousOne = true;
AutoResetEvent trackRemovedEvent = new(false);
AutoResetEvent tracksRemovedEvent = new(false);
var cuesheet = new Cuesheet();
cuesheet.TrackRemoved += (sender, trackAddRemoveEventArgs) => trackRemovedEvent.Set();
cuesheet.TracksRemoved += (sender, trackAddRemoveEventArgs) => tracksRemovedEvent.Set();
var track1 = new Track() { Artist = "1", Title = "1" };
var track2 = new Track() { Artist = "2", Title = "2" };
var track3 = new Track() { Artist = "3", Title = "3" };
Expand All @@ -449,14 +446,14 @@ public void RemoveTrackTest()
track5.End = new TimeSpan(0, 25, 0);
Assert.AreEqual(5, cuesheet.Tracks.Count);
cuesheet.RemoveTrack(track2);
Assert.AreEqual(true, trackRemovedEvent.WaitOne(1000));
Assert.AreEqual(true, tracksRemovedEvent.WaitOne(1000));
Assert.AreEqual((uint)2, track3.Position);
Assert.AreEqual((uint)3, track4.Position);
Assert.AreEqual((uint)4, track5.Position);
testHelper = new TestHelper();
testHelper.ApplicationOptions.LinkTracksWithPreviousOne = true;
cuesheet = new Cuesheet();
cuesheet.TrackRemoved += (sender, trackAddRemoveEventArgs) => trackRemovedEvent.Set();
cuesheet.TracksRemoved += (sender, trackAddRemoveEventArgs) => tracksRemovedEvent.Set();
track1 = new Track
{
Artist = "Track 1",
Expand Down Expand Up @@ -493,7 +490,7 @@ public void RemoveTrackTest()
cuesheet.AddTrack(track5, testHelper.ApplicationOptions);
var list = new List<Track>() { track2, track4 };
cuesheet.RemoveTracks(list.AsReadOnly());
Assert.AreEqual(true, trackRemovedEvent.WaitOne(1000));
Assert.AreEqual(true, tracksRemovedEvent.WaitOne(1000));
Assert.AreEqual(3, cuesheet.Tracks.Count);
Assert.AreEqual(new TimeSpan(0, 5, 0), track3.Begin);
Assert.AreEqual(new TimeSpan(0, 15, 0), track5.Begin);
Expand Down
6 changes: 3 additions & 3 deletions AudioCuesheetEditor.Tests/Model/UI/TraceChangeManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public async Task Import_ValidTextfile_IsUndoable()
var textImportService = new TextImportService();
var importManager = new ImportManager(sessionStateContainer, localStorageOptionsProviderMock.Object, textImportService, traceChangeManager);
bool eventFired = false;
sessionStateContainer.Cuesheet.TrackAdded += delegate
sessionStateContainer.Cuesheet.TracksAdded += delegate
{
eventFired = true;
};
Expand Down Expand Up @@ -228,7 +228,7 @@ public async Task UndoImport_ValidTextfile_ResetsToEmptyCuesheet()
var textImportService = new TextImportService();
var importManager = new ImportManager(sessionStateContainer, localStorageOptionsProviderMock.Object, textImportService, traceChangeManager);
bool eventFired = false;
sessionStateContainer.Cuesheet.TrackAdded += delegate
sessionStateContainer.Cuesheet.TracksAdded += delegate
{
eventFired = true;
};
Expand Down Expand Up @@ -268,7 +268,7 @@ public async Task UndoAndRedoImport_ValidTextfile_ResetsTextfileValues()
var textImportService = new TextImportService();
var importManager = new ImportManager(sessionStateContainer, localStorageOptionsProviderMock.Object, textImportService, traceChangeManager);
bool eventFired = false;
sessionStateContainer.Cuesheet.TrackAdded += delegate
sessionStateContainer.Cuesheet.TracksAdded += delegate
{
eventFired = true;
};
Expand Down
14 changes: 7 additions & 7 deletions AudioCuesheetEditor/Model/AudioCuesheet/Cuesheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public enum MoveDirection
Down
}

public class TrackAddRemoveEventArgs(Track track) : EventArgs
public class TracksAddedRemovedEventArgs(IEnumerable<Track> tracks) : EventArgs
{
public Track Track { get; } = track;
public IEnumerable<Track> Tracks { get; } = tracks;
}

public class CuesheetSectionAddRemoveEventArgs(CuesheetSection section) : EventArgs
Expand All @@ -54,8 +54,8 @@ public class Cuesheet(TraceChangeManager? traceChangeManager = null) : Validatea

public event EventHandler? AudioFileChanged;
public event EventHandler<TraceablePropertiesChangedEventArgs>? TraceablePropertyChanged;
public event EventHandler<TrackAddRemoveEventArgs>? TrackAdded;
public event EventHandler<TrackAddRemoveEventArgs>? TrackRemoved;
public event EventHandler<TracksAddedRemovedEventArgs>? TracksAdded;
public event EventHandler<TracksAddedRemovedEventArgs>? TracksRemoved;
public event EventHandler<CuesheetSectionAddRemoveEventArgs>? SectionAdded;
public event EventHandler<CuesheetSectionAddRemoveEventArgs>? SectionRemoved;

Expand Down Expand Up @@ -259,7 +259,7 @@ public void AddTrack(Track track, ApplicationOptions? applicationOptions = null,
OnTraceablePropertyChanged(previousValue, nameof(Tracks));
if (IsImporting == false)
{
TrackAdded?.Invoke(this, new TrackAddRemoveEventArgs(track));
TracksAdded?.Invoke(this, new TracksAddedRemovedEventArgs([track]));
}
}

Expand Down Expand Up @@ -299,7 +299,7 @@ public void RemoveTrack(Track track)
}
RecalculateLastTrackEnd();
OnTraceablePropertyChanged(previousValue, nameof(Tracks));
TrackRemoved?.Invoke(this, new TrackAddRemoveEventArgs(track));
TracksRemoved?.Invoke(this, new TracksAddedRemovedEventArgs([track]));
}

/// <summary>
Expand Down Expand Up @@ -333,7 +333,7 @@ public void RemoveTracks(IReadOnlyCollection<Track> tracksToRemove)
tracks.ForEach(x => x.IsLinkedToPreviousTrackChanged += Track_IsLinkedToPreviousTrackChanged);
RecalculateLastTrackEnd();
OnTraceablePropertyChanged(previousValue, nameof(Tracks));
//TODO: Doesn't fire TrackRemoved (with a list of removed tracks)
TracksRemoved?.Invoke(this, new TracksAddedRemovedEventArgs(intersection));
}

public Boolean MoveTrackPossible(Track track, MoveDirection moveDirection)
Expand Down
22 changes: 14 additions & 8 deletions AudioCuesheetEditor/Pages/EditSections.razor
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ along with Foobar. If not, see
{
track.ValidateablePropertyChanged -= Track_ValidateablePropertyChanged;
}
Cuesheet.TrackAdded -= Cuesheet_TrackAdded;
Cuesheet.TrackRemoved -= Cuesheet_TrackRemoved;
Cuesheet.TracksAdded -= Cuesheet_TracksAdded;
Cuesheet.TracksRemoved -= Cuesheet_TracksRemoved;
}
_traceChangeManager.UndoDone -= TraceChangeManager_UndoDone;
_traceChangeManager.RedoDone -= TraceChangeManager_RedoDone;
Expand All @@ -195,8 +195,8 @@ along with Foobar. If not, see
_localizationService.LocalizationChanged += LocalizationService_LocalizationChanged;
if (Cuesheet != null)
{
Cuesheet.TrackAdded += Cuesheet_TrackAdded;
Cuesheet.TrackRemoved += Cuesheet_TrackRemoved;
Cuesheet.TracksAdded += Cuesheet_TracksAdded;
Cuesheet.TracksRemoved += Cuesheet_TracksRemoved;
}
_traceChangeManager.UndoDone += TraceChangeManager_UndoDone;
_traceChangeManager.RedoDone += TraceChangeManager_RedoDone;
Expand Down Expand Up @@ -230,15 +230,21 @@ along with Foobar. If not, see
return Task.CompletedTask;
}

void Cuesheet_TrackAdded(object? sender, TrackAddRemoveEventArgs args)
void Cuesheet_TracksAdded(object? sender, TracksAddedRemovedEventArgs args)
{
args.Track.ValidateablePropertyChanged += Track_ValidateablePropertyChanged;
foreach (var track in args.Tracks)
{
track.ValidateablePropertyChanged += Track_ValidateablePropertyChanged;
}
validations?.ValidateAll().GetAwaiter().GetResult();
}

void Cuesheet_TrackRemoved(object? sender, TrackAddRemoveEventArgs args)
void Cuesheet_TracksRemoved(object? sender, TracksAddedRemovedEventArgs args)
{
args.Track.ValidateablePropertyChanged -= Track_ValidateablePropertyChanged;
foreach (var track in args.Tracks)
{
track.ValidateablePropertyChanged -= Track_ValidateablePropertyChanged;
}
validations?.ValidateAll().GetAwaiter().GetResult();
}

Expand Down
17 changes: 10 additions & 7 deletions AudioCuesheetEditor/Shared/TrackList/TrackList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ along with Foobar. If not, see
_localizationService.LocalizationChanged -= LocalizationService_LocalizationChanged;
_sessionStateContainer.CuesheetChanged -= SessionStateContainer_CuesheetChanged;
_sessionStateContainer.ImportCuesheetChanged -= SessionStateContainer_ImportCuesheetChanged;
_sessionStateContainer.Cuesheet.TrackRemoved -= Cuesheet_TrackRemoved;
_sessionStateContainer.Cuesheet.TrackAdded -= Cuesheet_TrackAdded;
_sessionStateContainer.Cuesheet.TracksRemoved -= Cuesheet_TracksRemoved;
_sessionStateContainer.Cuesheet.TracksAdded -= Cuesheet_TracksAdded;
DetachTrackFromValidateablePropertyChanged();
DetachCuesheetFromSplitPointsAddedRemoved();
}
Expand All @@ -227,8 +227,8 @@ along with Foobar. If not, see
_sessionStateContainer.CuesheetChanged += SessionStateContainer_CuesheetChanged;
_sessionStateContainer.ImportCuesheetChanged += SessionStateContainer_ImportCuesheetChanged;

_sessionStateContainer.Cuesheet.TrackAdded += Cuesheet_TrackAdded;
_sessionStateContainer.Cuesheet.TrackRemoved += Cuesheet_TrackRemoved;
_sessionStateContainer.Cuesheet.TracksAdded += Cuesheet_TracksAdded;
_sessionStateContainer.Cuesheet.TracksRemoved += Cuesheet_TracksRemoved;

AttachTracksToValidateablePropertyChanged();
AttachCuesheetToSplitPointsAddedRemoved();
Expand Down Expand Up @@ -378,16 +378,19 @@ along with Foobar. If not, see
StateHasChanged();
}

void Cuesheet_TrackAdded(object? sender, TrackAddRemoveEventArgs args)
void Cuesheet_TracksAdded(object? sender, TracksAddedRemovedEventArgs args)
{
StateHasChanged();
AttachTracksToValidateablePropertyChanged();
revalidate = true;
}

void Cuesheet_TrackRemoved(object? sender, TrackAddRemoveEventArgs args)
void Cuesheet_TracksRemoved(object? sender, TracksAddedRemovedEventArgs args)
{
DetachTrackFromValidateablePropertyChanged(args.Track);
foreach(var track in args.Tracks)
{
DetachTrackFromValidateablePropertyChanged(track);
}
revalidate = true;
}

Expand Down

0 comments on commit 6526742

Please sign in to comment.