-
Notifications
You must be signed in to change notification settings - Fork 700
Expand file tree
/
Copy pathZXingScannerViewRenderer.ios.cs
More file actions
123 lines (100 loc) · 3.14 KB
/
ZXingScannerViewRenderer.ios.cs
File metadata and controls
123 lines (100 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using Xamarin.Forms;
using ZXing.Net.Mobile.Forms;
using Xamarin.Forms.Platform.iOS;
using System.ComponentModel;
using System.Reflection;
using Foundation;
using ZXing.Net.Mobile.Forms.iOS;
using UIKit;
[assembly: ExportRenderer(typeof(ZXingScannerView), typeof(ZXingScannerViewRenderer))]
namespace ZXing.Net.Mobile.Forms.iOS
{
[Preserve(AllMembers = true)]
public class ZXingScannerViewRenderer : ViewRenderer<ZXingScannerView, ZXing.Mobile.ZXingScannerView>
{
// No-op to be called from app to prevent linker from stripping this out
public static void Init()
{
var _ = DateTime.Now;
}
protected ZXingScannerView formsView;
protected ZXing.Mobile.ZXingScannerView zxingView;
protected override async void OnElementChanged(ElementChangedEventArgs<ZXingScannerView> e)
{
if (Element is null) return;
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
formsView = Element;
if (zxingView == null)
{
// Process requests for autofocus
formsView.AutoFocusRequested += (x, y) =>
{
if (zxingView != null)
{
if (x < 0 && y < 0)
zxingView.AutoFocus();
else
zxingView.AutoFocus(x, y);
}
};
var cameraPermission = await Xamarin.Essentials.Permissions.RequestAsync<Xamarin.Essentials.Permissions.Camera>();
if (cameraPermission != Xamarin.Essentials.PermissionStatus.Granted)
{
Console.WriteLine("Missing Camera Permission");
return;
}
zxingView = new ZXing.Mobile.ZXingScannerView();
zxingView.UseCustomOverlayView = true;
zxingView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
base.SetNativeControl(zxingView);
if (formsView.IsScanning)
zxingView.StartScanning(formsView.RaiseScanResult, formsView.Options);
if (!formsView.IsAnalyzing)
zxingView.PauseAnalysis();
if (formsView.IsTorchOn)
zxingView.Torch(formsView.IsTorchOn);
}
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (zxingView == null)
return;
switch (e.PropertyName)
{
case nameof(ZXingScannerView.IsTorchOn):
zxingView.Torch(formsView.IsTorchOn);
break;
case nameof(ZXingScannerView.IsScanning):
if (formsView.IsScanning)
zxingView.StartScanning(formsView.RaiseScanResult, formsView.Options);
else
zxingView.StopScanning();
break;
case nameof(ZXingScannerView.IsAnalyzing):
if (formsView.IsAnalyzing)
zxingView.ResumeAnalysis();
else
zxingView.PauseAnalysis();
break;
}
}
public override void TouchesEnded(NSSet touches, UIKit.UIEvent evt)
{
base.TouchesEnded(touches, evt);
zxingView?.AutoFocus();
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
// Find the best guess at current orientation
var o = UIApplication.SharedApplication.StatusBarOrientation;
if (ViewController != null)
o = ViewController.InterfaceOrientation;
// Tell the native view to rotate
zxingView?.DidRotate(o);
}
}
}