[tasque/viewmodel: 46/78] Morph SortedNotifyCollection into ObservableSet.



commit 925bc4ec0c359cc4bfd052de3a155be8d1253ae3
Author: Antonius Riha <antoniusriha gmail com>
Date:   Sat Aug 4 15:33:26 2012 +0200

    Morph SortedNotifyCollection into ObservableSet.
    
    Using an existing mono framework type in (ReadOnly)ObservableCollection makes life at higher levels (ViewModel and View) a lot easier, since it integrates better with other existing mono types.
    It is not necessary to do sorting in the model. This is done in the ViewModel.
    
    * Backend.cs: Change (ReadOnly)SortedNotifyCollection to
      (ReadOnly)ObservableCollection.
    * Category.cs: Change (ReadOnly)SortedNotifyCollection to
      (ReadOnly)ObservableCollection and remove INotifyPropertyChanged
      implementation, as it is already inherited by ObservableCollection.
    * ObservableSet.cs: Implement ObservableSet as subclass of
      ObservableCollection. This extension ignores any attempts to add
      duplicate items but does not provide sorting og the elements.
    * ReadOnlyObservableSet.cs: Implement ReadOnlyObservableSet as
      subclass of ReadOnlyObservableCollection. It exposes the inherited
      protected CollectionChanged event publicly.
    * ReadOnlySortedNotifyCollection.cs: Dropped in favor of an
      implementation which subclasses ReadOnlyObservableCollection.
    * SortedNotifyCollection.cs: Dropped in favor of an implementation
      which subclasses ObservableCollection.

 src/libtasque/Backend.cs                        |   12 +-
 src/libtasque/Category.cs                       |   14 +--
 src/libtasque/ObservableSet.cs                  |   52 ++++++++
 src/libtasque/ReadOnlyObservableSet.cs          |   40 +++++++
 src/libtasque/ReadOnlySortedNotifyCollection.cs |   97 ---------------
 src/libtasque/SortedNotifyCollection.cs         |  142 -----------------------
 src/libtasque/libtasque.csproj                  |    4 +-
 7 files changed, 102 insertions(+), 259 deletions(-)
---
diff --git a/src/libtasque/Backend.cs b/src/libtasque/Backend.cs
index 6ac8919..b51f5d3 100644
--- a/src/libtasque/Backend.cs
+++ b/src/libtasque/Backend.cs
@@ -44,11 +44,11 @@ namespace Tasque
 
 			Name = name;
 
-			tasks = new SortedNotifyCollection<Task> ();
-			Tasks = new ReadOnlySortedNotifyCollection<Task> (tasks);
+			tasks = new ObservableSet<Task> ();
+			Tasks = new ReadOnlyObservableSet<Task> (tasks);
 
 			categoriesChangedSources = new List<INotifyCollectionChanged> ();
-			Categories = new SortedNotifyCollection<Category> ();
+			Categories = new ObservableSet<Category> ();
 			
 			// create default category here, because it is required for the model to be there. Overwrite
 			// default category preferably in child class constructor with more appropriate value
@@ -63,7 +63,7 @@ namespace Tasque
 		/// <value>
 		/// This returns all the ICategory items from the backend.
 		/// </value>
-		public SortedNotifyCollection<Category> Categories { get; private set; }
+		public ObservableSet<Category> Categories { get; private set; }
 
 		/// <value>
 		/// Indication that the backend has enough information
@@ -106,7 +106,7 @@ namespace Tasque
 		/// <value>
 		/// All the tasks provided by the backend.
 		/// </value>
-		public ReadOnlySortedNotifyCollection<Task> Tasks { get; private set; }
+		public ReadOnlyObservableSet<Task> Tasks { get; private set; }
 		#endregion
 		
 		#region Methods
@@ -268,6 +268,6 @@ namespace Tasque
 
 		List<INotifyCollectionChanged> categoriesChangedSources;
 		Category defaultCategory;
