/// <summary> /// Get latest opened Timetable /// </summary> /// <returns>latest opened timetable, or null if there is not any other timetable</returns> public static Timetable GetLatestOpenedTimeTable(SchoolTimetable st) { Timetable lastOpenedTimetable = null; //First we want to check, if we have any id id class memory if (st.IdOfLastOpenedTimeTable != -1) { //type=0 -> class //type=1 -> teacher lastOpenedTimetable = GetTimetableById(st.IdOfLastOpenedTimeTable, st); } //then, we want to check if there is any latest selected plan //in application memory //application memory is highest priority than a class memory if (LocalSettingsServices.ShowTimetableAtStartup.ContainsKey() && int.Parse(LocalSettingsServices.ShowTimetableAtStartup.GetKeyValue()) == 1 && LocalSettingsServices.ShowTimetableAtStartupValue.ContainsKey()) { var nameOfLastSelectedPlan = LocalSettingsServices.ShowTimetableAtStartupValue.GetKeyValue(); var lot = GetAllTimeTables(st).FirstOrDefault(p => p.name == nameOfLastSelectedPlan); if (lot != null) { lastOpenedTimetable = lot; } } return(lastOpenedTimetable); }
} // 0 - class | 1 - teacher public static List <Timetable> GetAllTimeTables(SchoolTimetable st) { var listOfTimeTables = st.TimetablesOfClasses.ToList(); listOfTimeTables.AddRange(st.TimetableOfTeachers); return(listOfTimeTables); }
//sets timetable to private instance public static void SetTimetable(SchoolTimetable st) { if (_timeTable == null) { _timeTable = st; } }
/// <summary> /// Gets ID of lesson from lesson Grid /// </summary> /// <param name="lessonGrid">grid where lesson's placed</param> /// <returns>Lesson index</returns> public static Lesson GetLessonFromLessonGrid(Grid lessonGrid, SchoolTimetable st) { var splittedText = ((TextBlock)lessonGrid.Children.First(p => p is TextBlock && ((TextBlock)p).Text.Contains("[]"))).Text. Split(' '); int jinloop = int.Parse(splittedText[1]); int iinloop = int.Parse(splittedText[2]); var t = Timetable.GetTimetableById(int.Parse(splittedText[3]), st); return(t.days[jinloop - 2].Lessons[iinloop - 1]); }
public static async Task <bool> Serialize(SchoolTimetable toSerialize) { var xmlSerializer = new XmlSerializer(typeof(SchoolTimetable)); using (var textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); StorageFile fileToSave; if (!IsFileExists()) { try { fileToSave = await LocalFolder.CreateFileAsync(planFileName); } catch { fileToSave = null; } } else { try { fileToSave = await LocalFolder.GetFileAsync(planFileName); } catch { fileToSave = null; } } if (fileToSave == null) { return(false); } try { await FileIO.WriteTextAsync(fileToSave, textWriter.ToString()); } catch { return(false); } return(true); } }
/// <summary> /// Gets timetable by Id /// </summary> /// <param name="id">id of timetable</param> /// <returns>timetable</returns> public static Timetable GetTimetableById(int id, SchoolTimetable st) { int idOfTimeTable; var numOfClassesTimeTables = st.TimetablesOfClasses.Count; var type = Lesson.LessonType.Class; //we want to translate absolute id to id of Class or id of Teachers if (id < numOfClassesTimeTables) { idOfTimeTable = id; } else { idOfTimeTable = id - numOfClassesTimeTables; type = Lesson.LessonType.Teacher; } return(type == Lesson.LessonType.Class ? st.TimetablesOfClasses[idOfTimeTable] : st.TimetableOfTeachers[idOfTimeTable]); }
/// <summary> /// Downloads a plan /// </summary> /// <param name="textToShowAtInfoCenter">Text to show, before downloading</param> private void DownloadTimeTables(string textToShowAtInfoCenter) { SplitViewContentScrollViewer.Visibility = Visibility.Collapsed; InfoCenterStackPanel.Visibility = Visibility.Visible; InfoCenterButton.Visibility = Visibility.Visible; if (HtmlServices.UserHasInternetConnection()) { InfoCenterText.Text = textToShowAtInfoCenter; _isLoaded = false; } else InfoCenterText.Text = "Aby odświeżyć plan zajęc, musisz mieć połączenie z internetem! Naciśnij przycisk poniżej aby spróbować ponownie"; if (_isButtonClickEventSubscribed) return; _isButtonClickEventSubscribed = true; InfoCenterButton.Click += async (s, es) => { //if user downloaded plan succesfully, but there was problem //with save and then clicked a button if (InfoCenterText.Text.Contains("NIE POWIODŁO SIĘ")) { HtmlServices.InvokeAllTimeTableDownloaded(); return; } //check again if user has an internet connection //if not, call this function again to change text if (!HtmlServices.UserHasInternetConnection()) { DownloadTimeTables(textToShowAtInfoCenter); return; } //if user wants to download a plan if (InfoCenterText.Text == textToShowAtInfoCenter || !InfoCenterText.Text.Contains("zakończone")) { InfoCenterButton.Visibility = Visibility.Collapsed; InfoCenterProgressRing.Visibility = Visibility.Collapsed; InfoCenterText.Text = "Trwa synchronizowanie planu..."; TimeTable.TimetableOfTeachers = new ObservableCollection<Timetable>(); TimeTable.TimetablesOfClasses = new ObservableCollection<Timetable>(); //we want to avoid the situations where OnTimeTableDownloaded event //will be subscribed two times if (!_isTimeTableDownloadedEventSubscribed) { _isTimeTableDownloadedEventSubscribed = true; //called on each timetable downloaded to show progress HtmlServices.OnTimeTableDownloaded += (timeTable, lenght) => { var numOfTimeTable = TimeTable.TimetablesOfClasses.Count + TimeTable.TimetableOfTeachers.Count(); if (timeTable.type == 0) TimeTable.TimetablesOfClasses.Add(timeTable); else TimeTable.TimetableOfTeachers.Add(timeTable); var percentOfDownloadedTimeTables = (int)(0.5f + (++numOfTimeTable * 100.0) / lenght); InfoCenterText.Text = "[" + percentOfDownloadedTimeTables.ToString() + "%] Trwa dodawanie: " + timeTable.name; }; } if (!_isAllTimeTablesDownloadedSubscribed) { _isAllTimeTablesDownloadedSubscribed = true; HtmlServices.OnAllTimeTablesDownloaded += async () => { InfoCenterText.Text = "Trwa zapisywanie planu zajęć..."; TimeTable.IdOfLastOpenedTimeTable = -1; bool isPlanSerializedSuccesfullly = await DataServices.Serialize(TimeTable); InfoCenterText.Text = !isPlanSerializedSuccesfullly ? "Zapisywanie planu zajęć NIE POWIODŁO SIĘ. Spróbować ponownie?" : "Synchronizowanie i zapisywanie planu zajęć zakończone."; InfoCenterButton.Visibility = Visibility.Visible; InfoCenterProgressRing.Visibility = Visibility.Collapsed; }; } //if user wants to download a plan but //for he timetable is shown if (_isLoaded) { InfoCenterStackPanel.Visibility = Visibility.Collapsed; InfoCenterButton.Visibility = Visibility.Collapsed; await ShowTimeTableAsync(Timetable.GetLatestOpenedTimeTable(TimeTable) ?? TimeTable.TimetablesOfClasses[0], false, true); } TimeTable = new SchoolTimetable(); await HtmlServices.GetData(); } else { //if plan was downloaded&saved succesfully and user clicked OK button _isLoaded = true; InfoCenterStackPanel.Visibility = Visibility.Collapsed; MenuListViewOfSections.ItemsSource = TimeTableOfSections; MenuListViewOfTeachers.ItemsSource = TimeTableOfTeachers; MenuSplitView.IsPaneOpen = true; } }; }
/// <summary> /// Loads plan /// </summary> private async void MainPage_Loaded(object sender, RoutedEventArgs e) { // load schooltimetable if (DataServices.IsFileExists()) { InfoCenterStackPanel.Visibility = Visibility.Visible; InfoCenterProgressRing.Visibility = Visibility.Visible; InfoCenterButton.Visibility = Visibility.Collapsed; InfoCenterText.Text = "Trwa wczytywanie planu zajęć..."; try { TimeTable = await DataServices.Deserialize(); } catch { TimeTable = null; } if (TimeTable == null || !TimeTable.TimetableOfTeachers.Any() || !TimeTable.TimetablesOfClasses.Any()) { //removes settings InfoCenterProgressRing.Visibility = Visibility.Collapsed; TimeTable = new SchoolTimetable(); DownloadTimeTables("Wystąpił problem podczas wczytywania planu lekcji, muisz pobrać go od nowa. Chcesz to zrobić teraz?"); return; } MenuListViewOfSections.ItemsSource = TimeTableOfSections; MenuListViewOfTeachers.ItemsSource = TimeTableOfTeachers; InfoCenterProgressRing.Visibility = Visibility.Collapsed; //show last opened timetable, first check settings config var numOfClassesTimeTables = TimeTable.IdOfLastOpenedTimeTable; if (LocalSettingsServices.ShowTimetableAtStartup.ContainsKey()) { if ((int.Parse(LocalSettingsServices.ShowTimetableAtStartup.GetKeyValue()) == 0)) numOfClassesTimeTables = -1; else numOfClassesTimeTables = 1; } //if user dont want to show last timetable or no one are sets var lastTimetable = Timetable.GetLatestOpenedTimeTable(TimeTable); if (numOfClassesTimeTables == -1 || lastTimetable == null) { InfoCenterText.Text = "Naciśnij przycisk menu u góry i wybierz interesujący Cię plan zajęć."; InfoCenterButton.Visibility = Visibility.Collapsed; _isLoaded = true; return; } InfoCenterStackPanel.Visibility = Visibility.Collapsed; if (lastTimetable.type == Lesson.LessonType.Class) { MenuListViewOfSections.SelectionChanged += (s, f) => MenuListViewOfSections.ScrollIntoView((s as ListView).SelectedItem, ScrollIntoViewAlignment.Leading); MenuListViewOfSections.SelectedItem = lastTimetable; } else { MenuListViewOfTeachers.SelectionChanged += (s, f) => MenuListViewOfTeachers.ScrollIntoView((s as ListView).SelectedItem, ScrollIntoViewAlignment.Leading); MenuListViewOfTeachers.SelectedItem = lastTimetable; } await ShowTimeTableAsync(lastTimetable, false, true); _isLoaded = true; return; } // if file doesnt exits DownloadTimeTables("By przeglądać plan zajęć, musiz go zsynchronizować, chcesz to zrobić teraz?"); }
/// <summary> /// Saves last opened timetable to file /// </summary> /// <param name="idOfTimeTable">Absolute id of Timetable</param> /// <param name="st">SchoolTimetable instance</param> /// <returns> /// null if latest opened timetable is same as idofTimetable, /// false if there was an error, true if saving was completed succesfully /// </returns> public static async Task <bool?> SaveLastOpenedTimeTableToFile(int idOfTimeTable, SchoolTimetable st) { if (idOfTimeTable == st.IdOfLastOpenedTimeTable) { return(null); } st.IdOfLastOpenedTimeTable = idOfTimeTable; int numOfTriesToSave = 3; //try save 3 times try { do { } while (!await Serialize(st) && --numOfTriesToSave == 0); } catch { return(false); } return(numOfTriesToSave > 0); }
/// <summary> /// Gets Id Of Timetable /// </summary> /// <returns>absolute id of timetable</returns> public static int GetIdOfTimetable(Timetable t, SchoolTimetable st) { return(t == null ? -1 : t.type == Lesson.LessonType.Teacher ? st.TimetablesOfClasses.Count + st.TimetableOfTeachers.IndexOf(t) : st.TimetablesOfClasses.IndexOf(t)); }