Skip to content

Commit

Permalink
Merge pull request #122 from Gamhub-io/fixes/refreshbutton_crash_iOS
Browse files Browse the repository at this point in the history
Fix crash ios
  • Loading branch information
bricefriha authored May 28, 2024
2 parents 8a7c574 + 9877c61 commit 7b19b9b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 94 deletions.
22 changes: 22 additions & 0 deletions AresNews/AresNews/Models/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,27 @@ public bool IsSaved
}
set { _isSaved = value; }
}
/// <summary>
/// Compare this article to another
/// </summary>
/// <param name="otherArticle">the other article you want to compare</param>
/// <returns></returns>
public bool IsEqualTo(Article otherArticle)
{
if (this == null || otherArticle == null)
{
return this == otherArticle;
}
foreach (var property in typeof(Article).GetProperties())
{
var value1 = property.GetValue(this);
var value2 = property.GetValue(otherArticle);
if (!Equals(value1, value2))
{
return false;
}
}
return true;
}
}
}
124 changes: 40 additions & 84 deletions AresNews/AresNews/ViewModels/NewsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Essentials;
Expand Down Expand Up @@ -189,9 +190,9 @@ public Command CloseSearch
}

// Property list of articles
private ObservableCollection<Article> _articles;
private ObservableRangeCollection<Article> _articles;

public ObservableCollection<Article> Articles
public ObservableRangeCollection<Article> Articles
{
get { return _articles; }
set
Expand All @@ -217,7 +218,7 @@ public ObservableCollection<Article> UnnoticedArticles
OnPropertyChanged(nameof(UnnoticedArticles));
}
}
private bool _onTopScroll;
private bool _onTopScroll = true;

public bool OnTopScroll
{
Expand Down Expand Up @@ -307,7 +308,7 @@ public NewsViewModel(NewsPage currentPage)
return;

CurrentApp.ShowLoadingIndicator();
_ = Task.Run(() =>
_ = Task.Run(async () =>
{
// Scroll up
CurrentPage.ScrollFeed();
Expand Down Expand Up @@ -438,20 +439,6 @@ await FetchArticles().ContinueWith((res) =>
Text = article.Title
});
});

//
//switch (Device.RuntimePlatform)
//{
// case Device.iOS:
// CurrentApp.ShowLoadingIndicator();
// _refreshFeed.Execute(null);
// break;
// case Device.Android:
// IsRefreshing = true;
// break;
// default:
// break;
//}
}

/// <summary>
Expand Down Expand Up @@ -495,7 +482,7 @@ public async Task FetchArticles(bool isFullRefresh = false)
if (string.IsNullOrEmpty(_lastCallDateTime))
{

Articles = new ObservableCollection<Article>((await CurrentApp.DataFetcher.GetMainFeedUpdate().ConfigureAwait(false)).Where(article => article.Blocked == null || article.Blocked == false));
Articles = new ObservableRangeCollection<Article>((await CurrentApp.DataFetcher.GetMainFeedUpdate().ConfigureAwait(false)).Where(article => article.Blocked == null || article.Blocked == false));

IsRefreshing = false;
_isLaunching = false;
Expand Down Expand Up @@ -533,7 +520,7 @@ public async Task FetchArticles(bool isFullRefresh = false)
}
catch (Exception ex)
{
// BLAME: the folowing lines are disgusting but it works
// BLAME: the following lines are disgusting but it works
// TODO: change this if possible
if (ex.Message.Contains("Network subsystem is down") && Device.RuntimePlatform == Device.Android && _wifiRestartCount < 3)
{
Expand All @@ -556,59 +543,56 @@ public async Task FetchArticles(bool isFullRefresh = false)
page.DisplayOfflineMessage(ex.Message);
}

// To avoid crashs: if this number is out of range we end the process
// To avoid crashes: if this number is out of range we end the process
if (articles == null || articles.Count() <= 0)
{
IsRefreshing = false;
return;
}

_= Task.Run(() =>
{
if (OnTopScroll)
// Update list of articles
UpdateArticles(articles);
else
{
UnnoticedArticles = new ObservableCollection<Article>(articles);
}
});
try
{
// Manage backuo
_ = RefreshDB();

}
catch
{
}
finally
{
_isLaunching = false;
}
//MainThread.BeginInvokeOnMainThread(() =>
//{

if (OnTopScroll)
{
// Update list of articles
UpdateArticles(articles);
try
{
// Manage backup
_ = RefreshDB();

_isLaunching = false;
IsRefreshing = false;
if (Device.RuntimePlatform == Device.iOS)
CurrentApp.RemoveLoadingIndicator();
}
catch
{
}
finally
{
_isLaunching = false;
}

// Register date of the refresh
//Preferences.Set("lastRefresh", _articles[0].FullPublishDate.ToString("dd-MM-yyy_HH:mm"));
IsRefreshing = false;
}

else
{
UnnoticedArticles = new ObservableCollection<Article>(articles);
}
//});


}
/// <summary>
/// Update the curent article feed by adding new elements
/// Update the current article feed by adding new elements
/// </summary>
/// <param name="articles">new articles</param>
private void UpdateArticles(IEnumerable<Article> articles)
{
// Create a copy of the input ObservableCollection
List<Article> listArticle = new (articles);
Collection<Article> listArticle = new (articles.ToList());

// Lists to store articles to be added and updated
List<Article> articlesToAdd = new ();
List<Article> articlesToUpdate = new ();
Collection<Article> articlesToUpdate = new ();

// Iterate through the copied list of articles
foreach (var current in listArticle)
Expand All @@ -617,43 +601,16 @@ private void UpdateArticles(IEnumerable<Article> articles)
Article existingArticle = _articles.FirstOrDefault(a => a.Id == current.Id);

if (existingArticle == null)
{
// Article doesn't exist in _articles, add it to the articlesToAdd list
articlesToAdd.Add(current);
}
else
{
Articles.Add(current);
else if (!existingArticle.IsEqualTo(current))
// Article exists in _articles, add it to the articlesToUpdate list
articlesToUpdate.Add(current);
}
}

