[blam/gtk-builder] FeedList: react to top-level additions



commit a873ed87537785b32595582883dc7ecdddc3f676
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Wed Oct 9 10:02:21 2013 +0200

    FeedList: react to top-level additions

 src/Blam.cs              |    4 ++--
 src/ChannelCollection.cs |   16 ++++++++++++----
 src/ChannelList.cs       |   33 ++++++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 7 deletions(-)
---
diff --git a/src/Blam.cs b/src/Blam.cs
index 4b1e7a0..9dfbc79 100644
--- a/src/Blam.cs
+++ b/src/Blam.cs
@@ -41,7 +41,7 @@ namespace Blam
 
                        channels = ChannelCollection.LoadFromFile (Defines.APP_HOMEDIR + "/collection.xml");
                        FeedListSw = bld.GetObject<ScrolledWindow>("feed-list-scroll");
-                       FeedList = new FeedList(channels.Channels, channels.Groups);
+                       FeedList = new FeedList(channels.ObservableChannels, channels.ObservableGroups);
                        FeedList.MainWindow = MainWindow;
                        FeedListSw.Child = FeedList;
 
@@ -83,7 +83,7 @@ namespace Blam
                                if (dialog.Run()) {
                                        var grp = new ChannelGroup();
                                        grp.Name = dialog.Name;
-                                       channels.Groups.Add(grp);
+                                       channels.Add(grp);
                                }
                        });
                }
diff --git a/src/ChannelCollection.cs b/src/ChannelCollection.cs
index ed3ad46..1564f4b 100644
--- a/src/ChannelCollection.cs
+++ b/src/ChannelCollection.cs
@@ -9,6 +9,7 @@ using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
+using System.Collections.ObjectModel;
 using System.Linq;
 using System.IO;
 using System.Reflection;
@@ -36,7 +37,7 @@ namespace Imendio.Blam {
        private static uint WRITE_TIMEOUT = 5 * 60 * 1000; // Every 5 minutes
        
        static XmlSerializer serializer = new XmlSerializer (typeof (ChannelCollection));
-       
+
        List<Channel> mChannels;
        [XmlElement ("Channel", typeof (Channel))]
        public List<Channel> Channels {
@@ -51,6 +52,10 @@ namespace Imendio.Blam {
        [XmlElement("Group", typeof(ChannelGroup))]
        public List<ChannelGroup> Groups {get; private set; }
 
+               // we use these not to break the compatibility with the XmlWriter/XmlReader
+               public ObservableCollection<Channel> ObservableChannels { get; private set; }
+               public ObservableCollection<ChannelGroup> ObservableGroups { get; private set; }
+
        public int NrOfUnreadItems {
            get {
                int unread = 0;
@@ -131,6 +136,9 @@ namespace Imendio.Blam {
                }
            }
 
+                       collection.ObservableChannels = new 
ObservableCollection<Channel>(collection.mChannels);
+                       collection.ObservableGroups = new 
ObservableCollection<ChannelGroup>(collection.Groups);
+
            collection.FileName = file;
             foreach(ChannelGroup chan in collection.Groups){
                 chan.PropertyChanged += collection.ChannelChanged;
@@ -195,14 +203,14 @@ namespace Imendio.Blam {
         public async Task Add (IChannel channel, bool update = false)
         {
             // If we already have this feed, simply return
-                       if (mChannels.Cast<Channel>().Any(channel.Url.Equals))
+                       if (mChannels.Any(channel.Url.Equals))
                 return;
 
                        if(channel.Name == null){
                                channel.Name = channel.Url;
                        }
 
-                       mChannels.Add (channel as Channel);
+                       ObservableChannels.Add((Channel)channel);
                        if (update) {
                                var updated = await channel.RefreshAsync();
                                if (updated)
@@ -219,7 +227,7 @@ namespace Imendio.Blam {
                        if (Groups.Any(grp.Name.Equals))
                                return;
 
-                       Groups.Add(grp);
+                       ObservableGroups.Add(grp);
                        NotifyAdd(grp);
                }
 
diff --git a/src/ChannelList.cs b/src/ChannelList.cs
index 1a0481e..0fab122 100644
--- a/src/ChannelList.cs
+++ b/src/ChannelList.cs
@@ -13,6 +13,9 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Runtime.InteropServices;
+using System.Collections.ObjectModel;
+using System.Reactive;
+using System.Reactive.Linq;
 using System.ComponentModel;
 using System.Threading;
 using Mono.Unix;
@@ -38,7 +41,13 @@ namespace Blam
                        sc.Post((state) => Model.EmitRowChanged(Model.GetPath(chan.Iter), chan.Iter), null);
                }
 
-               public FeedList(IEnumerable<Channel> channels, IEnumerable<ChannelGroup> groups)
+               IEnumerable<IChannel> enumerateChannels(IList chans)
+               {
+                       foreach (IChannel chan in chans)
+                               yield return chan;
+               }
+
+               public FeedList(ObservableCollection<Channel> channels, ObservableCollection<ChannelGroup> 
groups)
                {
                        name_col = new TreeViewColumn();
                        var renderer = new CellRendererText();
@@ -60,6 +69,28 @@ namespace Blam
 
                        var model = Model as TreeStore;
 
+                       var changes = Observable.Merge(
+                               Observable.FromEventPattern<NotifyCollectionChangedEventArgs>(channels, 
"CollectionChanged"),
+                               Observable.FromEventPattern<NotifyCollectionChangedEventArgs>(groups, 
"CollectionChanged"));
+
+                       var additions = changes.Where(x => x.EventArgs.Action == 
NotifyCollectionChangedAction.Add)
+                               .SelectMany(x => enumerateChannels(x.EventArgs.NewItems));
+
+                       var removals = changes.Where(x => x.EventArgs.Action == 
NotifyCollectionChangedAction.Remove)
+                               .SelectMany(x => enumerateChannels(x.EventArgs.OldItems));
+
+                       // let's not wire this up until we know how to deal with arbitrary removals
+                       //additions.Subscribe(chan => {
+                       //      Gtk.TreeIter iter = (model as TreeStore).AppendValues(chan);
+                       //      chan.Iter = iter;
+                       //
+                       //});
+
+                       removals.Subscribe(chan => {
+                               var iter = chan.Iter;
+                               (model as TreeStore).Remove(ref iter);
+                       });
+
                        foreach (Channel chan in channels) {
                                chan.Iter = model.AppendValues(chan);
                                chan.PropertyChanged += OnPropertyChanged;


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