<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.Web.Http;

namespace WebViewNavigateExtensions.Extensions
{
    public static class WebViewExtensions
    {
        public static Task<WebViewNavigationResult> NavigateAsync(
            this WebView webView, 
            Uri uri, 
            HttpMethod method = null,
            IHttpContent content = null, 
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var httpMethod = method ?? HttpMethod.Get;
            
            TypedEventHandler<WebView, WebViewNavigationCompletedEventArgs> completedHandler = null;
            WebViewNavigationFailedEventHandler failedHandler = null;
            var tcs = new TaskCompletionSource<WebViewNavigationResult>(cancellationToken);

            completedHandler = (sender, args) =>
            {
                webView.NavigationFailed -= failedHandler;
                webView.NavigationCompleted -= completedHandler;
                var status = args.IsSuccess ? 200 : (int) args.WebErrorStatus;
                if (!tcs.Task.IsCanceled)
                {
                    tcs.SetResult(new WebViewNavigationResult(args.Uri, status));
                }
            };

            failedHandler = (sender, args) =>
            {
                webView.NavigationFailed -= failedHandler;
                webView.NavigationCompleted -= completedHandler;
                if (!tcs.Task.IsCanceled)
                {
                    tcs.SetResult(new WebViewNavigationResult(args.Uri, (int)args.WebErrorStatus));
                }
            };

            Action cancellationAction = null;
            cancellationAction = () =>
            {
                webView.NavigationFailed -= failedHandler;
                webView.NavigationCompleted -= completedHandler;
                webView.Stop();
            };

            using (var registration = cancellationToken.Register(cancellationAction))
            {
                webView.NavigationCompleted += completedHandler;
                webView.NavigationFailed += failedHandler;

                var requestMessage = new HttpRequestMessage(httpMethod, uri);

                webView.NavigateWithHttpRequestMessage(requestMessage);

                return tcs.Task;
            }
        }

        public static Task<WebViewNavigationResult> NavigateAsync(
            this WebView webView, 
            string url, 
            HttpMethod method = null, 
            IHttpContent content = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            return webView.NavigateAsync(new Uri(url), method, content, cancellationToken);
        }
    }

    public class WebViewNavigationResult
    {
        public Uri Uri { get; }

        public int StatusCode { get; }

        public bool IsSuccess => StatusCode >= 200 && StatusCode < 300;

        public WebViewNavigationResult(Uri uri, int statusCode)
        {
            Uri = uri;
            StatusCode = statusCode;
        }
    }
}<commit_msg>Added support for UnsupportedUriScheme<commit_after>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.Web.Http;

namespace WebViewNavigateExtensions.Extensions
{
    public static class WebViewExtensions
    {
        public static async Task<WebViewNavigationResult> NavigateAsync(
            this WebView webView, 
            Uri uri, 
            HttpMethod method = null,
            IHttpContent content = null, 
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var httpMethod = method ?? HttpMethod.Get;
            
            TypedEventHandler<WebView, WebViewNavigationCompletedEventArgs> completedHandler = null;
            WebViewNavigationFailedEventHandler failedHandler = null;
            TypedEventHandler<WebView, WebViewUnsupportedUriSchemeIdentifiedEventArgs> unsupportedUriHandler = null;
            var tcs = new TaskCompletionSource<WebViewNavigationResult>(cancellationToken);

            completedHandler = (sender, args) =>
            {
                var status = args.IsSuccess ? 200 : (int) args.WebErrorStatus;
                if (!tcs.Task.IsCanceled)
                {
                    tcs.SetResult(new WebViewNavigationResult(args.Uri, status));
                }
            };

            failedHandler = (sender, args) =>
            {
                if (!tcs.Task.IsCanceled)
                {
                    tcs.SetResult(new WebViewNavigationResult(args.Uri, (int)args.WebErrorStatus));
                }
            };

            unsupportedUriHandler = (sender, args) =>
            {
                args.Handled = true;
                tcs.SetException(new UnsupportedUriSchemeException(args.Uri));
            };

            Action cancellationAction = null;
            cancellationAction = () =>
            {
                webView.NavigationFailed -= failedHandler;
                webView.NavigationCompleted -= completedHandler;
                webView.Stop();
            };

            using (var registration = cancellationToken.Register(cancellationAction))
            {
                webView.NavigationCompleted += completedHandler;
                webView.NavigationFailed += failedHandler;
                webView.UnsupportedUriSchemeIdentified += unsupportedUriHandler;

                var requestMessage = new HttpRequestMessage(httpMethod, uri);

                webView.NavigateWithHttpRequestMessage(requestMessage);

                var result = await tcs.Task;

                webView.NavigationFailed -= failedHandler;
                webView.NavigationCompleted -= completedHandler;
                webView.UnsupportedUriSchemeIdentified -= unsupportedUriHandler;

                return result;
            }
        }

        public static Task<WebViewNavigationResult> NavigateAsync(
            this WebView webView, 
            string url, 
            HttpMethod method = null, 
            IHttpContent content = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            return webView.NavigateAsync(new Uri(url), method, content, cancellationToken);
        }
    }

    public class WebViewNavigationResult
    {
        public Uri Uri { get; }

        public int StatusCode { get; }

        public bool IsSuccess => StatusCode >= 200 && StatusCode < 300;

        public WebViewNavigationResult(Uri uri, int statusCode)
        {
            Uri = uri;
            StatusCode = statusCode;
        }
    }

    public class UnsupportedUriSchemeException : Exception
    {
        public Uri Uri { get; }

        public UnsupportedUriSchemeException(Uri uri)
        {
            Uri = uri;
        }
    }
}
{'fraction_non_alphanumeric': 0.07133648474430597, 'fraction_numerical': 0.002578427159432746, 'mean_word_length': 1.8556237218813907, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6275962', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1578, 'n_words': 346}