wasSharpNET – Blame information for rev 22

Subversion Repositories:
Rev:
Rev Author Line No. Line
8 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Collections;
9 using System.Collections.Generic;
10 using System.Collections.Specialized;
11 using System.Linq;
12 using System.ServiceModel.Syndication;
13 using System.Xml;
14 using wasSharp.Collections.Specialized;
15 using wasSharp.Timers;
16  
17 namespace wasSharpNET.Syndication
18 {
19 ///////////////////////////////////////////////////////////////////////////
20 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
21 ///////////////////////////////////////////////////////////////////////////
22 /// <summary>
23 /// A Syndication implementation as an Observable collection.
24 /// </summary>
25 public class ObservableSyndication : ICollection<SyndicationItem>, INotifyCollectionChanged
26 {
27 private readonly ObservableDictionary<string, SyndicationItem> syndicationItems =
28 new ObservableDictionary<string, SyndicationItem>();
29  
30 private readonly Timer syndicationPoll;
31 private TimeSpan _defaultUpdateTime;
32  
33 public ObservableSyndication(string URL, TimeSpan defaultUpdateTime)
34 {
35 // Assign update variables.
36 _defaultUpdateTime = defaultUpdateTime;
37  
38 // Forward the collection change event.
39 syndicationItems.CollectionChanged += (o, p) => { CollectionChanged?.Invoke(this, p); };
40  
41 // Poll the feed.
42 syndicationPoll = new Timer(() =>
43 {
44 using (var reader = XmlReader.Create(URL))
45 {
46 SyndicationFeed.Load(reader)?
47 .Items
48 .AsParallel()
49 .Where(o => o.PublishDate.CompareTo(
50 DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(30)).Subtract(defaultUpdateTime)) > 0)
51 .ForAll(o =>
52 {
53 if (!syndicationItems.ContainsKey(o.Id))
54 {
55 syndicationItems.Add(o.Id, o);
56 }
57 });
58 }
59 }, defaultUpdateTime, defaultUpdateTime);
60 }
61  
62 public ObservableSyndication(string URL, uint milliseconds) : this(URL, TimeSpan.FromMilliseconds(milliseconds))
63 {
64 }
65  
66 public TimeSpan Refresh
67 {
68 get { return _defaultUpdateTime; }
69 set
70 {
71 _defaultUpdateTime = value;
72 syndicationPoll.Change(_defaultUpdateTime, _defaultUpdateTime);
73 }
74 }
75  
76 public int Count => syndicationItems.Count;
77  
78 public bool IsReadOnly => false;
79  
80 public void Add(SyndicationItem item)
81 {
82 syndicationItems.Add(item.Id, item);
83 }
84  
85 public void Clear()
86 {
87 syndicationItems.Clear();
88 }
89  
90 public bool Contains(SyndicationItem item)
91 {
92 return syndicationItems.ContainsKey(item.Id) && syndicationItems[item.Id].Equals(item);
93 }
94  
95 public void CopyTo(SyndicationItem[] array, int arrayIndex)
96 {
97 syndicationItems.Values.CopyTo(array, arrayIndex);
98 }
99  
100 public bool Remove(SyndicationItem item)
101 {
102 return syndicationItems.Remove(item.Id);
103 }
104  
105 public IEnumerator<SyndicationItem> GetEnumerator()
106 {
107 return syndicationItems.Values.GetEnumerator();
108 }
109  
110 IEnumerator IEnumerable.GetEnumerator()
111 {
112 return syndicationItems.GetEnumerator();
113 }
114  
115 public event NotifyCollectionChangedEventHandler CollectionChanged;
116 }
11 office 117 }