From b9f268d594d6c180742b5de345c981941200b329 Mon Sep 17 00:00:00 2001 From: Christian Fiedler Date: Fri, 19 Jul 2013 17:15:47 +0200 Subject: [PATCH 1/2] #103 and fixes in events and news --- .../Model/Departments/DepartmentModel.cs | 2 +- .../Model/Departments/FacultyModel.cs | 2 +- CampusAppWP8/CampusAppWP8/Model/MainModel.cs | 1 - .../CampusAppWP8/Model/RSS/RSSChannelModel.cs | 24 ++- .../CampusAppWP8/Pages/Events/EventPage.xaml | 15 +- .../Pages/Events/EventPage.xaml.cs | 85 ++++++----- .../CampusAppWP8/Pages/News/NewsPage.xaml | 15 +- .../CampusAppWP8/Pages/News/NewsPage.xaml.cs | 75 +++++----- .../CampusAppWP8/Utility/Utilities.cs | 138 +++++++++++++++++- 9 files changed, 253 insertions(+), 104 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Model/Departments/DepartmentModel.cs b/CampusAppWP8/CampusAppWP8/Model/Departments/DepartmentModel.cs index 5b7db58d..c2256507 100644 --- a/CampusAppWP8/CampusAppWP8/Model/Departments/DepartmentModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/Departments/DepartmentModel.cs @@ -72,7 +72,7 @@ namespace CampusAppWP8.Model.Departments /// Check if the content of the faculty lists hast changed since the /// last call of this function. /// - /// true, if changes happend since last request, otherwise false + /// true, if changes happen since last request, otherwise false public bool HasChanged() { bool retValue = false; diff --git a/CampusAppWP8/CampusAppWP8/Model/Departments/FacultyModel.cs b/CampusAppWP8/CampusAppWP8/Model/Departments/FacultyModel.cs index 1c3419ef..f7b7407c 100644 --- a/CampusAppWP8/CampusAppWP8/Model/Departments/FacultyModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/Departments/FacultyModel.cs @@ -221,7 +221,7 @@ namespace CampusAppWP8.Model.Departments /// /// Return true if there were changes in the chair list, otherwise false. /// - /// when true, the hasChanged flag will be reseted + /// when true, the hasChanged flag will be reset /// true, when changed, otherwise false public bool HasChanged(bool reset = true) { diff --git a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs index d2346440..c37f74cb 100644 --- a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs @@ -216,7 +216,6 @@ namespace CampusAppWP8 this.api.HttpGet(this.httpApiUri, this.OnLoadDataComplete); } - } /// diff --git a/CampusAppWP8/CampusAppWP8/Model/RSS/RSSChannelModel.cs b/CampusAppWP8/CampusAppWP8/Model/RSS/RSSChannelModel.cs index 52c1d499..fc8a8afe 100644 --- a/CampusAppWP8/CampusAppWP8/Model/RSS/RSSChannelModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/RSS/RSSChannelModel.cs @@ -8,6 +8,7 @@ namespace CampusAppWP8.Model.RSS { using System.Collections.ObjectModel; + using System.Collections.Specialized; using System.Xml.Serialization; /// @@ -26,6 +27,7 @@ namespace CampusAppWP8.Model.RSS public RSSChannelModel() { this.item = new ObservableCollection(); + this.item.CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnListChanged); } /// @@ -44,13 +46,25 @@ namespace CampusAppWP8.Model.RSS if (value != this.item) { this.item = value; - int i = 0; - foreach (RSSModel rssItem in this.item) - { - rssItem.Index = i++; - } } } } + + /// + /// Is called when the item list has changed. + /// Here used for the add event. + /// Set the index of the last list element. + /// + /// item list + /// event args + private void OnListChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (e.Action == NotifyCollectionChangedAction.Add) + { + ObservableCollection list = sender as ObservableCollection; + + list[list.Count - 1].Index = list.Count - 1; + } + } } } diff --git a/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml b/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml index 0f72f4bf..3a8919b5 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml +++ b/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml @@ -20,7 +20,7 @@ - + @@ -29,11 +29,10 @@ - + - @@ -47,14 +46,16 @@ - - - private bool isSourceSet = false; + /// + /// To store the index of the last selected PivotItem. + /// + private int lastSelectedIndex = -1; + /// /// Initializes a new instance of the class. /// public NewsPage() { this.InitializeComponent(); + + ApplicationBarIconButton linkBtn = new ApplicationBarIconButton(); + linkBtn.IconUri = new Uri(Icons.Link, UriKind.Relative); + linkBtn.Text = AppResources.NewsLinkBtn; + linkBtn.Click += new EventHandler(this.NewsLink_Click); + ApplicationBar.Buttons.Add(linkBtn); } /// @@ -53,7 +67,7 @@ namespace CampusAppWP8.Pages.News string pivotIndex = string.Empty; // Navigate to the selected pivotitem - if (NavigationContext.QueryString.TryGetValue("pivotindex", out pivotIndex)) + if (NavigationContext.QueryString.TryGetValue(Constants.ParamPivotIndex, out pivotIndex)) { int pivotIndexInt = int.Parse(pivotIndex); @@ -70,15 +84,22 @@ namespace CampusAppWP8.Pages.News } /// - /// On clicking the home button (lower left). - /// Navigate back to the news index page. + /// Called when the index of the selected PivotItem is changed. + /// Set the text Grid to visible and the WebBrowser to collapsed. /// - /// clicked button + /// parent pivot object /// event args - private void NewsHome_Click(object sender, RoutedEventArgs e) + private void OnPivotSelectionChange(object sender, SelectionChangedEventArgs e) { - // Navigate back to the news index page - NavigationService.GoBack(); + int selIndex = (sender as Pivot).SelectedIndex; + + if (this.lastSelectedIndex > -1) + { + Utilities.SetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsWebBrowser", Visibility.Collapsed, this.lastSelectedIndex); + Utilities.SetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsTextGrid", Visibility.Visible, this.lastSelectedIndex); + } + + this.lastSelectedIndex = selIndex; } /// @@ -88,41 +109,17 @@ namespace CampusAppWP8.Pages.News /// /// clicked button /// event args - private void NewsLink_Click(object sender, RoutedEventArgs e) + private void NewsLink_Click(object sender, EventArgs e) { - FrameworkElement fe = sender as FrameworkElement; - - // Open the webbrowser - Grid grfe = fe.Parent as Grid; - FrameworkElement newsTextGrid = null; - FrameworkElement newsWeb = null; - - foreach (FrameworkElement tempElem in grfe.Children) + if (Visibility.Visible == Utilities.GetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsTextGrid", this.NewsPivot.SelectedIndex)) { - if (tempElem.Name == "NewsTextGrid") - { - newsTextGrid = tempElem; - } - else if (tempElem.Name == "WebBrowser") - { - newsWeb = tempElem; - } + Utilities.SetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsTextGrid", Visibility.Collapsed, this.NewsPivot.SelectedIndex); + Utilities.SetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsWebBrowser", Visibility.Visible, this.NewsPivot.SelectedIndex); } - - if ((newsTextGrid != null) - && (newsWeb != null)) + else { - if (newsTextGrid.Visibility == Visibility.Visible) - { - newsTextGrid.Visibility = Visibility.Collapsed; - newsWeb.Visibility = Visibility.Visible; - (newsWeb as WebBrowser).Navigate(new Uri(fe.Tag.ToString(), UriKind.Absolute)); - } - else - { - newsWeb.Visibility = Visibility.Collapsed; - newsTextGrid.Visibility = Visibility.Visible; - } + Utilities.SetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsWebBrowser", Visibility.Collapsed, this.NewsPivot.SelectedIndex); + Utilities.SetElementVisibility(this.NewsPivot, "NewsParentGrid", "NewsTextGrid", Visibility.Visible, this.NewsPivot.SelectedIndex); } } } diff --git a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs index 2c700413..f324d9da 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs @@ -8,19 +8,61 @@ namespace CampusAppWP8.Utility { using System; - + using System.Collections.Generic; + using System.Linq; + using System.Windows; + using System.Windows.Controls; + using System.Windows.Media; + + /// + /// Collection of utility functions. + /// public static class Utilities { + /// + /// Comparison types. + /// public enum DifferenceType { + /// + /// Compare Equality. + /// Equal, + + /// + /// Compare if less. + /// Less, + + /// + /// Compare equality or less. + /// LessEqual, + + /// + /// Compare greater. + /// Greater, + + /// + /// Compare equality or greater. + /// GreaterEqual - }; - + } + /// + /// Compares the difference between a specified DateTime and Now + /// and the specified time difference (in Days). + /// + /// When type is DifferenceType.Less, the timespan between Now and date + /// is 5.0 (days) and totalDiff is 7.0, the function will return true, + /// because the timespan is Less then 7.0 . + /// + /// + /// comparison type + /// date to check + /// difference to check + /// true, if the comparison condition do not fail public static bool DayDifference(DifferenceType type, DateTime date, double totalDiff) { bool retValue = false; @@ -53,5 +95,95 @@ namespace CampusAppWP8.Utility return retValue; } + + /// + /// Return a list of child UIElements of the root object with the specified element name. + /// + /// root object + /// name of the element(s) + /// list of elements + public static List GetChild(DependencyObject rootObj, string elemName) + { + List retValue = new List(); + + for (int k = 0; k < VisualTreeHelper.GetChildrenCount(rootObj); k++) + { + var child = VisualTreeHelper.GetChild(rootObj, k); + + if ((child as FrameworkElement).Name.Equals(elemName)) + { + retValue.Add(child); + } + + var ret = retValue.Concat(GetChild(child as DependencyObject, elemName)); + retValue = ret.ToList(); + } + + return retValue; + } + + /// + /// Search for a UIElement with the specified name in the parent Grid and set its visibility. + /// + /// root object + /// name of the parent grid + /// name of the UIElement + /// new visibility property state + /// index of the element in the parent child list + public static void SetElementVisibility(DependencyObject rootObj, string parentGridName, string elemName, Visibility vis, int index = 0) + { + List l = Utilities.GetChild(rootObj, parentGridName); + Grid parentGrid = l[index] as Grid; + FrameworkElement elem = null; + + foreach (FrameworkElement tempElem in parentGrid.Children) + { + if (tempElem.Name == elemName) + { + elem = tempElem; + } + } + + if (elem != null) + { + elem.Visibility = vis; + } + } + + /// + /// Return the visibility property of a UIElement which is a child object of the specified parent grid element. + /// + /// root object + /// name of the parent grid + /// name of the element + /// index of the element in the child list of the parent + /// visibility state + public static Visibility GetElementVisibility(DependencyObject rootObj, string parentGridName, string elemName, int index = 0) + { + Visibility retValue; + + List l = Utilities.GetChild(rootObj, parentGridName); + Grid parentGrid = l[index] as Grid; + FrameworkElement elem = null; + + foreach (FrameworkElement tempElem in parentGrid.Children) + { + if (tempElem.Name == elemName) + { + elem = tempElem; + } + } + + if (elem != null) + { + retValue = elem.Visibility; + } + else + { + throw new NotImplementedException("Could not find a UIElement with name (" + elemName + ")"); + } + + return retValue; + } } } \ No newline at end of file From 9ebeaa81478aa41b4cd00683edf9a3b5a3c6fe55 Mon Sep 17 00:00:00 2001 From: Christian Fiedler Date: Fri, 19 Jul 2013 17:16:36 +0200 Subject: [PATCH 2/2] const