Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Pages/Lecture/LecturePage.xaml.cs
2013-09-05 11:21:54 +02:00

258 lines
9.5 KiB
C#

//-----------------------------------------------------------------------
// <copyright file="LecturePage.xaml.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>stubbfel</author>
// <sience>13.06.2013</sience>
//----------------------------------------------------------------------
namespace CampusAppWP8.Pages.Lecture
{
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Navigation;
using CampusAppWP8.Api.Lecture;
using CampusAppWP8.Model.Lecture;
using CampusAppWP8.Model.Utility;
using CampusAppWP8.Resources;
using CampusAppWP8.Utility.Lui.MessageBoxes;
using Microsoft.Phone.Controls;
/// <summary>
/// Class for the LecturePage
/// </summary>
public partial class LecturePage : PhoneApplicationPage
{
#region Member
/// <summary>
/// actual LectureAPI
/// </summary>
private LectureApi api;
/// <summary>
/// List for the courses of the BTU
/// </summary>
/// <remarks>
/// need to be extend to full list
/// </remarks>
private LecturePageModel pageModel;
/// <summary>
/// flag for initialed page
/// </summary>
private bool init;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="LecturePage" /> class.
/// </summary>
public LecturePage()
{
this.InitializeComponent();
this.init = false;
this.LoadPageModel();
this.SetupListPickers();
this.init = true;
}
#endregion
#region methods
#region protected
/// <summary>
/// Methods overrides the OnNavigatedFrom-Method
/// </summary>
/// <param name="e">some NavigationEventArgs</param>
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (NavigationMode.Back == e.NavigationMode)
{
// delete all models
App.SaveToIsolatedStorage<LecturePageModel>(Constants.IsolatedStorage_LecturePageModel, null);
App.SaveToIsolatedStorage<LectureList>(Constants.IsolatedStorage_LectureModel, null);
}
else
{
this.StoreSelectedIndex();
App.SaveToIsolatedStorage<LecturePageModel>(Constants.IsolatedStorage_LecturePageModel, this.pageModel);
}
base.OnNavigatedFrom(e);
}
#endregion
#region private
/// <summary>
/// Load the PageModel
/// </summary>
private void LoadPageModel()
{
this.pageModel = new LecturePageModel();
this.pageModel.LoadLists();
}
/// <summary>
/// Method sets the ItemSource of the ListPickers
/// </summary>
private void SetupListPickers()
{
this.Course.ItemsSource = this.pageModel.CourseList.List;
this.Degree.ItemsSource = this.pageModel.DegreeList.List;
this.From.ItemsSource = this.pageModel.FromNumberList.List;
this.To.ItemsSource = this.pageModel.ToNumberList.List;
this.Semester.ItemsSource = this.pageModel.SemesterList.List;
// load values from last request
LecturePageModel lastPageModel = App.LoadFromIsolatedStorage<LecturePageModel>(Constants.IsolatedStorage_LecturePageModel);
if (lastPageModel != null)
{
this.SetLastSelectedIndex(lastPageModel);
}
this.SetSelectedIndex();
}
/// <summary>
/// Method set the last selected index of the ListPickers
/// </summary>
/// <param name="lastPageModel">Last PageModel</param>
private void SetLastSelectedIndex(LecturePageModel lastPageModel)
{
this.pageModel.SelectCourseIndex = lastPageModel.SelectCourseIndex;
this.pageModel.SelectDegreeIndex = lastPageModel.SelectDegreeIndex;
this.pageModel.SelectFromIndex = lastPageModel.SelectFromIndex;
this.pageModel.SelectToIndex = lastPageModel.SelectToIndex;
this.pageModel.SelectSemesterIndex = lastPageModel.SelectSemesterIndex;
}
/// <summary>
/// Method set the last selected index of the ListPickers
/// </summary>
private void SetSelectedIndex()
{
this.Course.SelectedIndex = this.pageModel.SelectCourseIndex;
this.Degree.SelectedIndex = this.pageModel.SelectDegreeIndex;
this.Semester.SelectedIndex = this.pageModel.SelectSemesterIndex;
this.From.SelectedIndex = this.pageModel.SelectFromIndex;
this.To.SelectedIndex = this.pageModel.SelectToIndex;
}
/// <summary>
/// Method store the actual selectIndex to the models
/// </summary>
private void StoreSelectedIndex()
{
this.pageModel.SelectCourseIndex = this.Course.SelectedIndex;
this.pageModel.SelectDegreeIndex = this.Degree.SelectedIndex;
this.pageModel.SelectSemesterIndex = this.Semester.SelectedIndex;
this.pageModel.SelectFromIndex = this.From.SelectedIndex;
this.pageModel.SelectToIndex = this.To.SelectedIndex;
}
/// <summary>
/// Method send a request to the Feed
/// </summary>
/// <remarks>
/// have to refactors
/// </remarks>
/// <param name="sender">sender of this event</param>
/// <param name="e"> events arguments</param>
private void SendRequest(object sender, RoutedEventArgs e)
{
this.api = new LectureApi();
this.api.OnLoaded += new LectureApi.OnIO(this.ApiIsReady);
this.api.OnFailedLoad += new LectureApi.OnFailed(this.ApiIsFail);
this.ProgressBar.Visibility = System.Windows.Visibility.Visible;
List<UrlParamModel> parameterList = this.CreateUrlParameter();
this.api.SetUriParams(parameterList);
this.api.LoadData();
}
/// <summary>
/// Method read the values from the inputs and put them in a list of parameters
/// </summary>
/// <returns>a list of parameters</returns>
private List<UrlParamModel> CreateUrlParameter()
{
ListPickerItemModel semester = (ListPickerItemModel)this.Semester.SelectedItem;
ListPickerItemModel degree = (ListPickerItemModel)this.Degree.SelectedItem;
ListPickerItemModel course = (ListPickerItemModel)this.Course.SelectedItem;
ListPickerItemModel from = (ListPickerItemModel)this.From.SelectedItem;
ListPickerItemModel to = (ListPickerItemModel)this.To.SelectedItem;
List<UrlParamModel> parameterList = new List<UrlParamModel>();
parameterList.Add(new UrlParamModel(Constants.ParamGetLecture_Semester, semester.Value));
parameterList.Add(new UrlParamModel(Constants.ParamGetLecture_Degree, degree.Value));
parameterList.Add(new UrlParamModel(Constants.ParamGetLecture_Course, course.Value));
parameterList.Add(new UrlParamModel(Constants.ParamGetLecture_From, from.Value));
parameterList.Add(new UrlParamModel(Constants.ParamGetLecture_To, to.Value));
return parameterList;
}
/// <summary>
/// Method will be execute if the feed is ready
/// </summary>
private void ApiIsReady()
{
App.SaveToIsolatedStorage<LectureList>(Constants.IsolatedStorage_LectureModel, this.api.Model);
this.ProgressBar.Visibility = System.Windows.Visibility.Collapsed;
Uri url = new Uri(Constants.PathLecture_ResultPage, UriKind.Relative);
NavigationService.Navigate(url);
}
/// <summary>
/// EventHandler for changed degree selection
/// </summary>
/// <param name="sender">sender object</param>
/// <param name="e">some args</param>
private void DegreeSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (!this.init)
{
return;
}
this.pageModel.SelectDegreeIndex = this.Degree.SelectedIndex;
this.pageModel.LoadFromNumberList();
this.From.ItemsSource = this.pageModel.FromNumberList.List;
}
/// <summary>
/// EventHandler for changed from selection
/// </summary>
/// <param name="sender">sender object</param>
/// <param name="e">some args</param>
private void FromSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (!this.init)
{
return;
}
this.pageModel.SelectDegreeIndex = this.Degree.SelectedIndex;
this.pageModel.SelectFromIndex = this.From.SelectedIndex;
this.pageModel.LoadToNumberList();
this.To.ItemsSource = this.pageModel.ToNumberList.List;
}
/// <summary>
/// Method will be execute if the feed is failed
/// </summary>
private void ApiIsFail()
{
MessageBoxResult result = MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoad);
this.ProgressBar.Visibility = Visibility.Collapsed;
}
#endregion
#endregion
}
}