Implemented ui logic for saving.

This commit is contained in:
Tobias Erbshäußer
2020-06-16 16:16:06 +02:00
parent f566911d31
commit 1a94bb0692
6 changed files with 192 additions and 16 deletions
+16 -1
View File
@@ -11,9 +11,24 @@
Height="600"
MinWidth="600"
MinHeight="400"
Closing="OnClosing"
Closed="OnClosed"
Icon="logo.ico">
<Window.InputBindings>
<KeyBinding Modifiers="Control"
Key="S"
Command="{Binding SaveCommand, RelativeSource={RelativeSource AncestorType=local:BillingWindow}}" />
<KeyBinding Modifiers="Control + Alt"
Key="S"
Command="{Binding SaveAsCommand, RelativeSource={RelativeSource AncestorType=local:BillingWindow}}" />
<KeyBinding Modifiers="Control"
Key="P"
Command="{Binding PrintCommand, RelativeSource={RelativeSource AncestorType=local:BillingWindow}}" />
</Window.InputBindings>
<DockPanel>
<Frame Name="SideBarFrame"
Source="Pages/SideBar.xaml"
@@ -22,7 +37,7 @@
Focusable="False" />
<TabControl Name="TabControl"
BorderBrush="White">
BorderBrush="{x:Static local:Constants.MainColor}">
<TabItem Visibility="Collapsed">
<Frame Source="Pages/LandlordPage.xaml"
LoadCompleted="OnLandlordFrameLoadCompleted"
+130 -4
View File
@@ -1,20 +1,56 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Win32;
using UCalc.Data;
using UCalc.Models;
using UCalc.Pages;
namespace UCalc
{
public class DelegateCommand : ICommand
{
private readonly Action<object> _execute;
public DelegateCommand(Action<object> 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();
}
}
}
+2 -2
View File
@@ -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)
+10
View File
@@ -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]
+13 -3
View File
@@ -21,7 +21,15 @@
HighlightBackground="{x:Static local:Constants.SubMainColor}"
Grid.Row="0"
Grid.Column="0"
ToolTip="Speichern">
Click="OnSaveClick">
<controls:HighlightButton.ToolTip>
<TextBlock>
<Run Text="Speichern (Strg + S)" />
<LineBreak />
<Run Text="Speichern Unter (Strg + Alt + S)" />
</TextBlock>
</controls:HighlightButton.ToolTip>
<Viewbox Width="24"
Height="24"
Margin="8"
@@ -45,7 +53,8 @@
HighlightBackground="{x:Static local:Constants.SubMainColor}"
Grid.Row="0"
Grid.Column="1"
ToolTip="Drucken">
ToolTip="Drucken (Strg + P)"
Click="OnPrintClick">
<Viewbox Width="24"
Height="24"
Margin="8"
@@ -69,7 +78,8 @@
HighlightBackground="{x:Static local:Constants.SubMainColor}"
Grid.Row="0"
Grid.Column="2"
ToolTip="Über MietRechner">
ToolTip="Über MietRechner"
Click="OnAboutClick">
<Viewbox Width="24"
Height="24"
Margin="8"
+17 -2
View File
@@ -8,7 +8,7 @@ namespace UCalc.Pages
{
public partial class SideBar
{
public TabControl TabControl { get; set; }
public BillingWindow ParentWindow { get; set; }
public Model Model { get; set; }
public SideBar()
@@ -32,10 +32,25 @@ namespace UCalc.Pages
{
if (ReferenceEquals(sender, buttons[i]))
{
TabControl.SelectedIndex = i;
ParentWindow.TabControl.SelectedIndex = i;
break;
}
}
}
public void OnSaveClick(object sender, RoutedEventArgs e)
{
ParentWindow.Save();
}
public void OnPrintClick(object sender, RoutedEventArgs e)
{
ParentWindow.Print();
}
public void OnAboutClick(object sender, RoutedEventArgs e)
{
throw new System.NotImplementedException();
}
}
}