diff --git a/CourseSelection/Pages/MainUI.xaml b/CourseSelection/Pages/MainUI.xaml index 786ef5f..93d9eaa 100644 --- a/CourseSelection/Pages/MainUI.xaml +++ b/CourseSelection/Pages/MainUI.xaml @@ -7,7 +7,7 @@ xmlns:mui="http://firstfloorsoftware.com/ModernUI" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> - + @@ -17,7 +17,9 @@ - + @@ -29,7 +31,10 @@ - + @@ -48,22 +53,33 @@ Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" - Margin="0,0,0,10"/> + Margin="0,0,0,10"> + - + + + VerticalAlignment="Center" + Margin="5,0,0,0"> + + VerticalAlignment="Center" + Margin="5,0,0,0"> + - + + + Margin="0,0,0,10" + Click="Expander_Click"> - - + + + Grid.Row="3"> + - + + - + Text="Semester:" + VerticalAlignment="Center"> + + - - + - + @@ -163,8 +192,11 @@ - - + + - - + + - - + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/CourseSelection/Pages/MainUI.xaml.cs b/CourseSelection/Pages/MainUI.xaml.cs index 4897739..4604b75 100644 --- a/CourseSelection/Pages/MainUI.xaml.cs +++ b/CourseSelection/Pages/MainUI.xaml.cs @@ -15,6 +15,7 @@ using System.ComponentModel; using System.Collections.Specialized; using FirstFloor.ModernUI.Windows.Controls; +using System.Net.Http; namespace CourseSelection { @@ -41,7 +42,6 @@ public MainUI() private VMSet courseSet = new VMSet(); private HashSet CourseSet_Cache = new HashSet(); private Dictionary semesterList = new Dictionary(); - private Dictionary insList = new Dictionary(); public Color[] GorgeousColors = new Color[10]; public static TimeDictionary TimePeriod { get; set; } = new TimeDictionary(); @@ -80,9 +80,14 @@ private void MUIB_NoInstructors_Unchecked(object sender, RoutedEventArgs e) LB_NoInstructors.Visibility = Visibility.Collapsed; } - private void MUIB_Add_Click(object sender, RoutedEventArgs e) + private async void MUIB_Add_Click(object sender, RoutedEventArgs e) { - var course = AddCourse(TB_CourseName.Text); + Mask.Visibility = Visibility.Visible; + ProgressRing.IsActive = true; + var course = await AddCourse(TB_CourseName.Text); + ProgressRing.IsActive = false; + Mask.Visibility = Visibility.Hidden; + if (course == null) return; DrawCoursePanel(course); @@ -308,8 +313,13 @@ private void UpdateInsList() LB_NoInstructors.ItemsSource = set; } - private Course AddCourse(string courseName) + private async Task AddCourse(string courseName) { + if (courseName == "") + { + return null; + } + Course ret = null; courseName = courseName.ToUpper(); @@ -323,7 +333,18 @@ private Course AddCourse(string courseName) else { string sem = semesterList[(string)Semester.SelectedItem]; - Course course = new Crawler() { TermID = sem }.GetCourse(courseName); + + Course course; + try + { + course = await new Crawler() { TermID = sem }.GetCourse(courseName); + } + catch (Exception e) + { + showMessage(e); + return null; + } + if (course == null) return null; courseSet.Add(course); CourseSet_Cache.Add(course); @@ -335,6 +356,40 @@ private Course AddCourse(string courseName) return ret; } + private void showMessage(Exception exception) + { + if (exception is HttpRequestException) + { + ModernDialog.ShowMessage( + "Testudo may be under maintenance. \nUnfortunately, there is nothing we can do at this moment.\n\n" + exception.Message, + "Connection error", + MessageBoxButton.OK + ); + + //MessageBox.Show( + // "Unable to get the course info\nPlease check if there is any typo in course name", + // "Error", + // MessageBoxButton.OK, + // MessageBoxImage.Error + //); + } + else if (exception is AggregateException) + { + foreach (var e in (exception as AggregateException).InnerExceptions) + { + showMessage(e); + } + } + else + { + ModernDialog.ShowMessage( + exception.Message, + "Error", + MessageBoxButton.OK + ); + } + } + private void MUIB_NoInstructors_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (!(bool)e.NewValue) @@ -343,16 +398,20 @@ private void MUIB_NoInstructors_IsVisibleChanged(object sender, DependencyProper } } - private void Semester_SelectionChanged(object sender, SelectionChangedEventArgs e) + private async void Semester_SelectionChanged(object sender, SelectionChangedEventArgs e) { var courseList = courseSet.Select(c => c.Name).ToArray(); CourseSet_Cache.Clear(); courseSet.Clear(); SP_Course.Children.Clear(); + Mask.Visibility = Visibility.Visible; + ProgressRing.IsActive = true; foreach (var name in courseList) { - AddCourse(name); + await AddCourse(name); } + ProgressRing.IsActive = false; + Mask.Visibility = Visibility.Hidden; UpdateView(); }