using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; using Nonemm.Contests; namespace Nonemm.App.Dialogs; /// N1MM's goals editor, as the text the goals are kept in. It returns the /// goals in `Goals.ToText` form, or null when cancelled. public sealed partial class GoalsDialog : Window { private readonly string exportFolder; public GoalsDialog(string goals, string exportFolder) { this.exportFolder = exportFolder; InitializeComponent(); GoalsBox.Text = goals; } private void OnOk(object? sender, RoutedEventArgs e) { if (Checked() is { } text) { Close(text); } } private void OnCancel(object? sender, RoutedEventArgs e) => Close(null); /// Writes the goals to a file N1MM's Import Goals reads, keeping the /// dialog open. private async void OnExport(object? sender, RoutedEventArgs e) { if (Checked() is not { } text) { return; } IStorageFile? file = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { Title = "Export goals", SuggestedFileName = "goals.txt", SuggestedStartLocation = await StorageProvider.TryGetFolderFromPathAsync(exportFolder), }); if (file?.TryGetLocalPath() is not { } path) { return; } try { await File.WriteAllTextAsync(path, text); ProblemText.Text = $"{Path.GetFileName(path)} written."; } catch (IOException problem) { ProblemText.Text = $"could not write {path}: {problem.Message}"; } } private string? Checked() { try { ProblemText.Text = ""; return Goals.Parse(GoalsBox.Text ?? "").ToText(); } catch (FormatException problem) { ProblemText.Text = problem.Message; return null; } } }