Skip to content

Commit

Permalink
Fully hide date and identifier for account journal summary rows (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
lg2de authored Dec 9, 2023
1 parent 6029a2f commit f85adb1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/SimpleAccounting/Presentation/DateConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <copyright>
// Copyright (c) Lukas Grützmacher. All rights reserved.
// </copyright>

namespace lg2de.SimpleAccounting.Presentation;

using System;
using System.Globalization;
using System.Windows.Data;

/// <summary>
/// Implements a converter for <see cref="DateTime"/> into date only supporting <langword>null</langword>.
/// </summary>
public class DateConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
const int firstYear = 1900;
return value is not DateTime dateTime || dateTime.Year < firstYear
? null
: dateTime.ToString("d", culture);
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
9 changes: 7 additions & 2 deletions src/SimpleAccounting/Presentation/JournalItemBaseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace lg2de.SimpleAccounting.Presentation;

using System;
using System.Globalization;
using Caliburn.Micro;
using JetBrains.Annotations;

/// <summary>
/// Implements the base view model for items in all journals.
Expand All @@ -20,6 +22,9 @@ protected JournalItemBaseViewModel()

public ulong Identifier { get; set; }

public string IdentifierText =>
this.Identifier > 0 ? this.Identifier.ToString(CultureInfo.InvariantCulture) : string.Empty;

public DateTime Date { get; set; }

public string Text { get; set; } = string.Empty;
Expand All @@ -28,7 +33,7 @@ protected JournalItemBaseViewModel()

public bool IsEvenRow
{
get => this.isEvenRow;
[UsedImplicitly] get => this.isEvenRow;
set
{
if (value == this.isEvenRow)
Expand All @@ -41,5 +46,5 @@ public bool IsEvenRow
}
}

public int StorageIndex { get; protected set; } = -1;
public int StorageIndex { get; protected init; } = -1;
}
5 changes: 3 additions & 2 deletions src/SimpleAccounting/Presentation/ShellView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Setter Property="FontSize" Value="15" />
<Setter Property="Margin" Value="5" />
</Style>
<local:DateConverter x:Key="DateConverter" />
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="B" Modifiers="Control" Command="{Binding Menu.AddBookingsCommand}" />
Expand Down Expand Up @@ -202,10 +203,10 @@
<DataGrid.Columns>
<DataGridTextColumn
Header="{x:Static properties:Resources.Word_Date}" Width="60"
Binding="{Binding Date, StringFormat=\{0:d\}}"
Binding="{Binding Date, Converter={StaticResource DateConverter}}"
CellStyle="{StaticResource SummaryGridStyle}" />
<DataGridTextColumn Header="{x:Static properties:Resources.Word_BookingNumber_Short}" Width="75"
Binding="{Binding Identifier}"
Binding="{Binding IdentifierText}"
CellStyle="{StaticResource SummaryGridStyle}" />
<DataGridTextColumn Header="{x:Static properties:Resources.Word_BookingText}" Width="200"
Binding="{Binding Text}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <copyright>
// Copyright (c) Lukas Grützmacher. All rights reserved.
// </copyright>

namespace lg2de.SimpleAccounting.UnitTests.Presentation;

using System;
using System.Globalization;
using FluentAssertions;
using lg2de.SimpleAccounting.Presentation;
using Xunit;

public class DateConverterTests
{
[Fact]
public void Convert_NullInput_NullReturned()
{
var sut = new DateConverter();

var result = sut.Convert(null, typeof(string), null, CultureInfo.GetCultureInfo("de"));

result.Should().BeNull();
}

[Fact]
public void Convert_MinInput_NullReturned()
{
var sut = new DateConverter();

var result = sut.Convert(DateTime.MinValue, typeof(string), null, CultureInfo.GetCultureInfo("de"));

result.Should().BeNull();
}

[Theory]
[InlineData("de", "01.01.2023")]
[InlineData("en", "1/1/2023")]
public void Convert_ValidInput_FormattedReturned(string culture, string expectedResult)
{
var sut = new DateConverter();

var result = sut.Convert(
new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Local), typeof(string), null,
CultureInfo.GetCultureInfo(culture));

result.Should().Be(expectedResult);
}

[Fact]
public void ConvertBack_Throws()
{
var sut = new DateConverter();

sut.Invoking(x => x.ConvertBack(null, typeof(DateTime), null, CultureInfo.CurrentCulture)).Should()
.Throw<NotSupportedException>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// <copyright>
// Copyright (c) Lukas Grützmacher. All rights reserved.
// </copyright>

namespace lg2de.SimpleAccounting.UnitTests.Presentation;

using System.Diagnostics.CodeAnalysis;
using FluentAssertions;
using lg2de.SimpleAccounting.Presentation;
using Xunit;

public class JournalItemBaseViewModelTests
{
[SuppressMessage("Minor Code Smell", "S2094:Classes should not be empty")]
private class TestJournalItemBaseViewModel : JournalItemBaseViewModel;

[Fact]
public void IdentifierText_DefaultIdentifier_Empty()
{
var sut = new TestJournalItemBaseViewModel { Identifier = 0 };

sut.IdentifierText.Should().BeEmpty();
}

[Fact]
public void IdentifierText_SampleIdentifier_Formatted()
{
var sut = new TestJournalItemBaseViewModel { Identifier = 42 };

sut.IdentifierText.Should().Be("42");
}
}

0 comments on commit f85adb1

Please sign in to comment.