wasSharpNET – Diff between revs 27 and 29

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