WheresMyMoney/WheresMyMoney.Maui/PlannedPaymentsPage.xaml.cs
2025-01-25 00:27:02 +01:00

82 lines
3.0 KiB
C#

using System.Collections.ObjectModel;
using WheresMyMoney.Maui.Core;
namespace WheresMyMoney.Maui;
public partial class PlannedPaymentsPage : ContentPage
{
public ObservableCollection<FullPlannedPaymentViewModel> FullPlannedPaymentViewModels { get; set; } = [];
public PlannedPaymentsPage()
{
InitializeComponent();
BindingContext = this;
UpdateUi();
Repository.Instance.DataChanged += OnDataChanged;
}
private void OnDataChanged(object? sender, EventArgs e)
{
UpdateUi();
}
private void UpdateUi()
{
FullPlannedPaymentViewModels.Clear();
foreach (var plannedPayment in Repository.Instance.GetAllPlannedPayments())
{
FullPlannedPaymentViewModels.Add(new FullPlannedPaymentViewModel(plannedPayment.Id,
plannedPayment.Amount.ToString("C"), plannedPayment.Name,
plannedPayment.DateStart.ToString("yyyy-MM-dd"),
plannedPayment.DateEnd?.ToString("yyyy-MM-dd") ?? string.Empty, plannedPayment.DateEnd is not null,
plannedPayment.IsSubscription,
plannedPayment.Reoccurences,
ReoccurenceTypeToString(plannedPayment.Reoccurences, plannedPayment.ReoccurenceType)));
}
}
private static string ReoccurenceTypeToString(int? plannedPaymentReoccurences,
ReoccurenceType? plannedPaymentReoccurenceType) =>
plannedPaymentReoccurenceType switch
{
ReoccurenceType.Days => plannedPaymentReoccurences switch
{
1 => "dzień",
_ => "dni"
},
ReoccurenceType.Weeks => (plannedPaymentReoccurences, plannedPaymentReoccurences % 10) switch
{
(1, _) => "tydzień",
(_, > 1 and < 5) => "tygodnie",
_ => "tygodni"
},
ReoccurenceType.Months => (plannedPaymentReoccurences, plannedPaymentReoccurences % 10) switch
{
(1, _) => "miesiąc",
(_, > 1 and < 5) => "miesiące",
_ => "miesięcy"
},
ReoccurenceType.Years => (plannedPaymentReoccurences, plannedPaymentReoccurences % 10) switch
{
(1, _) => "rok",
(_, > 1 and < 5) => "lata",
_ => "lat"
},
ReoccurenceType.EveryLastDayOfMonth => "ostatni dzień miesiąca",
_ => string.Empty
};
private async void InsertPlannedPayment_OnClicked(object? sender, EventArgs e)
{
await Navigation.PushModalAsync(new AddPlannedPaymentPage());
}
private async void RemovePlannedPayment_OnClicked(object? sender, EventArgs e)
{
if (sender is not Button button) return;
var answer = await DisplayAlert("Usunąć?", "Czy na pewno chcesz usunąć tą płatność?", "Tak", "Nie");
if (!answer) return;
var id = (int)button.CommandParameter;
Repository.Instance.RemovePlannedPayment(id);
}
}