Files
win8phoneApp/CampusAppWP8/CampusAppWPortalLib8/Model/RSS/RSSChannelModel.cs
2013-10-15 15:16:22 +02:00

119 lines
3.9 KiB
C#

//-----------------------------------------------------------------------
// <copyright file="RSSChannelModel.cs" company="BTU/IIT">
// The MIT License (MIT). Copyright (c) 2013 BTU/IIT.
// </copyright>
// <author>fiedlchr</author>
// <date>15.10.2013</date>
// <summary>Implements the RSS channel model class</summary>
//-----------------------------------------------------------------------
namespace CampusAppWPortalLib8.Model.RSS
{
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Xml.Serialization;
/// <summary> Channel Model, which contains the RSS feed item list. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
public class RSSChannelModel
{
/// <summary> RSS feed information item list. </summary>
private ObservableCollection<RSSModel> item;
#region Constructor
/// <summary> Initializes a new instance of the <see cref="RSSChannelModel" /> class. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
public RSSChannelModel()
{
this.item = new ObservableCollection<RSSModel>();
this.item.CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnListChanged);
}
#endregion
#region Property
/// <summary> Gets or sets the RSS feed item list. </summary>
/// <value> The item. </value>
[XmlElement("item")]
public ObservableCollection<RSSModel> Item
{
get
{
return this.item;
}
set
{
if (value != this.item)
{
this.item = value;
}
}
}
#endregion
#region Method
/// <summary> Order by date. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="asc"> (Optional) the ascending. </param>
public void OrderByDate(bool asc = false)
{
this.item.CollectionChanged -= this.OnListChanged;
if (asc == false)
{
var sortedOC = from elem in this.item
orderby elem.DTTimestamp descending
select elem;
this.item = new ObservableCollection<RSSModel>(sortedOC);
this.FixIndex();
this.item.CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnListChanged);
}
else
{
var sortedOC = from elem in this.item
orderby elem.DTTimestamp ascending
select elem;
this.item = new ObservableCollection<RSSModel>(sortedOC);
this.FixIndex();
this.item.CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnListChanged);
}
}
/// <summary>
/// Is called when the item list has changed. Here used for the add event. Set the index of
/// the last list element.
/// </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="sender"> item list. </param>
/// <param name="e"> event args. </param>
private void OnListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
ObservableCollection<RSSModel> list = sender as ObservableCollection<RSSModel>;
list[list.Count - 1].Index = list.Count - 1;
}
}
/// <summary>Fixes the index property of the items.</summary>
/// <remarks>Fiedler, 15.10.2013.</remarks>
private void FixIndex()
{
for (int i = 0; i < this.item.Count(); i++)
{
this.item[i].Index = i;
}
}
#endregion
}
}