/// <summary> /// Refreshes the list containing the exercise history. /// </summary> /// <returns>True if everything was completed correctly</returns> /// <exception cref="UnsuccessfulApiCallException">Thrown if /// <see cref="ExerciseHistoryApi.CallEndpointAsObjectAsync()"/> fails.</exception> /// <exception cref="Exception">Thrown if something else fails.</exception> public async Task RefreshExerciseHistory() { List <Exercise> historyWithResults = new List <Exercise>(); ExerciseHistoryApi historyEndpoint = new ExerciseHistoryApi(App.OAuth2Account); // These exceptions are handled in the view directly. IList <UserActivity> history = new List <UserActivity>(); history = await historyEndpoint.CallEndpointAsObjectAsync().ConfigureAwait(false); // Performance ExerciseApi exerciseEndpoint; Exercise histEx; UserActivityEssay histActEssay; EssayExercise histExEssay; foreach (UserActivity histAct in history) { try { exerciseEndpoint = new ExerciseApi(App.OAuth2Account, histAct.ActivityId); histEx = await exerciseEndpoint.CallEndpointAsExerciseModel().ConfigureAwait(false); if (histAct is UserActivityEssay && histEx is EssayExercise) { histActEssay = (UserActivityEssay)histAct; histExEssay = (EssayExercise)histEx; histExEssay.Contents = histActEssay.Text; histExEssay.Status = histActEssay.Status; histExEssay.Timestamp = histActEssay.Timestamp; } else { historyWithResults.Add(histEx); } } catch (UnsuccessfulApiCallException ex) { Tools.Logger.Log(this.GetType().ToString(), "RefreshExerciseHistory method - Internal UserActivity loop", ex); // TODO: Add activity indicator // this.SwitchActivityIndicator(false); } catch (Exception ex) { Tools.Logger.Log(this.GetType().ToString(), "RefreshExerciseHistory method - Internal UserActivity loop", ex); // TODO: Add activity indicator // this.SwitchActivityIndicator(false); } } this._exerciseHistory = new ReadOnlyObservableCollection <Exercise>(new ObservableCollection <Exercise>(historyWithResults)); this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("History")); }
/// <summary> /// Refreshes the exercise history asynchronously. /// </summary> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private static async Task <ReadOnlyObservableCollection <Exercise> > GetHistoryAsync() { List <Exercise> historyWithResults = new List <Exercise>(); ExerciseHistoryApi historyEndpoint = new ExerciseHistoryApi(App.OAuth2Account); IEnumerable <UserActivity> history = new List <UserActivity>(); history = await Task.Run(async() => await historyEndpoint.CallEndpointAsObjectAsync()); // Remove dictionary searches (they are not meant to be displayed in the list). history = history.Where(x => x.GetType() != typeof(DictionarySearchExercise)); // Preload all exercises in the list to improve performance. ExerciseApi exerciseEndpoint; foreach (UserActivity histAct in history) { exerciseEndpoint = new ExerciseApi(App.OAuth2Account, histAct.ActivityId); historyWithResults.Add(await Task.Run(async() => await exerciseEndpoint.CallEndpointAsExerciseModel())); } return(new ReadOnlyObservableCollection <Exercise>(new ObservableCollection <Exercise>(historyWithResults))); }