Pixiview/Gallery/OptionPage.xaml.cs
2021-08-04 10:27:41 +08:00

245 lines
8.4 KiB
C#

using System;
using System.Threading.Tasks;
using Gallery.Resources;
using Gallery.UI;
using Gallery.Utils;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Gallery
{
public partial class OptionPage : AdaptedPage
{
public static readonly BindableProperty VersionProperty = BindableProperty.Create(
nameof(Version), typeof(string), typeof(OptionPage));
public string Version
{
get => (string)GetValue(VersionProperty);
set => SetValue(VersionProperty, value);
}
public static readonly BindableProperty DownloadIllustThreadsProperty = BindableProperty.Create(
nameof(DownloadIllustThreads), typeof(string), typeof(OptionPage));
public static readonly BindableProperty IsOnR18Property = BindableProperty.Create(
nameof(IsOnR18), typeof(bool), typeof(OptionPage));
public static readonly BindableProperty SyncFavTypeProperty = BindableProperty.Create(
nameof(SyncFavType), typeof(int), typeof(OptionPage), -1);
public static readonly BindableProperty IsProxiedProperty = BindableProperty.Create(
nameof(IsProxied), typeof(bool), typeof(OptionPage));
public static readonly BindableProperty HostProperty = BindableProperty.Create(
nameof(Host), typeof(string), typeof(OptionPage));
public static readonly BindableProperty PortProperty = BindableProperty.Create(
nameof(Port), typeof(string), typeof(OptionPage));
public static readonly BindableProperty CookieProperty = BindableProperty.Create(
nameof(Cookie), typeof(string), typeof(OptionPage));
public string DownloadIllustThreads
{
get => (string)GetValue(DownloadIllustThreadsProperty);
set => SetValue(DownloadIllustThreadsProperty, value);
}
public bool IsOnR18
{
get => (bool)GetValue(IsOnR18Property);
set => SetValue(IsOnR18Property, value);
}
public int SyncFavType
{
get => (int)GetValue(SyncFavTypeProperty);
set => SetValue(SyncFavTypeProperty, value);
}
public bool IsProxied
{
get => (bool)GetValue(IsProxiedProperty);
set => SetValue(IsProxiedProperty, value);
}
public string Host
{
get => (string)GetValue(HostProperty);
set => SetValue(HostProperty, value);
}
public string Port
{
get => (string)GetValue(PortProperty);
set => SetValue(PortProperty, value);
}
public string Cookie
{
get => (string)GetValue(CookieProperty);
set => SetValue(CookieProperty, value);
}
public OptionPage()
{
BindingContext = this;
InitializeComponent();
optionFavSync.Items = new[]
{
ResourceHelper.SyncNo,
ResourceHelper.SyncPrompt,
ResourceHelper.SyncAuto,
};
SyncFavType = 0;
#if OBSOLETE
#if __IOS__
string version = Foundation.NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
int build = int.Parse(Foundation.NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString());
#elif __ANDROID__
var context = Android.App.Application.Context;
var manager = context.PackageManager;
var info = manager.GetPackageInfo(context.PackageName, 0);
string version = info.VersionName;
long build = info.LongVersionCode;
#endif
Version = $"{version} ({build})";
#endif
}
protected override void OnAppearing()
{
base.OnAppearing();
Version = $"{AppInfo.VersionString} ({AppInfo.BuildString})";
IsOnR18 = Configs.IsOnR18;
SyncFavType = (int)Configs.SyncFavType;
DownloadIllustThreads = Configs.DownloadIllustThreads.ToString();
var proxy = Configs.Proxy;
if (proxy != null)
{
IsProxied = true;
Host = proxy.Address.Host;
Port = proxy.Address.Port.ToString();
}
else
{
IsProxied = false;
Host = Preferences.Get(Configs.HostKey, string.Empty);
Port = Preferences.Get(Configs.PortKey, string.Empty);
}
Cookie = Configs.Cookie;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var r18 = IsOnR18;
var syncType = SyncFavType;
var proxied = IsProxied;
if (Configs.IsOnR18 != r18)
{
Preferences.Set(Configs.IsOnR18Key, r18);
Configs.IsOnR18 = r18;
#if LOG
App.DebugPrint($"r-18 filter: {r18}");
#endif
}
if ((int)Configs.SyncFavType != syncType)
{
Preferences.Set(Configs.SyncFavTypeKey, syncType);
Configs.SyncFavType = (SyncType)syncType;
#if LOG
App.DebugPrint($"favorite sync type changed to {Configs.SyncFavType}");
#endif
}
if (int.TryParse(DownloadIllustThreads, out int threads) && threads > 0 && threads <= 10 && threads != Configs.DownloadIllustThreads)
{
Preferences.Set(Configs.DownloadIllustThreadsKey, threads);
Configs.DownloadIllustThreads = threads;
#if LOG
App.DebugPrint($"will use {threads} threads to download illust");
#endif
}
var proxy = Configs.Proxy;
var h = Host?.Trim();
_ = int.TryParse(Port, out int pt);
if (proxied)
{
if (proxy == null ||
proxy.Address.Host != h ||
proxy.Address.Port != pt)
{
Preferences.Set(Configs.IsProxiedKey, true);
Preferences.Set(Configs.HostKey, h);
if (pt > 0)
{
Preferences.Set(Configs.PortKey, pt);
}
if (!string.IsNullOrEmpty(h) && pt > 0)
{
try
{
if (h.IndexOf(':') >= 0)
{
h = $"[{h}]";
}
var uri = new Uri($"http://{h}:{pt}");
Configs.Proxy = new System.Net.WebProxy(uri);
#if LOG
App.DebugPrint($"set proxy to: {h}:{pt}");
#endif
}
catch (Exception ex)
{
App.DebugError("on.disappearing", $"failed to parse proxy: {h}:{pt}, error: {ex.Message}");
}
}
}
}
else
{
Preferences.Set(Configs.IsProxiedKey, false);
Preferences.Set(Configs.HostKey, h);
if (pt > 0)
{
Preferences.Set(Configs.PortKey, pt);
}
if (proxy != null)
{
Configs.Proxy = null;
#if LOG
App.DebugPrint("clear proxy");
#endif
}
}
var cookie = Cookie;
if (Configs.Cookie != cookie)
{
Configs.SetCookie(cookie, true);
var index = cookie.IndexOf("PHPSESSID=");
if (index >= 0)
{
var session = cookie.Substring(index + 10);
index = session.IndexOf('_');
if (index > 0)
{
session = session.Substring(0, index);
Configs.SetUserId(session, true);
#if LOG
App.DebugPrint($"cookie changed, user id: {session}");
#endif
Task.Run(() => AppShell.Current.DoLoginInformation(true));
}
}
}
}
private async void ShareCookie_Clicked(object sender, EventArgs e)
{
await Share.RequestAsync(new ShareTextRequest
{
Text = Cookie
});
}
}
}