Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #117

Merged
merged 3 commits into from
Mar 2, 2024
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
2 changes: 1 addition & 1 deletion AresNews/AresNews.Android/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions AresNews/AresNews/Services/Fetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CustardApi.Objects;
using System;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Threading.Tasks;

namespace AresNews.Services
Expand All @@ -10,6 +11,7 @@ public class Fetcher
{
public static string ProdHost { get; } = "api.gamhub.io";
public static string LocalHost { get; } = "gamhubdev.ddns.net";
private static string _dateFormat = "dd-MM-yyy_HH:mm:ss";

public Service WebService { get; private set; }
public Fetcher()
Expand All @@ -25,9 +27,37 @@ public Fetcher()
sslCertificate: true);
#endif
}
public async Task<Collection<Article>> GetMainFeedUpdate(string lastUpdate)
/// <summary>
/// Get the last 2 months worth of feed
/// </summary>
/// <returns>last 2 months worth of feed</returns>
public async Task<Collection<Article>> GetMainFeedUpdate()
{
return await App.WService.Get<Collection<Article>>(controller: "feeds", action: "update", parameters: new string[] { DateTime.Now.AddMonths(-2).ToString(lastUpdate) }, jsonBody: null);
return await App.WService.Get<Collection<Article>>(controller: "feeds",
action: "update",
parameters: new string[] { DateTime.Now.AddMonths(-2).ToString(_dateFormat) },
jsonBody: null,
unSuccessCallback: e => HandleHttpException(e));
}
/// <summary>
/// Get the lastest articles since given date
/// </summary>
/// <param name="dateUpdate">given date as "dd-MM-yyy_HH:mm:ss"</param>
/// <returns>lastest articles the date provided</returns>
public async Task<Collection<Article>> GetMainFeedUpdate(string dateUpdate)
{
return await App.WService.Get<Collection<Article>>(controller: "feeds",
action: "update",
parameters: new string[] { dateUpdate },
jsonBody: null,
unSuccessCallback: e => HandleHttpException(e));
}

private async void HandleHttpException(HttpResponseMessage err)
{
#if DEBUG
throw new Exception(await err.Content.ReadAsStringAsync());
#endif
}
}
}
3 changes: 3 additions & 0 deletions AresNews/AresNews/ViewModels/FeedsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public FeedsViewModel(FeedsPage page)
FeedTabs = new ObservableCollection<TabButton>();
Feeds = new ObservableCollection<Feed>(App.SqLiteConn.GetAllWithChildren<Feed>());
_articles = new ObservableCollection<Article>();

// Organise feeds into tabs
CopyFeedsToTabs();

Expand Down Expand Up @@ -435,6 +436,8 @@ private async void AggregateFeed(Feed feed, bool firstLoad = true)
timeUpdate = Articles?.First().FullPublishDate.ToUniversalTime().ToString("dd-MM-yyy_HH:mm:ss");

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

