[tasque/transition: 210/213] [libtasque] Improve BackendTaskCollection



commit b1367ec89170b39e0a2c130b820a50e1fbb5a73b
Author: Antonius Riha <antoniusriha gmail com>
Date:   Mon Aug 20 09:43:23 2012 +0200

    [libtasque] Improve BackendTaskCollection
    
    * initially register categories' changed events
    * on dispose: unregister from source collectino changed event
    * some formatting
    * AddColllection and RemoveCollection dropped. No need for them as
    source collection is maintained by another object
    * On source collection changed: register/unregister changed events of
    added/removed category

 src/libtasque/BackendTaskCollection.cs |   29 ++++++++++-------------------
 1 files changed, 10 insertions(+), 19 deletions(-)
---
diff --git a/src/libtasque/BackendTaskCollection.cs b/src/libtasque/BackendTaskCollection.cs
index d9935fd..b7a703e 100644
--- a/src/libtasque/BackendTaskCollection.cs
+++ b/src/libtasque/BackendTaskCollection.cs
@@ -36,13 +36,16 @@ namespace Tasque
 		{
 			if (categories == null)
 				throw new ArgumentNullException ("source");
-			categories.CollectionChanged += HandleSourceCollectionChanged;
 			this.categories = categories;
+			foreach (var item in categories)
+				item.CollectionChanged += HandleCollectionChanged;
+			categories.CollectionChanged += HandleSourceCollectionChanged;
 		}
 		
 		protected override void Dispose (bool disposing)
 		{
 			if (!disposed && disposing) {
+				categories.CollectionChanged -= HandleSourceCollectionChanged;
 				foreach (var item in categories)
 					item.CollectionChanged -= HandleCollectionChanged;
 				disposed = true;
@@ -75,8 +78,8 @@ namespace Tasque
 		
 		internal protected override IEnumerator<Task> GetEnumeratorProtected ()
 		{
-			Task [] tasks = new Task [CountProtected];
-			int i = 0;
+			var tasks = new Task [CountProtected];
+			var i = 0;
 			foreach (var collection in categories) {
 				foreach (var item in collection) {
 					if (tasks.Contains (item))
@@ -87,20 +90,6 @@ namespace Tasque
 			}
 		}
 		
-		void AddCollection (Category category)
-		{
-			categories.Add (category);
-			if (categories.Contains (category))
-			    category.CollectionChanged += HandleCollectionChanged;
-		}
-		
-		bool RemoveCollection (Category category)
-		{
-			if (categories.Contains (category))
-				category.CollectionChanged -= HandleCollectionChanged;
-			return categories.Remove (category);
-		}
-		
 		void HandleCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
 		{
 			OnCollectionChanged (e);
@@ -110,10 +99,12 @@ namespace Tasque
 		{
 			switch (e.Action) {
 			case NotifyCollectionChangedAction.Add:
-				AddCollection (e.NewItems [0] as Category);
+				var newCat = e.NewItems [0] as Category;
+				newCat.CollectionChanged += HandleCollectionChanged;
 				break;
 			case NotifyCollectionChangedAction.Remove:
-				RemoveCollection (e.OldItems [0] as Category);
+				var oldCat = e.OldItems [0] as Category;
+				oldCat.CollectionChanged -= HandleCollectionChanged;
 				break;
 			}
 		}



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