From c605f5463cab55fccd9dbeea16a3315e298c5a83 Mon Sep 17 00:00:00 2001 From: bricefriha Date: Wed, 22 Jan 2025 12:51:10 +0000 Subject: [PATCH] Feed vm - cleanup --- App/ViewModels/FeedsViewModel.cs | 1112 +++++++++++++++--------------- App/ViewModels/NewsViewModel.cs | 1 + 2 files changed, 553 insertions(+), 560 deletions(-) diff --git a/App/ViewModels/FeedsViewModel.cs b/App/ViewModels/FeedsViewModel.cs index 3ff3605d..029a6626 100644 --- a/App/ViewModels/FeedsViewModel.cs +++ b/App/ViewModels/FeedsViewModel.cs @@ -7,17 +7,15 @@ using System.Diagnostics; using Command = Microsoft.Maui.Controls.Command; using MvvmHelpers; -using CommunityToolkit.Mvvm.Messaging; -using GamHubApp.Services; using SQLite; -namespace GamHubApp.ViewModels +namespace GamHubApp.ViewModels; + +public class FeedsViewModel : BaseViewModel { - public class FeedsViewModel : BaseViewModel - { - private bool _dataLoaded = false; - public List UpdateOrders { get; private set; } - private ObservableCollection _feeds = new(); + private bool _dataLoaded = false; + public List UpdateOrders { get; private set; } + private ObservableCollection _feeds = new(); public ObservableCollection Feeds { @@ -28,42 +26,42 @@ public ObservableCollection Feeds OnPropertyChanged(nameof(Feeds)); } } - private ObservableCollection _feedTabs; + private ObservableCollection _feedTabs; public ObservableCollection FeedTabs { get { return _feedTabs; } set { - _feedTabs = value; + _feedTabs = value; OnPropertyChanged(nameof(FeedTabs)); } } - private int _currentFeedIndex; + private int _currentFeedIndex; - public int CurrentFeedIndex + public int CurrentFeedIndex + { + get { return _currentFeedIndex; } + set { - get { return _currentFeedIndex; } - set - { - _currentFeedIndex = value; - //SwitchTabs(_currentFeedIndex); + _currentFeedIndex = value; + //SwitchTabs(_currentFeedIndex); - OnPropertyChanged(nameof(CurrentFeedIndex)); + OnPropertyChanged(nameof(CurrentFeedIndex)); - _oldIndex = _currentFeedIndex; - } + _oldIndex = _currentFeedIndex; } + } - private bool _isRefreshing; + private bool _isRefreshing; - public bool IsRefreshing + public bool IsRefreshing { get { return _isRefreshing; } set { - _isRefreshing = value; + _isRefreshing = value; OnPropertyChanged(nameof(IsRefreshing)); } } @@ -75,716 +73,710 @@ public ObservableRangeCollection
Articles get { return _articles; } set { - _articles = value; + _articles = value; OnPropertyChanged(nameof(Articles)); } } - private Feed _selectedFeed; + private Feed _selectedFeed; - public Feed SelectedFeed + public Feed SelectedFeed + { + get { return _selectedFeed; } + set { - get { return _selectedFeed; } - set - { - _selectedFeed = value; - CurrentFeedIndex = _feeds.IndexOf(value); - Refresh(value,true); - OnPropertyChanged(nameof(SelectedFeed)); - } + _selectedFeed = value; + CurrentFeedIndex = _feeds.IndexOf(value); + Refresh(value,true); + OnPropertyChanged(nameof(SelectedFeed)); } + } - private TabButton _selectedFeedTab; - public TabButton SelectedFeedTab + private TabButton _selectedFeedTab; + public TabButton SelectedFeedTab + { + get { return _selectedFeedTab; } + set { - get { return _selectedFeedTab; } - set - { - if (_selectedFeedTab != value) + if (_selectedFeedTab != value) + { + // Set the IsSelected property for the newly selected tab + _selectedFeedTab = value; + int index = _feedTabs.IndexOf(value); + FeedTabs[index].IsSelected = true; + SelectedFeed = _feeds[index]; + // Reset the IsSelected property for other tabs + for (int i = 0; i < _feedTabs.Count; i++) { - // Set the IsSelected property for the newly selected tab - _selectedFeedTab = value; - int index = _feedTabs.IndexOf(value); - FeedTabs[index].IsSelected = true; - SelectedFeed = _feeds[index]; - // Reset the IsSelected property for other tabs - for (int i = 0; i < _feedTabs.Count; i++) + if (i != index) { - if (i != index) - { - FeedTabs[i].IsSelected = false; - } + FeedTabs[i].IsSelected = false; } - - OnPropertyChanged(nameof(SelectedFeedTab)); } + + OnPropertyChanged(nameof(SelectedFeedTab)); } } + } - public Command RefreshAll => new Command((feed) => - { - IsRefreshing = true; - Refresh(feed); + public Command RefreshAll => new Command((feed) => + { + IsRefreshing = true; + Refresh(feed); - }); + }); - public Command SwitchFeed => new Command((feed) => - { - if (IsBusy) - return; - //IsRefreshing = true; + public Command SwitchFeed => new Command((feed) => + { + if (IsBusy) + return; + //IsRefreshing = true; - CurrentApp.ShowLoadingIndicator(); - _ = SwitchFeedAsync(feed); + CurrentApp.ShowLoadingIndicator(); + _ = SwitchFeedAsync(feed); - RefreshArticles.Execute(null); - CurrentApp.RemoveLoadingIndicator(); + RefreshArticles.Execute(null); + CurrentApp.RemoveLoadingIndicator(); - }); + }); - private async Task SwitchFeedAsync(Feed feed) + private async Task SwitchFeedAsync(Feed feed) + { + await Task.Run(() => { - await Task.Run(() => - { - UnnoticedArticles.Clear(); - // Set new feed - SwitchTabs(_feeds.IndexOf(feed)); - SelectedFeed.IsLoaded = false; - }); - } + UnnoticedArticles.Clear(); + // Set new feed + SwitchTabs(_feeds.IndexOf(feed)); + SelectedFeed.IsLoaded = false; + }); + } - public App CurrentApp { get; } + public App CurrentApp { get; } - public Command UncoverNewArticles { get; private set; } - private FeedsPage CurrentPage { get; set; } + public Command UncoverNewArticles { get; private set; } + private FeedsPage CurrentPage { get; set; } - public Command Delete => new Command( (feed) => - { - CurrentApp.OpenPopUp (new DeleteFeedPopUp(_selectedFeed, this), CurrentPage); + public Command Delete => new Command( (feed) => + { + CurrentApp.OpenPopUp (new DeleteFeedPopUp(_selectedFeed, this), CurrentPage); - }); + }); - public Command Rename => new Command( (feed) => - { - CurrentApp.OpenPopUp (new RenameFeedPopUp(_selectedFeed, this), CurrentPage); + public Command Rename => new Command( (feed) => + { + CurrentApp.OpenPopUp (new RenameFeedPopUp(_selectedFeed, this), CurrentPage); - }); + }); - public Command Edit => new Command(async (feed) => - { - IsFromDetail = true; - CurrentFocusIndex = _feeds.IndexOf(_selectedFeed); - await CurrentPage.Navigation.PushAsync(new EditFeedPage(_selectedFeed, this)); + public Command Edit => new Command(async (feed) => + { + IsFromDetail = true; + CurrentFocusIndex = _feeds.IndexOf(_selectedFeed); + await CurrentPage.Navigation.PushAsync(new EditFeedPage(_selectedFeed, this)); - }); + }); - private bool _onTopScroll; + private bool _onTopScroll; - public bool OnTopScroll + public bool OnTopScroll + { + get { return _onTopScroll; } + set { - get { return _onTopScroll; } - set - { - _onTopScroll = value; - OnPropertyChanged(nameof(OnTopScroll)); - } + _onTopScroll = value; + OnPropertyChanged(nameof(OnTopScroll)); } + } - public Command RefreshArticles =>new (() => - { + public Command RefreshArticles =>new (() => + { - if (IsBusy) + if (IsBusy) + return; + MainThread.BeginInvokeOnMainThread(async () => + { + if (_feeds.Count <= 0) return; - MainThread.BeginInvokeOnMainThread(async () => + //IsRefreshing = true; + await Task.Run(() => { - if (_feeds.Count <= 0) - return; - //IsRefreshing = true; - await Task.Run(() => + try { - try - { - if (SelectedFeed == null) - SelectedFeed = _feeds[0]; - Refresh(SelectedFeed); - } - catch - { - // in case of error, refresh the first feed - RefreshFirstFeed(); - } - }); + if (SelectedFeed == null) + SelectedFeed = _feeds[0]; + Refresh(SelectedFeed); + } + catch + { + // in case of error, refresh the first feed + RefreshFirstFeed(); + } }); - _dataLoaded = true; }); + _dataLoaded = true; + }); public FeedsViewModel(FeedsPage page) + { + // CurrentApp and CurrentPage will allow use to access to global properties + CurrentApp = App.Current as App; + CurrentPage = page; + + // Instantiate definitions + FeedTabs = new ObservableRangeCollection(); + using (var conn = new SQLiteConnection(App.GeneralDBpath)) { - // CurrentApp and CurrentPage will allow use to access to global properties - CurrentApp = App.Current as App; - CurrentPage = page; + Feeds = new ObservableCollection(conn.GetAllWithChildren()); + conn.Close(); + } + _articles = new ObservableRangeCollection
(); - // Instantiate definitions - FeedTabs = new ObservableRangeCollection(); - using (var conn = new SQLiteConnection(App.GeneralDBpath)) - { - Feeds = new ObservableCollection(conn.GetAllWithChildren()); - conn.Close(); - } - _articles = new ObservableRangeCollection
(); + // Organise feeds into tabs + CopyFeedsToTabs(); - // Organise feeds into tabs - CopyFeedsToTabs(); + // Select first item + CurrentFeedIndex = 0; + UncoverNewArticles = new Command(() => + { + if (UnnoticedArticles == null) + return; + if (UnnoticedArticles.Count <= 0) + return; - // Select first item - CurrentFeedIndex = 0; - UncoverNewArticles = new Command(() => + CurrentApp.ShowLoadingIndicator(); + int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == SelectedFeed.Id)); + _ = Task.Run(() => { - if (UnnoticedArticles == null) - return; - if (UnnoticedArticles.Count <= 0) - return; + // Scroll up + CurrentPage.ScrollFeed(); - CurrentApp.ShowLoadingIndicator(); - int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == SelectedFeed.Id)); - _ = Task.Run(() => - { - // Scroll up - CurrentPage.ScrollFeed(); - - // Add the unnoticed articles - UpdateArticles([.. UnnoticedArticles], SelectedFeed, indexFeed); + // Add the unnoticed articles + UpdateArticles([.. UnnoticedArticles], SelectedFeed, indexFeed); - UnnoticedArticles.Clear(); + UnnoticedArticles.Clear(); - }).ContinueWith(res => CurrentApp.RemoveLoadingIndicator()); + }).ContinueWith(res => CurrentApp.RemoveLoadingIndicator()); - }); + }); - UpdateOrders = new List(); + UpdateOrders = new List(); - if (_feeds.Count <= 0) - return; + if (_feeds.Count <= 0) + return; - IsRefreshing = true; + IsRefreshing = true; - RefreshArticles.Execute(null); + RefreshArticles.Execute(null); - } + } - private ObservableCollection
_unnoticedArticles = new (); + private ObservableCollection
_unnoticedArticles = new (); - public ObservableCollection
UnnoticedArticles + public ObservableCollection
UnnoticedArticles + { + get { return _unnoticedArticles; } + set { - get { return _unnoticedArticles; } - set - { - _unnoticedArticles = value; - if (_unnoticedArticles?.Count > 0) - CurrentPage.ShowRefreshButton(); - else - CurrentPage.RemoveRefreshButton(); + _unnoticedArticles = value; + if (_unnoticedArticles?.Count > 0) + CurrentPage.ShowRefreshButton(); + else + CurrentPage.RemoveRefreshButton(); - OnPropertyChanged(nameof(UnnoticedArticles)); - } + OnPropertyChanged(nameof(UnnoticedArticles)); } + } - public Command FeedSelect + public Command FeedSelect + { + get { return new Command ((feedId) => { - get { return new Command ((feedId) => - { - if (string.IsNullOrEmpty(feedId)) - return; + if (string.IsNullOrEmpty(feedId)) + return; - int nextIndex = _feeds.IndexOf(_feeds.FirstOrDefault(feed => feed.Id == feedId)); + int nextIndex = _feeds.IndexOf(_feeds.FirstOrDefault(feed => feed.Id == feedId)); - if (nextIndex == -1) - return; - Articles.Clear(); - SelectedFeedTab = _feedTabs[nextIndex]; + if (nextIndex == -1) + return; + Articles.Clear(); + SelectedFeedTab = _feedTabs[nextIndex]; - // Flag all the other feeds as unloaded - for (int i = 0; i < _feeds.Count; i++) + // Flag all the other feeds as unloaded + for (int i = 0; i < _feeds.Count; i++) + { + if (i != nextIndex) { - if (i != nextIndex) - { - _feeds[i].IsLoaded = false; + _feeds[i].IsLoaded = false; - } } + } - }); } - } - /// - /// Refresh selected feed - /// - /// feed of choice - /// Whether we refresh the feed from scratch or not - public void Refresh(Feed feed, bool force = false) + }); } + } + /// + /// Refresh selected feed + /// + /// feed of choice + /// Whether we refresh the feed from scratch or not + public void Refresh(Feed feed, bool force = false) { - if (IsBusy) - return; + if (IsBusy) + return; - IsBusy = true; - CurrentApp.ShowLoadingIndicator(); + IsBusy = true; + CurrentApp.ShowLoadingIndicator(); - // Determine whether or not it's the first time loading the article of this feed - bool isFirstLoad = _articles == null || _articles.Count <= 0; + // Determine whether or not it's the first time loading the article of this feed + bool isFirstLoad = _articles == null || _articles.Count <= 0; - Task.Run(async () => - await AggregateFeed(feed, isFirstLoad || force).ContinueWith(res => - { - // End the loading indicator - IsRefreshing = false; - IsBusy = false; - CurrentApp.RemoveLoadingIndicator(); - }) - ); - - } - - /// - /// Load articles via search - /// - /// feed we are searching - private async Task AggregateFeed(Feed feed, bool force = true) - { - int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == feed.Id)); - - List
articles = new (); - - // Figure out if the feed deserve an update - string timeUpdate = string.Empty; - if (force) + Task.Run(async () => + await AggregateFeed(feed, isFirstLoad || force).ContinueWith(res => { - Articles.Clear(); - UnnoticedArticles.Clear(); - } + // End the loading indicator + IsRefreshing = false; + IsBusy = false; + CurrentApp.RemoveLoadingIndicator(); + }) + ); + + } - if (Articles?.Count != 0) - timeUpdate = Articles?.First().FullPublishDate.ToUniversalTime().ToString("dd-MM-yyy_HH:mm:ss"); + /// + /// Load articles via search + /// + /// feed we are searching + private async Task AggregateFeed(Feed feed, bool force = true) + { + int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == feed.Id)); - bool needUpdate = feed.IsLoaded && !string.IsNullOrEmpty(timeUpdate); + List
articles = new (); + + // Figure out if the feed deserve an update + string timeUpdate = string.Empty; + if (force) + { + Articles.Clear(); + UnnoticedArticles.Clear(); + } - // Make sure we have internet connection - if (Connectivity.NetworkAccess == NetworkAccess.Internet) - articles = (await CurrentApp.DataFetcher.GetFeedArticles(feed.Keywords, - timeUpdate, - needUpdate)).Where(article => (article.Blocked == null || article.Blocked == false) && article.Source.IsActive).ToList(); + if (Articles?.Count != 0) + timeUpdate = Articles?.First().FullPublishDate.ToUniversalTime().ToString("dd-MM-yyy_HH:mm:ss"); - // Offline search - else - { - var words = feed.Keywords.Split(' '); - for (int i = 0; i < words.Count(); i++) - { - var word = words[i]; - articles.AddRange(feed.Articles?.Where((e) => e.Title.Contains(word))); - } + bool needUpdate = feed.IsLoaded && !string.IsNullOrEmpty(timeUpdate); - } - if (force) - { - // Update list of articles - InsertArticles(articles); - SelectedFeed.IsLoaded = true; + // Make sure we have internet connection + if (Connectivity.NetworkAccess == NetworkAccess.Internet) + articles = (await CurrentApp.DataFetcher.GetFeedArticles(feed.Keywords, + timeUpdate, + needUpdate)).Where(article => (article.Blocked == null || article.Blocked == false) && article.Source.IsActive).ToList(); - IsRefreshing = false; - return; - } - - if (needUpdate) + // Offline search + else + { + var words = feed.Keywords.Split(' '); + for (int i = 0; i < words.Count(); i++) { - if (articles.Count > 0) - if (OnTopScroll || _articles?.Count < 1) - UpdateArticles(articles, feed, indexFeed); - else - UnnoticedArticles = new ObservableCollection
( articles); + var word = words[i]; + articles.AddRange(feed.Articles?.Where((e) => e.Title.Contains(word))); } - else - // Update list of articles - InsertArticles(articles); + + } + if (force) + { + // Update list of articles + InsertArticles(articles); SelectedFeed.IsLoaded = true; IsRefreshing = false; - + return; } - /// - /// Insert a set of articles in the current list - /// - /// articles to add - private void InsertArticles(IEnumerable
articles) + if (needUpdate) { - ObservableRangeCollection
articlesOld = new (_articles); - Articles = new ObservableRangeCollection
(); - - Articles.AddRange(articles); - if (articlesOld.Any()) - Articles.AddRange(articlesOld); + if (articles.Count > 0) + if (OnTopScroll || _articles?.Count < 1) + UpdateArticles(articles, feed, indexFeed); + else + UnnoticedArticles = new ObservableCollection
( articles); } + else + // Update list of articles + InsertArticles(articles); + SelectedFeed.IsLoaded = true; + + IsRefreshing = false; + + } + + /// + /// Insert a set of articles in the current list + /// + /// articles to add + private void InsertArticles(IEnumerable
articles) + { + ObservableRangeCollection
articlesOld = new (_articles); + Articles = new ObservableRangeCollection
(); - // See detail of the article - public Command GoToDetail + Articles.AddRange(articles); + if (articlesOld.Any()) + Articles.AddRange(articlesOld); + } + + // See detail of the article + public Command GoToDetail + { + get { - get + return new Command( (id) => { - return new Command( (id) => - { - var articlePage = new ArticlePage(Articles.FirstOrDefault(art => art.Id == id.ToString())); + var articlePage = new ArticlePage(Articles.FirstOrDefault(art => art.Id == id.ToString())); - _ = App.Current.Windows[0].Page.Navigation.PushAsync(articlePage); - }); ; - } + _ = App.Current.Windows[0].Page.Navigation.PushAsync(articlePage); + }); ; } + } - private bool _isMenuOpen; - private int _oldIndex; + private bool _isMenuOpen; + private int _oldIndex; - public bool IsMenuOpen + public bool IsMenuOpen + { + get { return _isMenuOpen; } + set { - get { return _isMenuOpen; } - set - { - _isMenuOpen = value; - OnPropertyChanged(nameof(IsMenuOpen)); - } + _isMenuOpen = value; + OnPropertyChanged(nameof(IsMenuOpen)); } + } - public bool IsFromDetail { get; set; } - public int CurrentFocusIndex { get; set; } - public bool ListHasBeenUpdated { get; set; } + public bool IsFromDetail { get; set; } + public int CurrentFocusIndex { get; set; } + public bool ListHasBeenUpdated { get; set; } - /// - /// Update the current article feed by adding new elements - /// - /// new articles - private void UpdateArticles(List
articles, Feed feed, int indexFeed) + /// + /// Update the current article feed by adding new elements + /// + /// new articles + private void UpdateArticles(List
articles, Feed feed, int indexFeed) + { + for (int i = 0; i < articles.Count(); ++i) { - for (int i = 0; i < articles.Count(); ++i) + var current = articles[i]; + Article existingArticle = feed.Articles?.FirstOrDefault(a => a.Id == current.Id); + if (existingArticle == null) { - var current = articles[i]; - Article existingArticle = feed.Articles?.FirstOrDefault(a => a.Id == current.Id); - if (existingArticle == null) - { - Article item = articles.FirstOrDefault(a => a.Id == current.Id); + Article item = articles.FirstOrDefault(a => a.Id == current.Id); - var index = articles.IndexOf(item); - - // Add article one by one for a better visual effect - Articles.Insert(index == -1 ? 0 + i : index, current); - } - else - { - int index = Feeds[indexFeed].Articles.IndexOf(existingArticle); - // replace the existing one with the new one - Articles.Remove(existingArticle); - Articles.Insert(index, current); - } + var index = articles.IndexOf(item); + // Add article one by one for a better visual effect + Articles.Insert(index == -1 ? 0 + i : index, current); + } + else + { + int index = Feeds[indexFeed].Articles.IndexOf(existingArticle); + // replace the existing one with the new one + Articles.Remove(existingArticle); + Articles.Insert(index, current); } + } + } - /// - /// Remove a feed from the list - /// - /// feed we want to remove - public async Task RemoveFeed(Feed feed) + /// + /// Remove a feed from the list + /// + /// feed we want to remove + public async Task RemoveFeed(Feed feed) + { + CurrentApp.ShowLoadingIndicator(); + IsBusy = true; + int feedIndex = Feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == feed.Id)); + int indexNext = feedIndex + 1; + + int indexPrev = 0; + if (feedIndex > 0) + indexPrev = feedIndex - 1; + + Feeds.RemoveAt(feedIndex); + FeedTabs.RemoveAt(feedIndex); + + Feed feedInView = new Feed(); + + await Task.Factory.StartNew(() => { - CurrentApp.ShowLoadingIndicator(); - IsBusy = true; - int feedIndex = Feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == feed.Id)); - int indexNext = feedIndex + 1; - - int indexPrev = 0; - if (feedIndex > 0) - indexPrev = feedIndex - 1; - //Feeds.Remove(feed); - Feeds.RemoveAt(feedIndex); - FeedTabs.RemoveAt(feedIndex); - - Feed feedInView = new Feed(); - - await Task.Factory.StartNew(() => + if (_selectedFeed == null) + CurrentFeedIndex = 0; + if (_selectedFeed.Id == feed.Id) { - if (_selectedFeed == null) - CurrentFeedIndex = 0; - if (_selectedFeed.Id == feed.Id) + if (_feeds.Count <= 0) { - if (_feeds.Count <= 0) - { - Articles?.Clear(); - return; + Articles?.Clear(); + return; - } + } - // We try to establish the next feed - if (indexPrev >= 0) - { - CurrentFeedIndex = indexPrev; - feedInView = _feeds[indexPrev]; - } - else - { - CurrentFeedIndex = indexNext; - feedInView = _feeds[indexNext]; - } + // We try to establish the next feed + if (indexPrev >= 0) + { + CurrentFeedIndex = indexPrev; + feedInView = _feeds[indexPrev]; } else - CurrentFeedIndex = 0; - - }) - .ContinueWith((Action)((e) => - { - // Load selected feed - _ = SwitchFeedAsync(feedInView).ContinueWith((res) => { - - CurrentApp.RemoveLoadingIndicator(); - - }); - })); - } - - /// - /// Update a feed - /// - /// Updated data of feed - public void UpdateCurrentFeed (Feed feed) - { - SelectedFeed = feed; - Feed feedToUpdate = Feeds.FirstOrDefault((e) => e.Id == feed.Id); - - // Update the item visually - if (feedToUpdate != null) - { - int index = _feeds.IndexOf(feedToUpdate); - var f =_feeds[index] = feed; + CurrentFeedIndex = indexNext; + feedInView = _feeds[indexNext]; + } } - } + else + CurrentFeedIndex = 0; - /// - /// Processed launched when the page reappear - /// - public void Resume() + }) + .ContinueWith((Action)((e) => { - if (!_dataLoaded) + // Load selected feed + _ = SwitchFeedAsync(feedInView).ContinueWith((res) => { - using (var conn = new SQLiteConnection(App.GeneralDBpath)) - { - Feeds = new ObservableCollection(conn.GetAllWithChildren()); - conn.Close(); - } - // Refresh the first feed - RefreshFirstFeed(); - // Organise feeds into tabs - CopyFeedsToTabs(); + CurrentApp.RemoveLoadingIndicator(); - return; - }; + }); + })); + } - CurrentPage.CloseDropdownMenu(); - try - { - IsBusy = true; - Articles.Clear(); - UpdateFeeds(); - } -#if DEBUG - catch (Exception ex) - { - Debug.WriteLine($"Selected Feed {JsonConvert.SerializeObject(_selectedFeed)}"); - Debug.WriteLine(ex.Message); - } -#else - catch - { - } -#endif - } + /// + /// Update a feed + /// + /// Updated data of feed + public void UpdateCurrentFeed (Feed feed) + { + SelectedFeed = feed; + Feed feedToUpdate = Feeds.FirstOrDefault((e) => e.Id == feed.Id); - /// - /// Method to refresh the first feed - /// - private void RefreshFirstFeed() + // Update the item visually + if (feedToUpdate != null) { - if (_feeds.Count > 0) - SelectedFeed = _feeds[0]; + int index = _feeds.IndexOf(feedToUpdate); + var f =_feeds[index] = feed; } + } - /// - /// Update the feeds list - /// - private void UpdateFeeds() + /// + /// Processed launched when the page reappear + /// + public void Resume() + { + if (!_dataLoaded) { - if (!_dataLoaded) return; - if (_feeds == null /*|| _selectedFeed ==null*/) - return; - Collection updatedFeeds; using (var conn = new SQLiteConnection(App.GeneralDBpath)) { - // Get the updated list of feed - updatedFeeds = new ObservableCollection(conn.GetAllWithChildren()); + Feeds = new ObservableCollection(conn.GetAllWithChildren()); conn.Close(); } + // Refresh the first feed + RefreshFirstFeed(); - if (updatedFeeds is null) - return; + // Organise feeds into tabs + CopyFeedsToTabs(); - List newFeeds = updatedFeeds.Where(feed => !_feeds.Any(item => item.Id == feed.Id)).ToList(); - List removedFeeds = _feeds.Where(feed => !updatedFeeds.Any(item => item.Id == feed.Id)).ToList(); + return; + }; - // Add the new feeds - foreach (var feed in newFeeds) - { - AddFeed(feed); - } - // Remove the outdated feeds - foreach (var feed in removedFeeds) - { - _ =RemoveFeed(feed); - } + CurrentPage.CloseDropdownMenu(); + try + { + IsBusy = true; + Articles.Clear(); + UpdateFeeds(); } - - /// - /// Add a feed to the list: this method not only add it to the list of feed but also create a tab for this feed - /// - /// feed that will be added - private void AddFeed(Feed feed) +#if DEBUG + catch (Exception ex) + { + Debug.WriteLine($"Selected Feed {JsonConvert.SerializeObject(_selectedFeed)}"); + Debug.WriteLine(ex.Message); + } +#else + catch { - Feeds.Add(feed); + } +#endif + } - // Add a tab for this feed - FeedTabs.Add(new() - { - Id = feed.Id, - Title = feed.Title, + /// + /// Method to refresh the first feed + /// + private void RefreshFirstFeed() + { + if (_feeds.Count > 0) + SelectedFeed = _feeds[0]; + } - }); - } + /// + /// Update the feeds list + /// + private void UpdateFeeds() + { + if (!_dataLoaded) return; + if (_feeds == null) return; - /// - /// Copies the items from the collection to the collection, transforming them into items. - /// - private void CopyFeedsToTabs() + Collection updatedFeeds; + using (var conn = new SQLiteConnection(App.GeneralDBpath)) { - FeedTabs.Clear(); + // Get the updated list of feed + updatedFeeds = new ObservableCollection(conn.GetAllWithChildren()); + conn.Close(); + } - var tabButtons = _feeds.Select(feed => new TabButton - { - Id = feed.Id, - Title = feed.Title - // Set other properties of TabButton if needed - }); + if (updatedFeeds is null) + return; - foreach (var tabButton in tabButtons) - { - FeedTabs.Add(tabButton); - } - - if (FeedTabs.Count >0) - FeedTabs[_currentFeedIndex].IsSelected = true; + List newFeeds = updatedFeeds.Where(feed => !_feeds.Any(item => item.Id == feed.Id)).ToList(); + List removedFeeds = _feeds.Where(feed => !updatedFeeds.Any(item => item.Id == feed.Id)).ToList(); + // Add the new feeds + foreach (var feed in newFeeds) + { + AddFeed(feed); + } + // Remove the outdated feeds + foreach (var feed in removedFeeds) + { + _ =RemoveFeed(feed); } + } + + /// + /// Add a feed to the list: this method not only add it to the list of feed but also create a tab for this feed + /// + /// feed that will be added + private void AddFeed(Feed feed) + { + Feeds.Add(feed); - /// - /// Select a tab from its index - /// - /// the index of the tab - private void SelectTab(int index) + // Add a tab for this feed + FeedTabs.Add(new() { - _oldIndex = index; + Id = feed.Id, + Title = feed.Title, + }); + } - FeedTabs[index].IsSelected = true; - } + /// + /// Copies the items from the collection to the collection, transforming them into items. + /// + private void CopyFeedsToTabs() + { + FeedTabs.Clear(); - /// - /// Select a tab after the select tab was deleted - /// - /// index of the former tab - public void SelectDefaultTab(int fromIndex) + var tabButtons = _feeds.Select(feed => new TabButton { - int indexNext = fromIndex + 1; - int indexPrev = fromIndex - 1; + Id = feed.Id, + Title = feed.Title + // Set other properties of TabButton if needed + }); - int tabIndex = 0; - if (_feeds.Count <= 0) - return; + foreach (var tabButton in tabButtons) + { + FeedTabs.Add(tabButton); + } - // We try to establish the next feed - if (indexPrev >= 0 ) - tabIndex = indexPrev; - else if (indexNext <= _feeds.Count -1) - tabIndex = indexPrev; + if (FeedTabs.Count >0) + FeedTabs[_currentFeedIndex].IsSelected = true; - if (tabIndex == -1) - tabIndex ++; - // Select the tab that we determined - SelectTab(tabIndex); + } - // Refresh the tab - //Refresh(_feeds[tabIndex]); - SelectedFeed = _feeds[tabIndex]; - } + /// + /// Select a tab from its index + /// + /// the index of the tab + private void SelectTab(int index) + { + _oldIndex = index; - /// - /// Remove a feed using it's index - /// - /// index of the feed - public void RemoveFeedByIndex(int feedIndex) - { - Articles?.Clear(); - Feeds.RemoveAt(feedIndex); - SelectDefaultTab(feedIndex); - } - /// - /// Delect a tab from its index - /// - /// the index of the tab - private void DeselectTab(int index) - { - try - { - if (index == -1) - index = 0; - //FeedTabs[index].BackgroundColour = (Xamarin.Forms.Color)Application.Current.Resources["LightDark"]; - if (FeedTabs.Count <= 0) - return; - FeedTabs[index].IsSelected = false; - } - catch - { + FeedTabs[index].IsSelected = true; + } - } - } + /// + /// Select a tab after the select tab was deleted + /// + /// index of the former tab + public void SelectDefaultTab(int fromIndex) + { + int indexNext = fromIndex + 1; + int indexPrev = fromIndex - 1; + + int tabIndex = 0; + if (_feeds.Count <= 0) + return; + + // We try to establish the next feed + if (indexPrev >= 0 ) + tabIndex = indexPrev; + else if (indexNext <= _feeds.Count -1) + tabIndex = indexPrev; + + if (tabIndex == -1) + tabIndex ++; + // Select the tab that we determined + SelectTab(tabIndex); + + // Refresh the tab + SelectedFeed = _feeds[tabIndex]; + } + + /// + /// Remove a feed using it's index + /// + /// index of the feed + public void RemoveFeedByIndex(int feedIndex) + { + Articles?.Clear(); + Feeds.RemoveAt(feedIndex); + SelectDefaultTab(feedIndex); + } - /// - /// Switch from a tab to another - /// - /// index of the tab you want to switch to - private void SwitchTabs(int index) + /// + /// Delect a tab from its index + /// + /// the index of the tab + private void DeselectTab(int index) + { + try { if (index == -1) - return; + index = 0; + if (FeedTabs.Count <= 0) + return; + FeedTabs[index].IsSelected = false; + } + catch {} + } - // Deselect the previous tab - if (_oldIndex <= _feeds.Count -1) - DeselectTab(_oldIndex); + /// + /// Switch from a tab to another + /// + /// index of the tab you want to switch to + private void SwitchTabs(int index) + { + if (index == -1) + return; - // Select the new one - SelectTab(index); - } + // Deselect the previous tab + if (_oldIndex <= _feeds.Count -1) + DeselectTab(_oldIndex); + // Select the new one + SelectTab(index); } + } diff --git a/App/ViewModels/NewsViewModel.cs b/App/ViewModels/NewsViewModel.cs index b36d50ae..997c64e2 100644 --- a/App/ViewModels/NewsViewModel.cs +++ b/App/ViewModels/NewsViewModel.cs @@ -352,6 +352,7 @@ public NewsViewModel(NewsPage currentPage) Feeds[index] = updatedFeed; }); + // Handle if a article change sees a change of bookmark state WeakReferenceMessenger.Default.Register(this, (MessageHandler)((r, m) => {