Skip to content

Commit

Permalink
#474 - Show diff in summary.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed May 10, 2024
1 parent b76d4bd commit 0b181d2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/Money.Blazor.Host/Pages/BalancesMonth.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ else
{
var diff = model.IncomeSummary - model.ExpenseSummary;
<small class="@(diff.Value > 0 ? "text-success" : "text-danger")">
@CurrencyFormatter.Format(diff)
@CurrencyFormatter.Format(diff, applyPlusForPositiveNumbers: true)
</small>
}
</div>
Expand All @@ -66,7 +66,7 @@ else
{
var diff = model.IncomeSummary - model.ExpenseSummary;
<small class="@(diff.Value > 0 ? "text-success" : "text-danger")">
@CurrencyFormatter.Format(diff)
@CurrencyFormatter.Format(diff, applyPlusForPositiveNumbers: true)
</small>
}
</div>
Expand Down
45 changes: 30 additions & 15 deletions src/Money.Blazor.Host/Pages/Summary.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@

@if (SelectedPeriod != null)
{
<div class="mb-3">
<SortButton TType="SummarySortType" @bind-Current="@SortDescriptor" Changed="@OnSorted" />

<PeriodSelector Selected="SelectedPeriod" Previous="PeriodGuesses" ExactGetter="GetPeriodsAsync" LinkFactory="UrlSummary" />

<span class="pl-2">
<Loading Context="@CategoriesLoading" />
</span>
<div class="d-flex justify-content-between mb-3">
<div>
<PeriodSelector Selected="SelectedPeriod" Previous="PeriodGuesses" ExactGetter="GetPeriodsAsync" LinkFactory="UrlSummary" />
<span class="pl-2">
<Loading Context="@CategoriesLoading" />
</span>
</div>
<div class="d-flex">
<EnumSelector Text="Display" TType="SummaryDisplayType" @bind-Current="@SelectedDisplayType" Changed="StateHasChanged" />
<span class="pl-2">
<SortButton TType="SummarySortType" @bind-Current="@SortDescriptor" Changed="@OnSorted" />
</span>
</div>
</div>
}

Expand All @@ -44,16 +49,26 @@

<div class="value">
<div class="amount">
@if (IncomeTotal != null)
@if (SelectedDisplayType == SummaryDisplayType.Total)
{
<span class="text-success mr-2" @onclick="@(() => OpenOverviewIncomes(SelectedPeriod))">
@FormatPrice(IncomeTotal)
@if (IncomeTotal != null)
{
<span class="text-success mr-2" @onclick="@(() => OpenOverviewIncomes(SelectedPeriod))">
@FormatPrice(IncomeTotal)
</span>
}

<span class="text-danger">
@FormatPrice(ExpenseTotal, CurrencyFormatter.FormatZero.Zero)
</span>
}
else
{
var diff = (IncomeTotal ?? new Price(0, ExpenseTotal.Currency)) - ExpenseTotal;
<span class="@(diff.Value > 0 ? "text-success" : "text-danger")">
@FormatPrice(diff, applyPlusForPositiveNumbers: true)
</span>
}

<span class="text-danger">
@FormatPrice(ExpenseTotal, CurrencyFormatter.FormatZero.Zero)
</span>
</div>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/Money.Blazor.Host/Pages/Summary.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public partial class Summary<T> :
protected LoadingContext CategoriesLoading { get; } = new LoadingContext();
protected Price IncomeTotal { get; private set; }
protected Price ExpenseTotal { get; private set; }
protected SummaryDisplayType SelectedDisplayType { get; set; }
protected List<CategoryWithAmountModel> Categories { get; private set; }

protected SortDescriptor<SummarySortType> SortDescriptor { get; set; }
Expand Down Expand Up @@ -191,8 +192,8 @@ protected string GetPercentualValue(CategoryWithAmountModel category)
return percentage.ToString("0.##", CultureInfo.InvariantCulture);
}

protected string FormatPrice(Price price, CurrencyFormatter.FormatZero formatZero = CurrencyFormatter.FormatZero.Empty)
=> formatter.Format(price, formatZero);
protected string FormatPrice(Price price, CurrencyFormatter.FormatZero formatZero = CurrencyFormatter.FormatZero.Empty, bool applyPlusForPositiveNumbers = false)
=> formatter.Format(price, formatZero, applyPlusForPositiveNumbers: applyPlusForPositiveNumbers);

public void Dispose()
=> UnBindEvents();
Expand Down
17 changes: 17 additions & 0 deletions src/Money.Blazor.Host/Pages/SummaryDisplayType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Pages;

public enum SummaryDisplayType
{
[Description("Total")]
Total,
[Description("Diff")]
Diff
}
13 changes: 9 additions & 4 deletions src/Money.Blazor.Host/Services/CurrencyFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void EnsureCurrencies()
}
}

public string Format(Price price, FormatZero zero = FormatZero.Zero, bool applyUserDigits = true)
public string Format(Price price, FormatZero zero = FormatZero.Zero, bool applyUserDigits = true, bool applyPlusForPositiveNumbers = false)
{
if (price == null || price.Value == 0)
{
Expand All @@ -60,10 +60,10 @@ public string Format(Price price, FormatZero zero = FormatZero.Zero, bool applyU
};
}

return FormatInternal(price, applyUserDigits);
return FormatInternal(price, applyUserDigits, applyPlusForPositiveNumbers);
}

private string FormatInternal(Price price, bool applyUserDigits = true)
private string FormatInternal(Price price, bool applyUserDigits = true, bool applyPlusForPositiveNumbers = false)
{
Ensure.NotNull(price, "price");

Expand All @@ -83,7 +83,12 @@ private string FormatInternal(Price price, bool applyUserDigits = true)
modifiedCulture.NumberFormat.CurrencySymbol = currency.Symbol;
}

return price.Value.ToString("C", modifiedCulture ?? culture);
string value = price.Value.ToString("C", modifiedCulture ?? culture);
if (applyPlusForPositiveNumbers && price.Value > 0)
value = $"+{value}";

Console.WriteLine($"DEBUG {value} {applyPlusForPositiveNumbers}");
return value;
}

public enum FormatZero
Expand Down
10 changes: 0 additions & 10 deletions src/Money.Blazor.Host/wwwroot/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,6 @@ h4 {
cursor: pointer;
}

.summary {
position: relative;
}
.summary .sort {
position: absolute;
right: 0;
}
.summary .sort .btn {
border-color: transparent;
}
.summary .months {
padding-right: 70px;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Money.Blazor.Host/wwwroot/css/site.min.css

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions src/Money.Blazor.Host/wwwroot/css/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,6 @@ h4 {


.summary {
position: relative;

.sort {
position: absolute;
right: 0;

.btn {
border-color: transparent;
}
}

.months {
padding-right: 70px;
}
Expand Down

0 comments on commit 0b181d2

Please sign in to comment.