Skip to content

Commit c0d6dfd

Browse files
authored
[Android] Clean up a little bit WebView (#2431)
* [Android] Clean uo a little bit WebView * [Android] Call mapper methods not static methods * Fix order * [Android] Check state before loading source * Add ProcessSourceWhenReady * Update WebViewHandler.Android.cs
1 parent 6cd68f8 commit c0d6dfd

File tree

4 files changed

+61
-41
lines changed

4 files changed

+61
-41
lines changed
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,67 @@
11
using Android.Webkit;
2-
using Android.Widget;
32
using static Android.Views.ViewGroup;
43
using AWebView = Android.Webkit.WebView;
54

65
namespace Microsoft.Maui.Handlers
76
{
87
public partial class WebViewHandler : ViewHandler<IWebView, AWebView>
98
{
9+
WebViewClient? _webViewClient;
10+
WebChromeClient? _webChromeClient;
11+
bool _firstRun = true;
12+
1013
protected override AWebView CreateNativeView()
1114
{
12-
var aWebView = new MauiWebView(Context!)
15+
return new MauiWebView(Context!)
1316
{
14-
#pragma warning disable 618 // This can probably be replaced with LinearLayout(LayoutParams.MatchParent, LayoutParams.MatchParent); just need to test that theory
15-
LayoutParameters = new AbsoluteLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent, 0, 0)
16-
#pragma warning restore 618
17+
LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent)
1718
};
19+
}
1820

19-
if (aWebView.Settings != null)
20-
{
21-
aWebView.Settings.JavaScriptEnabled = true;
22-
aWebView.Settings.DomStorageEnabled = true;
23-
}
24-
return aWebView;
21+
public override void SetVirtualView(IView view)
22+
{
23+
_firstRun = true;
24+
base.SetVirtualView(view);
25+
// At this time all the mappers were already called
26+
_firstRun = false;
27+
ProcessSourceWhenReady(this, VirtualView);
2528
}
2629

2730
protected override void DisconnectHandler(AWebView nativeView)
2831
{
2932
nativeView.StopLoading();
30-
nativeView?.Dispose();
3133
}
3234

3335
public static void MapSource(WebViewHandler handler, IWebView webView)
3436
{
35-
IWebViewDelegate? webViewDelegate = handler.NativeView as IWebViewDelegate;
37+
ProcessSourceWhenReady(handler, webView);
38+
}
3639

40+
public static void MapWebViewClient(WebViewHandler handler, IWebView webView)
41+
{
42+
handler.NativeView.SetWebViewClient(handler._webViewClient ??= new WebViewClient());
43+
}
44+
45+
public static void MapWebChromeClient(WebViewHandler handler, IWebView webView)
46+
{
47+
handler.NativeView.SetWebChromeClient(handler._webChromeClient ??= new WebChromeClient());
48+
49+
}
50+
51+
public static void MapWebViewSettings(WebViewHandler handler, IWebView webView)
52+
{
53+
handler.NativeView.UpdateSettings(webView, true, true);
54+
}
55+
56+
static void ProcessSourceWhenReady(WebViewHandler handler, IWebView webView)
57+
{
58+
//We want to load the source after making sure the mapper for webclients
59+
//and settings were called already
60+
if (handler._firstRun)
61+
return;
62+
63+
IWebViewDelegate? webViewDelegate = handler.NativeView as IWebViewDelegate;
3764
handler.NativeView?.UpdateSource(webView, webViewDelegate);
3865
}
3966
}
40-
}
67+
}

src/Core/src/Handlers/WebView/WebViewHandler.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
namespace Microsoft.Maui.Handlers
1+
#if __ANDROID__
2+
using Android.Webkit;
3+
#endif
4+
5+
namespace Microsoft.Maui.Handlers
26
{
37
public partial class WebViewHandler
48
{
59
public static PropertyMapper<IWebView, WebViewHandler> WebViewMapper = new PropertyMapper<IWebView, WebViewHandler>(ViewHandler.ViewMapper)
610
{
7-
[nameof(IWebView.Source)] = MapSource
11+
[nameof(IWebView.Source)] = MapSource,
12+
#if __ANDROID__
13+
[nameof(WebViewClient)] = MapWebViewClient,
14+
[nameof(WebChromeClient)] = MapWebChromeClient,
15+
[nameof(WebView.Settings)] = MapWebViewSettings
16+
#endif
817
};
918

1019
public WebViewHandler() : base(WebViewMapper)

src/Core/src/Platform/Android/MauiWebView.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@ public class MauiWebView : WebView, IWebViewDelegate
1010
{
1111
public const string AssetBaseUrl = "file:///android_asset/";
1212

13-
WebViewClient? _webViewClient;
14-
WebChromeClient? _webChromeClient;
15-
1613
public MauiWebView(Context context) : base(context)
1714
{
18-
19-
_webViewClient = GetWebViewClient();
20-
SetWebViewClient(_webViewClient);
21-
22-
_webChromeClient = GetWebChromeClient();
23-
SetWebChromeClient(_webChromeClient);
2415
}
2516

2617
void IWebViewDelegate.LoadHtml(string? html, string? baseUrl)
@@ -32,21 +23,5 @@ void IWebViewDelegate.LoadUrl(string? url)
3223
{
3324
LoadUrl(url ?? string.Empty);
3425
}
35-
36-
protected override void Dispose(bool disposing)
37-
{
38-
if (disposing)
39-
{
40-
_webViewClient?.Dispose();
41-
_webChromeClient?.Dispose();
42-
}
43-
base.Dispose(disposing);
44-
}
45-
46-
protected virtual WebViewClient GetWebViewClient() =>
47-
new WebViewClient();
48-
49-
protected virtual WebChromeClient GetWebChromeClient() =>
50-
new WebChromeClient();
5126
}
5227
}

src/Core/src/Platform/Android/WebViewExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@ public static void UpdateSource(this AWebView nativeWebView, IWebView webView, I
1414
if (webViewDelegate != null)
1515
webView.Source?.Load(webViewDelegate);
1616
}
17+
18+
public static void UpdateSettings(this AWebView nativeWebView, IWebView webView, bool javaScriptEnabled, bool domStorageEnabled)
19+
{
20+
if (nativeWebView.Settings == null)
21+
return;
22+
23+
nativeWebView.Settings.JavaScriptEnabled = javaScriptEnabled;
24+
nativeWebView.Settings.DomStorageEnabled = domStorageEnabled;
25+
}
1726
}
1827
}

0 commit comments

Comments
 (0)