using System.Globalization; using WheresMyMoney.Maui.Core; namespace WheresMyMoney.Maui; public partial class ModifyPlannedPaymentPage : ContentPage { private readonly PlannedPayment? _plannedPayment; public ModifyPlannedPaymentPage(int id) { InitializeComponent(); RepeatPicker.SelectedIndex = 2; _plannedPayment = Repository.Instance.GetPlannedPayment(id); } protected override async void OnAppearing() { base.OnAppearing(); if (_plannedPayment is null) { await Navigation.PopModalAsync(); } else { NameEntry.Text = _plannedPayment.Name; AmountEntry.Text = _plannedPayment.Amount.ToString("C"); SubscriptionSwitch.IsToggled = _plannedPayment.IsSubscription; RepeatPicker.SelectedIndex = (int?)_plannedPayment.ReoccurenceType ?? 2; EveryEntry.Text = _plannedPayment.Reoccurences?.ToString() ?? "1"; StartDateEntry.Date = _plannedPayment.DateStart.Date; EndDateEntry.Date = _plannedPayment.DateEnd?.Date ?? DateTime.Today; IndefiniteSwitch.IsToggled = _plannedPayment.DateEnd is null; } } private async void Modify_OnClicked(object? sender, EventArgs e) { if (!decimal.TryParse(AmountEntry.Text, NumberStyles.Currency, CultureInfo.CurrentCulture, out var amount)) return; var isSubscription = SubscriptionSwitch.IsToggled; var every = -1; var reoccurenceType = (ReoccurenceType)RepeatPicker.SelectedIndex; if (isSubscription) { isSubscription = int.TryParse(EveryEntry.Text, out every) && every > 0; } Repository.Instance.UpdatePlannedPayment(_plannedPayment! with { Amount = amount, Name = NameEntry.Text, DateStart = StartDateEntry.Date, DateEnd = !isSubscription || IndefiniteSwitch.IsToggled ? null : EndDateEntry.Date, IsSubscription = isSubscription, Reoccurences = isSubscription ? every : null, ReoccurenceType = isSubscription ? reoccurenceType : null }); await Navigation.PopModalAsync(); } private void NameEntry_OnCompleted(object? sender, EventArgs e) { if (SubscriptionSwitch.IsToggled) { EveryEntry.Focus(); } else { EveryEntry_OnCompleted(sender, e); } } private void EveryEntry_OnCompleted(object? sender, EventArgs e) { this.AmountEntry.Focus(); } }