Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Pages/TimeTable/TimeTableWeek.xaml.cs
Christian Fiedler 8365b5b0dc TimeTable done
2013-11-12 17:19:47 +01:00

322 lines
12 KiB
C#

//-----------------------------------------------------------------------------
// <copyright file="TimeTableWeek.xaml.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>fiedlchr</author>
// <sience>30.09.2013</sience>
//-----------------------------------------------------------------------------
namespace CampusAppWP8.Pages.TimeTable
{
using System;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CampusAppWP8.Model.TimeTable;
using CampusAppWP8.Resources;
public partial class TimeTableWeek : PhoneApplicationPage
{
private static readonly int PIVOT_PAGES = 3;
private static readonly int PIVOT_PAGES_HALF_DOWN = 1;
private int lastSelectedIndex = 0;
private ObservableCollection<WeekViewPageItem> itemList = new ObservableCollection<WeekViewPageItem>();
public TimeTableWeek()
{
this.InitializeComponent();
TimeTable.InitFeed();
DateTime firstDay = this.GetFirstDayOfWeek(DateTime.Now).AddDays(-7 * PIVOT_PAGES_HALF_DOWN);
for (int i = 0; i < PIVOT_PAGES; i++)
{
int weekNr = this.GetWeekNumber(firstDay);
string weekStr = ((weekNr % 2) != 0) ? "A" : "B";
WeekViewPageItem newItem = new WeekViewPageItem(firstDay, weekStr, weekNr, 6);
this.itemList.Add(newItem);
firstDay = firstDay.AddDays(7);
}
ApplicationBarIconButton dayViewBtn = new ApplicationBarIconButton();
dayViewBtn.Text = AppResources.DayView;
dayViewBtn.IconUri = new Uri(Icons.Info, UriKind.Relative);
dayViewBtn.Click += new EventHandler(this.OnClickDayView);
ApplicationBar.Buttons.Add(dayViewBtn);
ApplicationBarIconButton todayBtn = new ApplicationBarIconButton();
todayBtn.Text = AppResources.ToDay;
todayBtn.IconUri = new Uri(Icons.Info, UriKind.Relative);
todayBtn.Click += new EventHandler(this.OnClickToday);
ApplicationBar.Buttons.Add(todayBtn);
ApplicationBarIconButton propBtn = new ApplicationBarIconButton();
propBtn.Text = AppResources.Properties;
propBtn.IconUri = new Uri(Icons.Info, UriKind.Relative);
propBtn.Click += new EventHandler(this.OnClickProperties);
ApplicationBar.Buttons.Add(propBtn);
ApplicationBarIconButton addBtn = new ApplicationBarIconButton();
addBtn.Text = AppResources.Add;
addBtn.IconUri = new Uri(Icons.Add, UriKind.Relative);
addBtn.Click += new EventHandler(this.OnClickCreate);
ApplicationBar.Buttons.Add(addBtn);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
TimeTable.Feed.Model.Appointments.CollectionChanged -= this.OnListChanged;
}
else
{
this.ThePivot.ItemsSource = this.itemList;
this.lastSelectedIndex = PIVOT_PAGES_HALF_DOWN;
this.ThePivot.SelectedIndex = PIVOT_PAGES_HALF_DOWN;
this.CheckAppointments();
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
TimeTable.Feed.SaveData();
}
private DateTime GetFirstDayOfWeek(DateTime dayInWeek)
{
DateTime retValue = dayInWeek.Date;
while(retValue.DayOfWeek.Equals(DayOfWeek.Monday) == false)
{
retValue = retValue.AddDays(-1);
}
return retValue;
}
private int GetWeekNumber(DateTime date)
{
int retValue = 0;
var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
retValue = cal.GetWeekOfYear(date, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
return retValue;
}
private int GetDayOfWeekIndex(DateTime dayInWeek)
{
int retValue = -1;
switch (dayInWeek.DayOfWeek)
{
case DayOfWeek.Monday: retValue = 0; break;
case DayOfWeek.Tuesday: retValue = 1; break;
case DayOfWeek.Wednesday: retValue = 2; break;
case DayOfWeek.Thursday: retValue = 3; break;
case DayOfWeek.Friday: retValue = 4; break;
case DayOfWeek.Saturday: retValue = 5; break;
case DayOfWeek.Sunday: retValue = 6; break;
}
return retValue;
}
private void OnClickDayView(object sender, EventArgs e)
{
Uri url = new Uri(Constants.PathTimeTable_Day, UriKind.Relative);
Page page = App.RootFrame.Content as Page;
page.NavigationService.Navigate(url);
}
private void OnClickToday(object sender, EventArgs e)
{
DateTime firstDay = this.GetFirstDayOfWeek(DateTime.Now);
int testIndex = -1;
for (int i = 0; i < this.itemList.Count; i++)
{
if (this.itemList[i].FromDT.Equals(firstDay) == true)
{
testIndex = i;
}
}
if (testIndex < 0)
{
this.ThePivot.SelectedIndex = PIVOT_PAGES_HALF_DOWN;
firstDay = firstDay.AddDays(-7 * PIVOT_PAGES_HALF_DOWN);
for (int i = 0; i < PIVOT_PAGES; i++)
{
this.itemList[i].FromDT = firstDay;
this.itemList[i].ToDT = this.itemList[i].FromDT.AddDays(6);
this.itemList[i].WeekNr = this.GetWeekNumber(this.itemList[i].FromDT);
this.itemList[i].WeekStr = ((this.itemList[i].WeekNr % 2) != 0) ? "A" : "B";
this.itemList[i].AppointmentList.Clear();
this.CheckAppointments(i);
firstDay = firstDay.AddDays(7);
}
}
else
{
this.ThePivot.SelectedIndex = testIndex;
}
}
private void OnClickProperties(object sender, EventArgs e)
{
Uri url = new Uri(Constants.PathTimeTable_Properties, UriKind.Relative);
Page page = App.RootFrame.Content as Page;
page.NavigationService.Navigate(url);
}
private void OnClickCreate(object sender, EventArgs e)
{
TimeTable.Feed.Model.Appointments.CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnListChanged);
Uri url = new Uri(Constants.PathTimeTable_AppointmentEdit, UriKind.Relative);
Page page = App.RootFrame.Content as Page;
page.NavigationService.Navigate(url);
}
private void OnPivotSelectionChanged(object sender, SelectionChangedEventArgs e)
{
int delta = this.ThePivot.SelectedIndex - this.lastSelectedIndex;
if (delta < -1) delta = 1;
else if (delta > 1) delta = -1;
int index = (this.ThePivot.SelectedIndex + (delta * PIVOT_PAGES_HALF_DOWN) + PIVOT_PAGES) % PIVOT_PAGES;
if (delta == 0)
{
return;
}
this.itemList[index].FromDT = this.itemList[this.ThePivot.SelectedIndex].FromDT.AddDays((7 * delta));
this.itemList[index].ToDT = this.itemList[this.ThePivot.SelectedIndex].ToDT.AddDays((7 * delta));
this.itemList[index].WeekNr = this.GetWeekNumber(this.itemList[index].FromDT);
this.itemList[index].WeekStr = ((this.itemList[index].WeekNr % 2) != 0) ? "A" : "B";
this.itemList[index].AppointmentList.Clear();
this.CheckAppointments(index);
this.lastSelectedIndex = this.ThePivot.SelectedIndex;
}
private void OnAppointmentClick(AppointmentModel model)
{
int index = TimeTable.Feed.Model.Appointments.IndexOf(model);
if (index >= 0)
{
TimeTable.Feed.Model.Appointments.CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnListChanged);
string urlString = Constants.PathTimeTable_Appointment + "?" + Constants.Param_Appointment_Index + "=" + index;
Uri url = new Uri(urlString, UriKind.Relative);
Page page = App.RootFrame.Content as Page;
page.NavigationService.Navigate(url);
}
}
private void OnListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
AppointmentModel tempModel = null;
if (e.Action == NotifyCollectionChangedAction.Add)
{
tempModel = e.NewItems[0] as AppointmentModel;
for (int i = 0; i < PIVOT_PAGES; i++)
{
if (tempModel.IsDate(this.itemList[i].FromDT, 4) >= 0)
{
tempModel.OnClick += this.OnAppointmentClick;
this.itemList[i].AppointmentList.Add(tempModel);
}
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
tempModel = e.OldItems[0] as AppointmentModel;
for (int i = 0; i < PIVOT_PAGES; i++)
{
if (this.itemList[i].AppointmentList.IndexOf(tempModel) >= 0)
{
tempModel.OnClick -= this.OnAppointmentClick;
this.itemList[i].AppointmentList.Remove(tempModel);
}
}
}
}
private void CheckAppointments(int pageIndex = -1)
{
if (TimeTable.Feed == null)
{
throw new NullReferenceException("feed == null");
}
else if (TimeTable.Feed.Model == null)
{
throw new NullReferenceException("model == null");
}
else if (TimeTable.Feed.Model.Appointments == null)
{
throw new NullReferenceException("Appointments == null");
}
if (pageIndex < 0)
{
for (int m = 0; m < TimeTable.Feed.Model.Appointments.Count; m++)
{
AppointmentModel temp = TimeTable.Feed.Model.Appointments[m];
for (int i = 0; i < PIVOT_PAGES; i++)
{
if (temp.IsDate(this.itemList[i].FromDT, 4) >= 0)
{
temp.OnClick += this.OnAppointmentClick;
this.itemList[i].AppointmentList.Add(temp);
}
}
}
}
else
{
for (int m = 0; m < TimeTable.Feed.Model.Appointments.Count; m++)
{
AppointmentModel temp = TimeTable.Feed.Model.Appointments[m];
if (temp.IsDate(this.itemList[pageIndex].FromDT, 4) >= 0)
{
temp.OnClick += this.OnAppointmentClick;
this.itemList[pageIndex].AppointmentList.Add(temp);
}
}
}
}
}
}