[blam] Channel: streamline Update()



commit f97c0c8a57e9d281f71bbefa05bfea44834ecfb6
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Sun Jun 16 02:05:22 2013 +0200

    Channel: streamline Update()
    
    Use LINQ to express the operations better and give a better overview
    of what we're doing.

 src/Channel.cs |   63 ++++++++++++++++++++++++--------------------------------
 1 files changed, 27 insertions(+), 36 deletions(-)
---
diff --git a/src/Channel.cs b/src/Channel.cs
index 4ad911a..9a414f2 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -6,6 +6,7 @@
 
 using System.Collections;
 using System;
+using System.Linq;
 using System.Net;
 using System.Net.Security;
 using System.Xml;
@@ -226,46 +227,36 @@ namespace Imendio.Blam {
         /**
          * Update this channel with the given feed
          */
-        public bool Update(SyndicationFeed feed)
-        {
-            lock(obj){
-                /* If our name is sub-optimal */
-                if((Name == "" || Name == Url) &&
-                   feed.Title.Text != null) {
-                    Name = HtmlUtils.StripHtml(feed.Title.Text.Trim());
-                }
+               public bool Update(SyndicationFeed feed)
+               {
+                       /* If our name is sub-optimal */
+                       if((Name == "" || Name == Url) &&
+                          feed.Title.Text != null) {
+                               Name = HtmlUtils.StripHtml(feed.Title.Text.Trim());
+                       }
 
-                LastRefreshed = DateTime.Now;
-                /*
-                 * The new item list is just the new list, so create it
-                 * on the spot
-                 */
-                ArrayList nitems = new ArrayList();
-                foreach(SyndicationItem sitem in feed.Items){
-                                       nitems.Add(sitem.Id);
-                    /* If didn't have it already, add it to the store */
-                    if(!item_list.Contains(sitem.Id)) {
-                        store.Add(new Item(sitem));
-                                       } else {
-                                               if (sitem.Id == null)
-                                                       continue;
-                                               
-                                               store.Get(sitem.Id).Update(sitem);
-                                       }
-                }
+                       LastRefreshed = DateTime.Now;
 
-                /* Delete the items that are no longer in the feed */
-                foreach(string id in item_list){
-                    if(!nitems.Contains(id)){
-                        store.Remove(id);
-                    }
-                }
+                       lock (obj ){
+                               var common = feed.Items.Where(i => item_list.Contains(i.Id));
+                               foreach (var item in common)
+                                       store.Get(item.Id).Update(item);
 
-                item_list = nitems;
-            }
+                               // new items
+                               foreach (var item in feed.Items.Except(common))
+                                       store.Add(new Item(item));
 
-            return true;
-        }
+                               // items no longer in the feed are evicted
+                               var ids = feed.Items.Select(i => i.Id);
+                               var evict = item_list.Cast<string>().Except(ids);
+                               foreach (var id in evict)
+                                       store.Remove(id);
+
+                               item_list = new ArrayList(ids.ToArray());
+
+                               return true;
+                       }
+               }
 
         public void ItemUpdated(Item item)
         {


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