-		SortedNotifyCollection<Task> tasks;
+		ObservableSet<Task> tasks;
 	}
 }
diff --git a/src/libtasque/Category.cs b/src/libtasque/Category.cs
index 0b2037b..87354af 100644
--- a/src/libtasque/Category.cs
+++ b/src/libtasque/Category.cs
@@ -28,7 +28,7 @@ using System.ComponentModel;
 
 namespace Tasque
 {
-	public class Category : SortedNotifyCollection<Task>, IComparable<Category>, INotifyPropertyChanged
+	public class Category : ObservableSet<Task>, IComparable<Category>
 	{
 		public Category (string name)
 		{
@@ -44,7 +44,7 @@ namespace Tasque
 				if (value == name) {
 					name = value;
 					OnNameChanged ();
-					OnPropertyChanged ("Name");
+					OnPropertyChanged (new PropertyChangedEventArgs ("Name"));
 				}
 			}
 		}
@@ -57,18 +57,8 @@ namespace Tasque
 			return Name.CompareTo (other.Name);
 		}
 
-		#region INotifyPropertyChanged implementation
-		public event PropertyChangedEventHandler PropertyChanged;
-		#endregion
-
 		protected virtual void OnNameChanged () {}
 
-		protected void OnPropertyChanged (string propertyName)
-		{
-			if (PropertyChanged != null)
-				PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
-		}
-
 		string name;
 	}
 }
diff --git a/src/libtasque/ObservableSet.cs b/src/libtasque/ObservableSet.cs
new file mode 100644
index 0000000..79d54fa
--- /dev/null
+++ b/src/libtasque/ObservableSet.cs
@@ -0,0 +1,52 @@
+// 
+// ObservableSet.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using System.Collections.ObjectModel;
+
+namespace Tasque
+{
+	public class ObservableSet<T> : ObservableCollection<T>
+	{
+		protected override void InsertItem (int index, T item)
+		{
+			if (item == null)
+				throw new ArgumentNullException ("item");
+			
+			if (Contains (item))
+				return;
+			
+			base.InsertItem (index, item);
+		}
+		
+		protected override void SetItem (int index, T item)
+		{
+			if (Contains (item))
+				RemoveAt (index);
+			else
+				base.SetItem (index, item);
+		}
+	}
+}
diff --git a/src/libtasque/ReadOnlyObservableSet.cs b/src/libtasque/ReadOnlyObservableSet.cs
new file mode 100644
index 0000000..01e77c8
--- /dev/null
+++ b/src/libtasque/ReadOnlyObservableSet.cs
@@ -0,0 +1,40 @@
+// 
+// ReadOnlyObservableSet.cs
+//  
+// Author:
+//       Antonius Riha <antoniusriha gmail com>
+// 
+// Copyright (c) 2012 Antonius Riha
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System.Collections.ObjectModel;
+using System.Collections.Specialized;
+
+namespace Tasque
+{
+	public class ReadOnlyObservableSet<T> : ReadOnlyObservableCollection<T>
+	{
+		public ReadOnlyObservableSet (ObservableSet<T> source) : base (source) {}
+		
+		public new event NotifyCollectionChangedEventHandler CollectionChanged {
+			add { base.CollectionChanged += value; }
+			remove { base.CollectionChanged -= value; }
+		}
+	}
+}
diff --git a/src/libtasque/libtasque.csproj b/src/libtasque/libtasque.csproj
index 6bc083b..061dd9c 100644
--- a/src/libtasque/libtasque.csproj
+++ b/src/libtasque/libtasque.csproj
@@ -112,8 +112,8 @@
     <Compile Include="TaskNote.cs" />
     <Compile Include="TaskNoteSupport.cs" />
     <Compile Include="TaskCompletionDateComparer.cs" />
-    <Compile Include="SortedNotifyCollection.cs" />
-    <Compile Include="ReadOnlySortedNotifyCollection.cs" />
+    <Compile Include="ObservableSet.cs" />
+    <Compile Include="ReadOnlyObservableSet.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]