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