From 9cc5aeaf25563d55c29b5fd94ddb765b742d56b7 Mon Sep 17 00:00:00 2001 From: Brice Friha Date: Sat, 2 Mar 2024 10:32:33 +0000 Subject: [PATCH 1/2] #115 - crash related to issue with a sqlite --- .../Resources/Resource.designer.cs | 2 +- .../AresNews/ViewModels/FeedsViewModel.cs | 3 +++ AresNews/AresNews/ViewModels/NewsViewModel.cs | 26 ++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/AresNews/AresNews.Android/Resources/Resource.designer.cs b/AresNews/AresNews.Android/Resources/Resource.designer.cs index e4036847..4c6f71be 100644 --- a/AresNews/AresNews.Android/Resources/Resource.designer.cs +++ b/AresNews/AresNews.Android/Resources/Resource.designer.cs @@ -14,7 +14,7 @@ namespace AresNews.Droid { - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "13.2.1.111")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "13.2.2.120")] public partial class Resource { diff --git a/AresNews/AresNews/ViewModels/FeedsViewModel.cs b/AresNews/AresNews/ViewModels/FeedsViewModel.cs index cc92efe2..f16bf6cb 100644 --- a/AresNews/AresNews/ViewModels/FeedsViewModel.cs +++ b/AresNews/AresNews/ViewModels/FeedsViewModel.cs @@ -237,6 +237,7 @@ public FeedsViewModel(FeedsPage page) FeedTabs = new ObservableCollection(); Feeds = new ObservableCollection(App.SqLiteConn.GetAllWithChildren()); _articles = new ObservableCollection
(); + // Organise feeds into tabs CopyFeedsToTabs(); @@ -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) { diff --git a/AresNews/AresNews/ViewModels/NewsViewModel.cs b/AresNews/AresNews/ViewModels/NewsViewModel.cs index f5d41b83..c16d6fd1 100644 --- a/AresNews/AresNews/ViewModels/NewsViewModel.cs +++ b/AresNews/AresNews/ViewModels/NewsViewModel.cs @@ -3,6 +3,7 @@ using AresNews.Services; using AresNews.Views; using MvvmHelpers; +using SQLite; using SQLiteNetExtensions.Extensions; using System; using System.Collections; @@ -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 { @@ -617,22 +618,29 @@ private void UpdateArticles(IEnumerable
articles) } } - + /// + /// Sync the local db + /// + /// private async Task RefreshDB() { try { await Task.Run(() => { + lock (collisionLock) + { + using var conn = new SQLiteConnection(App.BackUpConn.DatabasePath); - App.BackUpConn.DeleteAll
(); + conn.DeleteAll
(); - 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); + } } }); } @@ -688,6 +696,10 @@ private async void SearchArticles(ObservableRangeCollection
articles) _prevSearch = SearchText; } + /// + /// Get all the articles from the db + /// + /// private static IEnumerable
GetBackupFromDb() { return App.BackUpConn.GetAllWithChildren
(recursive: true).Where(article => article.Blocked == null || article.Blocked == false).Reverse
(); From b3510101afcb60495963ce910517d86a96cda2fd Mon Sep 17 00:00:00 2001 From: Brice Friha Date: Sat, 2 Mar 2024 11:10:42 +0000 Subject: [PATCH 2/2] Optimse code for FetchArticles() --- AresNews/AresNews/Services/Fetcher.cs | 34 +++++++++++++++++-- AresNews/AresNews/ViewModels/NewsViewModel.cs | 31 +++++++---------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/AresNews/AresNews/Services/Fetcher.cs b/AresNews/AresNews/Services/Fetcher.cs index ee804432..631a20ac 100644 --- a/AresNews/AresNews/Services/Fetcher.cs +++ b/AresNews/AresNews/Services/Fetcher.cs @@ -2,6 +2,7 @@ using CustardApi.Objects; using System; using System.Collections.ObjectModel; +using System.Net.Http; using System.Threading.Tasks; namespace AresNews.Services @@ -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() @@ -25,9 +27,37 @@ public Fetcher() sslCertificate: true); #endif } - public async Task> GetMainFeedUpdate(string lastUpdate) + /// + /// Get the last 2 months worth of feed + /// + /// last 2 months worth of feed + public async Task> GetMainFeedUpdate() { - return await App.WService.Get>(controller: "feeds", action: "update", parameters: new string[] { DateTime.Now.AddMonths(-2).ToString(lastUpdate) }, jsonBody: null); + return await App.WService.Get>(controller: "feeds", + action: "update", + parameters: new string[] { DateTime.Now.AddMonths(-2).ToString(_dateFormat) }, + jsonBody: null, + unSuccessCallback: e => HandleHttpException(e)); + } + /// + /// Get the lastest articles since given date + /// + /// given date as "dd-MM-yyy_HH:mm:ss" + /// lastest articles the date provided + public async Task> GetMainFeedUpdate(string dateUpdate) + { + return await App.WService.Get>(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 } } } diff --git a/AresNews/AresNews/ViewModels/NewsViewModel.cs b/AresNews/AresNews/ViewModels/NewsViewModel.cs index c16d6fd1..61fb6b7e 100644 --- a/AresNews/AresNews/ViewModels/NewsViewModel.cs +++ b/AresNews/AresNews/ViewModels/NewsViewModel.cs @@ -395,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); }); } /// @@ -408,9 +407,11 @@ public async void FetchArticles(bool isFullRefresh = false) if (isFullRefresh) { - //articles = await App.WService.Get>(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
(_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
(articles.Where(article => article.Blocked == null || article.Blocked == false)); @@ -423,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) @@ -443,7 +444,7 @@ public async void FetchArticles(bool isFullRefresh = false) if (string.IsNullOrEmpty(_lastCallDateTime)) { - Articles = new ObservableCollection
((await App.WService.Get>(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
((await CurrentApp.DataFetcher.GetMainFeedUpdate()).Where(article => article.Blocked == null || article.Blocked == false)); IsRefreshing = false; _isLaunching = false; @@ -453,16 +454,8 @@ public async void FetchArticles(bool isFullRefresh = false) if (_articles?.Count() > 0) { - articles = new ObservableRangeCollection
((await App.WService.Get>(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
((await CurrentApp.DataFetcher.GetMainFeedUpdate(_lastCallDateTime)).Where(article => article.Blocked == null || article.Blocked == false)); + } else { if (_isLaunching) @@ -622,11 +615,11 @@ private void UpdateArticles(IEnumerable
articles) /// Sync the local db ///
/// - private async Task RefreshDB() + private Task RefreshDB() { try { - await Task.Run(() => + return Task.Run(() => { lock (collisionLock) {