diff --git a/Pixiview/MainPage.xaml.cs b/Pixiview/MainPage.xaml.cs index 45f6f59..604a07e 100644 --- a/Pixiview/MainPage.xaml.cs +++ b/Pixiview/MainPage.xaml.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading.Tasks; using Pixiview.UI; using Pixiview.Utils; +using Xamarin.Essentials; using Xamarin.Forms; namespace Pixiview @@ -34,6 +35,7 @@ namespace Pixiview private readonly ParallelOptions parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Configs.MaxThreads }; private IllustData illustData; + private bool loaded; public MainPage() { @@ -45,12 +47,44 @@ namespace Pixiview public override void OnLoad() { 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() { 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 => { diff --git a/Pixiview/Utils/Stores.cs b/Pixiview/Utils/Stores.cs index ca127f3..341f8a0 100644 --- a/Pixiview/Utils/Stores.cs +++ b/Pixiview/Utils/Stores.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; +using Xamarin.Essentials; using Xamarin.Forms; namespace Pixiview.Utils @@ -19,6 +20,22 @@ namespace Pixiview.Utils private const string userFolder = "user-profile"; 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 LoadIllustData() { var file = Path.Combine(PersonalFolder, illustFile); @@ -144,6 +161,7 @@ namespace Pixiview.Utils catch (Exception ex) { tries++; + System.Threading.Thread.Sleep(400); App.DebugError("try.do", $"tries: {tries}, error: {ex.Message}"); } } @@ -163,28 +181,31 @@ namespace Pixiview.Utils { BaseAddress = new Uri($"{uri.Scheme}://{uri.Host}") }; - using (var request = new HttpRequestMessage(HttpMethod.Get, uri.PathAndQuery) + return TryCount(() => { - 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) + using (var request = new HttpRequestMessage(HttpMethod.Get, uri.PathAndQuery) { - 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 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 const int MaxThreads = 3;