From 0109e71a56ed3ac596bd5ce528067a9f4e4c7996 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 13 Nov 2012 00:25:03 +1100 Subject: [PATCH] Created CommonExtensions project. Moved files around --- Client/Client.csproj | 30 +++++----- Client/{ => Controls}/App.xaml | 0 Client/{ => Controls}/App.xaml.cs | 0 Client/{ => Controls}/FilterDialog.xaml | 0 Client/{ => Controls}/FilterDialog.xaml.cs | 0 Client/{ => Controls}/Help.xaml | 0 Client/{ => Controls}/Help.xaml.cs | 0 Client/{ => Controls}/MainWindow.xaml | 0 Client/{ => Controls}/MainWindow.xaml.cs | 6 +- Client/{ => Controls}/Options.xaml | 0 Client/{ => Controls}/Options.xaml.cs | 0 Client/{ => Controls}/postponedialog.xaml | 0 Client/{ => Controls}/postponedialog.xaml.cs | 0 .../{Utilities.cs => ExceptionExtensions.cs} | 16 +----- Client/MainWindowViewModel.cs | 34 +++++++++--- CommonExtensions/CommonExtensions.csproj | 55 +++++++++++++++++++ CommonExtensions/IEnumerableExtensions.cs | 25 +++++++++ CommonExtensions/Properties/AssemblyInfo.cs | 36 ++++++++++++ CommonExtensions/StringExtensions.cs | 34 ++++++++++++ ToDo.Net.sln | 12 ++++ ToDoLib/ExtensionMethods.cs | 53 ------------------ ToDoLib/Log.cs | 1 + ToDoLib/Task.cs | 1 + ToDoLib/ToDoLib.csproj | 7 ++- 24 files changed, 217 insertions(+), 93 deletions(-) rename Client/{ => Controls}/App.xaml (100%) rename Client/{ => Controls}/App.xaml.cs (100%) rename Client/{ => Controls}/FilterDialog.xaml (100%) rename Client/{ => Controls}/FilterDialog.xaml.cs (100%) rename Client/{ => Controls}/Help.xaml (100%) rename Client/{ => Controls}/Help.xaml.cs (100%) rename Client/{ => Controls}/MainWindow.xaml (100%) rename Client/{ => Controls}/MainWindow.xaml.cs (99%) rename Client/{ => Controls}/Options.xaml (100%) rename Client/{ => Controls}/Options.xaml.cs (100%) rename Client/{ => Controls}/postponedialog.xaml (100%) rename Client/{ => Controls}/postponedialog.xaml.cs (100%) rename Client/{Utilities.cs => ExceptionExtensions.cs} (60%) create mode 100644 CommonExtensions/CommonExtensions.csproj create mode 100644 CommonExtensions/IEnumerableExtensions.cs create mode 100644 CommonExtensions/Properties/AssemblyInfo.cs create mode 100644 CommonExtensions/StringExtensions.cs delete mode 100644 ToDoLib/ExtensionMethods.cs diff --git a/Client/Client.csproj b/Client/Client.csproj index 407319e7..5af4eca6 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -74,26 +74,27 @@ - + MSBuild:Compile Designer + - + PostponeDialog.xaml - + FilterDialog.xaml - + Help.xaml - + Options.xaml @@ -110,33 +111,32 @@ True User.settings - - + MSBuild:Compile Designer - + Designer MSBuild:Compile - + Designer MSBuild:Compile - + MSBuild:Compile Designer - + App.xaml Code - + MainWindow.xaml Code - + Designer MSBuild:Compile @@ -157,6 +157,10 @@ {C51DEEDA-B581-401B-870B-DBB7D10119BF} ColorFont + + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F} + CommonExtensions + {07AA9CEA-4EF0-4B47-AFBD-6F74AD9C4653} ToDoLib diff --git a/Client/App.xaml b/Client/Controls/App.xaml similarity index 100% rename from Client/App.xaml rename to Client/Controls/App.xaml diff --git a/Client/App.xaml.cs b/Client/Controls/App.xaml.cs similarity index 100% rename from Client/App.xaml.cs rename to Client/Controls/App.xaml.cs diff --git a/Client/FilterDialog.xaml b/Client/Controls/FilterDialog.xaml similarity index 100% rename from Client/FilterDialog.xaml rename to Client/Controls/FilterDialog.xaml diff --git a/Client/FilterDialog.xaml.cs b/Client/Controls/FilterDialog.xaml.cs similarity index 100% rename from Client/FilterDialog.xaml.cs rename to Client/Controls/FilterDialog.xaml.cs diff --git a/Client/Help.xaml b/Client/Controls/Help.xaml similarity index 100% rename from Client/Help.xaml rename to Client/Controls/Help.xaml diff --git a/Client/Help.xaml.cs b/Client/Controls/Help.xaml.cs similarity index 100% rename from Client/Help.xaml.cs rename to Client/Controls/Help.xaml.cs diff --git a/Client/MainWindow.xaml b/Client/Controls/MainWindow.xaml similarity index 100% rename from Client/MainWindow.xaml rename to Client/Controls/MainWindow.xaml diff --git a/Client/MainWindow.xaml.cs b/Client/Controls/MainWindow.xaml.cs similarity index 99% rename from Client/MainWindow.xaml.cs rename to Client/Controls/MainWindow.xaml.cs index 1c774ad9..a86fa2b8 100644 --- a/Client/MainWindow.xaml.cs +++ b/Client/Controls/MainWindow.xaml.cs @@ -41,7 +41,7 @@ public MainWindow() { Application.Current.DispatcherUnhandledException += (o, e) => { - HandleException("An unexpected error occurred", e.Exception); + Helper.HandleException("An unexpected error occurred", e.Exception); e.Handled = true; }; @@ -450,7 +450,7 @@ private void lbTasks_PreviewKeyUp(object sender, KeyEventArgs e) ViewModel.KeyboardShortcut(e.Key, e.KeyboardDevice.Modifiers); } - //this is just for j and k - the nav keys. Using KeyDown allows for holding the key to navigate + // Using KeyDown allows for holding the key to navigate private void lbTasks_PreviewKeyDown(object sender, KeyEventArgs e) { if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt) && lbTasks.HasItems) @@ -513,7 +513,7 @@ private void lbTasks_PreviewKeyDown(object sender, KeyEventArgs e) private void lbTasks_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { e.Handled = true; - KeyboardShortcut(Key.U); + ViewModel.KeyboardShortcut(Key.U); } #endregion diff --git a/Client/Options.xaml b/Client/Controls/Options.xaml similarity index 100% rename from Client/Options.xaml rename to Client/Controls/Options.xaml diff --git a/Client/Options.xaml.cs b/Client/Controls/Options.xaml.cs similarity index 100% rename from Client/Options.xaml.cs rename to Client/Controls/Options.xaml.cs diff --git a/Client/postponedialog.xaml b/Client/Controls/postponedialog.xaml similarity index 100% rename from Client/postponedialog.xaml rename to Client/Controls/postponedialog.xaml diff --git a/Client/postponedialog.xaml.cs b/Client/Controls/postponedialog.xaml.cs similarity index 100% rename from Client/postponedialog.xaml.cs rename to Client/Controls/postponedialog.xaml.cs diff --git a/Client/Utilities.cs b/Client/ExceptionExtensions.cs similarity index 60% rename from Client/Utilities.cs rename to Client/ExceptionExtensions.cs index 819539e1..e859aa15 100644 --- a/Client/Utilities.cs +++ b/Client/ExceptionExtensions.cs @@ -7,21 +7,9 @@ namespace Client { - public class Utilities + public static class ExceptionExtensions { - public void Try(Action action, string errorMessage) - { - try - { - action(); - } - catch (Exception ex) - { - HandleException(errorMessage, ex); - } - } - - public void HandleException(string errorMessage, Exception ex) + public static void Handle(this Exception ex, string errorMessage) { Log.Error(errorMessage, ex); MessageBox.Show(errorMessage + Environment.NewLine + ex.Message + Environment.NewLine + "Please see Help -> Show Error Log for more details", diff --git a/Client/MainWindowViewModel.cs b/Client/MainWindowViewModel.cs index c055351e..713aa607 100644 --- a/Client/MainWindowViewModel.cs +++ b/Client/MainWindowViewModel.cs @@ -8,6 +8,7 @@ using System.Windows.Input; using System.IO; using ColorFont; +using CommonExtensions; namespace Client { @@ -31,7 +32,7 @@ public MainWindowViewModel(MainWindow window) UpdateDisplayedTasks(); } - public SortType SortType + public SortType SortType { get { return _sortType; } set @@ -60,7 +61,7 @@ public void LoadTasks(string filePath) } catch (Exception ex) { - HandleException("An error occurred while opening " + filePath, ex); + ex.Handle("An error occurred while opening " + filePath); } } @@ -172,7 +173,15 @@ public void KeyboardShortcut(Key key, ModifierKeys modifierKeys = ModifierKeys.N if (res == MessageBoxResult.Yes) { - Try(() => _taskList.Delete((Task)_window.lbTasks.SelectedItem), "Error deleting task"); + try + { + _taskList.Delete((Task)_window.lbTasks.SelectedItem); + } + catch (Exception ex) + { + ex.Handle("Error deleting task"); + } + UpdateDisplayedTasks(); } } @@ -245,7 +254,7 @@ public void UpdateDisplayedTasks() } catch (Exception ex) { - HandleException("Error while sorting tasks", ex); + ex.Handle("Error while sorting tasks"); } if (selected == null) @@ -288,7 +297,14 @@ private void Refresh() private void Reload() { - Try(() => _taskList.ReloadTasks(), "Error loading tasks"); + try + { + _taskList.ReloadTasks(); + } + catch (Exception ex) + { + ex.Handle("Error loading tasks"); + } } public void ShowFilterDialog() @@ -300,7 +316,7 @@ public void ShowFilterDialog() f.FilterTextPreset1 = User.Default.FilterTextPreset1; f.FilterTextPreset2 = User.Default.FilterTextPreset2; f.FilterTextPreset3 = User.Default.FilterTextPreset3; - + if (f.ShowDialog().Value) { User.Default.FilterText = f.FilterText.Trim(); @@ -317,7 +333,7 @@ public static IEnumerable FilterList(IEnumerable tasks) { var filters = User.Default.FilterText; var comparer = User.Default.FilterCaseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase; - + if (String.IsNullOrEmpty(filters)) return tasks; @@ -334,7 +350,7 @@ public static IEnumerable FilterList(IEnumerable tasks) else if (filter.Equals("due:future", StringComparison.OrdinalIgnoreCase) && task.DueDate.IsDateGreaterThan(DateTime.Now)) continue; - else if (filter.Equals("due:past", StringComparison.OrdinalIgnoreCase) + else if (filter.Equals("due:past", StringComparison.OrdinalIgnoreCase) && task.DueDate.IsDateLessThan(DateTime.Now)) continue; else if (filter.Equals("due:active", StringComparison.OrdinalIgnoreCase) @@ -452,7 +468,7 @@ private void ToggleComplete(Task task) } catch (Exception ex) { - HandleException("An error occurred while updating the task's completed status", ex); + ex.Handle("An error occurred while updating the task's completed status"); } } diff --git a/CommonExtensions/CommonExtensions.csproj b/CommonExtensions/CommonExtensions.csproj new file mode 100644 index 00000000..6f684599 --- /dev/null +++ b/CommonExtensions/CommonExtensions.csproj @@ -0,0 +1,55 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F} + Library + Properties + CommonExtensions + CommonExtensions + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CommonExtensions/IEnumerableExtensions.cs b/CommonExtensions/IEnumerableExtensions.cs new file mode 100644 index 00000000..ea9daa7f --- /dev/null +++ b/CommonExtensions/IEnumerableExtensions.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CommonExtensions +{ + public static class IEnumerableExtensions + { + public static void Each(this IEnumerable items, Action action) + { + if (!items.IsNullOrEmpty()) + { + foreach (var item in items) + if (item != null) + action(item); + } + } + + public static bool IsNullOrEmpty(this IEnumerable items) + { + return items == null || items.Count() == 0; + } + } +} diff --git a/CommonExtensions/Properties/AssemblyInfo.cs b/CommonExtensions/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..8b1bb188 --- /dev/null +++ b/CommonExtensions/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CommonExtensions")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("CommonExtensions")] +[assembly: AssemblyCopyright("Copyright © 2012")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c667f573-1652-4a43-9b87-c6f3a662af4f")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/CommonExtensions/StringExtensions.cs b/CommonExtensions/StringExtensions.cs new file mode 100644 index 00000000..589ddc88 --- /dev/null +++ b/CommonExtensions/StringExtensions.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CommonExtensions +{ + public static class StringExtensions + { + public static bool IsDateGreaterThan(this string dateString, DateTime date) + { + if (dateString.IsNullOrEmpty()) + return false; + + DateTime comparisonDate; + if (!DateTime.TryParse(dateString, out comparisonDate)) + return false; + + return comparisonDate.Date > date.Date; + } + + public static bool IsDateLessThan(this string dateString, DateTime date) + { + if (dateString.IsNullOrEmpty()) + return false; + + DateTime comparisonDate; + if (!DateTime.TryParse(dateString, out comparisonDate)) + return false; + + return comparisonDate.Date < date.Date; + } + } +} diff --git a/ToDo.Net.sln b/ToDo.Net.sln index eba19f89..27535b24 100644 --- a/ToDo.Net.sln +++ b/ToDo.Net.sln @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuildController", "BuildCon EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ColorFont", "ColorFont\ColorFont.csproj", "{C51DEEDA-B581-401B-870B-DBB7D10119BF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonExtensions", "CommonExtensions\CommonExtensions.csproj", "{5CE19CC1-833A-4950-B86E-CBF59FA00D6F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -85,6 +87,16 @@ Global {C51DEEDA-B581-401B-870B-DBB7D10119BF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {C51DEEDA-B581-401B-870B-DBB7D10119BF}.Release|Mixed Platforms.Build.0 = Release|Any CPU {C51DEEDA-B581-401B-870B-DBB7D10119BF}.Release|x86.ActiveCfg = Release|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Debug|x86.ActiveCfg = Debug|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Release|Any CPU.Build.0 = Release|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ToDoLib/ExtensionMethods.cs b/ToDoLib/ExtensionMethods.cs deleted file mode 100644 index 6170f3ad..00000000 --- a/ToDoLib/ExtensionMethods.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace ToDoLib -{ - public static class ExtensionMethods - { - public static void Each(this IEnumerable items, Action action) - { - if (!items.IsNullOrEmpty()) - { - foreach (var item in items) - if (item != null) - action(item); - } - } - - public static bool IsNullOrEmpty(this IEnumerable items) - { - return items == null || items.Count() == 0; - } - - public static bool Contains(this string source, string toCheck, StringComparison comp) - { - return source.IndexOf(toCheck, comp) >= 0; - } - - public static bool IsDateGreaterThan(this string dateString, DateTime date) - { - if (dateString.IsNullOrEmpty()) - return false; - - DateTime comparisonDate; - if (!DateTime.TryParse(dateString, out comparisonDate)) - return false; - - return comparisonDate.Date > date.Date; - } - - public static bool IsDateLessThan(this string dateString, DateTime date) - { - if (dateString.IsNullOrEmpty()) - return false; - - DateTime comparisonDate; - if (!DateTime.TryParse(dateString, out comparisonDate)) - return false; - - return comparisonDate.Date < date.Date; - } - } -} diff --git a/ToDoLib/Log.cs b/ToDoLib/Log.cs index ed539fd0..07e4e1d1 100644 --- a/ToDoLib/Log.cs +++ b/ToDoLib/Log.cs @@ -2,6 +2,7 @@ using System.IO; using System.Text; using System.Threading; +using CommonExtensions; namespace ToDoLib { diff --git a/ToDoLib/Task.cs b/ToDoLib/Task.cs index ce0ed63d..296f863c 100644 --- a/ToDoLib/Task.cs +++ b/ToDoLib/Task.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; using System.Globalization; +using CommonExtensions; namespace ToDoLib { diff --git a/ToDoLib/ToDoLib.csproj b/ToDoLib/ToDoLib.csproj index 5b32ca37..b1f7d6fe 100644 --- a/ToDoLib/ToDoLib.csproj +++ b/ToDoLib/ToDoLib.csproj @@ -43,13 +43,18 @@ Properties\SharedAssemblyInfo.cs - + + + {5CE19CC1-833A-4950-B86E-CBF59FA00D6F} + CommonExtensions + +