diff --git a/ucalc/BillingWindow.xaml b/ucalc/BillingWindow.xaml index 50144c4..3cc2d58 100644 --- a/ucalc/BillingWindow.xaml +++ b/ucalc/BillingWindow.xaml @@ -11,9 +11,24 @@ Height="600" MinWidth="600" MinHeight="400" + Closing="OnClosing" Closed="OnClosed" Icon="logo.ico"> + + + + + + + + + BorderBrush="{x:Static local:Constants.MainColor}"> _execute; + + public DelegateCommand(Action execute) + { + _execute = execute; + } + + public bool CanExecute(object parameter) + { + CanExecuteChanged?.Invoke(this, EventArgs.Empty); + return true; + } + + public void Execute(object parameter) + { + _execute(parameter); + } + + public event EventHandler CanExecuteChanged; + } + public partial class BillingWindow { + public string FilePath { get; set; } public Model Model { get; } - public BillingWindow(Billing billing) + public ICommand SaveCommand { get; } + public ICommand SaveAsCommand { get; } + public ICommand PrintCommand { get; } + + public BillingWindow(string filePath, Billing billing) { + FilePath = filePath; Model = new Model(billing); + SaveCommand = new DelegateCommand(parameter => { Save(); }); + SaveAsCommand = new DelegateCommand(parameter => { Save(true); }); + PrintCommand = new DelegateCommand(parameter => { Print(); }); InitializeComponent(); @@ -22,6 +58,36 @@ namespace UCalc $"MietRechner - Abrechnung von {billing.StartDate.ToString(Constants.DateFormat)} - {billing.EndDate.Date.ToString(Constants.DateFormat)}"; } + private void OnClosing(object sender, CancelEventArgs e) + { + if (!Model.Root.Modified) + { + return; + } + + while (true) + { + switch (MessageBox.Show("Möchten Sie die letzten Änderungen speichern?", "Speichern?", + MessageBoxButton.YesNoCancel, MessageBoxImage.Question)) + { + case MessageBoxResult.Yes: + if (Save()) + { + return; + } + + break; + case MessageBoxResult.No: + return; + case MessageBoxResult.Cancel: + e.Cancel = true; + return; + default: + throw new ArgumentOutOfRangeException(); + } + } + } + private void OnClosed(object sender, EventArgs e) { Application.Current.MainWindow?.Show(); @@ -30,10 +96,9 @@ namespace UCalc private void OnSideBarFrameLoadCompleted(object sender, NavigationEventArgs e) { var sideBar = (SideBar) ((Frame) sender).Content; - sideBar.TabControl = TabControl; - sideBar.LandlordButton.Selected = true; - + sideBar.ParentWindow = this; sideBar.Model = Model; + sideBar.LandlordButton.Selected = true; } private void OnLandlordFrameLoadCompleted(object sender, NavigationEventArgs e) @@ -66,5 +131,66 @@ namespace UCalc page.House = Model.Root.House; page.ParentWindow = this; } + + public bool Save(bool rename = false) + { + if (Model.Root.Errors.Count > 0) + { + MessageBox.Show( + "Fehlerhafte Einträge (wie z.B. ungültige Beträge) gehen beim Speichern und anschließenden Laden verloren.", + "Warnung!", MessageBoxButton.OK, MessageBoxImage.Exclamation); + } + + while (true) + { + if (string.IsNullOrEmpty(FilePath) || rename) + { + var dialog = new SaveFileDialog + {Filter = "MietRechner Datei (*.mr) | *.mr", FileName = FilePath ?? ""}; + if (dialog.ShowDialog() != true) + { + return false; + } + + FilePath = dialog.FileName; + if (!FilePath.EndsWith(".mr")) + { + FilePath += ".mr"; + } + } + + var billing = Model.Dump(); + try + { + new BillingLoader().Store(FilePath, billing); + Model.ResetModified(); + return true; + } + catch (IOException) + { + if (MessageBox.Show( + $"Die Daten konnten nicht in \"{FilePath}\" gespeichert werden.\nMöchten Sie die Daten an einem anderen Ort speichern?", + "Fehler!", MessageBoxButton.YesNo, MessageBoxImage.Error) == MessageBoxResult.No) + { + return false; + } + + rename = true; + } + } + } + + public bool Print() + { + if (Model.Root.Errors.Count > 0) + { + MessageBox.Show( + "Bitte beheben Sie zuerst die angezeigten Fehler, bevor Sie das Dokument drucken können.", + "Fehler!", MessageBoxButton.OK, MessageBoxImage.Error); + return false; + } + + throw new System.NotImplementedException(); + } } } \ No newline at end of file diff --git a/ucalc/MainWindow.xaml.cs b/ucalc/MainWindow.xaml.cs index 74fdf7c..9a3163a 100644 --- a/ucalc/MainWindow.xaml.cs +++ b/ucalc/MainWindow.xaml.cs @@ -22,7 +22,7 @@ namespace UCalc if (newWindow.ShowDialog() == true) { - new BillingWindow(newWindow.Billing).Show(); + new BillingWindow(null, newWindow.Billing).Show(); Hide(); } } @@ -75,7 +75,7 @@ namespace UCalc var billing = new BillingLoader().Load(path); App.RecentlyOpenedList.Add(new RecentlyOpenedItem(path)); - new BillingWindow(billing).Show(); + new BillingWindow(path, billing).Show(); Hide(); } catch (IOException) diff --git a/ucalc/Models/Model.cs b/ucalc/Models/Model.cs index 4175295..0349be4 100644 --- a/ucalc/Models/Model.cs +++ b/ucalc/Models/Model.cs @@ -147,6 +147,16 @@ namespace UCalc.Models return _validator; } + public Billing Dump() + { + throw new NotImplementedException(); + } + + public void ResetModified() + { + throw new NotImplementedException(); + } + public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] diff --git a/ucalc/Pages/SideBar.xaml b/ucalc/Pages/SideBar.xaml index c632952..dd59dd6 100644 --- a/ucalc/Pages/SideBar.xaml +++ b/ucalc/Pages/SideBar.xaml @@ -21,7 +21,15 @@ HighlightBackground="{x:Static local:Constants.SubMainColor}" Grid.Row="0" Grid.Column="0" - ToolTip="Speichern"> + Click="OnSaveClick"> + + + + + + + + + ToolTip="Drucken (Strg + P)" + Click="OnPrintClick"> + ToolTip="Über MietRechner" + Click="OnAboutClick"> + Property="{Binding Path=Model.Root.Landlord, RelativeSource={RelativeSource AncestorType=pages:SideBar}}" />