Skip to content

Commit

Permalink
Clean up code for current view controller
Browse files Browse the repository at this point in the history
This might actually help an oldie but a goodie and fix #138
  • Loading branch information
Redth committed Mar 13, 2020
1 parent 7d302a6 commit a25146d
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions ZXing.Net.Mobile/iOS/MobileBarcodeScanner.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,14 @@ namespace ZXing.Mobile
public partial class MobileBarcodeScanner : MobileBarcodeScannerBase
{
IScannerViewController viewController;

WeakReference<UIViewController> weakAppController;
ManualResetEvent scanResultResetEvent = new ManualResetEvent(false);
readonly WeakReference<UIViewController> weakAppController;
readonly ManualResetEvent scanResultResetEvent = new ManualResetEvent(false);

public MobileBarcodeScanner(UIViewController delegateController)
=> weakAppController = new WeakReference<UIViewController>(delegateController);

public MobileBarcodeScanner()
{
foreach (var window in UIApplication.SharedApplication.Windows)
{
if (window.RootViewController != null)
{
weakAppController = new WeakReference<UIViewController>(window.RootViewController);
break;
}
}
}
=> weakAppController = new WeakReference<UIViewController>(Xamarin.Essentials.Platform.GetCurrentUIViewController());

public Task<Result> Scan(bool useAVCaptureEngine)
=> Scan(new MobileBarcodeScanningOptions(), useAVCaptureEngine);
Expand Down Expand Up @@ -56,12 +46,11 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa
if (useAVCaptureEngine)
allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats);

UIViewController appController;
if (weakAppController.TryGetTarget(out appController))
if (weakAppController.TryGetTarget(out var appController))
{
var tcs = new TaskCompletionSource<object>();

appController.InvokeOnMainThread(() =>
appController?.InvokeOnMainThread(() =>
{
if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported)
{
Expand Down Expand Up @@ -93,7 +82,7 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa
scanHandler(barcodeResult);
};
appController.PresentViewController((UIViewController)viewController, true, null);
appController?.PresentViewController((UIViewController)viewController, true, null);
});
}
}
Expand Down Expand Up @@ -122,10 +111,8 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa
if (weakAppController.TryGetTarget(out var appController))
{
appController.InvokeOnMainThread(() =>
appController?.InvokeOnMainThread(() =>
{
if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported)
{
viewController = new AVCaptureScannerViewController(options, this);
Expand Down Expand Up @@ -167,7 +154,7 @@ void InternalScanContinuously(MobileBarcodeScanningOptions options, bool useAVCa
});
};
appController.PresentViewController((UIViewController)viewController, true, null);
appController?.PresentViewController((UIViewController)viewController, true, null);
});
}
Expand Down

0 comments on commit a25146d

Please sign in to comment.