wasSharpNET – Blame information for rev 11

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 private string URL;
33  
34 public ObservableSyndication(string URL, TimeSpan defaultUpdateTime)
35 {
36 // Assign update variables.
37 _defaultUpdateTime = defaultUpdateTime;
38 this.URL = URL;
39  
40 // Forward the collection change event.
41 syndicationItems.CollectionChanged += (o, p) => { CollectionChanged?.Invoke(this, p); };
42  
43 // Poll the feed.
44 syndicationPoll = new Timer(() =>
45 {
46 using (var reader = XmlReader.Create(URL))
47 {
48 SyndicationFeed.Load(reader)?
49 .Items
50 .AsParallel()
51 .Where(o => o.PublishDate.CompareTo(
52 DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(30)).Subtract(defaultUpdateTime)) > 0)
53 .ForAll(o =>
54 {
55 if (!syndicationItems.ContainsKey(o.Id))
56 {
57 syndicationItems.Add(o.Id, o);
58 }
59 });
60 }
61 }, defaultUpdateTime, defaultUpdateTime);
62 }
63  
64 public ObservableSyndication(string URL, uint milliseconds) : this(URL, TimeSpan.FromMilliseconds(milliseconds))
65 {
66 }
67  
68 public TimeSpan Refresh
69 {
70 get { return _defaultUpdateTime; }
71 set
72 {
73 _defaultUpdateTime = value;
74 syndicationPoll.Change(_defaultUpdateTime, _defaultUpdateTime);
75 }
76 }
77  
78 public int Count => syndicationItems.Count;
79  
80 public bool IsReadOnly => false;
81  
82 public void Add(SyndicationItem item)
83 {
84 syndicationItems.Add(item.Id, item);
85 }
86  
87 public void Clear()
88 {
89 syndicationItems.Clear();
90 }
91  
92 public bool Contains(SyndicationItem item)
93 {
94 return syndicationItems.ContainsKey(item.Id) && syndicationItems[item.Id].Equals(item);
95 }
96  
97 public void CopyTo(SyndicationItem[] array, int arrayIndex)
98 {
99 syndicationItems.Values.CopyTo(array, arrayIndex);
100 }
101  
102 public bool Remove(SyndicationItem item)
103 {
104 return syndicationItems.Remove(item.Id);
105 }
106  
107 public IEnumerator<SyndicationItem> GetEnumerator()
108 {
109 return syndicationItems.Values.GetEnumerator();
110 }
111  
112 IEnumerator IEnumerable.GetEnumerator()
113 {
114 return syndicationItems.GetEnumerator();
115 }
116  
117 public event NotifyCollectionChangedEventHandler CollectionChanged;
118 }
11 office 119 }