Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions App/Services/Fetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Plugin.FirebasePushNotifications;
using System.Net.Sockets;


#if IOS
using Maui.RevenueCat.InAppBilling.Services;
#endif
Expand Down Expand Up @@ -51,7 +52,7 @@
public string PlatformsStr { get; private set; }

#if IOS
private readonly IRevenueCatBilling _revenueCatBilling;

Check warning on line 55 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-ios / iOS Publish

The field 'Fetcher._revenueCatBilling' is never used
#endif
public Fetcher(GeneralDataBase generalDataBase,
BackUpDataBase backUpDataBase,
Expand Down Expand Up @@ -212,7 +213,7 @@
/// <param name="timeUpdate">time of the last update (if applicable)</param>
/// <param name="needUpdate">does the feed need an update</param>
/// <returns></returns>
public async Task<Collection<Article>> GetFeedArticles(string keywords, string timeUpdate = null, bool needUpdate = false)
public async Task<Collection<Article>> GetFeedArticles(string keywords, string timeUpdate = null, bool needUpdate = false, CancellationTokenSource cancellationTokenSource = null)
{
if (!Fetcher.CheckFeasability())
return new Collection<Article>();
Expand All @@ -222,16 +223,18 @@
{
if (string.IsNullOrEmpty(keywords))
return new Collection<Article>();
//using (cancellationTokenSource) ;
// Convert the spaces to make it url friendly
keywords = keywords.Trim().Replace(' ', '+');

var haeders = (Headers?.Count ?? 0) > 0 ? Headers : await GetHeaders();
return await WithRetryAsync(() =>
WebService.Get<Collection<Article>>(controller: "feeds",
action: needUpdate ? "update" : null,
singleUseHeaders: haeders,
parameters: needUpdate ? [timeUpdate, keywords] : [keywords],
unSuccessCallback: (err) => _ = HandleHttpException(err)));
action: needUpdate ? "update" : null,
singleUseHeaders: haeders,
cancellationToken: cancellationTokenSource.Token,
parameters: needUpdate ? [timeUpdate, keywords] : [keywords],
unSuccessCallback: (err) => _ = HandleHttpException(err)));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -311,7 +314,7 @@
cancellationToken: cts.Token,
unSuccessCallback: e => _ = HandleHttpException(e)));
}
catch (Exception ex)

Check warning on line 317 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-ios / iOS Publish

The variable 'ex' is declared but never used

Check warning on line 317 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-android / Android Publish

The variable 'ex' is declared but never used
{
#if DEBUG
Debug.WriteLine(ex);
Expand Down Expand Up @@ -386,7 +389,7 @@
parameters: parameters,jsonBody: null,
unSuccessCallback: e => _ = HandleHttpException(e))));
}
catch (Exception ex)

Check warning on line 392 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-ios / iOS Publish

The variable 'ex' is declared but never used

Check warning on line 392 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-android / Android Publish

The variable 'ex' is declared but never used
{
#if DEBUG
Debug.WriteLine(ex);
Expand Down Expand Up @@ -429,7 +432,7 @@
return [];
return new ([.. articleTrends]);
}
catch (Exception ex)

Check warning on line 435 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-ios / iOS Publish

The variable 'ex' is declared but never used

Check warning on line 435 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-android / Android Publish

The variable 'ex' is declared but never used
{
#if DEBUG
Debug.WriteLine(ex);
Expand Down Expand Up @@ -477,7 +480,7 @@
return new ([.. dealTrends]);

}
catch (Exception ex)

Check warning on line 483 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-ios / iOS Publish

The variable 'ex' is declared but never used

Check warning on line 483 in App/Services/Fetcher.cs

View workflow job for this annotation

GitHub Actions / build-android / Android Publish

The variable 'ex' is declared but never used
{
#if DEBUG
Debug.WriteLine(ex);
Expand Down
73 changes: 31 additions & 42 deletions App/ViewModels/FeedsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public ObservableRangeCollection<Article> Articles
get { return _articles; }
set
{
_articles = value;
_articles = value;
OnPropertyChanged(nameof(Articles));
}
}
Expand Down Expand Up @@ -140,11 +140,9 @@ public TabButton SelectedFeedTab
if (IsBusy)
return;

CurrentApp.ShowLoadingIndicator();
_ = SwitchFeedAsync(feed);

RefreshArticles.Execute(null);
CurrentApp.RemoveLoadingIndicator();

});

