using System.Collections.ObjectModel; using CommunityToolkit.Maui.Views; using WheresMyMoney.Maui.Core; namespace WheresMyMoney.Maui; public partial class MobilePlannedPaymentsPage : ContentPage { public ObservableCollection FullPlannedPaymentViewModels { get; } = []; public MobilePlannedPaymentsPage() { 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), new Command(async () => { var popup = new PlannedPlannedPopup(); var result = await this.ShowPopupAsync(popup, CancellationToken.None); if (result is not PlannedPlannedPopup.PlannedPaymentPopupChoice choice) return; switch (choice) { case PlannedPlannedPopup.PlannedPaymentPopupChoice.Modify: await Navigation.PushModalAsync(new ModifyPlannedPaymentPage(plannedPayment.Id)); break; case PlannedPlannedPopup.PlannedPaymentPopupChoice.Remove: await RemovePlannedPayment(plannedPayment.Id); break; default: return; } }))); } } 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 Task RemovePlannedPayment(int id) { var answer = await DisplayAlert("Usunąć?", "Czy na pewno chcesz usunąć tą płatność?", "Tak", "Nie"); if (!answer) return; Repository.Instance.RemovePlannedPayment(id); } }