Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EPIC-4283] - Xamarin SDK Update to 4.2.0 #118

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
853 changes: 660 additions & 193 deletions Libraries.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<JavaMaximumHeapSize></JavaMaximumHeapSize>
<AndroidFastDeploymentType></AndroidFastDeploymentType>
<AndroidManifest></AndroidManifest>
<AssemblyOriginatorKeyFile></AssemblyOriginatorKeyFile>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<LangVersion>8.0</LangVersion>
<JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
Expand All @@ -69,7 +68,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2612" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.10.2</Version>
<Version>4.2.0-beta.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using AndroidBarcode = IO.Scanbot.Sdk.Barcode.Entity.BarcodeItem;
using ScanbotSDK.Xamarin.Forms;
using IO.Scanbot.Barcodescanner.Model;

/*
This is the Android Custom Renderer that will provide the actual implementation for BarcodeCameraView.
Expand All @@ -43,6 +44,7 @@ class AndroidBarcodeCameraRenderer : ViewRenderer<BarcodeCameraView, FrameLayout
protected BarcodeScannerView cameraView;
private readonly int REQUEST_PERMISSION_CODE = 200;
BarcodeResultDelegate resultHandler;
private bool showToast;

public AndroidBarcodeCameraRenderer(Context context) : base(context)
{
Expand Down Expand Up @@ -136,14 +138,26 @@ public void OnCameraOpened()

private void OnSelectionOverlayBarcodeClicked(object sender, AndroidBarcode e)
{
var items = new List<AndroidBarcode> { e };
var args = new BarcodeEventArgs(new IO.Scanbot.Sdk.Barcode.Entity.BarcodeScanningResult(items, new Java.Util.Date().Time), null);
OnBarcodeResult(this, args);
ScanbotSDK.Xamarin.Forms.BarcodeScanningResult outResult = new ScanbotSDK.Xamarin.Forms.BarcodeScanningResult
{
Barcodes = new List<Barcode> { e.ToFormsBarcode() },
Image = e.Image.ToImageSource()
};

Element.OnBarcodeScanResult?.Invoke(outResult);
}

private void OnBarcodeResult(object sender, BarcodeEventArgs e)
{
if (e.Result != null)
if (!SBSDK.IsLicenseValid && !showToast)
{
showToast = true;
cameraView.Post(() => Toast.MakeText(Context.GetActivity(), "License has expired!", ToastLength.Long).Show());
return;
}

var overlayEnabled = Element.OverlayConfiguration?.Enabled ?? false;
if (overlayEnabled == false || Element.OverlayConfiguration?.AutomaticSelectionEnabled == true)
{
ScanbotSDK.Xamarin.Forms.BarcodeScanningResult outResult = new ScanbotSDK.Xamarin.Forms.BarcodeScanningResult
{
Expand All @@ -153,11 +167,6 @@ private void OnBarcodeResult(object sender, BarcodeEventArgs e)

Element.OnBarcodeScanResult?.Invoke(outResult);
}

if (e.Error != null)
{
cameraView.Post(() => Toast.MakeText(Context.GetActivity(), "License has expired!", ToastLength.Long).Show());
}
}

#endregion
Expand Down Expand Up @@ -186,11 +195,6 @@ private void SetSelectionOverlayConfiguration()
cameraView.SelectionOverlayController.SetEnabled(Element.OverlayConfiguration.Enabled);
cameraView.SelectionOverlayController.SetBarcodeHighlightedDelegate(this);
cameraView.SelectionOverlayController.SetBarcodeAppearanceDelegate(this);

if (resultHandler != null)
{
resultHandler.Success -= OnBarcodeResult;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Native.Renderers.Example.Forms.iOS.Utils;
using ScanbotSDK.iOS;
using ScanbotSDK.Xamarin.Forms;

namespace Native.Renderers.Example.Forms.iOS
{

// Since we cannot directly inherit from SBSDKBarcodeScannerViewControllerDelegate in our ViewRenderer,
// we have created this wrapper class to allow binding to its events through the use of delegates
internal class BarcodeScannerDelegate : SBSDKBarcodeScannerViewControllerDelegate
{
public delegate void OnDetectHandler(SBSDKBarcodeScannerResult[] codes);
public OnDetectHandler OnDetect;

public override void DidDetectBarcodes(SBSDKBarcodeScannerViewController controller, SBSDKBarcodeScannerResult[] codes)
{
OnDetect?.Invoke(codes);
}

public override bool ShouldDetectBarcodes(SBSDKBarcodeScannerViewController controller)
{
if (!SBSDK.IsLicenseValid)
{
ViewUtils.ShowAlert("License Expired!", "Ok");
return false;
}
return true;
}
}

internal class BarcodeTrackingOverlayDelegate : SBSDKBarcodeTrackingOverlayControllerDelegate
{
public delegate void DidTapOnBarcodeAROverlay(SBSDKBarcodeScannerResult barcode);
public DidTapOnBarcodeAROverlay DidTapBarcodeOverlay;

public override void DidTapOnBarcode(SBSDKBarcodeTrackingOverlayController controller, SBSDKBarcodeScannerResult barcode)
{
DidTapBarcodeOverlay?.Invoke(barcode);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<None Include="Info.plist" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Renderers\IOSBarcodeCameraRenderer.cs" />
<Compile Include="Utils\ViewUtils.cs" />
<Compile Include="Delegates\BarcodeScannerDelegates.cs" />
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="Resources\LaunchScreen.storyboard" />
Expand Down Expand Up @@ -135,12 +137,14 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2612" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.10.2</Version>
<Version>4.2.0-beta.1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
<ItemGroup>
<Folder Include="Renderers\" />
<Folder Include="Utils\" />
<Folder Include="Delegates\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Native.Renderers.Example.Forms\Native.Renderers.Example.Forms.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ private UIViewController CurrentViewController
}
}

private BarcodeCameraView.BarcodeScannerResultHandler barcodeScannerResultHandler;

public IOSBarcodeCameraRenderer() : base() { }

protected override void OnElementChanged(ElementChangedEventArgs<BarcodeCameraView> e)
Expand All @@ -46,61 +44,42 @@ protected override void OnElementChanged(ElementChangedEventArgs<BarcodeCameraVi
base.OnElementChanged(e);
}

private void HandleBarcodeScannerResults(SBSDKBarcodeScannerResult[] codes)
{
barcodeScannerResultHandler?.Invoke(new BarcodeScanningResult()
{
Barcodes = codes.ToFormsBarcodes()
});
}

public override void LayoutSubviews()
{
base.LayoutSubviews();
if (Control == null) { return; }

if (CurrentViewController.ChildViewControllers.First() is PageRenderer pageRendererVc)
if (cameraView?.initialised == false && CurrentViewController.ChildViewControllers.First() is PageRenderer pageRendererVc)
{
cameraView.Initialize(pageRendererVc);
cameraView.ScannerDelegate.OnDetect = HandleBarcodeScannerResults;
barcodeScannerResultHandler = Element.OnBarcodeScanResult;
cameraView.SetSelectionOverlayConfiguration(Element.OverlayConfiguration);
cameraView.SetBarcodeConfigurations(Element);
}
}
}

// Since we cannot directly inherit from SBSDKBarcodeScannerViewControllerDelegate in our ViewRenderer,
// we have created this wrapper class to allow binding to its events through the use of delegates
class BarcodeScannerDelegate : SBSDKBarcodeScannerViewControllerDelegate
{
public delegate void OnDetectHandler(SBSDKBarcodeScannerResult[] codes);
public OnDetectHandler OnDetect;


public override void DidDetectBarcodes(SBSDKBarcodeScannerViewController controller, SBSDKBarcodeScannerResult[] codes)
{
OnDetect?.Invoke(codes);
}

public override bool ShouldDetectBarcodes(SBSDKBarcodeScannerViewController controller)
{
return true;
}
}

class IOSBarcodeCameraView : UIView
internal class IOSBarcodeCameraView : UIView
{
public bool initialised = false;
public SBSDKBarcodeScannerViewController Controller { get; private set; }
public BarcodeScannerDelegate ScannerDelegate { get; private set; }
private BarcodeScannerDelegate scannerDelegate;
private BarcodeCameraView element;

public IOSBarcodeCameraView(CGRect frame) : base(frame) { }

public void Initialize(UIViewController parentViewController)
{
initialised = true;
Controller = new SBSDKBarcodeScannerViewController(parentViewController, this);
ScannerDelegate = new BarcodeScannerDelegate();
Controller.Delegate = ScannerDelegate;
scannerDelegate = new BarcodeScannerDelegate();
Controller.Delegate = scannerDelegate;
}

internal void SetBarcodeConfigurations(BarcodeCameraView element)
{
this.element = element;
Controller.AcceptedBarcodeTypes = SBSDKBarcodeType.AllTypes;
scannerDelegate.OnDetect = HandleBarcodeScannerResults;
SetSelectionOverlayConfiguration(element.OverlayConfiguration);
}

internal void SetSelectionOverlayConfiguration(SelectionOverlayConfiguration configuration)
Expand All @@ -123,15 +102,45 @@ internal void SetSelectionOverlayConfiguration(SelectionOverlayConfiguration con
textStyle.TextColor = configuration.TextColor.ToUIColor();
textStyle.SelectedTextColor = configuration.HighlightedTextColor?.ToUIColor();
textStyle.TextBackgroundColor = configuration.TextContainerColor.ToUIColor();
textStyle.TextBackgroundSelectedColor= configuration.HighlightedTextContainerColor?.ToUIColor();
textStyle.TextBackgroundSelectedColor = configuration.HighlightedTextContainerColor?.ToUIColor();

overlayConfiguration.IsAutomaticSelectionEnabled = configuration.AutomaticSelectionEnabled;
overlayConfiguration.TextStyle = textStyle;
overlayConfiguration.PolygonStyle = polygonStyle;

Controller.IsTrackingOverlayEnabled = configuration.Enabled;
Controller.TrackingOverlayController.Configuration = overlayConfiguration;
Controller.TrackingOverlayController.Delegate = new BarcodeTrackingOverlayDelegate
{
DidTapBarcodeOverlay = HandleDidTapOnBarcodeOverlay
};
}
}

private void HandleBarcodeScannerResults(SBSDKBarcodeScannerResult[] codes)
{
var returnResults = true;
if (Controller.IsTrackingOverlayEnabled)
{
// return results if tracking overlay is set to automatic selection true
returnResults = Controller.TrackingOverlayController?.Configuration?.IsAutomaticSelectionEnabled ?? false;
}

if (returnResults)
{
this.element?.OnBarcodeScanResult.Invoke(new BarcodeScanningResult()
{
Barcodes = codes.ToFormsBarcodes()
});
}
}

private void HandleDidTapOnBarcodeOverlay(SBSDKBarcodeScannerResult barcode)
{
this.element?.OnBarcodeScanResult.Invoke(new BarcodeScanningResult()
{
Barcodes = new System.Collections.Generic.List<Barcode> { barcode.ToFormsBarcode() }
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UIKit;

namespace Native.Renderers.Example.Forms.iOS.Utils
{
public static class ViewUtils
{
/// <summary>
/// RootViewContoller from the view hierarchy.
/// </summary>
internal static UIViewController RooViewController => (UIApplication.SharedApplication?.Delegate as AppDelegate)?.Window?.RootViewController;

// ------------------------------------------------------------------------------------------------------------------------
// Displays a popup message with message and a single button.s
// ------------------------------------------------------------------------------------------------------------------------
internal static void ShowAlert(string message, string buttonTitle)
{
var alert = UIAlertController.Create("Alert", message, UIAlertControllerStyle.Alert);
var action = UIAlertAction.Create(buttonTitle ?? "Ok", UIAlertActionStyle.Cancel, (obj) => { });
alert.AddAction(action);
RooViewController?.PresentViewController(alert, true, null);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2612" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms" Version="3.10.2" />
<PackageReference Include="ScanbotSDK.Xamarin.Forms" Version="4.2.0-beta.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
Expand Down
15 changes: 10 additions & 5 deletions Scanbot.SDK.Example.Forms.Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Android.App;
using Android.Runtime;
using Android.Util;
using ScanbotSDK.Xamarin;
using ScanbotSDK.Xamarin.Forms;

namespace Scanbot.SDK.Example.Forms.Droid
Expand Down Expand Up @@ -44,11 +45,15 @@ public override void OnCreate()
// If no StorageBaseDirectory is specified, the default will be used
StorageBaseDirectory = GetDemoStorageBaseDirectory(),
DetectorType = DocumentDetectorType.MLBased,
Encryption = new ScanbotSDK.Xamarin.SBSDKEncryption
{
Mode = ScanbotSDK.Xamarin.EncryptionMode.AES256,
Password = "S0m3W3irDL0ngPa$$w0rdino!!!!"
}
// Uncomment the below to test our encyption functionality.
//Encryption = new SBSDKEncryption
//{
// Mode = EncryptionMode.AES256,
// Password = "S0m3W3irDL0ngPa$$w0rdino!!!!"
//}
// Note: all the images and files exported through the SDK will
// not be openable from external applications, since they will be
// encrypted.
};
SBSDKInitializer.Initialize(this, LICENSE_KEY, configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<Version>2.1.2</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.10.2</Version>
<Version>4.2.0-beta.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 10 additions & 5 deletions Scanbot.SDK.Example.Forms.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;

using Foundation;
using ScanbotSDK.Xamarin;
using ScanbotSDK.Xamarin.Forms;
using UIKit;

Expand Down Expand Up @@ -30,11 +31,15 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options)
EnableLogging = true,
StorageBaseDirectory = GetDemoStorageBaseDirectory(),
DetectorType = DocumentDetectorType.MLBased,
Encryption = new ScanbotSDK.Xamarin.SBSDKEncryption
{
Mode = ScanbotSDK.Xamarin.EncryptionMode.AES256,
Password = "S0m3W3irDL0ngPa$$w0rdino!!!!"
}
// Uncomment the below to test our encyption functionality.
//Encryption = new SBSDKEncryption
//{
// Mode = EncryptionMode.AES256,
// Password = "S0m3W3irDL0ngPa$$w0rdino!!!!"
//}
// Note: all the images and files exported through the SDK will
// not be openable from external applications, since they will be
// encrypted.
});

global::Xamarin.Forms.Forms.Init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<Version>5.0.0.2612</Version>
</PackageReference>
<PackageReference Include="ScanbotSDK.Xamarin.Forms">
<Version>3.10.2</Version>
<Version>4.2.0-beta.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
Loading