From 1aaa934b0eb036fcb14d14475a4ca9876f2cf544 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Mon, 14 Oct 2013 17:13:28 +0200 Subject: [PATCH 1/4] add determine campus offline --- .../Pages/Mensa/MensaPage.xaml.cs | 51 +++- .../CampusAppWP8/Utility/Utilities.cs | 245 ++++++++++++------ 2 files changed, 212 insertions(+), 84 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Pages/Mensa/MensaPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Mensa/MensaPage.xaml.cs index 2f0ddf64..0ee00176 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Mensa/MensaPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Mensa/MensaPage.xaml.cs @@ -19,6 +19,7 @@ namespace CampusAppWP8.Pages.Mensa using CampusAppWPortalLib8.Model.Mensa; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; + using CampusAppWPortalLib8.Model.Settings; /// /// Class for the MensaPage @@ -50,7 +51,7 @@ namespace CampusAppWP8.Pages.Mensa /// /// API for Localization /// - private CampusSpsApi campusApi; + // private CampusSpsApi campusApi; /// /// Flag for forcing webLoad @@ -129,7 +130,7 @@ namespace CampusAppWP8.Pages.Mensa /// /// Method will be execute if the SPSAPI is ready /// - private void SpsApiIsReady() + /*private void SpsApiIsReady() { if (this.Dispatcher != null) { @@ -139,30 +140,56 @@ namespace CampusAppWP8.Pages.Mensa { this.InitializeFeed(this.campusApi.GetCampus()); } - } + }*/ /// /// Method will be execute if the SPSAPI is failed /// - private void SpsApiIsFail() + /*private void SpsApiIsFail() { if (this.Dispatcher != null) { this.Dispatcher.BeginInvoke(new Action(() => MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorCampusLoc))); this.Dispatcher.BeginInvoke(new Action(() => this.InitializeFeed(Settings.UserProfil.DefaultCampus))); } - } + }*/ /// /// Method determine campus and load data from correct feed /// private void DeterminCurrentCampusAndLoadFeed() { - this.campusApi = new CampusSpsApi(); + Campus mensaCampus = Utilities.DetermineCampus(); + + if (this.Dispatcher != null) + { + if (mensaCampus == Campus.UserSettingCampus) + { + this.Dispatcher.BeginInvoke(new Action(() => this.InitializeFeed(Settings.UserProfil.DefaultCampus))); + } + else + { + this.Dispatcher.BeginInvoke(new Action(() => this.InitializeFeed(mensaCampus))); + } + + } + else + { + if (mensaCampus == Campus.UserSettingCampus) + { + this.InitializeFeed(Settings.UserProfil.DefaultCampus); + } + else + { + this.InitializeFeed(mensaCampus); + } + } + + /* this.campusApi = new CampusSpsApi(); this.campusApi.OnLoaded += new SpsApi.OnIO(this.SpsApiIsReady); this.campusApi.OnFailedLoad += new SpsApi.OnFailed(this.SpsApiIsFail); this.campusApi.SetupCurrentCampusRequest(); - this.campusApi.LoadData(); + this.campusApi.LoadData();*/ } /// @@ -193,7 +220,7 @@ namespace CampusAppWP8.Pages.Mensa /// private void InitializeFeed() { - if (Settings.AppSetting.GeoWatchEnable && Settings.AppSetting.UniNetwork) + if (Settings.AppSetting.GeoWatchEnable) { Thread thread = new Thread(new ThreadStart(this.DeterminCurrentCampusAndLoadFeed)); thread.Start(); @@ -382,8 +409,10 @@ namespace CampusAppWP8.Pages.Mensa } } - #endregion - + /// Event handler. Called by MenuItem for click events. + /// Stubbfel, 14.10.2013. + /// button object. + /// Routed event information. private void MenuItem_Click(object sender, RoutedEventArgs e) { FrameworkElement frameelement = sender as FrameworkElement; @@ -396,5 +425,7 @@ namespace CampusAppWP8.Pages.Mensa } #endregion + + #endregion } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs index 5982d25f..da03d882 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/Utilities.cs @@ -1,36 +1,49 @@ -//----------------------------------------------------------------------------- +//----------------------------------------------------------------------- // -// Company copyright tag. +// The MIT License (MIT). Copyright (c) 2013 BTU/IIT. // -// fiedlchr -// 16.07.2013 -//----------------------------------------------------------------------------- +// Stubbfel +// 14.10.2013 +// Implements the utilities class +//----------------------------------------------------------------------- namespace CampusAppWP8.Utility { using System; using System.Collections.Generic; using System.Device.Location; using System.Globalization; - using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using CampusAppWP8.Resources; - using Microsoft.Phone.Net.NetworkInformation; - using CampusAppWPortalLib8.Utility; using CampusAppWPortalLib8.Model; - using Microsoft.Phone.Shell; + using CampusAppWPortalLib8.Model.Settings; + using CampusAppWPortalLib8.Utility; + using Microsoft.Phone.Net.NetworkInformation; - /// - /// Collection of utility functions. - /// + /// Collection of utility functions. + /// Stubbfel, 14.10.2013. public static class Utilities { + #region member + + /// The north campus of cottbus. + private static GeoCoordinate northCB = new GeoCoordinate(51.77670359509875,14.293908825617); + + /// The middle campus of cottbus. + private static GeoCoordinate midCB = new GeoCoordinate(51.76737987049448,14.324056352976152); + + /// The south campus of cottbus. + private static GeoCoordinate southCB = new GeoCoordinate(51.72668339740452,14.319497377197282); + + /// The middle campus of senftenberg. + private static GeoCoordinate midSFB = new GeoCoordinate(51.522217168257356,13.986618441187698); + + #endregion #region Enums - /// - /// Comparison types. - /// + /// Comparison types. + /// Stubbfel, 14.10.2013. public enum DifferenceType { /// @@ -64,18 +77,18 @@ namespace CampusAppWP8.Utility #region Method /// - /// 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 . - /// + /// Compares the difference between a specified DateTime and Now and the specified time + /// difference (in Days). /// - /// comparison type - /// date to check - /// difference to check - /// true, if the comparison condition do not fail + /// Stubbfel, 14.10.2013. + /// comparison type. + /// date to check. + /// difference to check. + /// true, if the comparison condition do not fail. + /// + /// 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 . + /// public static bool DayDifference(DifferenceType type, DateTime date, double totalDiff) { bool retValue = false; @@ -110,11 +123,12 @@ namespace CampusAppWP8.Utility } /// - /// Return a list of child UIElements of the root object with the specified element name. + /// 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 + /// Stubbfel, 14.10.2013. + /// root object. + /// name of the element(s) + /// list of elements. public static List GetChild(DependencyObject rootObj, string elemName) { List retValue = new List(); @@ -136,14 +150,16 @@ namespace CampusAppWP8.Utility return retValue; } - /// - /// Return the element. - /// - /// - /// - /// - /// - /// + /// Return the element. + /// Stubbfel, 14.10.2013. + /// + /// Thrown when the index is outside the required range. + /// + /// . + /// . + /// . + /// (Optional) + /// The element. public static FrameworkElement GetElement(DependencyObject rootObj, string parentGridName, string elemName, int index = 0) { List l = Utilities.GetChild(rootObj, parentGridName); @@ -168,13 +184,16 @@ namespace CampusAppWP8.Utility } /// - /// Search for a UIElement with the specified name in the parent Grid and set its visibility. + /// 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 + /// Stubbfel, 14.10.2013. + /// root object. + /// name of the parent grid. + /// name of the UIElement. + /// new visibility property state. + /// + /// (Optional) index of the element in the parent child list. + /// public static void SetElementVisibility(DependencyObject rootObj, string parentGridName, string elemName, Visibility vis, int index = 0) { FrameworkElement elem = GetElement(rootObj, parentGridName, elemName, index); @@ -186,18 +205,25 @@ namespace CampusAppWP8.Utility } /// - /// Return the visibility property of a UIElement which is a child object of the specified parent grid element. + /// 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 + /// Stubbfel, 14.10.2013. + /// + /// Thrown when the requested operation is unimplemented. + /// + /// root object. + /// name of the parent grid. + /// name of the element. + /// + /// (Optional) 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; FrameworkElement elem = GetElement(rootObj, parentGridName, elemName, index); - + if (elem != null) { retValue = elem.Visibility; @@ -210,10 +236,9 @@ namespace CampusAppWP8.Utility return retValue; } - /// - /// Method determine the current position of the phone - /// - /// the position of the phone + /// Method determine the current position of the phone. + /// Stubbfel, 14.10.2013. + /// the position of the phone. public static GeoPosition DetermineCurrentPosition() { if (!Settings.AppSetting.GeoWatchEnable) @@ -240,19 +265,18 @@ namespace CampusAppWP8.Utility return geoposition; } - /// - /// Method set current positon to Zero - /// + /// Method set current positon to Zero. + /// Stubbfel, 14.10.2013. public static void SetGeoPositionToZero() { - string time = DateTime.Now.Ticks.ToString(); + string time = DateTime.Now.Ticks.ToString(); App.SaveToAppState(Constants.GeoWatch_CurrentPosition_Lat, "0"); App.SaveToAppState(Constants.GeoWatch_CurrentPosition_Long, "0"); App.SaveToAppState(Constants.GeoWatch_CurrentPosition_Time, time); } - /// - /// Method determine and store the current position of the phone - /// + + /// Method determine and store the current position of the phone. + /// Stubbfel, 14.10.2013. public static void DetermineAndStoreCurrentPositionForce() { GeoPosition geoposition = Utilities.DetermineCurrentPosition(); @@ -276,8 +300,9 @@ namespace CampusAppWP8.Utility } /// - /// Method determine and store the current position of the phone, in 15 min interval + /// Method determine and store the current position of the phone, in 15 min interval. /// + /// Stubbfel, 14.10.2013. public static void DetermineAndStoreCurrentPosition() { string lat = App.LoadFromAppState(Constants.GeoWatch_CurrentPosition_Lat); @@ -303,9 +328,12 @@ namespace CampusAppWP8.Utility } } - /// Query if the phone is in the uni network. Method compares only Network name and Description! - /// Stubbfel, 26.08.2013. - /// true if uni network is available, false if not. + /// + /// Query if the phone is in the uni network. Method compares only Network name and + /// Description! + /// + /// Stubbfel, 26.08.2013. + /// true if uni network is available, false if not. public static bool IsUniNetworkAvailable() { NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList(); @@ -324,9 +352,9 @@ namespace CampusAppWP8.Utility return false; } - /// Queries if a wifi is available. - /// Stubbfel, 26.08.2013. - /// true if a wifi is available, false if not. + /// Queries if a wifi is available. + /// Stubbfel, 26.08.2013. + /// true if a wifi is available, false if not. public static bool IsWifiAvailable() { NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList(); @@ -343,10 +371,10 @@ namespace CampusAppWP8.Utility return false; } - /// Gets load modus. Is check if the only Wifi option is active - /// Stubbfel, 27.08.2013. - /// Generic type parameter. - /// The load modus< t> + /// Gets load modus. Is check if the only Wifi option is active. + /// Stubbfel, 27.08.2013. + /// Generic type parameter. + /// The load modus< t> public static ForceType GetLoadModus() { if (Settings.AppSetting.OnlyWifi && !Settings.AppSetting.WifiEnable) @@ -359,7 +387,11 @@ namespace CampusAppWP8.Utility } } - public static bool IsRoomId(string placeId) + /// Query if 'placeId' is room identifier. + /// Stubbfel, 14.10.2013. + /// Identifier for the place. + /// true if room identifier, false if not. + public static bool IsRoomId(string placeId) { if (Wp8StringManager.IsDigitsOnly(placeId) && placeId.Length > 7) { @@ -368,6 +400,71 @@ namespace CampusAppWP8.Utility return false; } + /// Gets the determined campus. + /// Stubbfel, 14.10.2013. + /// The Campus. + public static Campus DetermineCampus() + { + Campus result = Campus.UserSettingCampus; + + Utilities.DetermineAndStoreCurrentPosition(); + double lat; + double log; + if (!double.TryParse(App.LoadFromAppState(Constants.GeoWatch_CurrentPosition_Lat), NumberStyles.Number, CultureInfo.InvariantCulture, out lat) + || !double.TryParse(App.LoadFromAppState(Constants.GeoWatch_CurrentPosition_Long), NumberStyles.Number, CultureInfo.InvariantCulture, out log)) + { + return result; + } + + Point currentPoint = new Point(log, lat); + + + double minDistance = Utilities.CalcDistance(currentPoint, new Point(Utilities.northCB.Longitude, Utilities.northCB.Latitude)); + + if (minDistance < 0.01) + { + result = Campus.CB_NORTH; + } + double tmpDistance = Utilities.CalcDistance(currentPoint, new Point(Utilities.midCB.Longitude, Utilities.midCB.Latitude)); + + if (tmpDistance < 0.01 && tmpDistance < minDistance) + { + minDistance = tmpDistance; + result = Campus.CB_MAIN; + } + + tmpDistance = Utilities.CalcDistance(currentPoint, new Point(Utilities.southCB.Longitude, Utilities.southCB.Latitude)); + + if (tmpDistance < 0.01 && tmpDistance < minDistance) + { + minDistance = tmpDistance; + result = Campus.CB_SOUTH; + } + + tmpDistance = Utilities.CalcDistance(currentPoint, new Point(Utilities.midSFB.Longitude, Utilities.midSFB.Latitude)); + + if (tmpDistance < 0.01 && tmpDistance < minDistance) + { + minDistance = tmpDistance; + result = Campus.SFB_MAIN; + } + + return result; + } + + /// Calculates the distance. + /// Stubbfel, 14.10.2013. + /// the source point. + /// the destination Point. + /// The calculated distance. + public static double CalcDistance(Point src, Point dst) + { + double xPow = Math.Pow(dst.X - src.X, 2); + double yPow = Math.Pow(dst.Y - src.Y, 2); + double result = Math.Sqrt(xPow + yPow); + return Math.Sqrt(Math.Pow(dst.X - src.X, 2) + Math.Pow(dst.Y - src.Y, 2)); + } + #endregion } } \ No newline at end of file From 87fc91073a80febab6022a32e6e2deb2be8da60c Mon Sep 17 00:00:00 2001 From: stubbfel Date: Mon, 14 Oct 2013 17:23:54 +0200 Subject: [PATCH 2/4] add loading thread to exam page --- .../CampusAppWP8/Pages/Exams/Exams.xaml.cs | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs index 337b65d8..72142fa0 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs @@ -16,6 +16,8 @@ namespace CampusAppWP8.Pages.Exams using CampusAppWP8.Utility; using CampusAppWP8.Utility.Lui.MessageBoxes; using Microsoft.Phone.Controls; + using System.Threading; + using System; /// class of ExamsPage. /// Stubbfel, 02.09.2013. @@ -63,7 +65,9 @@ namespace CampusAppWP8.Pages.Exams this.InitializeFeed(); } - this.feed.LoadData(Utilities.GetLoadModus()); + Thread thread = new Thread(new ThreadStart(this.LoadingFeed)); + thread.Start(); + } /// @@ -88,6 +92,14 @@ namespace CampusAppWP8.Pages.Exams #region private + /// Loading feed. + /// Stubbfel, 14.10.2013. + private void LoadingFeed() + { + this.feed.LoadData(Utilities.GetLoadModus()); + } + + /// Method initialize the Feed. /// Stubbfel, 02.09.2013. private void InitializeFeed() @@ -102,8 +114,17 @@ namespace CampusAppWP8.Pages.Exams /// Stubbfel, 02.09.2013. private void FeedIsReady() { - this.SetupExamList(); - this.DefHeader.ProgressVisibility = Visibility.Collapsed; + if (this.Dispatcher != null) + { + this.Dispatcher.BeginInvoke(new Action(() => this.SetupExamList())); + this.Dispatcher.BeginInvoke(new Action(() => this.DefHeader.ProgressVisibility = Visibility.Collapsed)); + } + else + { + this.SetupExamList(); + this.DefHeader.ProgressVisibility = Visibility.Collapsed; + + } } /// Executes the PDF reader operation. @@ -173,7 +194,14 @@ namespace CampusAppWP8.Pages.Exams /// Stubbfel, 02.09.2013. private void FeedIsFailWeb() { - MessageBoxResult result = MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadWeb); + if (this.Dispatcher != null) + { + this.Dispatcher.BeginInvoke(new Action(() => MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadWeb))); + } + else + { + MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadWeb); + } this.feed.ForceReadFile(); } @@ -181,8 +209,16 @@ namespace CampusAppWP8.Pages.Exams /// Stubbfel, 02.09.2013. private void FeedIsFailFile() { - MessageBoxResult result = MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadFile); - this.DefHeader.ProgressVisibility = Visibility.Collapsed; + if (this.Dispatcher != null) + { + this.Dispatcher.BeginInvoke(new Action(() => MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadFile))); + this.Dispatcher.BeginInvoke(new Action(() => this.DefHeader.ProgressVisibility = Visibility.Collapsed)); + } + else + { + MessageBoxes.ShowMainModelErrorMessageBox(AppResources.MsgBox_ErrorMainModelLoadFile); + this.DefHeader.ProgressVisibility = Visibility.Collapsed; + } } /// Event handler. Called by Button for click events. From b6d33730ee2e6a7e395a35e9424ddf0e23bd89e1 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Mon, 14 Oct 2013 17:50:52 +0200 Subject: [PATCH 3/4] add thread to exmepage --- CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs index 72142fa0..cf2c0e0a 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs @@ -43,7 +43,6 @@ namespace CampusAppWP8.Pages.Exams this.BachelorItem.Header = CampusAppWPortalLib8.Resources.AppResources.Degree_Bachelor; this.MasterItem.Header = CampusAppWPortalLib8.Resources.AppResources.Degree_Master; this.DiplomItem.Header = CampusAppWPortalLib8.Resources.AppResources.Degree_Diploma; - this.DefHeader.ProgressVisibility = Visibility.Visible; this.InitializeFeed(); } @@ -65,6 +64,7 @@ namespace CampusAppWP8.Pages.Exams this.InitializeFeed(); } + this.DefHeader.ProgressVisibility = Visibility.Visible; Thread thread = new Thread(new ThreadStart(this.LoadingFeed)); thread.Start(); @@ -117,13 +117,10 @@ namespace CampusAppWP8.Pages.Exams if (this.Dispatcher != null) { this.Dispatcher.BeginInvoke(new Action(() => this.SetupExamList())); - this.Dispatcher.BeginInvoke(new Action(() => this.DefHeader.ProgressVisibility = Visibility.Collapsed)); } else { this.SetupExamList(); - this.DefHeader.ProgressVisibility = Visibility.Collapsed; - } } @@ -158,6 +155,7 @@ namespace CampusAppWP8.Pages.Exams this.MasterPanel.ItemsSource = masterList; this.DiplomaPanel.ItemsSource = diplomaList; this.ExamPivot.SelectedIndex = this.CalcSelectedIndex(); + this.DefHeader.ProgressVisibility = Visibility.Collapsed; } /// Calculates the selected index. From 6f6129ceb2e0fcbe606a1139bfbfbb395eba2e9c Mon Sep 17 00:00:00 2001 From: stubbfel Date: Mon, 14 Oct 2013 17:51:26 +0200 Subject: [PATCH 4/4] add UserSettingCampus --- CampusAppWP8/CampusAppWPortalLib8/Model/Settings/CampusTypes.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CampusAppWP8/CampusAppWPortalLib8/Model/Settings/CampusTypes.cs b/CampusAppWP8/CampusAppWPortalLib8/Model/Settings/CampusTypes.cs index 950f2490..dee8c063 100644 --- a/CampusAppWP8/CampusAppWPortalLib8/Model/Settings/CampusTypes.cs +++ b/CampusAppWP8/CampusAppWPortalLib8/Model/Settings/CampusTypes.cs @@ -11,6 +11,8 @@ namespace CampusAppWPortalLib8.Model.Settings /// public enum Campus { + /// An enum constant representing the user setting campus option. + UserSettingCampus = 0, /// /// Cottbus -> MainCampus ///