[blam] Further reduce the amount of signaling through Application



commit dbfd819817bb157c548f023de8e8324a47103fd1
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Tue Jun 18 14:44:56 2013 +0200

    Further reduce the amount of signaling through Application
    
    Make the tray icon, item list and item view talk directly instead of
    setting values through a callback in Application.

 src/Application.cs       |   30 ++++---------------
 src/ChannelCollection.cs |    2 +-
 src/ItemList.cs          |   69 +++++++++++++++++++++++++++++----------------
 src/ItemView.cs          |   64 +++++++++++++++++++++++++-----------------
 src/TrayIcon.cs          |   29 ++++++++++++++++++-
 5 files changed, 118 insertions(+), 76 deletions(-)
---
diff --git a/src/Application.cs b/src/Application.cs
index 5c3328d..baab1b5 100644
--- a/src/Application.cs
+++ b/src/Application.cs
@@ -192,7 +192,10 @@ namespace Imendio.Blam {
             channelList.RemoveChannelEvent     += RemoveChannelActivated;
             channelList.RefreshChannelEvent    += RefreshChannelActivated;
 
-            itemView = new ItemView ();
+                       itemList = new ItemList(itemView, channelList);
+                       ((Container)itemListSw).Child = itemList;
+
+            itemView = new ItemView(itemList);
 
             Frame f = new Frame ();
             f.Shadow = ShadowType.In;
@@ -200,14 +203,11 @@ namespace Imendio.Blam {
             itemPaned.Add2 (f);
             f.Show ();
             itemView.OnUrl += OnUrl;
-            //itemView.Widget.DomMouseClick += new Gecko.DomMouseClickHandler(OnButtonPressEvent);
-
-            itemList = new ItemList(itemView);
-            ((Container)itemListSw).Child = itemList;
 
-            itemList.ItemSelected += ItemSelected;
+                       // a bit silly to do it every time, but works
+                       itemList.PropertyChanged += (sender, e) => printMenuItem.Sensitive = true;
 
-            trayIcon = new TrayIcon (Catalog.GetString ("Blam News Reader"));
+            trayIcon = new TrayIcon (Catalog.GetString ("Blam News Reader"), mCollection);
             trayIcon.ButtonPressEvent += TrayIconButtonPressCb;
             trayIcon.RefreshAllEvent += RefreshAllActivated;
             trayIcon.PreferencesEvent += PreferencesActivated;
@@ -332,8 +332,6 @@ namespace Imendio.Blam {
                 return;
             }
 
-            itemList.CurrentChannel = channel;
-
             blogNameLabel.Markup = "<b>" + channel.Name + "</b>";
             mainWindow.Title = "Blam - " + channel.Name;
 
@@ -349,12 +347,6 @@ namespace Imendio.Blam {
             editEntryKeywordsMenuItem.Sensitive = sensitive;
         }
 
-        private void ItemSelected(Imendio.Blam.Item item)
-        {
-            itemView.CurrentItem = item;
-            printMenuItem.Sensitive = true;
-        }
-
         public void MarkEntryAsUnreadActivated (object obj, EventArgs args)
         {
             Item item = itemList.GetSelected ();
@@ -840,14 +832,6 @@ namespace Imendio.Blam {
                                                  channelsLabelText +
                                                  "</b>",
                                                  nrOfUnread);
-
-            /* Total number of unread items */
-            string str = string.Format (Catalog.GetPluralString ("{0} unread item", "{0} unread items", 
nrOfUnread),
-                                             nrOfUnread);
-            str += " ";
-            /* Number of new (not-skipped-over) entries. Gets appended to previous string */
-            str += string.Format(Catalog.GetPluralString("({0} new)", "({0} new)", nrOfNew), nrOfNew);
-            trayIcon.Tooltip = str;
         }
 
        public static void SetProcessName(string name)
diff --git a/src/ChannelCollection.cs b/src/ChannelCollection.cs
index 88c6db2..f581127 100644
--- a/src/ChannelCollection.cs
+++ b/src/ChannelCollection.cs
@@ -19,7 +19,7 @@ using System.ComponentModel;
 
 namespace Imendio.Blam {
 
-       public class ChannelCollection : INotifyCollectionChanged {
+       public class ChannelCollection : INotifyPropertyChanged, INotifyCollectionChanged {
 
                public event NotifyCollectionChangedEventHandler CollectionChanged;
                public event PropertyChangedEventHandler PropertyChanged;
diff --git a/src/ItemList.cs b/src/ItemList.cs
index 7111d5e..347bf3a 100644
--- a/src/ItemList.cs
+++ b/src/ItemList.cs
@@ -15,13 +15,14 @@ using System.Collections;
 using System.Threading;
 using System.Threading.Tasks;
 using System.ServiceModel.Syndication;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
 
 namespace Imendio.Blam {
     
-       public class ItemList : Gtk.TreeView {
+       public class ItemList : Gtk.TreeView, INotifyPropertyChanged {
 
-       public delegate void ItemSelectedHandler(Imendio.Blam.Item item);
-       public event ItemSelectedHandler ItemSelected;
+               public event PropertyChangedEventHandler PropertyChanged;
 
        private ItemView itemView;
        
@@ -29,21 +30,45 @@ namespace Imendio.Blam {
     private TreeViewColumn iconColumn;
     CancellationTokenSource MarkReadCancel;
 
-               private IChannel channel;
-       public IChannel CurrentChannel {
-           get {
-               return channel;
-           }
+               IChannel _CurrentChannel;
+               public IChannel CurrentChannel {
+                       get {
+                               return _CurrentChannel;
+                       }
+                       set {
+                               if (_CurrentChannel != value) {
+                                       _CurrentChannel = value;
+                                       UpdateList();
+                                       NotifyPropertyChanged();
+                               }
+                       }
+               }
 
-           set {
-               channel = value;
-               UpdateList();
-           }
-       }
-       
-       public ItemList(ItemView itemView) 
+               Item _Current;
+               public Item Current {
+                       get {
+                               return _Current;
+                       }
+                       set {
+                               if (_Current != value) {
+                                       _Current = value;
+                                       NotifyPropertyChanged();
+                               }
+                       }
+               }
+
+
+               void NotifyPropertyChanged([CallerMemberName] string name = "")
+               {
+                       if (PropertyChanged != null) {
+                               PropertyChanged(this, new PropertyChangedEventArgs(name));
+                       }
+               }
+
+       public ItemList(ItemView itemView, ChannelList channelList)
        {
            this.itemView = itemView;
+                       channelList.ChannelSelectedEvent += channel => CurrentChannel = channel;
 
         CellRendererPixbuf cell2 = new CellRendererPixbuf();
         iconColumn = new TreeViewColumn();
@@ -128,13 +153,13 @@ namespace Imendio.Blam {
 
                public void UpdateList()
                {
-                       if (channel == null)
+                       if (CurrentChannel == null)
                                return;
 
                        var list = Model as ListStore;
 
                        list.Clear();
-                       foreach(string id in channel.ItemList){
+                       foreach(string id in CurrentChannel.ItemList){
                                if (id == null)
                                        continue;
 
@@ -185,7 +210,8 @@ namespace Imendio.Blam {
                        if (MarkReadCancel != null)
                                MarkReadCancel.Cancel();
 
-                       EmitItemSelected(item);
+                       Current = item;
+
                        bool useTimeout = (bool) Conf.Get(Preference.MARK_ITEMS_READ, false);
 
                        if (!useTimeout) {
@@ -275,13 +301,6 @@ namespace Imendio.Blam {
         }
     }
        
-       private void EmitItemSelected (Imendio.Blam.Item item) 
-       {
-           if (ItemSelected != null) {
-               ItemSelected (item);
-           }
-       }
-       
        private void SetSortOrder (bool reverseEntries)
        {
            SortType sortType = SortType.Ascending;
diff --git a/src/ItemView.cs b/src/ItemView.cs
index 8a630a0..a19fc32 100644
--- a/src/ItemView.cs
+++ b/src/ItemView.cs
@@ -18,6 +18,7 @@ using System.Reflection;
 using System.IO;
 using System.Net;
 using System.Text;
+using System.ComponentModel;
 using System.ServiceModel.Syndication;
 
 namespace Imendio.Blam {
@@ -26,23 +27,26 @@ namespace Imendio.Blam {
 #if ENABLE_FONTS
         private WebSettings webSettings;
 #endif
-       private Imendio.Blam.Item currentItem;
     private string baseDir = null;
        private string last_link = null;
        public bool PageLoaded;
 
         public event StringUpdatedHandler OnUrl;
 
-       public Imendio.Blam.Item CurrentItem {
-           get {
-               return currentItem;
-           }
-            
-           set {
-               currentItem = value;
-               Load ();
-           }
-       }
+               Item _Current;
+               public Item Current {
+                       get {
+                               return _Current;
+                       }
+                       set {
+                               if (_Current != value) {
+                                       _Current = value;
+                                       Load();
+                               }
+                       }
+               }
+
+               ItemList itemList;
 
         public WebView Widget {
             get {
@@ -50,8 +54,10 @@ namespace Imendio.Blam {
             }
         }
 
-       public ItemView () : base()
-        {
+               public ItemView(ItemList list) : base()
+               {
+            itemList = list;
+                       itemList.PropertyChanged += ItemListPropertyChanged;
             this.webView = new WebView ();
 #if ENABLE_FONTS
             this.webSettings = new WebSettings ();
@@ -96,29 +102,35 @@ namespace Imendio.Blam {
             webView.Show ();
             PageLoaded = false;
         }
-        
+
+               void ItemListPropertyChanged (object sender, PropertyChangedEventArgs e)
+               {
+                       if (e.PropertyName.Equals("Current"))
+                               Current = ((ItemList)sender).Current;
+               }
+
         private void Load()
         {
                        Theme theme = Application.TheApp.ThemeManager.CurrentTheme;
             string author;
-            if(currentItem.Authors.Count == 0){
+            if(Current.Authors.Count == 0){
                 author = string.Empty;
-            } else if(currentItem.Authors[0].Name != null){
-                author = String.Format(Catalog.GetString("by {0}"), currentItem.Authors[0].Name);
+            } else if(Current.Authors[0].Name != null){
+                author = String.Format(Catalog.GetString("by {0}"), Current.Authors[0].Name);
             } else {
                 author  = "&nbsp;";
             }
             string link    = Catalog.GetString("Show in browser");
-            string pubdate = (!currentItem.LastUpdatedTime.Equals(DateTimeOffset.MinValue)) ?
-                                           currentItem.LastUpdatedTime.ToString("D", 
System.Globalization.CultureInfo.CurrentUICulture) :
-                               (!currentItem.PublishDate.Equals(DateTimeOffset.MinValue)) ?
-                  currentItem.PublishDate.ToString("D", System.Globalization.CultureInfo.CurrentUICulture) :
+            string pubdate = (!Current.LastUpdatedTime.Equals(DateTimeOffset.MinValue)) ?
+                                           Current.LastUpdatedTime.ToString("D", 
System.Globalization.CultureInfo.CurrentUICulture) :
+                               (!Current.PublishDate.Equals(DateTimeOffset.MinValue)) ?
+                  Current.PublishDate.ToString("D", System.Globalization.CultureInfo.CurrentUICulture) :
                                  Catalog.GetString("[No date available]");
-            string text = currentItem.Summary != null ? currentItem.Summary.Text
-                : currentItem.Content != null ? (currentItem.Content as TextSyndicationContent).Text : null;
-                       string title = currentItem.Title.Text;
-            string url = currentItem.Links.Count == 0 ? currentItem.Id :
-                currentItem.Links[0].Uri.ToString();
+            string text = Current.Summary != null ? Current.Summary.Text
+                : Current.Content != null ? (Current.Content as TextSyndicationContent).Text : null;
+                       string title = Current.Title.Text;
+            string url = Current.Links.Count == 0 ? Current.Id :
+                Current.Links[0].Uri.ToString();
 
             baseDir        = "file://" + theme.Path;
 
diff --git a/src/TrayIcon.cs b/src/TrayIcon.cs
index f95c416..15d1541 100644
--- a/src/TrayIcon.cs
+++ b/src/TrayIcon.cs
@@ -8,10 +8,12 @@
 using Gtk;
 using Mono.Unix;
 using System;
+using System.ComponentModel;
 
 namespace Imendio.Blam {
     public class TrayIcon {
     private Gtk.StatusIcon mIcon = null;
+    ChannelCollection Collection { get; set; }
 
        public event EventHandler RefreshAllEvent;
        public event EventHandler PreferencesEvent;
@@ -26,15 +28,40 @@ namespace Imendio.Blam {
            }
        }
 
-       public TrayIcon (string name)
+       public TrayIcon (string name, ChannelCollection collection)
        {
         mIcon = new Gtk.StatusIcon();
         mIcon.IconName = "blam";
         mIcon.Tooltip = name;
         mIcon.Activate += ButtonPressedCb;
         mIcon.PopupMenu += PopupCb;
+                       Collection = collection;
+        Collection.PropertyChanged += HandlePropertyChanged;
+                       Collection.CollectionChanged += HandleCollectionChanged;
        }
+
+               void HandleCollectionChanged (object sender, 
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
+               {
+                       UpdateTooltip();
+               }
+
+               void HandlePropertyChanged (object sender, PropertyChangedEventArgs e)
+               {
+                       UpdateTooltip();
+               }
        
+               void UpdateTooltip()
+               {
+                       var nrUnread = Collection.NrOfUnreadItems;
+                       var nrNew = Collection.NrOfNewItems;
+
+                       var strUnread = string.Format(Catalog.GetPluralString ("{0} unread item", "{0} unread 
items", nrUnread), nrUnread);
+                       var strNew = string.Format(Catalog.GetPluralString("({0} new)", "({0} new)", nrNew), 
nrNew);
+                       var tooltip = strUnread + " " + strNew;
+
+                       Tooltip = tooltip;
+               }
+
     public void Hide()
     {
         mIcon.Visible = false;


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