[blam/gtk-builder] Channel: use generics and LINQ



commit 9ebea6e84f1b19e24c6e4698adf2ed32480d142b
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Wed Oct 9 15:08:48 2013 +0200

    Channel: use generics and LINQ

 src/Channel.cs           |  165 ++++++++++++++++------------------------------
 src/ChannelCollection.cs |    6 +-
 src/ChannelGroup.cs      |   58 +++++-----------
 src/ChannelList.cs       |    2 +-
 src/ItemStore.cs         |    1 +
 5 files changed, 80 insertions(+), 152 deletions(-)
---
diff --git a/src/Channel.cs b/src/Channel.cs
index ab12bc3..5c53b37 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -17,6 +17,8 @@ using System.ComponentModel;
 using System.ServiceModel.Syndication;
 using System.Security.Cryptography.X509Certificates;
 using System.Runtime.CompilerServices;
+using System.Collections.Generic;
+using System.Collections.Concurrent;
 
 namespace Imendio.Blam {
 
@@ -26,7 +28,7 @@ namespace Imendio.Blam {
         int NrOfUnreadItems {get; }
         string Name {get; set; }
         string Url {get; set; }
-        ArrayList ItemList {get; set; }
+        IEnumerable<string> ItemList { get; }
         Gtk.TreeIter Iter {get; set; }
         bool MarkAsRead();
         Item GetItem(string id);
@@ -47,8 +49,7 @@ namespace Imendio.Blam {
                [XmlAttribute] public string http_username = "";
                [XmlAttribute] public string http_password = "";
 
-        ArrayList item_list = null;
-        Object obj = new Object();
+               List<string> items;
 
                public event PropertyChangedEventHandler PropertyChanged;
 
@@ -80,67 +81,36 @@ namespace Imendio.Blam {
                        }
                }
 
-        private Gtk.TreeIter mIter;
-
                public int NrOfItems {
                        get {
-                lock(obj){
-                    return item_list.Count;
-                }
+                               lock (items)
+                                       return items.Count();
                        }
                }
 
         public int NrOfUnreadItems {
             get {
-                int unread = 0;
-
-                Item item;
-                lock(obj){
-                    foreach (string id in item_list) {
-                        if(id != null){
-                            item = ItemStore.Get(id);
-                            if(item.Unread){
-                                ++unread;
-                            }
-                        }
-                    }
-                }
-
-                return unread;
+                               lock (items) {
+                                       return items
+                                               .Select(id => ItemStore.Get(id))
+                                               .Count(item => item.Unread);
+                               }
             }
         }
 
         public int NrOfNewItems {
             get {
-                int new_items = 0;
-                Item item;
-                lock(obj){
-                    foreach(string id in item_list){
-                        if(id == null){ /* TODO: Figure out why this happens */
-                            continue;
-                        }
-                        item = ItemStore.Get(id);
-                        if(item != null && item.Unread && !item.Old){
-                            ++new_items;
-                        }
-                    }
-                }
-
-                return new_items;
+                lock(items){
+                                       return items
+                                               .Select(id => ItemStore.Get(id))
+                                               .Count(item => item.Unread && !item.Old);
+                               }
             }
         }
 
-        public ArrayList ItemList {
+        public IEnumerable<string> ItemList {
             get {
-                lock(obj){
-                    return item_list;
-                }
-            }
-            set {
-                lock(obj){
-                    item_list = value;
-                    Setup();
-                }
+                               return items;
             }
         }
 
@@ -152,20 +122,12 @@ namespace Imendio.Blam {
                }
 
                [XmlIgnore]
-        public Gtk.TreeIter Iter {
-            get {
-                return mIter;
-            }
-            set {
-                mIter = value;
-            }
-        }
+               public Gtk.TreeIter Iter { get; set; }
 
-        public Channel ()
-        {
-            item_list = new ArrayList();
-            mIter = new Gtk.TreeIter();
-        }
+               public Channel()
+               {
+                       items = new List<string>();
+               }
 
         public Channel (string name, string url) : this()
         {
@@ -180,27 +142,23 @@ namespace Imendio.Blam {
 
         public void Setup()
         {
-            lock(obj){
-                ArrayList nlist = new ArrayList();
-                foreach(string id in item_list){
-                    if(id == null){
-                        continue;
-                    }
-                    Item item = ItemStore.Get(id);
-                    if(item == null){
-                    } else {
-                        nlist.Add(id);
-                        item.PropertyChanged += ItemChanged;
-                    }
-                }
-                item_list = nlist;
-            }
+                       lock (items) {
+                               var newItems = items
+                                       .Select(ItemStore.Get)
+                                       .Where(x => x != null)
+                                       .ToArray();
+
+                               foreach (var item in newItems)
+                                       item.PropertyChanged += ItemChanged;
+
+                               items = newItems.Select(item => item.Id).ToList();
+                       }
         }
 
                public void RemoveItems()
                {
-                       lock(obj){
-                               foreach(string id in item_list){
+                       lock(items){
+                               foreach(string id in items){
                                        ItemStore.Remove(id);
                                }
                        }
@@ -209,19 +167,11 @@ namespace Imendio.Blam {
                public bool MarkAsRead ()
                {
                        bool updated = false;
-                       Item item;
-
-                       lock (obj) {
-                               foreach (string id in item_list) {
-                                       if (id == null) {
-                                               System.Console.WriteLine("null id {0} on {1}", id, Url);
-                                               continue;
-                                       }
-                                       item = ItemStore.Get(id);
-                                       if (item.Unread) {
-                                               item.Unread = false;
-                                               updated = true;
-                                       }
+
+                       lock (items) {
+                               foreach (var item in items.Select(id => ItemStore.Get(id))) {
+                                       item.Unread = false;
+                                       updated = true;
                                }
                        }
 
@@ -244,22 +194,23 @@ namespace Imendio.Blam {
 
                        LastRefreshed = DateTime.Now;
 
-                       lock (obj ){
-                               var common = feed.Items.Where(i => item_list.Contains(i.Id));
+                       lock (items) {
+                               var newItems = feed.Items;
+                               var common = newItems.Where(item => items.Contains(item.Id));
                                foreach (var item in common)
                                        ItemStore.Get(item.Id).Update(item);
 
                                // new items
-                               foreach (var item in feed.Items.Except(common))
+                               foreach (var item in newItems.Where(item => !items.Contains(item.Id)))
                                        ItemStore.Add(new Item(item));
 
                                // items no longer in the feed are evicted
-                               var ids = feed.Items.Select(i => i.Id);
-                               var evict = item_list.Cast<string>().Except(ids);
+                               var ids = feed.Items.Select(item => item.Id);
+                               var evict = items.Where(id => !ids.Contains(id));
                                foreach (var id in evict)
                                        ItemStore.Remove(id);
 
-                               item_list = new ArrayList(ids.ToArray());
+                               items = ids.ToList();
                        }
 
                        NotifyPropertyChanged("NrOfUnreadItems");
@@ -282,17 +233,17 @@ namespace Imendio.Blam {
          */
         public void Add(Item item)
         {
-            if(item_list.Contains(item.Id)){
-                /* In this case we only need to update */
-                ItemStore.Get(item.Id).Update(item);
-                return;
-            }
+                       lock (items) {
+                               if(items.Contains(item.Id)){
+                                       /* In this case we only need to update */
+                                       ItemStore.Get(item.Id).Update(item);
+                                       return;
+                               }
 
-                       ItemStore.Add(item);
-            lock(obj){
-                item_list.Add(item.Id);
-            }
-                       ItemStore.Get(item.Id).PropertyChanged += ItemChanged;
+                               ItemStore.Add(item);
+                               items.Add(item.Id);
+                               ItemStore.Get(item.Id).PropertyChanged += ItemChanged;
+                       }
         }
 
                public bool GetHasKeyword (string keyword)
diff --git a/src/ChannelCollection.cs b/src/ChannelCollection.cs
index 1564f4b..ca8b877 100644
--- a/src/ChannelCollection.cs
+++ b/src/ChannelCollection.cs
@@ -231,7 +231,7 @@ namespace Imendio.Blam {
                        NotifyAdd(grp);
                }
 
-        public void Add(ChannelGroup group, IChannel channel)
+        public void Add(ChannelGroup group, Channel channel)
         {
             group.Add(channel);
                        channel.PropertyChanged += ChannelChanged;
@@ -260,7 +260,7 @@ namespace Imendio.Blam {
                                /* It's not a first-level channel or group. Dig deeper. */
                                foreach(ChannelGroup group in Groups){
                                        if(group.Channels.Contains(channel)){
-                                               group.Channels.Remove(channel);
+                                               group.Remove((Channel)channel);
                                                break;
                                        }
                                }
@@ -282,7 +282,7 @@ namespace Imendio.Blam {
                        if (dst == null)
                                Add(src);
                        else
-                               Add((ChannelGroup)dst, src);
+                               Add((ChannelGroup)dst, (Channel)src);
 
                        MarkAsDirty();
                }
diff --git a/src/ChannelGroup.cs b/src/ChannelGroup.cs
index 162c9b0..df4968b 100644
--- a/src/ChannelGroup.cs
+++ b/src/ChannelGroup.cs
@@ -6,6 +6,8 @@
 
 using System;
 using System.Collections;
+using System.Collections.Specialized;
+using System.Collections.Generic;
 using System.Xml.Serialization;
 using System.Threading.Tasks;
 using System.Linq;
@@ -15,68 +17,37 @@ using System.Runtime.CompilerServices;
 namespace Imendio.Blam
 {
     [XmlType("group")]
-    public class ChannelGroup : IChannel, INotifyPropertyChanged
+    public class ChannelGroup : IChannel, INotifyPropertyChanged, INotifyCollectionChanged
     {
         [XmlAttribute("Name")] public string Int_Name = null;
         [XmlAttribute("Url")] public string Int_Url = null;
-        [XmlElement("Channel", typeof(Channel))] public ArrayList Channels;
+        [XmlElement("Channel", typeof(Channel))] public List<Channel> Channels;
         private Gtk.TreeIter mIter;
 
                public event PropertyChangedEventHandler PropertyChanged;
+               public event NotifyCollectionChangedEventHandler CollectionChanged;
 
-        public ArrayList ItemList {
+        public IEnumerable<string> ItemList {
             get { /* FIXME: Cache this value. */
-                ArrayList tmp = new ArrayList();
-                foreach(Channel chan in Channels){
-                    tmp.AddRange(chan.ItemList);
-                }
-                return tmp;
+                               return Channels.SelectMany(c => c.ItemList);
             }
-            set {}
         }
         
         public int NrOfUnreadItems {
             get {
-                int nr = 0;
-
-                if(Channels.Count == 0)
-                    return nr;
-
-                foreach(IChannel channel in Channels){
-                    nr += channel.NrOfUnreadItems;
-                }
-
-                return nr;
+                               return Channels.Cast<Channel>().Select(c => c.NrOfUnreadItems).Sum();
             }
         }
 
         public int NrOfNewItems {
             get {
-                int nr = 0;
-
-                if(Channels.Count == 0)
-                    return nr;
-
-                foreach(IChannel channel in Channels){
-                    nr += channel.NrOfUnreadItems;
-                }
-
-                return nr;
+                               return Channels.Cast<Channel>().Select(c => c.NrOfNewItems).Sum();
             }
         }
 
         public int NrOfItems {
             get {
-                int n = 0;
-
-                if(Channels.Count == 0)
-                    return n;
-
-                foreach(IChannel channel in Channels){
-                    n += channel.NrOfItems;
-                }
-                
-                return n;
+                               return Channels.Cast<Channel>().Select(c => c.NrOfItems).Sum();
             }
         }
         public string Name {
@@ -107,7 +78,7 @@ namespace Imendio.Blam
             }
         }
 
-               public void Add(IChannel chan)
+               public void Add(Channel chan)
                {
                        foreach(Channel ch in Channels){
                                if(ch.Url == chan.Url)
@@ -120,6 +91,11 @@ namespace Imendio.Blam
                        Channels.Add(chan);
                }
 
+               public void Remove(Channel channel)
+               {
+                       Channels.Remove(channel);
+               }
+
                public void RemoveItems()
                {
                        foreach(Channel chan in Channels){
@@ -130,7 +106,7 @@ namespace Imendio.Blam
         public ChannelGroup() : base()
         {
             if(Channels == null)
-                Channels = new ArrayList();
+                Channels = new List<Channel>();
         }
 
                void ChannelChanged(object sender, PropertyChangedEventArgs args)
diff --git a/src/ChannelList.cs b/src/ChannelList.cs
index c7ef740..7bce0b5 100644
--- a/src/ChannelList.cs
+++ b/src/ChannelList.cs
@@ -395,7 +395,7 @@ namespace Imendio.Blam {
                                if (dst is ChannelGroup) {
                                        // if it's already in this group, don't do anything
                                        var grp = (ChannelGroup)dst;
-                                       if (grp.Channels.Contains(src))
+                                       if (grp.Channels.Contains((Channel)src))
                                                return;
 
                                        Collection.Move(src, dst);
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index 88dd0bc..44bbe6c 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -11,6 +11,7 @@ namespace Imendio.Blam
 {
     public class ItemStore
     {
+               //static Dictionary<string, Item> Items;
                static ConcurrentDictionary<string, Item> Items;
         static string itemfile = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE;
         static string itemfile_tmp = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE + ".tmp";


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