wasSharpNET – Blame information for rev 27

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(
27 office 50 DateTimeOffset.UtcNow.Subtract(TimeSpan.FromDays(30))
51 .Subtract(defaultUpdateTime)) > 0)
8 office 52 .ForAll(o =>
53 {
54 if (!syndicationItems.ContainsKey(o.Id))
55 syndicationItems.Add(o.Id, o);
56 });
57 }
58 }, defaultUpdateTime, defaultUpdateTime);
59 }
60  
61 public ObservableSyndication(string URL, uint milliseconds) : this(URL, TimeSpan.FromMilliseconds(milliseconds))
62 {
63 }
64  
65 public TimeSpan Refresh
66 {
67 get { return _defaultUpdateTime; }
68 set
69 {
70 _defaultUpdateTime = value;
71 syndicationPoll.Change(_defaultUpdateTime, _defaultUpdateTime);
72 }
73 }
74  
75 public int Count => syndicationItems.Count;
76  
77 public bool IsReadOnly => false;
78  
79 public void Add(SyndicationItem item)
80 {
81 syndicationItems.Add(item.Id, item);
82 }
83  
84 public void Clear()
85 {
86 syndicationItems.Clear();
87 }
88  
89 public bool Contains(SyndicationItem item)
90 {
91 return syndicationItems.ContainsKey(item.Id) && syndicationItems[item.Id].Equals(item);
92 }
93  
94 public void CopyTo(SyndicationItem[] array, int arrayIndex)
95 {
96 syndicationItems.Values.CopyTo(array, arrayIndex);
97 }
98  
99 public bool Remove(SyndicationItem item)
100 {
101 return syndicationItems.Remove(item.Id);
102 }
103  
104 public IEnumerator<SyndicationItem> GetEnumerator()
105 {
106 return syndicationItems.Values.GetEnumerator();
107 }
108  
109 IEnumerator IEnumerable.GetEnumerator()
110 {
111 return syndicationItems.GetEnumerator();
112 }
113  
114 public event NotifyCollectionChangedEventHandler CollectionChanged;
115 }
27 office 116 }