From 2c3dadc20a2ad002aa2ad14666fb6b8528c10228 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Fri, 9 Jun 2017 12:44:49 -0700 Subject: [PATCH] C# 7 refactoring --- MvvmHelpers/BaseViewModel.cs | 28 ++++++++++++------------ MvvmHelpers/Grouping.cs | 9 ++++---- MvvmHelpers/ObservableObject.cs | 2 +- MvvmHelpers/ObservableRangeCollection.cs | 18 +++++---------- MvvmHelpers/Utils.cs | 5 +---- README.md | 1 - 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/MvvmHelpers/BaseViewModel.cs b/MvvmHelpers/BaseViewModel.cs index 532ebee..48540a0 100644 --- a/MvvmHelpers/BaseViewModel.cs +++ b/MvvmHelpers/BaseViewModel.cs @@ -13,8 +13,8 @@ public class BaseViewModel : ObservableObject /// The title. public string Title { - get { return title; } - set { SetProperty(ref title, value); } + get => title; + set => SetProperty(ref title, value); } string subtitle = string.Empty; @@ -25,8 +25,8 @@ public string Title /// The subtitle. public string Subtitle { - get { return subtitle; } - set { SetProperty(ref subtitle, value); } + get => subtitle; + set => SetProperty(ref subtitle, value); } string icon = string.Empty; @@ -37,8 +37,8 @@ public string Subtitle /// The icon. public string Icon { - get { return icon; } - set { SetProperty(ref icon, value); } + get => icon; + set => SetProperty(ref icon, value); } bool isBusy; @@ -49,7 +49,7 @@ public string Icon /// true if this instance is busy; otherwise, false. public bool IsBusy { - get { return isBusy; } + get => isBusy; set { if (SetProperty(ref isBusy, value)) @@ -65,7 +65,7 @@ public bool IsBusy /// true if this instance is not busy; otherwise, false. public bool IsNotBusy { - get { return isNotBusy; } + get => isNotBusy; set { if(SetProperty(ref isNotBusy, value)) @@ -81,8 +81,8 @@ public bool IsNotBusy /// true if this instance can load more; otherwise, false. public bool CanLoadMore { - get { return canLoadMore; } - set { SetProperty(ref canLoadMore, value); } + get => canLoadMore; + set => SetProperty(ref canLoadMore, value); } @@ -94,8 +94,8 @@ public bool CanLoadMore /// The header. public string Header { - get { return header; } - set { SetProperty(ref header, value); } + get => header; + set => SetProperty(ref header, value); } string footer = string.Empty; @@ -106,8 +106,8 @@ public string Header /// The footer. public string Footer { - get { return footer; } - set { SetProperty(ref footer, value); } + get => footer; + set => SetProperty(ref footer, value); } } } diff --git a/MvvmHelpers/Grouping.cs b/MvvmHelpers/Grouping.cs index 2120314..1e34d30 100644 --- a/MvvmHelpers/Grouping.cs +++ b/MvvmHelpers/Grouping.cs @@ -1,5 +1,4 @@ -using System.Collections.ObjectModel; -using System.Collections.Generic; +using System.Collections.Generic; namespace MvvmHelpers { @@ -12,7 +11,7 @@ public class Grouping : ObservableRangeCollection /// Gets the key. /// /// The key. - public TKey Key { get; private set; } + public TKey Key { get; } /// /// Returns list of items in the grouping. @@ -41,12 +40,12 @@ public class Grouping : ObservableRangeCollection /// Gets the key. /// /// The key. - public TKey Key { get; private set; } + public TKey Key { get; } /// /// Gets the subkey of the grouping /// - public TSubKey SubKey { get; private set; } + public TSubKey SubKey { get; } /// /// Returns list of items in the grouping. diff --git a/MvvmHelpers/ObservableObject.cs b/MvvmHelpers/ObservableObject.cs index 324f541..25075ce 100644 --- a/MvvmHelpers/ObservableObject.cs +++ b/MvvmHelpers/ObservableObject.cs @@ -19,7 +19,7 @@ public class ObservableObject : INotifyPropertyChanged /// Property name. /// On changed. /// The 1st type parameter. - protected bool SetProperty( + protected virtual bool SetProperty( ref T backingStore, T value, [CallerMemberName]string propertyName = "", Action onChanged = null) diff --git a/MvvmHelpers/ObservableRangeCollection.cs b/MvvmHelpers/ObservableRangeCollection.cs index 5932363..7fbd31b 100644 --- a/MvvmHelpers/ObservableRangeCollection.cs +++ b/MvvmHelpers/ObservableRangeCollection.cs @@ -38,18 +38,16 @@ public ObservableRangeCollection(IEnumerable collection) public void AddRange(IEnumerable collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Add) { if (notificationMode != NotifyCollectionChangedAction.Add && notificationMode != NotifyCollectionChangedAction.Reset) - throw new ArgumentException("Mode must be either Add or Reset for AddRange.", "notificationMode"); + throw new ArgumentException("Mode must be either Add or Reset for AddRange.", nameof(notificationMode)); if (collection == null) - throw new ArgumentNullException("collection"); + throw new ArgumentNullException(nameof(collection)); CheckReentrancy(); if (notificationMode == NotifyCollectionChangedAction.Reset) { foreach (var i in collection) - { Items.Add(i); - } OnPropertyChanged(new PropertyChangedEventArgs("Count")); OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); @@ -61,9 +59,7 @@ public void AddRange(IEnumerable collection, NotifyCollectionChangedAction no int startIndex = Count; var changedItems = collection is List ? (List)collection : new List(collection); foreach (var i in changedItems) - { Items.Add(i); - } OnPropertyChanged(new PropertyChangedEventArgs("Count")); OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); @@ -76,9 +72,9 @@ public void AddRange(IEnumerable collection, NotifyCollectionChangedAction no public void RemoveRange(IEnumerable collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Reset) { if (notificationMode != NotifyCollectionChangedAction.Remove && notificationMode != NotifyCollectionChangedAction.Reset) - throw new ArgumentException("Mode must be either Remove or Reset for RemoveRange.", "notificationMode"); + throw new ArgumentException("Mode must be either Remove or Reset for RemoveRange.", nameof(notificationMode)); if (collection == null) - throw new ArgumentNullException("collection"); + throw new ArgumentNullException(nameof(collection)); CheckReentrancy(); @@ -87,6 +83,7 @@ public void RemoveRange(IEnumerable collection, NotifyCollectionChangedAction foreach (var i in collection) Items.Remove(i); + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); return; @@ -110,10 +107,7 @@ public void RemoveRange(IEnumerable collection, NotifyCollectionChangedAction /// /// Clears the current collection and replaces it with the specified item. /// - public void Replace(T item) - { - ReplaceRange(new T[] { item }); - } + public void Replace(T item) => ReplaceRange(new T[] { item }); /// /// Clears the current collection and replaces it with the specified collection. diff --git a/MvvmHelpers/Utils.cs b/MvvmHelpers/Utils.cs index c387451..8cddd04 100644 --- a/MvvmHelpers/Utils.cs +++ b/MvvmHelpers/Utils.cs @@ -20,10 +20,7 @@ public async static Task WithTimeout(this Task task, int timeoutInMilli var retTask = await Task.WhenAny(task, Task.Delay(timeoutInMilliseconds)) .ConfigureAwait(false); - if (retTask is Task) - return task.Result; - - return default(T); + return retTask is Task ? task.Result : default(T); } /// diff --git a/README.md b/README.md index 8b377ae..0f8f885 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ Collection of MVVM helper classes for any application. - Grab the NuGet: https://www.nuget.org/packages/Refractored.MvvmHelpers Build Status: [![Build status](https://ci.appveyor.com/api/projects/status/nsj9rae1g93hy62o/branch/master?svg=true)](https://ci.appveyor.com/project/JamesMontemagno/mvvm-helpers/branch/master)