Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom labels not saving settings properly #213

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/AudioBand.Test/ViewModels/CustomLabelViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,34 @@ void CustomLabel_ColorChangedThenCanceled_TextSegmentsHaveCorrectColor()

Assert.All(vm.TextSegments, segment => Assert.Equal(Colors.Blue, segment.Color));
}

[Fact]
void CustomLabel_CancelEdit_ModelHasNoChanges()
{
var formatString = "test";
var model = new CustomLabel {FormatString = formatString};
var vm = new CustomLabelViewModel(model, _dialogMock.Object, _sessionMock.Object, _messageBusMock.Object);

vm.FormatString = "new";
vm.CancelEdit();

Assert.Equal(formatString, vm.FormatString);
Assert.Equal(formatString, model.FormatString);
}

[Fact]
void CustomLabel_SaveEdit_ModelHasNewChanges()
{
var formatString = "test";
var newFormatstring = "format";
var model = new CustomLabel { FormatString = formatString };
var vm = new CustomLabelViewModel(model, _dialogMock.Object, _sessionMock.Object, _messageBusMock.Object);

vm.FormatString = newFormatstring;
vm.EndEdit();

Assert.Equal(newFormatstring, vm.FormatString);
Assert.Equal(newFormatstring, model.FormatString);
}
}
}
24 changes: 24 additions & 0 deletions src/AudioBand.Test/ViewModels/CustomLabelsViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,29 @@ public void ProfileChanged_NewLabelsHaveCorrectAudioSessionData()
_appSettingsMock.Raise(m => m.ProfileChanged += null, null, EventArgs.Empty);
Assert.True(vm.CustomLabels[0].IsPlaying);
}

[Fact]
public void RemoveLabel_PublishEdit()
{
_appSettingsMock.SetupGet(x => x.CustomLabels).Returns(new List<CustomLabel> { new CustomLabel() });
_dialogMock.Setup(o => o.ShowConfirmationDialog(It.IsAny<ConfirmationDialogType>(), It.IsAny<object>())).Returns(true);
_viewModel = new CustomLabelsViewModel(_appSettingsMock.Object, _dialogMock.Object, _sessionMock.Object, _messageBus.Object);
var label = _viewModel.CustomLabels[0];
_viewModel.RemoveLabelCommand.Execute(label);

Assert.True(_viewModel.IsEditing);
_messageBus.Verify(m => m.Publish(It.IsAny<EditStartMessage>(), It.IsAny<string>()));
}

[Fact]
public void AddLabel_PublishEdit()
{
_appSettingsMock.SetupGet(x => x.CustomLabels).Returns(new List<CustomLabel> { new CustomLabel() });
_viewModel = new CustomLabelsViewModel(_appSettingsMock.Object, _dialogMock.Object, _sessionMock.Object, _messageBus.Object);
_viewModel.AddLabelCommand.Execute(null);

Assert.True(_viewModel.IsEditing);
_messageBus.Verify(m => m.Publish(It.IsAny<EditStartMessage>(), It.IsAny<string>()));
}
}
}
9 changes: 9 additions & 0 deletions src/AudioBand/ViewModels/CustomLabelViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace AudioBand.ViewModels
/// </summary>
public class CustomLabelViewModel : LayoutViewModelBase<CustomLabel>
{
private readonly CustomLabel _source;
private readonly IAudioSession _audioSession;
private bool _isPlaying;
private IEnumerable<TextSegment> _textSegments;
Expand All @@ -31,6 +32,7 @@ public class CustomLabelViewModel : LayoutViewModelBase<CustomLabel>
public CustomLabelViewModel(CustomLabel source, IDialogService dialogService, IAudioSession audioSession, IMessageBus messageBus)
: base(messageBus, source)
{
_source = source;
_audioSession = audioSession;
_audioSession.PropertyChanged += AudioSessionOnPropertyChanged;

Expand Down Expand Up @@ -243,6 +245,13 @@ protected override void OnReset()
ReParseSegments();
}

/// <inheritdoc />
protected override void OnEndEdit()
{
base.OnEndEdit();
MapSelf(Model, _source);
}

private void AudioSessionOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(IAudioSession.IsPlaying))
Expand Down
10 changes: 8 additions & 2 deletions src/AudioBand/ViewModels/CustomLabelsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ public CustomLabelsViewModel(IAppSettings appsettings, IDialogService dialogServ
/// <inheritdoc/>
protected override void OnBeginEdit()
{
base.OnBeginEdit();

_added.Clear();
_removed.Clear();
}

/// <inheritdoc/>
protected override void OnCancelEdit()
{
base.OnCancelEdit();

foreach (var label in _added)
{
CustomLabels.Remove(label);
Expand All @@ -85,6 +89,8 @@ protected override void OnCancelEdit()
/// <inheritdoc/>
protected override void OnEndEdit()
{
base.OnEndEdit();

_added.Clear();
_removed.Clear();

Expand All @@ -108,13 +114,13 @@ private void AddLabelCommandOnExecute()

private void RemoveLabelCommandOnExecute(CustomLabelViewModel labelViewModel)
{
BeginEdit();

if (!_dialogService.ShowConfirmationDialog(ConfirmationDialogType.DeleteLabel, labelViewModel.Name))
{
return;
}

BeginEdit();

CustomLabels.Remove(labelViewModel);

// Only add to removed if not a new label
Expand Down