Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Model/TimeTable/AppointmentModel.cs
Christian Fiedler 3ce33a803b bla
2013-09-23 14:00:05 +02:00

286 lines
8.8 KiB
C#

//-----------------------------------------------------------------------------
// <copyright file="AppointmentModel.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>fiedlchr</author>
// <sience>26.08.2013</sience>
//-----------------------------------------------------------------------------
namespace CampusAppWP8.Model.TimeTable
{
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using CampusAppWP8.Utility;
using CampusAppWP8.Utility.ICSProperties;
/// <summary>
/// Model for appointments.
/// </summary>
[XmlRoot("root")]
public class AppointmentModel
{
public static readonly double DAY_HOUR_SPACING = 50;
/// <summary>The Visual object.</summary>
private Rectangle rect = null;
private Canvas canvas = null;
private double offsetY = 0;
private double height = 0;
private ICalObject icalObj = null;
public delegate void OnAppointmentClick(AppointmentModel sender);
public event OnAppointmentClick OnClick = null;
/// <summary>Initializes a new instance of the <see cref="AppointmentModel" /> class. </summary>
public AppointmentModel()
{
this.rect = new Rectangle();
this.canvas = new Canvas();
this.canvas.DoubleTap += new EventHandler<System.Windows.Input.GestureEventArgs>(this.OnCanvasClick);
}
public AppointmentModel(string icsData) : this()
{
this.icalObj = ICSManager.ImportFromICS(icsData);
this.CalcYOffset();
this.CalcRect();
}
public double GetYOffset
{
get
{
return this.offsetY;
}
}
public Canvas GetCanvas()
{
return this.canvas;
}
public int IsDate(DateTime date, int daySpan = 0)
{
int retValue = -1;
ICalObject eventICal = this.GetVEventObj();
DTStart startTime = eventICal.GetProperty(ICSTag.DT_START) as DTStart;
if (startTime != null)
{
int toDaySpan = 0;
while ((toDaySpan <= daySpan) && (retValue == -1))
{
DateTime tempDT = date.AddDays(toDaySpan);
if (startTime.Value.Year.Equals(tempDT.Year)
&& startTime.Value.Month.Equals(tempDT.Month)
&& startTime.Value.Day.Equals(tempDT.Day))
{
retValue = toDaySpan;
}
toDaySpan++;
}
}
return retValue;
}
private void CalcRect()
{
this.CalcHeight();
this.rect.Width = 200;
this.rect.MinHeight = this.height;
this.rect.MaxHeight = 600;
this.rect.Height = this.height;
this.rect.StrokeThickness = 1;
this.rect.Stroke = new SolidColorBrush(Colors.DarkGray);
this.rect.Fill = new SolidColorBrush(Colors.Green);
this.rect.RadiusX = 10;
this.rect.RadiusY = 10;
this.canvas.Children.Add(this.rect);
if (this.icalObj != null)
{
ICalObject eventObj = this.GetVEventObj();
Summary title = eventObj.GetProperty(ICSTag.SUMMARY) as Summary;
TextBlock txtTitle = new TextBlock();
txtTitle.Text = title.Value;
txtTitle.FontSize = 12;
txtTitle.FontWeight = FontWeights.Bold;
txtTitle.TextWrapping = TextWrapping.Wrap;
txtTitle.Width = this.rect.Width - 4.0;
txtTitle.SetValue(Canvas.LeftProperty, 5.0);
txtTitle.SetValue(Canvas.TopProperty, 5.0);
/*
Description desc = eventObj.GetProperty(ICSTag.DESCRIPTION) as Description;
TextBlock txtDesc = new TextBlock();
txtDesc.Text = desc.Value;
txtDesc.FontSize = 12;
txtDesc.TextWrapping = TextWrapping.Wrap;
txtDesc.Width = this.rect.Width - 4.0;
txtDesc.SetValue(Canvas.LeftProperty, 2.0);
txtDesc.SetValue(Canvas.TopProperty, 20.0);
txtDesc.Height = this.height - 22.0;
*/
this.canvas.Children.Add(txtTitle);
//this.canvas.Children.Add(txtDesc);
}
}
private void CalcYOffset()
{
ICalObject eventObj = this.GetVEventObj();
DTStart startTimeObj = eventObj.GetProperty(ICSTag.DT_START) as DTStart;
if (startTimeObj == null)
{
throw new NullReferenceException();
}
DateTime startTimeValue = startTimeObj.Value;
this.offsetY = (startTimeValue.Hour * AppointmentModel.DAY_HOUR_SPACING) + (startTimeValue.Minute / 60.0 * AppointmentModel.DAY_HOUR_SPACING);
}
private void CalcHeight()
{
ICalObject eventObj = this.GetVEventObj();
DTStart startTimeObj = eventObj.GetProperty(ICSTag.DT_START) as DTStart;
DTEnd endTimeObj = eventObj.GetProperty(ICSTag.DT_END) as DTEnd;
if (startTimeObj == null
|| endTimeObj == null)
{
throw new NullReferenceException();
}
DateTime startTimeValue = startTimeObj.Value;
DateTime endTimeValue = endTimeObj.Value;
TimeSpan span = endTimeValue.Subtract(startTimeValue);
this.height = span.TotalHours * AppointmentModel.DAY_HOUR_SPACING;
if (this.height < (AppointmentModel.DAY_HOUR_SPACING / 4))
{
this.height = AppointmentModel.DAY_HOUR_SPACING / 4;
}
}
private ICalObject GetVEventObj()
{
ICalObject retValue = this.icalObj.GetProperty(ICSTag.VEVENT) as ICalObject;
if (retValue == null)
{
throw new NullReferenceException();
}
return retValue;
}
private void OnCanvasClick(object sender, EventArgs e)
{
if (this.OnClick != null)
{
this.OnClick(this);
}
}
// ------------------------------------------------------
public string Title
{
get
{
ICalObject eventICal = this.GetVEventObj();
Summary tempSum = eventICal.GetProperty(ICSTag.SUMMARY) as Summary;
return tempSum.Value;
}
}
public string Date
{
get
{
string retValue = string.Empty;
ICalObject eventICal = this.GetVEventObj();
DTStart startDT = eventICal.GetProperty(ICSTag.DT_START) as DTStart;
DTEnd endDT = eventICal.GetProperty(ICSTag.DT_END) as DTEnd;
if (startDT != null && endDT != null)
{
if (startDT.Value.Year.Equals(endDT.Value.Year) && startDT.Value.Month.Equals(endDT.Value.Month) && startDT.Value.Day.Equals(endDT.Value.Day))
{
retValue = string.Format("{0:d}, {0:HH:ss} - {1:HH:ss}", startDT.Value, endDT.Value);
}
else
{
retValue = string.Format("{0:d} {0:HH:ss} - {1:d} {1:HH:ss}", startDT.Value, endDT.Value);
}
}
return retValue;
}
}
public string Location
{
get
{
string retValue = string.Empty;
ICalObject eventICal = this.GetVEventObj();
Location loc = eventICal.GetProperty(ICSTag.LOCATION) as Location;
if (loc != null)
{
retValue = loc.Value;
}
return retValue;
}
}
public string Description
{
get
{
string retValue = string.Empty;
ICalObject eventICal = this.GetVEventObj();
Description desc = eventICal.GetProperty(ICSTag.DESCRIPTION) as Description;
if (desc != null)
{
retValue = desc.Value;
}
return retValue;
}
}
}
}