Skip to content

Commit

Permalink
Refactored MobileBarcodeScanner, ScanPage and ZXingScannerControl
Browse files Browse the repository at this point in the history
MobileBarcodeScanner no longer sets static properties, but instead passes instances of config/settings via the page navigation.  This greatly reduces clutter in the ScanPage
ScanPage is also no longer responsible for trying to handle back navigation, and MobileBarcodeScanner will take care of this.  This means ScanPage could in theory be used independently of MobileBarcodeScanner now (it also now implements IMobileBarcodeScanner to follow the same pattern that iOS/Android do with pages/views).
  • Loading branch information
Redth committed Aug 22, 2016
1 parent c9d5143 commit 07f8e79
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 201 deletions.
94 changes: 50 additions & 44 deletions Source/ZXing.Net.Mobile.WindowsUniversal/MobileBarcodeScanner.cs
Expand Up @@ -18,98 +18,104 @@ public MobileBarcodeScanner(CoreDispatcher dispatcher) : base()
this.Dispatcher = dispatcher;
}

internal ScanPage ScanPage { get; set; }

public CoreDispatcher Dispatcher { get; set; }

public Frame RootFrame { get; set; }

public override void ScanContinuously(MobileBarcodeScanningOptions options, Action<Result> scanHandler)
public override async void ScanContinuously(MobileBarcodeScanningOptions options, Action<Result> scanHandler)
{
//Navigate: /ZxingSharp.WindowsPhone;component/Scan.xaml
var rootFrame = RootFrame ?? Window.Current.Content as Frame ?? ((FrameworkElement) Window.Current.Content).GetFirstChildOfType<Frame>();
var dispatcher = Dispatcher ?? Window.Current.Dispatcher;

ScanPage.ScanningOptions = options;
ScanPage.ResultFoundAction = scanHandler;

ScanPage.UseCustomOverlay = this.UseCustomOverlay;
ScanPage.CustomOverlay = this.CustomOverlay;
ScanPage.TopText = TopText;
ScanPage.BottomText = BottomText;
ScanPage.ContinuousScanning = true;

dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
rootFrame.Navigate(typeof(ScanPage));
rootFrame.Navigate(typeof(ScanPage), new ScanPageNavigationParameters
{
Options = options,
ResultHandler = scanHandler,
Scanner = this,
ContinuousScanning = true
});
});
}

public override Task<Result> Scan(MobileBarcodeScanningOptions options)
public override async Task<Result> Scan(MobileBarcodeScanningOptions options)
{
var rootFrame = RootFrame ?? Window.Current.Content as Frame ?? ((FrameworkElement) Window.Current.Content).GetFirstChildOfType<Frame>();
var dispatcher = Dispatcher ?? Window.Current.Dispatcher;

return Task.Factory.StartNew(new Func<Result>(() =>
{
var scanResultResetEvent = new System.Threading.ManualResetEvent(false);
var tcsScanResult = new TaskCompletionSource<Result>();

Result result = null;
ScanPage.ScanningOptions = options;
ScanPage.ResultFoundAction = (r) =>
{
result = r;
scanResultResetEvent.Set();
};
ScanPage.UseCustomOverlay = this.UseCustomOverlay;
ScanPage.CustomOverlay = this.CustomOverlay;
ScanPage.TopText = TopText;
ScanPage.BottomText = BottomText;
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
rootFrame.Navigate(typeof(ScanPage), new ScanPageNavigationParameters
{
rootFrame.Navigate(typeof(ScanPage));
Options = options,
ResultHandler = r =>
{
tcsScanResult.SetResult(r);
},
Scanner = this,
ContinuousScanning = false
});
scanResultResetEvent.WaitOne();
});

var result = await tcsScanResult.Task;

await dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
if (rootFrame.CanGoBack)
rootFrame.GoBack();
});

return result;
}));
return result;
}

public override void Cancel()
public override async void Cancel()
{
ScanPage.RequestCancel();
var rootFrame = RootFrame ?? Window.Current.Content as Frame ?? ((FrameworkElement)Window.Current.Content).GetFirstChildOfType<Frame>();
var dispatcher = Dispatcher ?? Window.Current.Dispatcher;

ScanPage?.Cancel();

await dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
if (rootFrame.CanGoBack)
rootFrame.GoBack();
});
}

public override void Torch(bool on)
{
ScanPage.RequestTorch(on);
ScanPage?.Torch(on);
}

public override void ToggleTorch()
{
ScanPage.RequestToggleTorch();
ScanPage?.ToggleTorch();
}

public override bool IsTorchOn
{
get { return ScanPage.RequestIsTorchOn(); }
get { return ScanPage?.IsTorchOn ?? false; }
}

public override void AutoFocus()
{
ScanPage.RequestAutoFocus();
ScanPage?.AutoFocus();
}

public override void PauseAnalysis()
{
ScanPage.RequestPauseAnalysis();
ScanPage?.PauseAnalysis();
}

public override void ResumeAnalysis()
{
ScanPage.RequestResumeAnalysis();
ScanPage?.ResumeAnalysis();
}

public UIElement CustomOverlay
Expand Down

0 comments on commit 07f8e79

Please sign in to comment.