Expand All @@ -163,6 +161,8 @@ await Task.Run(() =>

private GeneralDataBase _generalDB;

private CancellationTokenSource _articlesCancelationToken =new ();

public Command UncoverNewArticles { get; private set; }

public Command<Feed> Delete => new Command<Feed>( (feed) =>
Expand Down Expand Up @@ -207,7 +207,6 @@ public bool OnTopScroll
{
if (_feeds.Count <= 0)
return;
//IsRefreshing = true;
await Task.Run(() =>
{
try
Expand Down Expand Up @@ -260,7 +259,7 @@ public FeedsViewModel(GeneralDataBase generalDataBase)
if (UnnoticedArticles.Count <= 0)
return;

CurrentApp.ShowLoadingIndicator();
//CurrentApp.ShowLoadingIndicator();
int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == SelectedFeed.Id));
_ = Task.Run(() =>
{
Expand All @@ -272,7 +271,7 @@ public FeedsViewModel(GeneralDataBase generalDataBase)

UnnoticedArticles.Clear();

}).ContinueWith(res => CurrentApp.RemoveLoadingIndicator());
});

});

Expand Down Expand Up @@ -341,11 +340,6 @@ public Command<string> FeedSelect
/// <param name="force">Whether we refresh the feed from scratch or not</param>
public void Refresh(Feed feed, bool force = false)
{
if (IsBusy)
return;

IsBusy = true;
CurrentApp.ShowLoadingIndicator();

_= Task.Run(async () =>
{
Expand All @@ -355,11 +349,7 @@ public void Refresh(Feed feed, bool force = false)

try
{
await AggregateFeed(feed, isFirstLoad || force).ContinueWith(res =>
{
CurrentApp.RemoveLoadingIndicator();

});
await AggregateFeed(feed, isFirstLoad || force);
}
finally
{
Expand All @@ -381,24 +371,28 @@ private async Task AggregateFeed(Feed feed, bool force = true)

List<Article> articles = new ();

// Figure out if the feed deserve an update
string timeUpdate = string.Empty;
if (force)
{
Articles.Clear();
UnnoticedArticles.Clear();
}
// 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");

if (Articles?.Count != 0)
timeUpdate = Articles?.First().FullPublishDate.ToUniversalTime().ToString("dd-MM-yyy_HH:mm:ss");
bool needUpdate = feed.IsLoaded && !string.IsNullOrEmpty(timeUpdate);

bool needUpdate = feed.IsLoaded && !string.IsNullOrEmpty(timeUpdate);

// Make sure we have internet connection
if (Connectivity.NetworkAccess == NetworkAccess.Internet)
_articlesCancelationToken.Cancel();
_articlesCancelationToken = new CancellationTokenSource();
// 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();
needUpdate,
_articlesCancelationToken)).Where(article => (article.Blocked == null || article.Blocked == false) && article.Source.IsActive).ToList();

// Offline search
else
Expand Down Expand Up @@ -446,17 +440,17 @@ private async Task AggregateFeed(Feed feed, bool force = true)
private void InsertArticles(IEnumerable<Article> articles, bool force = false)
{
ObservableRangeCollection<Article> articlesOld = new (_articles);
if (force)
{
Articles = new ObservableRangeCollection<Article>(articles);
return;
}
if (force)
{
Articles = new ObservableRangeCollection<Article>(articles);
return;
}

Articles = new ObservableRangeCollection<Article>();
Articles = new ObservableRangeCollection<Article>();

Articles.AddRange(articles);
if (articlesOld.Any())
Articles.AddRange(articlesOld);
Articles.AddRange(articlesOld);

}

Expand Down Expand Up @@ -575,12 +569,7 @@ await Task.Factory.StartNew(() =>
.ContinueWith((Action<Task>)((e) =>
{
// Load selected feed
_ = SwitchFeedAsync(feedInView).ContinueWith((res) =>
{

CurrentApp.RemoveLoadingIndicator();

});
_ = SwitchFeedAsync(feedInView);
}));
}

Expand Down
13 changes: 13 additions & 0 deletions App/Views/FeedsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
</RefreshView.BackgroundColor>
<Grid>
<CollectionView ItemsSource="{Binding Articles}"
IsEnabled="{Binding Articles.Count,
Converter={StaticResource ZeroToFalseConverter}}"
VerticalScrollBarVisibility="Never"
x:Name="newsCollectionView"
Scrolled="newsCollectionView_Scrolled"
Expand All @@ -130,6 +132,17 @@
</BoxView.HeightRequest>
</BoxView>
</CollectionView.Header>
<CollectionView.EmptyView>
<StackLayout VerticalOptions="Fill">
<controls:ArticlePreview Margin="8,5,0,0"/>
<controls:ArticlePreview Margin="8,5,0,0"/>
<controls:ArticlePreview Margin="8,5,0,0"/>
<controls:ArticlePreview Margin="8,5,0,0"/>
<controls:ArticlePreview Margin="8,5,0,0"/>
<controls:ArticlePreview Margin="8,5,0,0"/>

</StackLayout>
</CollectionView.EmptyView>
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical"/>
</CollectionView.ItemsLayout>
Expand Down
Loading