Skip to content

Commit

Permalink
Merge pull request #32 from bricefriha/fix/issue#30
Browse files Browse the repository at this point in the history
Avoid crashes if it's not connected to internet
  • Loading branch information
bricefriha authored Sep 18, 2021
2 parents 2d8d6c0 + 04c2252 commit 517377e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion AresNews/AresNews.Android/AresNews.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<Version>0.1.6</Version>
</PackageReference>
<PackageReference Include="sqlite-net-pcl">
<Version>1.7.335</Version>
<Version>1.8.116</Version>
</PackageReference>
<PackageReference Include="SQLiteNetExtensions">
<Version>2.1.0</Version>
Expand Down
15 changes: 14 additions & 1 deletion AresNews/AresNews/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: ExportFont("FontAwesome5Free-Regular-400.otf", Alias = "FaRegular")]
Expand All @@ -24,6 +25,7 @@ public partial class App : Application
public static Collection<Source> Sources { get; private set; }
// Property SqlLite Connection
public static SQLiteConnection SqLiteConn { get; set; }
public static SQLiteConnection BackUpConn { get; set; }
public static Service WService { get; set; }


Expand All @@ -41,7 +43,7 @@ public App()
{
#if __LOCAL__
// Set webservice
WService = new Service(host: "192.168.1.15",
WService = new Service(host: "192.168.1.12",
port: 3000,
sslCertificate: false);
#else
Expand All @@ -62,11 +64,15 @@ public App()

SqLiteConn.CreateTable<Source>();
SqLiteConn.CreateTable<Article>();
BackUpConn.CreateTable<Source>();
BackUpConn.CreateTable<Article>();

// Close the db
//CloseDb();

MainPage = new AppShell();


}
/// <summary>
/// Function to close the database
Expand All @@ -85,14 +91,21 @@ public static void StartDb()
string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

var path = Path.Combine(libraryPath, "ares.db3");
var pathBackUp = Path.Combine(libraryPath, "aresBackup.db3");

// Verify if a data base already exist
if (!File.Exists(path))
// Create the folder path.
File.Create(path);

// Verify if a data base already exist
if (!File.Exists(pathBackUp))
// Create the folder path.
File.Create(pathBackUp);

// Sqlite connection
SqLiteConn = new SQLiteConnection(path);
BackUpConn = new SQLiteConnection(pathBackUp);

}

Expand Down
2 changes: 1 addition & 1 deletion AresNews/AresNews/AresNews.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PackageReference Include="Custard" Version="0.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Refractored.MvvmHelpers" Version="1.6.2" />
<PackageReference Include="sqlite-net-pcl" Version="1.7.335" />
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="SQLiteNetExtensions" Version="2.1.0" />
<PackageReference Include="System.ServiceModel.Syndication" Version="5.0.0" />
<PackageReference Include="Xam.Plugin.HtmlLabel" Version="5.0.0" />
Expand Down
13 changes: 13 additions & 0 deletions AresNews/AresNews/Models/BackupArticles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AresNews.Models
{
public class BackupArticles : Article
{

public DateTime DateBackup { get; private set; } = DateTime.Now;

}
}
13 changes: 0 additions & 13 deletions AresNews/AresNews/Models/CustomSyndItem.cs

This file was deleted.

5 changes: 4 additions & 1 deletion AresNews/AresNews/ViewModels/BookmarkViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ await Share.RequestAsync(new ShareTextRequest
});
});
}

/// <summary>
/// Get all the articles bookmarked from the local database
/// </summary>
/// <returns></returns>
private static IEnumerable<Article> GetArticlesFromDb()
{
return App.SqLiteConn.GetAllWithChildren<Article>(recursive: true).Reverse<Article>();
Expand Down
24 changes: 23 additions & 1 deletion AresNews/AresNews/ViewModels/NewsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ObservableCollection<Article> Articles
OnPropertyChanged();
}
}
private NewsPage CurrentPage { get; set; }
// Command to add a Bookmark
private readonly Command _addBookmark;

Expand Down Expand Up @@ -188,7 +189,24 @@ await Share.RequestAsync(new ShareTextRequest
/// </summary>
public async void FetchArticles()
{
var articles = new ObservableCollection<Article>((IEnumerable<Article>)await App.WService.Get<IEnumerable<Article>>("feeds"));
var articles = new ObservableCollection<Article>();

try
{
articles = await App.WService.Get<ObservableCollection<Article>>("feeds");

// TODO: Empty the table before doing this
App.BackUpConn.DeleteAll<Article>();
App.BackUpConn.InsertAllWithChildren(articles);

}
catch (Exception ex)
{
articles = new ObservableCollection<Article> (GetBackupFromDb()) ;

var page = (NewsPage)((IShellSectionController)Shell.Current?.CurrentItem?.CurrentItem).PresentedPage;
page.DisplayOfflineMessage();
}


// Update list of articles
Expand All @@ -210,6 +228,10 @@ public async void RefreshArticles ()
}

}
private static IEnumerable<Article> GetBackupFromDb()
{
return App.BackUpConn.GetAllWithChildren<Article>(recursive: true).Reverse<Article>();
}
void ObservableCollectionCallback(IEnumerable collection, object context, Action accessMethod, bool writeAccess)
{
// `lock` ensures that only one thread access the collection at a time
Expand Down
13 changes: 12 additions & 1 deletion AresNews/AresNews/Views/NewsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.CommunityToolkit.Extensions;
using Xamarin.Forms.Xaml;

namespace AresNews.Views
Expand Down Expand Up @@ -40,6 +41,16 @@ public NewsPage()
protected override void OnAppearing()
{
base.OnAppearing();

// Is the app connected to the internet
//if (Connectivity.NetworkAccess != NetworkAccess.Internet)
//{
// this.DisplayToastAsync("You're offline", 60000);
//}
}
public void DisplayOfflineMessage ()
{
this.DisplayToastAsync("You're offline", 60000);
}
}
}

0 comments on commit 517377e

Please sign in to comment.