feature: option page - set proxy

This commit is contained in:
Tsanie Lily 2020-05-09 22:32:02 +08:00
parent f42705d1d2
commit bdee786f3a
12 changed files with 266 additions and 10 deletions

View File

@ -81,6 +81,7 @@
<Compile Include="Renderers\AppShellSection\AppShellSectionRootHeader.cs" />
<Compile Include="Renderers\SegmentedControlRenderer.cs" />
<Compile Include="Effects\LongPressEffectImplement.cs" />
<Compile Include="Renderers\OptionEntryRenderer.cs" />
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="Resources\LaunchScreen.storyboard" />

View File

@ -0,0 +1,23 @@
using System;
using Pixiview.iOS.Renderers;
using Pixiview.UI;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(OptionEntry), typeof(OptionEntryRenderer))]
namespace Pixiview.iOS.Renderers
{
public class OptionEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var control = Control;
if (control != null)
{
control.BorderStyle = UIKit.UITextBorderStyle.None;
}
}
}
}

View File

@ -36,6 +36,29 @@ namespace Pixiview
SetTheme(theme, true);
}
private void InitPreferences()
{
var isProxied = Preferences.Get(Configs.IsProxiedKey, false);
if (isProxied)
{
var host = Preferences.Get(Configs.HostKey, string.Empty);
int port = Preferences.Get(Configs.PortKey, 0);
if (!string.IsNullOrEmpty(host) && port > 0)
{
Configs.Proxy = new System.Net.WebProxy(host, port);
DebugPrint($"load proxy: {host}:{port}");
}
else
{
Configs.Proxy = null;
}
}
else
{
Configs.Proxy = null;
}
}
private void InitLanguage(IEnvironmentService service)
{
var ci = service.GetCurrentCultureInfo();
@ -76,6 +99,7 @@ namespace Pixiview
MainPage = new AppShell();
InitResources(service);
InitPreferences();
}
protected override void OnSleep()

View File

@ -4,9 +4,22 @@
xmlns:u="clr-namespace:Pixiview.UI"
xmlns:r="clr-namespace:Pixiview.Resources"
x:Class="Pixiview.OptionPage"
BackgroundColor="{DynamicResource WindowColor}"
Title="{r:Text Option}">
<ContentPage.Content>
<Label Text="Option" Margin="20"/>
</ContentPage.Content>
<TableView Intent="Settings" VerticalOptions="Start"
BackgroundColor="{DynamicResource NavColor}">
<TableRoot>
<TableSection Title="{r:Text Proxy}">
<u:OptionSwitchCell Title="{r:Text Enabled}"
IsToggled="{Binding IsProxied, Mode=TwoWay}"/>
</TableSection>
<TableSection Title="{r:Text Detail}">
<u:OptionEntryCell Title="{r:Text Host}"
Text="{Binding Host, Mode=TwoWay}"
Keyboard="Url" Placeholder="www.example.com"/>
<u:OptionEntryCell Title="{r:Text Port}"
Text="{Binding Port, Mode=TwoWay}"
Keyboard="Numeric" Placeholder="8080"/>
</TableSection>
</TableRoot>
</TableView>
</u:AdaptedPage>

View File

