network issues

This commit is contained in:
Tsanie Lily 2020-05-05 02:21:09 +08:00
parent 5e403a86d7
commit 0ce7757ec4
2 changed files with 68 additions and 13 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Pixiview.UI; using Pixiview.UI;
using Pixiview.Utils; using Pixiview.Utils;
using Xamarin.Essentials;
using Xamarin.Forms; using Xamarin.Forms;
namespace Pixiview namespace Pixiview
@ -34,6 +35,7 @@ namespace Pixiview
private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads }; private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads };
private IllustData illustData; private IllustData illustData;
private bool loaded;
public MainPage() public MainPage()
{ {
@ -45,12 +47,44 @@ namespace Pixiview
public override void OnLoad() public override void OnLoad()
{ {
App.DebugPrint($"folder: {Stores.PersonalFolder}"); App.DebugPrint($"folder: {Stores.PersonalFolder}");
Task.Run(DoLoadIllusts); }
protected override void OnAppearing()
{
base.OnAppearing();
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
if (!loaded && Stores.NetworkAvailable)
{
loaded = true;
Task.Run(DoLoadIllusts);
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Connectivity.ConnectivityChanged -= Connectivity_ConnectivityChanged;
}
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
if (!loaded && (e.NetworkAccess == NetworkAccess.Internet || e.NetworkAccess == NetworkAccess.ConstrainedInternet))
{
loaded = true;
Task.Run(DoLoadIllusts);
}
} }
async void DoLoadIllusts() async void DoLoadIllusts()
{ {
illustData = await Stores.LoadIllustData(); illustData = await Stores.LoadIllustData();
if (illustData == null)
{
App.DebugError("illusts.load", "failed to load illusts data.");
return;
}
var data = illustData.body.page.follow.Select(i => var data = illustData.body.page.follow.Select(i =>
{ {

View File

@ -6,6 +6,7 @@ using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Xamarin.Essentials;
using Xamarin.Forms; using Xamarin.Forms;
namespace Pixiview.Utils namespace Pixiview.Utils
@ -19,6 +20,22 @@ namespace Pixiview.Utils
private const string userFolder = "user-profile"; private const string userFolder = "user-profile";
private const string illustFile = "illust.json"; private const string illustFile = "illust.json";
public static bool NetworkAvailable
{
get
{
try
{
return Connectivity.NetworkAccess == NetworkAccess.Internet
|| Connectivity.NetworkAccess == NetworkAccess.ConstrainedInternet;
}
catch
{
return false;
}
}
}
public static async Task<IllustData> LoadIllustData() public static async Task<IllustData> LoadIllustData()
{ {
var file = Path.Combine(PersonalFolder, illustFile); var file = Path.Combine(PersonalFolder, illustFile);
@ -144,6 +161,7 @@ namespace Pixiview.Utils
catch (Exception ex) catch (Exception ex)
{ {
tries++; tries++;
System.Threading.Thread.Sleep(400);
App.DebugError("try.do", $"tries: {tries}, error: {ex.Message}"); App.DebugError("try.do", $"tries: {tries}, error: {ex.Message}");
} }
} }
@ -163,28 +181,31 @@ namespace Pixiview.Utils
{ {
BaseAddress = new Uri($"{uri.Scheme}://{uri.Host}") BaseAddress = new Uri($"{uri.Scheme}://{uri.Host}")
}; };
using (var request = new HttpRequestMessage(HttpMethod.Get, uri.PathAndQuery) return TryCount(() =>
{ {
Version = new Version(2, 0) using (var request = new HttpRequestMessage(HttpMethod.Get, uri.PathAndQuery)
})
{
request.Headers.Referrer = referer == null ? Configs.Referer : new Uri(referer);
request.Headers.Add("user-agent", Configs.UserAgent);
if (headers != null)
{ {
foreach (var (header, value) in headers) Version = new Version(2, 0)
})
{
request.Headers.Referrer = referer == null ? Configs.Referer : new Uri(referer);
request.Headers.Add("user-agent", Configs.UserAgent);
if (headers != null)
{ {
request.Headers.Add(header, value); foreach (var (header, value) in headers)
{
request.Headers.Add(header, value);
}
} }
return client.SendAsync(request).Result;
} }
return TryCount(() => client.SendAsync(request).Result); });
}
} }
} }
public static class Configs public static class Configs
{ {
public static readonly WebProxy Proxy = new WebProxy("10.0.10.100", 8088); public static readonly WebProxy Proxy = new WebProxy("router.tsanie.us", 8088);
public static readonly Uri Referer = new Uri("https://www.pixiv.net/"); public static readonly Uri Referer = new Uri("https://www.pixiv.net/");
public const int MaxThreads = 3; public const int MaxThreads = 3;