Gallery/Gallery.Share/Views/OptionPage.xaml.cs
2021-08-11 14:09:03 +08:00

153 lines
5.3 KiB
C#

using System;
using Gallery.Resources.UI;
using Gallery.Util;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Gallery.Views
{
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 DownloadThreadsProperty = BindableProperty.Create(nameof(DownloadThreads), typeof(string), typeof(OptionPage));
public static readonly BindableProperty IsProxiedProperty = BindableProperty.Create(nameof(IsProxied), typeof(bool), typeof(OptionPage));
public static readonly BindableProperty ProxyHostProperty = BindableProperty.Create(nameof(ProxyHost), typeof(string), typeof(OptionPage));
public static readonly BindableProperty ProxyPortProperty = BindableProperty.Create(nameof(ProxyPort), typeof(string), typeof(OptionPage));
public string DownloadThreads
{
get => (string)GetValue(DownloadThreadsProperty);
set => SetValue(DownloadThreadsProperty, value);
}
public bool IsProxied
{
get => (bool)GetValue(IsProxiedProperty);
set => SetValue(IsProxiedProperty, value);
}
public string ProxyHost
{
get => (string)GetValue(ProxyHostProperty);
set => SetValue(ProxyHostProperty, value);
}
public string ProxyPort
{
get => (string)GetValue(ProxyPortProperty);
set => SetValue(ProxyPortProperty, value);
}
public OptionPage()
{
InitializeComponent();
#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})";
DownloadThreads = Config.DownloadThreads.ToString();
var proxy = Config.Proxy;
if (proxy != null)
{
IsProxied = true;
ProxyHost = proxy.Address.Host;
ProxyPort = proxy.Address.Port.ToString();
}
else
{
IsProxied = false;
ProxyHost = Preferences.Get(Config.ProxyHostKey, string.Empty);
ProxyPort = Preferences.Get(Config.ProxyPortKey, string.Empty);
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var proxied = IsProxied;
if (int.TryParse(DownloadThreads, out int threads) && threads > 0 && threads <= 10 && threads != Config.DownloadThreads)
{
Preferences.Set(Config.DownloadThreadsKey, threads);
Config.DownloadThreads = threads;
#if DEBUG
Log.Print($"will use {threads} threads to download image");
#endif
}
var proxy = Config.Proxy;
var h = ProxyHost?.Trim();
int.TryParse(ProxyPort, out int pt);
if (proxied &&
!string.IsNullOrEmpty(h) &&
pt > 0 && pt < 65535)
{
if (proxy == null ||
proxy.Address.Host != h ||
proxy.Address.Port != pt)
{
Preferences.Set(Config.IsProxiedKey, true);
Preferences.Set(Config.ProxyHostKey, h);
Preferences.Set(Config.ProxyPortKey, pt);
try
{
if (h.IndexOf(':') >= 0)
{
h = $"[{h}]";
}
var uri = new Uri($"http://{h}:{pt}");
Config.Proxy = new System.Net.WebProxy(uri);
#if DEBUG
Log.Print($"set proxy to: {uri}");
#endif
}
catch (Exception ex)
{
Log.Error("on.disappearing", $"failed to parse proxy: {h}:{pt}, error: {ex.Message}");
}
}
}
else
{
Preferences.Set(Config.IsProxiedKey, false);
Preferences.Set(Config.ProxyHostKey, h);
if (pt > 0)
{
Preferences.Set(Config.ProxyPortKey, pt);
}
if (proxy != null)
{
Config.Proxy = null;
#if DEBUG
Log.Print("clear proxy");
#endif
}
}
}
}
}