// Add new articles to the Articles collection
foreach (var articleToAdd in articlesToAdd)
{
// Find a matching article in the 'articles' collection
Article item = articles.FirstOrDefault(a => a.Id == articleToAdd.Id);

if (item != null)
{
// If a matching article is found, get its index
var index = articles.IndexOf(item);

// Insert the new article at the found index or at the beginning if index is -1
Articles.Insert(index == -1 ? 0 : index, articleToAdd);
}
else
{
// If no matching article is found, add the new article at the end
Articles.Add(articleToAdd);
}
}

foreach (var articleToUpdate in articlesToUpdate)
{
// Find the existing article to be updated in the '_articles' collection
Article existingArticle = _articles.FirstOrDefault(a => a.Id == articleToUpdate.Id);

if (existingArticle != null)
{
// Get the index of the existing article in the 'Articles' collection
Expand All @@ -665,7 +622,6 @@ private void UpdateArticles(IEnumerable<Article> articles)
}
// If existingArticle is null, handle the case where the article to update was not found
}

}
/// <summary>
/// Sync the local db
Expand Down
5 changes: 2 additions & 3 deletions AresNews/AresNews/Views/FeedsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using AresNews.ViewModels;
using System;
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.CommunityToolkit.UI.Views;

namespace AresNews.Views
{
Expand All @@ -28,7 +28,6 @@ protected override void OnAppearing()
{
base.OnAppearing();


_vm.Resume();

}/// <summary>
Expand Down Expand Up @@ -120,7 +119,7 @@ public void RemoveRefreshButton()
private void newsCollectionView_Scrolled(object sender, ItemsViewScrolledEventArgs e)
{

// FIguring out if the scroll is on top of the screen
// Figuring out if the scroll is on top of the screen
_vm.OnTopScroll = e.FirstVisibleItemIndex == 0;
}
/// <summary>
Expand Down
9 changes: 2 additions & 7 deletions AresNews/AresNews/Views/NewsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,8 @@
CommandParameter="{Binding IsSearching, Converter={StaticResource BoolInverseConverter}}"
x:Name="newsRefreshView"
HorizontalOptions="CenterAndExpand"
RefreshColor="{StaticResource PrimaryAccent}"
>
<!--<RefreshView.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,30,0,0"/>
</OnPlatform>
</RefreshView.Padding>-->
RefreshColor="{StaticResource PrimaryAccent}">

<RefreshView.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="Android" Value="{StaticResource LightDark}"/>
Expand Down

0 comments on commit 7b19b9b

Please sign in to comment.