// Make sure we have internet connection
if (Connectivity.NetworkAccess == NetworkAccess.Internet)
{

Expand Down
57 changes: 31 additions & 26 deletions AresNews/AresNews/ViewModels/NewsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AresNews.Services;
using AresNews.Views;
using MvvmHelpers;
using SQLite;
using SQLiteNetExtensions.Extensions;
using System;
using System.Collections;
Expand All @@ -27,7 +28,7 @@ public class NewsViewModel : BaseViewModel

private int _wifiRestartCount = 0;
private bool _isInCustomFeed;

private static object collisionLock = new();
private bool _isSearching;
public bool IsSearching
{
Expand Down Expand Up @@ -394,7 +395,6 @@ await Share.RequestAsync(new ShareTextRequest
}

FetchArticles(_articles.Count >=0);
//Articles.ForEach(article => { if (article.Source == null) article.Source = App.Sources.FirstOrDefault(s => s.MongoId == article.SourceId); });
}

/// <summary>
Expand All @@ -407,9 +407,11 @@ public async void FetchArticles(bool isFullRefresh = false)
if (isFullRefresh)
{

//articles = await App.WService.Get<ObservableRangeCollection<Article>>(controller: "feeds", action: "update", parameters: new string[] { DateTime.Now.AddMonths(-2).ToString("dd-MM-yyy_HH:mm:ss") }, jsonBody: null);
// the articles of the last 2 months
articles = new (await CurrentApp.DataFetcher.GetMainFeedUpdate());

var oldFeed = new Collection<Article>(_articles.Where(ats => ats.FullPublishDate > DateTime.Now.AddMonths(-2)).ToList());

articles = new (await CurrentApp.DataFetcher.GetMainFeedUpdate("dd-MM-yyy_HH:mm:ss"));
_isLaunching = false;
Articles.Clear();
Articles = new ObservableRangeCollection<Article>(articles.Where(article => article.Blocked == null || article.Blocked == false));
Expand All @@ -422,12 +424,12 @@ public async void FetchArticles(bool isFullRefresh = false)
if (Device.RuntimePlatform == Device.iOS)
CurrentApp.RemoveLoadingIndicator();

await RefreshDB();
// Check if we have new articles before refreshing the DB
if (oldFeed.Count != articles.Count)
await RefreshDB();
return;
}



try
{
if (_articles?.Count() > 0)
Expand All @@ -442,7 +444,7 @@ public async void FetchArticles(bool isFullRefresh = false)
if (string.IsNullOrEmpty(_lastCallDateTime))
{

Articles = new ObservableCollection<Article>((await App.WService.Get<ObservableCollection<Article>>(controller: "feeds", action: "update", parameters: new string[] { DateTime.Now.AddMonths(-2).ToString("dd-MM-yyy_HH:mm:ss") })).Where(article => article.Blocked == null || article.Blocked == false));
Articles = new ObservableCollection<Article>((await CurrentApp.DataFetcher.GetMainFeedUpdate()).Where(article => article.Blocked == null || article.Blocked == false));

IsRefreshing = false;
_isLaunching = false;
Expand All @@ -452,16 +454,8 @@ public async void FetchArticles(bool isFullRefresh = false)
if (_articles?.Count() > 0)
{

articles = new ObservableRangeCollection<Article>((await App.WService.Get<ObservableRangeCollection<Article>>(controller: "feeds", action: "update", parameters: new string[] { _lastCallDateTime }, unSuccessCallback: async (err) =>
{
#if DEBUG
throw new Exception (await err.Content.ReadAsStringAsync());
#endif
})).Where(article => article.Blocked == null || article.Blocked == false));



}
articles = new ObservableRangeCollection<Article>((await CurrentApp.DataFetcher.GetMainFeedUpdate(_lastCallDateTime)).Where(article => article.Blocked == null || article.Blocked == false));
}
else
{
if (_isLaunching)
Expand Down Expand Up @@ -617,22 +611,29 @@ private void UpdateArticles(IEnumerable<Article> articles)
}

}

private async Task RefreshDB()
/// <summary>
/// Sync the local db
/// </summary>
/// <returns></returns>
private Task RefreshDB()
{
try
{
await Task.Run(() =>
return Task.Run(() =>
{
lock (collisionLock)
{
using var conn = new SQLiteConnection(App.BackUpConn.DatabasePath);

App.BackUpConn.DeleteAll<Article>();
conn.DeleteAll<Article>();

App.BackUpConn.InsertAllWithChildren(_articles);
conn.InsertAllWithChildren(_articles);

foreach (var source in _articles.Select(a => a.Source).Distinct().ToList())
{
App.BackUpConn.InsertOrReplace(source);
foreach (var source in _articles.Select(a => a.Source).Distinct().ToList())
{
conn.InsertOrReplace(source);

}
}
});
}
Expand Down Expand Up @@ -688,6 +689,10 @@ private async void SearchArticles(ObservableRangeCollection<Article> articles)
_prevSearch = SearchText;

}
/// <summary>
/// Get all the articles from the db
/// </summary>
/// <returns></returns>
private static IEnumerable<Article> GetBackupFromDb()
{
return App.BackUpConn.GetAllWithChildren<Article>(recursive: true).Where(article => article.Blocked == null || article.Blocked == false).Reverse<Article>();
Expand Down