From 03000908063212dee07415a9de10560a5c881c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Erbsh=C3=A4u=C3=9Fer?= Date: Wed, 10 Jun 2020 10:30:14 +0200 Subject: [PATCH] Implemented page switching with side bar buttons. --- ucalc/App.xaml.cs | 2 +- ucalc/BillingWindow.xaml | 37 +++- ucalc/BillingWindow.xaml.cs | 21 ++- ucalc/Controls/ErrorCounter.xaml | 9 + ucalc/Controls/ErrorCounter.xaml.cs | 10 ++ ucalc/Controls/Helpers.cs | 30 ++++ ucalc/Controls/HighlightButton.cs | 132 +++++++++++++++ ucalc/Data/BillingLoader.cs | 18 ++ ucalc/Data/RecentlyOpened.cs | 49 +++++- ucalc/MainWindow.xaml | 10 +- ucalc/MainWindow.xaml.cs | 24 ++- ucalc/Pages/CostsPage.xaml | 9 + ucalc/Pages/CostsPage.xaml.cs | 12 ++ ucalc/Pages/DetailsPage.xaml | 9 + ucalc/Pages/DetailsPage.xaml.cs | 12 ++ ucalc/Pages/HousePage.xaml | 9 + ucalc/Pages/HousePage.xaml.cs | 10 ++ ucalc/Pages/LandlordPage.xaml | 9 + ucalc/Pages/LandlordPage.xaml.cs | 12 ++ ucalc/Pages/SideBar.xaml | 253 ++++++++++++++++++++++++++++ ucalc/Pages/SideBar.xaml.cs | 39 +++++ ucalc/Pages/TenantsPage.xaml | 9 + ucalc/Pages/TenantsPage.xaml.cs | 12 ++ ucalc/ucalc.csproj | 46 ++++- 24 files changed, 762 insertions(+), 21 deletions(-) create mode 100644 ucalc/Controls/ErrorCounter.xaml create mode 100644 ucalc/Controls/ErrorCounter.xaml.cs create mode 100644 ucalc/Controls/Helpers.cs create mode 100644 ucalc/Controls/HighlightButton.cs create mode 100644 ucalc/Data/BillingLoader.cs create mode 100644 ucalc/Pages/CostsPage.xaml create mode 100644 ucalc/Pages/CostsPage.xaml.cs create mode 100644 ucalc/Pages/DetailsPage.xaml create mode 100644 ucalc/Pages/DetailsPage.xaml.cs create mode 100644 ucalc/Pages/HousePage.xaml create mode 100644 ucalc/Pages/HousePage.xaml.cs create mode 100644 ucalc/Pages/LandlordPage.xaml create mode 100644 ucalc/Pages/LandlordPage.xaml.cs create mode 100644 ucalc/Pages/SideBar.xaml create mode 100644 ucalc/Pages/SideBar.xaml.cs create mode 100644 ucalc/Pages/TenantsPage.xaml create mode 100644 ucalc/Pages/TenantsPage.xaml.cs diff --git a/ucalc/App.xaml.cs b/ucalc/App.xaml.cs index a703461..616fa75 100644 --- a/ucalc/App.xaml.cs +++ b/ucalc/App.xaml.cs @@ -9,7 +9,7 @@ namespace UCalc public App() { - _recentlyOpenedList = new RecentlyOpenedList(); + _recentlyOpenedList = new RecentlyOpenedList("recently.txt"); } } } \ No newline at end of file diff --git a/ucalc/BillingWindow.xaml b/ucalc/BillingWindow.xaml index 70c020c..8b29f1d 100644 --- a/ucalc/BillingWindow.xaml +++ b/ucalc/BillingWindow.xaml @@ -5,6 +5,37 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:UCalc" mc:Ignorable="d" - Title="BillingWindow" Height="450" Width="800"> - - + Title="MietRechner" + Width="800" + Height="600" + MinWidth="600" + MinHeight="400" + Closed="OnClosed"> + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ucalc/BillingWindow.xaml.cs b/ucalc/BillingWindow.xaml.cs index 1c09c9f..97bd516 100644 --- a/ucalc/BillingWindow.xaml.cs +++ b/ucalc/BillingWindow.xaml.cs @@ -1,4 +1,8 @@ -using UCalc.Data; +using System; +using System.Windows; +using System.Windows.Navigation; +using UCalc.Data; +using UCalc.Pages; namespace UCalc { @@ -12,6 +16,21 @@ namespace UCalc _savedBilling = savedBilling; Billing = billing; InitializeComponent(); + + Title = + $"MietRechner - Abrechnung von {billing.StartDate.ToShortDateString()} - {billing.EndDate.Date.ToShortDateString()}"; + } + + private void OnClosed(object sender, EventArgs e) + { + Application.Current.MainWindow?.Show(); + } + + private void OnSideBarFrameLoadCompleted(object sender, NavigationEventArgs e) + { + var sideBar = (SideBar) SideBarFrame.Content; + sideBar.TabControl = TabControl; + sideBar.LandlordButton.Selected = true; } } } \ No newline at end of file diff --git a/ucalc/Controls/ErrorCounter.xaml b/ucalc/Controls/ErrorCounter.xaml new file mode 100644 index 0000000..3c219c5 --- /dev/null +++ b/ucalc/Controls/ErrorCounter.xaml @@ -0,0 +1,9 @@ + + + diff --git a/ucalc/Controls/ErrorCounter.xaml.cs b/ucalc/Controls/ErrorCounter.xaml.cs new file mode 100644 index 0000000..df8dae4 --- /dev/null +++ b/ucalc/Controls/ErrorCounter.xaml.cs @@ -0,0 +1,10 @@ +namespace UCalc.Controls +{ + public partial class ErrorCounter + { + public ErrorCounter() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/ucalc/Controls/Helpers.cs b/ucalc/Controls/Helpers.cs new file mode 100644 index 0000000..865f82d --- /dev/null +++ b/ucalc/Controls/Helpers.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace UCalc.Controls +{ + public static class Helpers + { + public static IEnumerable FindChildren(this Panel parent) where T : FrameworkElement + { + foreach (var child in parent.Children) + { + if (child is T t) + { + yield return t; + } + } + } + + public static void ChangeColor(this Viewbox viewbox, SolidColorBrush brush) + { + foreach (var path in ((Canvas) viewbox.Child).FindChildren()) + { + path.Fill = brush; + } + } + } +} \ No newline at end of file diff --git a/ucalc/Controls/HighlightButton.cs b/ucalc/Controls/HighlightButton.cs new file mode 100644 index 0000000..10e0f17 --- /dev/null +++ b/ucalc/Controls/HighlightButton.cs @@ -0,0 +1,132 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace UCalc.Controls +{ + public class HighlightButton : DockPanel + { + public static readonly RoutedEvent ClickEvent = + EventManager.RegisterRoutedEvent(nameof(Click), RoutingStrategy.Bubble, typeof(RoutedEventHandler), + typeof(HighlightButton)); + + public event RoutedEventHandler Click + { + add => AddHandler(ClickEvent, value); + remove => RemoveHandler(ClickEvent, value); + } + + public SolidColorBrush HighlightForeground { get; set; } + public SolidColorBrush HighlightBackground { get; set; } + public bool Selectable { get; set; } + private bool _selected; + + public bool Selected + { + get => _selected; + set + { + if (_selected && !value) + { + Unhighlight(); + } + else if (value) + { + if (!_selected) + { + Highlight(); + } + + RaiseEvent(new RoutedEventArgs(ClickEvent)); + } + + _selected = value; + } + } + + public HighlightButton() + { + HighlightForeground = Brushes.White; + HighlightBackground = Brushes.Black; + } + + private void SetColors(SolidColorBrush foreground, SolidColorBrush background) + { + Background = background; + + foreach (var child in Children) + { + switch (child) + { + case Label label: + label.Foreground = foreground; + break; + case Path path: + path.Fill = foreground; + break; + case Viewbox viewbox: + viewbox.ChangeColor(foreground); + break; + } + } + } + + private void Highlight() + { + SetColors(HighlightBackground, HighlightForeground); + } + + private void Unhighlight() + { + SetColors(HighlightForeground, HighlightBackground); + } + + protected override void OnMouseEnter(MouseEventArgs e) + { + base.OnMouseEnter(e); + + if (!Selected) + { + Highlight(); + } + } + + protected override void OnMouseLeave(MouseEventArgs e) + { + base.OnMouseLeave(e); + + if (!Selected) + { + Unhighlight(); + } + } + + protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) + { + base.OnMouseLeftButtonUp(e); + + if (Selectable) + { + Selected = true; + } + else + { + RaiseEvent(new RoutedEventArgs(ClickEvent)); + } + } + + protected override void OnInitialized(EventArgs e) + { + base.OnInitialized(e); + + if (!Selected) + { + Highlight(); + Unhighlight(); + } + } + } +} \ No newline at end of file diff --git a/ucalc/Data/BillingLoader.cs b/ucalc/Data/BillingLoader.cs new file mode 100644 index 0000000..3bf9595 --- /dev/null +++ b/ucalc/Data/BillingLoader.cs @@ -0,0 +1,18 @@ +using System; + +namespace UCalc.Data +{ + public class BillingLoader + { + public Billing Load(string path) + { + // TODO + return new Billing(); + } + + public void Store(string path, Billing billing) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/ucalc/Data/RecentlyOpened.cs b/ucalc/Data/RecentlyOpened.cs index fefb881..dc30b1e 100644 --- a/ucalc/Data/RecentlyOpened.cs +++ b/ucalc/Data/RecentlyOpened.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; +using System.Linq; namespace UCalc.Data { @@ -40,11 +42,43 @@ namespace UCalc.Data public class RecentlyOpenedList : ICollection { + private const int MaxCount = 10; + private readonly string _path; private readonly List _list; - public RecentlyOpenedList() + public RecentlyOpenedList(string path) { + _path = path; _list = new List(); + + Load(path); + } + + private void Load(string path) + { + try + { + var lines = File.ReadAllLines(path); + + _list.AddRange(lines.Where(itemPath => itemPath != "").Take(MaxCount) + .Select(itemPath => new RecentlyOpenedItem(itemPath))); + } + catch (IOException) + { + // Do nothing + } + } + + private void Store(string path) + { + try + { + File.WriteAllLines(path, _list.Select(item => item.Path)); + } + catch (IOException) + { + // Do nothing + } } public IEnumerator GetEnumerator() @@ -67,15 +101,19 @@ namespace UCalc.Data _list.Insert(0, item); - if (_list.Count > 10) + if (_list.Count > MaxCount) { - _list.RemoveAt(10); + _list.RemoveAt(MaxCount); } + + Store(_path); } public void Clear() { _list.Clear(); + + Store(_path); } public bool Contains(RecentlyOpenedItem item) @@ -90,7 +128,10 @@ namespace UCalc.Data public bool Remove(RecentlyOpenedItem item) { - return _list.Remove(item); + var result = _list.Remove(item); + + Store(_path); + return result; } public int Count => _list.Count; diff --git a/ucalc/MainWindow.xaml b/ucalc/MainWindow.xaml index 1397d60..f561b6a 100644 --- a/ucalc/MainWindow.xaml +++ b/ucalc/MainWindow.xaml @@ -44,7 +44,7 @@ - + -