@ -1,12 +1,79 @@
using Pixiview.UI;
using Pixiview.Utils;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Pixiview
{
public partial class OptionPage : AdaptedPage
{
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 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 OptionPage()
{
BindingContext = this;
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
IsProxied = Preferences.Get(Configs.IsProxiedKey, false);
Host = Preferences.Get(Configs.HostKey, string.Empty);
int pt = Preferences.Get(Configs.PortKey, 0);
if (pt > 0)
{
Port = pt.ToString();
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var proxied = IsProxied;
var h = Host?.Trim();
Preferences.Set(Configs.IsProxiedKey, proxied);
Preferences.Set(Configs.HostKey, h);
var p = Port;
if (int.TryParse(p, out int pt) && pt > 0)
{
Preferences.Set(Configs.PortKey, pt);
}
if (proxied && !string.IsNullOrEmpty(h) && pt > 0)
{
Configs.Proxy = new System.Net.WebProxy(h, pt);
App.DebugPrint($"set proxy to: {h}:{pt}");
}
else
{
Configs.Proxy = null;
App.DebugPrint($"clear proxy");
}
}
}
}

View File

@ -5,6 +5,11 @@
<Cancel>取消</Cancel>
<Yes></Yes>
<No></No>
<Proxy>代理</Proxy>
<Detail>详细</Detail>
<Enabled>启用</Enabled>
<Host>主机</Host>
<Port>端口</Port>
<R18>R-18</R18>
<Follow>已关注</Follow>
<Recommends>推荐</Recommends>

106
Pixiview/UI/OptionCell.cs Normal file
View File

@ -0,0 +1,106 @@
using Pixiview.UI.Theme;
using Pixiview.Utils;
using Xamarin.Forms;
namespace Pixiview.UI
{
public class OptionEntry : Entry { }
public abstract class OptionCell : ViewCell
{
public static readonly BindableProperty TitleProperty = BindableProperty.Create(
nameof(Title), typeof(string), typeof(OptionCell));
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
protected abstract View Content { get; }
public OptionCell()
{
View = new Grid
{
BindingContext = this,
Padding = new Thickness(20, 0),
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(.2, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(.8, GridUnitType.Star) }
},
Children =
{
new Label
{
LineBreakMode = LineBreakMode.TailTruncation,
VerticalOptions = LayoutOptions.Center
}
.Binding(Label.TextProperty, nameof(Title))
.DynamicResource(Label.TextColorProperty, ThemeBase.TextColor),
Content.GridColumn(1)
}
}
.DynamicResource(VisualElement.BackgroundColorProperty, ThemeBase.OptionTintColor);
}
}
public class OptionSwitchCell : OptionCell
{
public static readonly BindableProperty IsToggledProperty = BindableProperty.Create(
nameof(IsToggled), typeof(bool), typeof(OptionSwitchCell));
public bool IsToggled
{
get => (bool)GetValue(IsToggledProperty);
set => SetValue(IsToggledProperty, value);
}
protected override View Content => new Switch
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center
}.Binding(Switch.IsToggledProperty, nameof(IsToggled), BindingMode.TwoWay);
}
public class OptionEntryCell : OptionCell
{
public static readonly BindableProperty TextProperty = BindableProperty.Create(
nameof(Text), typeof(string), typeof(OptionSwitchCell));
public static readonly BindableProperty KeyboardProperty = BindableProperty.Create(
nameof(Keyboard), typeof(Keyboard), typeof(OptionSwitchCell));
public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(
nameof(Placeholder), typeof(string), typeof(OptionSwitchCell));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public Keyboard Keyboard
{
get => (Keyboard)GetValue(KeyboardProperty);
set => SetValue(KeyboardProperty, value);
}
public string Placeholder
{
get => (string)GetValue(PlaceholderProperty);
set => SetValue(PlaceholderProperty, value);
}
protected override View Content => new OptionEntry
{
HorizontalOptions = LayoutOptions.Fill,
HorizontalTextAlignment = TextAlignment.End,
VerticalOptions = LayoutOptions.Center,
ReturnType = ReturnType.Next
}
.Binding(Entry.TextProperty, nameof(Text), BindingMode.TwoWay)
.Binding(InputView.KeyboardProperty, nameof(Keyboard))
.Binding(Entry.PlaceholderProperty, nameof(Placeholder))
.DynamicResource(Entry.TextColorProperty, ThemeBase.TextColor)
.DynamicResource(VisualElement.BackgroundColorProperty, ThemeBase.OptionTintColor);
}
}

View File

@ -36,6 +36,7 @@ namespace Pixiview.UI.Theme
Add(MaskColor, Color.FromRgba(0xff, 0xff, 0xff, 0x50));
Add(NavColor, Color.Black);
Add(NavSelectedColor, Color.FromRgb(0x22, 0x22, 0x22));
Add(OptionTintColor, Color.FromRgb(0x11, 0x11, 0x11));
}
}
}

View File

@ -36,6 +36,7 @@ namespace Pixiview.UI.Theme
Add(MaskColor, Color.FromRgba(0, 0, 0, 0x50));
Add(NavColor, Color.FromRgb(0xf0, 0xf0, 0xf0));
Add(NavSelectedColor, Color.LightGray);
Add(OptionTintColor, Color.White);
}
}
}

View File

@ -23,6 +23,7 @@ namespace Pixiview.UI.Theme
public const string MaskColor = nameof(MaskColor);
public const string NavColor = nameof(NavColor);
public const string NavSelectedColor = nameof(NavSelectedColor);
public const string OptionTintColor = nameof(OptionTintColor);
public const string IconLightFontFamily = nameof(IconLightFontFamily);
public const string IconRegularFontFamily = nameof(IconRegularFontFamily);

View File

@ -4,15 +4,15 @@ namespace Pixiview.Utils
{
public static class Extensions
{
public static T Binding<T>(this T view, BindableProperty property, string name) where T : BindableObject
public static T Binding<T>(this T view, BindableProperty property, string name, BindingMode mode = BindingMode.Default) where T : BindableObject
{
if (name == null)
{
view.SetValue(property, default(T));
view.SetValue(property, property.DefaultValue);
}
else
{
view.SetBinding(property, name);
view.SetBinding(property, name, mode);
}
return view;
}
@ -28,6 +28,12 @@ namespace Pixiview.Utils
Grid.SetRow(view, row);
return view;
}
public static T GridColumn<T>(this T view, int column) where T : BindableObject
{
Grid.SetColumn(view, column);
return view;
}
}
public static class Screen

View File

@ -439,12 +439,16 @@ namespace Pixiview.Utils
{
App.DebugPrint($"GET: {url}");
var uri = new Uri(url);
var proxy = Configs.Proxy;
var handler = new HttpClientHandler
{
Proxy = Configs.Proxy,
UseProxy = true,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
if (proxy != null)
{
handler.Proxy = proxy;
handler.UseProxy = true;
}
var client = new HttpClient(handler)
{
BaseAddress = new Uri($"{uri.Scheme}://{uri.Host}")
@ -468,7 +472,11 @@ namespace Pixiview.Utils
public static class Configs
{
public static readonly WebProxy Proxy = new WebProxy("router.tsanie.us", 8088);
public const string IsProxiedKey = "isProxied";
public const string HostKey = "host";
public const string PortKey = "port";
public static WebProxy Proxy;
public const int MaxThreads = 3;
public const string Referer = "https://www.pixiv.net/";