Skip to content

Commit

Permalink
Feeds - update | fix and optimise loads when a property is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
bricefriha committed Jan 22, 2025
1 parent 491e391 commit 57990be
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
2 changes: 2 additions & 0 deletions App/Services/FeedUpdatedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace GamHubApp.Services;

public class FeedUpdatedMessage : ValueChangedMessage<Feed>
{
public Feed Feed { get; set; }
public FeedUpdatedMessage(Feed feed) : base(feed)
{
Feed = feed;
}
}
11 changes: 4 additions & 7 deletions App/ViewModels/EditFeedViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GamHubApp.Models;
using CommunityToolkit.Mvvm.Messaging;
using GamHubApp.Models;
using GamHubApp.Services;
using GamHubApp.Views;
using SQLite;
namespace GamHubApp.ViewModels;
Expand Down Expand Up @@ -62,13 +64,8 @@ public RenameFeedPopUp Page
System.Collections.ObjectModel.ObservableCollection<Feed> feeds = _context.Feeds;
_context.CurrentFeedIndex = index = feeds.IndexOf(feeds.FirstOrDefault(feed => feed.Id == _feed.Id));
_context.FeedTabs[index].Title = _feed.Title;
WeakReferenceMessenger.Default.Send(new FeedUpdatedMessage(_feed));

//_context.UpdateOrders.Add(new UpdateOrder
//{
// Feed = _feed,
// Update = UpdateOrder.FeedUpdate.Edit

//});
});

public Microsoft.Maui.Controls.Command Cancel => new Microsoft.Maui.Controls.Command(async () =>
Expand Down
55 changes: 36 additions & 19 deletions App/ViewModels/FeedsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Feed SelectedFeed
{
_selectedFeed = value;
CurrentFeedIndex = _feeds.IndexOf(value);
Refresh(value);
Refresh(value,true);
OnPropertyChanged(nameof(SelectedFeed));
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ 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<TabButton>();
using (var conn = new SQLiteConnection(App.GeneralDBpath))
Expand Down Expand Up @@ -274,8 +274,6 @@ public FeedsViewModel(FeedsPage page)

RefreshArticles.Execute(null);



}

private ObservableCollection<Article> _unnoticedArticles = new ();
Expand Down Expand Up @@ -322,8 +320,12 @@ public Command<string> FeedSelect

}); }
}

public void Refresh(Feed feed)
/// <summary>
/// Refresh selected feed
/// </summary>
/// <param name="feed">feed of choice</param>
/// <param name="force">Whether we refresh the feed from scratch or not</param>
public void Refresh(Feed feed, bool force = false)
{
if (IsBusy)
return;
Expand All @@ -334,28 +336,35 @@ public void Refresh(Feed feed)
// Determine whether or not it's the first time loading the article of this feed
bool isFirstLoad = _articles == null || _articles.Count <= 0;


_ = AggregateFeed(feed, isFirstLoad).ContinueWith(res =>
{
// End the loading indicator
IsRefreshing = false;
IsBusy = false;
CurrentApp.RemoveLoadingIndicator();
});
Task.Run(async () =>
await AggregateFeed(feed, isFirstLoad || force).ContinueWith(res =>
{
// End the loading indicator
IsRefreshing = false;
IsBusy = false;
CurrentApp.RemoveLoadingIndicator();
})
);

}

/// <summary>
/// Load articles via search
/// </summary>
/// <param name="feed">feed we are searching</param>
private async Task AggregateFeed(Feed feed, bool firstLoad = true)
private async Task AggregateFeed(Feed feed, bool force = true)
{
int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == feed.Id));

List<Article> articles = new ();

// Figure out if the feed deserve an update
string timeUpdate = string.Empty;
if (force)
{
Articles.Clear();
UnnoticedArticles.Clear();
}

if (Articles?.Count != 0)
timeUpdate = Articles?.First().FullPublishDate.ToUniversalTime().ToString("dd-MM-yyy_HH:mm:ss");
Expand All @@ -379,11 +388,20 @@ private async Task AggregateFeed(Feed feed, bool firstLoad = true)
}

}
if (force)
{
// Update list of articles
InsertArticles(articles);
SelectedFeed.IsLoaded = true;

IsRefreshing = false;
return;
}

if (needUpdate)
{
if (articles.Count > 0)
if (OnTopScroll)
if (OnTopScroll || _articles?.Count < 1)
UpdateArticles(articles, feed, indexFeed);
else
UnnoticedArticles = new ObservableCollection<Article>( articles);
Expand Down Expand Up @@ -576,10 +594,9 @@ public void Resume()
CurrentPage.CloseDropdownMenu();
try
{
IsBusy = true;
Articles.Clear();
UpdateFeeds();

if (_selectedFeed != null)
Refresh(_selectedFeed);
}
#if DEBUG
catch (Exception ex)
Expand Down
11 changes: 11 additions & 0 deletions App/ViewModels/NewsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ public NewsViewModel(NewsPage currentPage)
CurrentFeed = new Feed();



WeakReferenceMessenger.Default.Register<FeedUpdatedMessage>(this, (r, m) =>
{
Feed updatedFeed = m.Feed;
int index = Feeds.IndexOf(Feeds.FirstOrDefault(feed => feed.Id == updatedFeed.Id));

if (index == -1)
return;

Feeds[index] = updatedFeed;
});
// Handle if a article change sees a change of bookmark state
WeakReferenceMessenger.Default.Register(this, (MessageHandler<object, BookmarkChangedMessage>)((r, m) =>
{
Expand Down

0 comments on commit 57990be

Please sign in to comment.