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