private async void DataServiceOnDataRefreshed(object sender, DataRefreshedEventArgs dataRefreshedEventArgs) { await DispatcherHelper.UIDispatcher.InvokeAsync(() => { try { switch (dataRefreshedEventArgs.DataType) { case DataType.TopStoriesArticles: UpdateHeadLine(); UpdateTopStoriesArticles(); HasInternet = dataRefreshedEventArgs.IsSuccess; if (!dataRefreshedEventArgs.IsSuccess && !IsCachedModeMessageDisplayed) { IsCachedModeMessageDisplayed = true; } IsRefreshingTopStoriesArticles = false; break; case DataType.BreakingNewsArticles: UpdateBreakingStoriesArticles(); HasInternet = dataRefreshedEventArgs.IsSuccess; if (!dataRefreshedEventArgs.IsSuccess && !IsCachedModeMessageDisplayed) { IsCachedModeMessageDisplayed = true; } IsRefreshingBreakingNewsArticles = false; break; case DataType.Categories: IsRefreshingCategories = true; if (ListOfCategories != null && ListOfCategories.Count != 0) { IsRefreshingCategories = false; return; } else if (ListOfCategories == null) { ListOfCategories = new ObservableCollection <CategoryDetailsModel>(); } ListOfCategories.Clear(); foreach (var category in _dataService.ListOfCategories) { ListOfCategories.Add(category); } IsRefreshingCategories = false; break; } } catch (Exception exception) { if (Debugger.IsAttached) { Debug.WriteLine("MainViewModel:" + exception); } } }); }
private async void RefreshCategories() { DispatcherHelper.UIDispatcher.BeginInvoke(() => { IsRefreshingCategories = true; if (ListOfCategories == null) { ListOfCategories = new ObservableCollection <CategoryDetailsModel>(); } }); try { var categories = new List <CategoryDetailsModel>(); foreach (var category in await _dataService.GetCategoryDetailsAsync(true)) { categories.Add(category); } if (categories != null && categories.Count >= 1) { DispatcherHelper.UIDispatcher.BeginInvoke(() => { ListOfCategories.Clear(); foreach (var category in categories.OrderBy(o => o.Order)) { ListOfCategories.Add(category); } }); } } catch (Exception exception) { if (Debugger.IsAttached) { Debug.WriteLine("MainViewModel:" + exception); } } DispatcherHelper.UIDispatcher.BeginInvoke(() => { IsRefreshingCategories = false; if (ListOfCategories != null && ListOfCategories.Count > 0) { if (FavCategories == null) { FavCategories = new ObservableCollection <CategoryDetailsModel>(); } FavCategories.Clear(); foreach (var category in ListOfCategories) { if (category.IsPinned == true) { FavCategories.Add(category); } } } }); }
private async void LoadOrRefreshCategories() { DispatcherHelper.UIDispatcher.BeginInvoke(() => { IsRefreshingCategories = true; }); try { if (ListOfCategories == null || ListOfCategories.Count != 0) { DispatcherHelper.UIDispatcher.BeginInvoke(() => { IsRefreshingCategories = false; }); return; } var categories = new List <CategoryDetailsModel>(); var categoriesFromDS = await _dataService.GetCategoryDetailsAsync(); if (categoriesFromDS != null && categoriesFromDS.Count > 0) { foreach (var category in categoriesFromDS) { categories.Add(category); } DispatcherHelper.UIDispatcher.BeginInvoke(() => { ListOfCategories.Clear(); foreach (var category in categories.OrderBy(o => o.Order)) { ListOfCategories.Add(category); } }); } } catch (Exception exception) { if (Debugger.IsAttached) { Debug.WriteLine("MainViewModel:" + exception); } } DispatcherHelper.UIDispatcher.BeginInvoke(() => { IsRefreshingCategories = false; }); }