diff --git a/Pixiview.iOS/Pixiview.iOS.csproj b/Pixiview.iOS/Pixiview.iOS.csproj index b4a394a..35d2c29 100644 --- a/Pixiview.iOS/Pixiview.iOS.csproj +++ b/Pixiview.iOS/Pixiview.iOS.csproj @@ -83,6 +83,7 @@ + diff --git a/Pixiview.iOS/Renderers/HybridWebViewRenderer.cs b/Pixiview.iOS/Renderers/HybridWebViewRenderer.cs new file mode 100644 index 0000000..a3cdf90 --- /dev/null +++ b/Pixiview.iOS/Renderers/HybridWebViewRenderer.cs @@ -0,0 +1,80 @@ +using System; +using Foundation; +using Pixiview.iOS.Renderers; +using Pixiview.Login; +using Pixiview.Utils; +using WebKit; +using Xamarin.Forms; +using Xamarin.Forms.Platform.iOS; + +[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))] +namespace Pixiview.iOS.Renderers +{ + public class HybridWebViewRenderer : WkWebViewRenderer + { + public HybridWebViewRenderer() : this(new WKWebViewConfiguration()) + { + } + + public HybridWebViewRenderer(WKWebViewConfiguration config) : base(config) + { + } + + protected override void OnElementChanged(VisualElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (e.NewElement is HybridWebView webView) + { + string url = webView.Uri; + //Configuration.SetUrlSchemeHandler + CustomUserAgent = webView.UserAgent; + NavigationDelegate = new PixivNavigationDelegate(webView); + + LoadRequest(new NSUrlRequest(NSUrl.FromString(url))); + } + } + + private class PixivNavigationDelegate : WKNavigationDelegate + { + private readonly HybridWebView hybridWebView; + + public PixivNavigationDelegate(HybridWebView hybrid) + { + hybridWebView = hybrid; + } + + public override void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, Action decisionHandler) + { + var url = webView.Url; + if (url.Host == "www.pixiv.net" && url.Path == "/") + { + if (navigationResponse.Response is NSHttpUrlResponse response) + { + if (response.AllHeaderFields.TryGetValue(new NSString("x-userid"), out var val)) + { + Configs.SetUserId(val.ToString(), true); + } + } + } + + decisionHandler(WKNavigationResponsePolicy.Allow); + } + + public override async void DidFinishNavigation(WKWebView webView, WKNavigation navigation) + { + var url = webView.Url; + if (url.Host == "www.pixiv.net" && url.Path == "/") + { + var store = webView.Configuration.WebsiteDataStore.HttpCookieStore; + var result = await Configs.RequestCookieContainer(store); + + if (result && hybridWebView != null) + { + hybridWebView.OnLoginHandle(); + } + } + } + } + } +} diff --git a/Pixiview/App.cs b/Pixiview/App.cs index 27d7d70..3f522f0 100644 --- a/Pixiview/App.cs +++ b/Pixiview/App.cs @@ -31,6 +31,9 @@ namespace Pixiview private void InitPreferences() { + Configs.SetCookie(Preferences.Get(Configs.CookieKey, null)); + Configs.SetUserId(Preferences.Get(Configs.UserIdKey, null)); + Configs.IsOnR18 = Preferences.Get(Configs.IsOnR18Key, false); var isProxied = Preferences.Get(Configs.IsProxiedKey, false); if (isProxied) diff --git a/Pixiview/AppShell.xaml b/Pixiview/AppShell.xaml index dea4794..16eb2b4 100644 --- a/Pixiview/AppShell.xaml +++ b/Pixiview/AppShell.xaml @@ -15,6 +15,9 @@ + + + diff --git a/Pixiview/AppShell.xaml.cs b/Pixiview/AppShell.xaml.cs index 9fa8a8e..4360359 100644 --- a/Pixiview/AppShell.xaml.cs +++ b/Pixiview/AppShell.xaml.cs @@ -1,5 +1,7 @@ using System; +using Pixiview.Login; using Pixiview.Utils; +using Xamarin.Essentials; using Xamarin.Forms; namespace Pixiview @@ -42,6 +44,35 @@ namespace Pixiview StatusBarHeight = height }); } + + private void TapGestureRecognizer_Tapped(object sender, EventArgs e) + { + PushToLogin(null); + } + + private bool isLoginOpened; + + public void PushToLogin(Action after) + { + if (isLoginOpened) + { + return; + } + isLoginOpened = true; + var loginPage = new LoginPage(()=> + { + isLoginOpened = false; + after?.Invoke(); + }); + loginPage.Disappearing += (sender, e) => + { + isLoginOpened = false; + }; + MainThread.BeginInvokeOnMainThread(async () => + { + await Navigation.PushModalAsync(loginPage); + }); + } } public class BarHeightEventArgs : EventArgs diff --git a/Pixiview/Illust/IllustCollectionPage.cs b/Pixiview/Illust/IllustCollectionPage.cs index 62e2fa7..2a3018c 100644 --- a/Pixiview/Illust/IllustCollectionPage.cs +++ b/Pixiview/Illust/IllustCollectionPage.cs @@ -530,6 +530,10 @@ namespace Pixiview.Illust illustData = DoLoadIllustData(force); if (illustData == null) { + AppShell.Current.PushToLogin(()=> + { + StartLoad(true); + }); //App.DebugError("illusts.load", "failed to load illusts data."); IsLoading = false; IsBottomLoading = false; diff --git a/Pixiview/Login/HybridWebView.cs b/Pixiview/Login/HybridWebView.cs new file mode 100644 index 0000000..67c9961 --- /dev/null +++ b/Pixiview/Login/HybridWebView.cs @@ -0,0 +1,35 @@ +using System; +using Xamarin.Forms; + +namespace Pixiview.Login +{ + public class HybridWebView : WebView + { + public static readonly BindableProperty UriProperty = BindableProperty.Create( + nameof(Uri), + typeof(string), + typeof(HybridWebView)); + public static readonly BindableProperty UserAgentProperty = BindableProperty.Create( + nameof(UserAgent), + typeof(string), + typeof(HybridWebView)); + + public event EventHandler LoginHandle; + + public string Uri + { + get => (string)GetValue(UriProperty); + set => SetValue(UriProperty, value); + } + public string UserAgent + { + get => (string)GetValue(UserAgentProperty); + set => SetValue(UserAgentProperty, value); + } + + public void OnLoginHandle() + { + LoginHandle?.Invoke(this, EventArgs.Empty); + } + } +} diff --git a/Pixiview/Login/LoginPage.xaml b/Pixiview/Login/LoginPage.xaml new file mode 100644 index 0000000..73deaee --- /dev/null +++ b/Pixiview/Login/LoginPage.xaml @@ -0,0 +1,22 @@ + + + + + +