Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Julian Steenbakker
2025-04-17 23:24:50 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f5cfe4553eb74cfa60d5565b1b75a386889a4c16
f5cfe455
1 parent
5276188e
fix: hot-restart by adding force parameter
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
57 additions
and
36 deletions
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt
ios/Classes/MobileScanner.swift
ios/Classes/MobileScannerPlugin.swift
lib/src/method_channel/mobile_scanner_method_channel.dart
lib/src/mobile_scanner.dart
macos/mobile_scanner/Sources/mobile_scanner/MobileScannerPlugin.swift
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
View file @
f5cfe45
...
...
@@ -440,12 +440,14 @@ class MobileScanner(
/**
* Pause barcode scanning.
*/
fun pause() {
fun pause(force: Boolean = false) {
if (!force) {
if (isPaused) {
throw AlreadyPaused()
} else if (isStopped()) {
throw AlreadyStopped()
}
}
pauseCamera()
}
...
...
@@ -453,10 +455,12 @@ class MobileScanner(
/**
* Stop barcode scanning.
*/
fun stop() {
fun stop(force: Boolean = false) {
if (!force) {
if (!isPaused && isStopped()) {
throw AlreadyStopped()
}
}
releaseCamera()
}
...
...
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt
View file @
f5cfe45
...
...
@@ -118,8 +118,8 @@ class MobileScannerHandler(
}
})
"start" -> start(call, result)
"pause" -> pause(result)
"stop" -> stop(result)
"pause" -> pause(call, result)
"stop" -> stop(call, result)
"toggleTorch" -> toggleTorch(result)
"analyzeImage" -> analyzeImage(call, result)
"setScale" -> setScale(call, result)
...
...
@@ -214,9 +214,10 @@ class MobileScannerHandler(
)
}
private fun pause(result: MethodChannel.Result) {
private fun pause(call: MethodCall, result: MethodChannel.Result) {
val force: Boolean = call.argument<Boolean>("force") ?: false
try {
mobileScanner!!.pause()
mobileScanner!!.pause(
force
)
result.success(null)
} catch (e: Exception) {
when (e) {
...
...
@@ -226,9 +227,10 @@ class MobileScannerHandler(
}
}
private fun stop(result: MethodChannel.Result) {
private fun stop(call: MethodCall, result: MethodChannel.Result) {
val force: Boolean = call.argument<Boolean>("force") ?: false
try {
mobileScanner!!.stop()
mobileScanner!!.stop(
force
)
result.success(null)
} catch (e: AlreadyStopped) {
result.success(null)
...
...
ios/Classes/MobileScanner.swift
View file @
f5cfe45
...
...
@@ -303,18 +303,20 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
}
/// Pause scanning for barcodes
func
pause
()
throws
{
func
pause
(
force
:
Bool
=
false
)
throws
{
if
(
!
force
)
{
if
(
paused
)
{
throw
MobileScannerError
.
alreadyPaused
}
else
if
(
stopped
)
{
throw
MobileScannerError
.
alreadyStopped
}
}
releaseCamera
()
}
/// Stop scanning for barcodes
func
stop
()
throws
{
if
(
!
paused
&&
stopped
)
{
func
stop
(
force
:
Bool
=
false
)
throws
{
if
(
!
paused
&&
stopped
&&
!
force
)
{
throw
MobileScannerError
.
alreadyStopped
}
releaseCamera
()
...
...
@@ -343,7 +345,9 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
}
private
func
releaseTexture
()
{
if
(
textureId
!=
nil
)
{
registry
?
.
unregisterTexture
(
textureId
)
}
textureId
=
nil
scanner
=
nil
}
...
...
ios/Classes/MobileScannerPlugin.swift
View file @
f5cfe45
...
...
@@ -106,9 +106,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
case
"start"
:
start
(
call
,
result
)
case
"pause"
:
pause
(
result
)
pause
(
call
,
result
)
case
"stop"
:
stop
(
result
)
stop
(
call
,
result
)
case
"toggleTorch"
:
toggleTorch
(
result
)
case
"analyzeImage"
:
...
...
@@ -170,17 +170,19 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
}
/// Stops the mobileScanner without closing the texture.
private
func
pause
(
_
result
:
@escaping
FlutterResult
)
{
private
func
pause
(
_
call
:
FlutterMethodCall
,
_
result
:
@escaping
FlutterResult
)
{
let
force
=
(
call
.
arguments
as?
Bool
)
??
false
do
{
try
mobileScanner
.
pause
()
try
mobileScanner
.
pause
(
force
:
force
)
}
catch
{}
result
(
nil
)
}
/// Stops the mobileScanner and closes the texture.
private
func
stop
(
_
result
:
@escaping
FlutterResult
)
{
private
func
stop
(
_
call
:
FlutterMethodCall
,
_
result
:
@escaping
FlutterResult
)
{
let
force
=
(
call
.
arguments
as?
Bool
)
??
false
do
{
try
mobileScanner
.
stop
()
try
mobileScanner
.
stop
(
force
:
force
)
}
catch
{}
result
(
nil
)
}
...
...
lib/src/method_channel/mobile_scanner_method_channel.dart
View file @
f5cfe45
...
...
@@ -292,26 +292,26 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
}
@override
Future
<
void
>
stop
()
async
{
if
(
_textureId
==
null
)
{
Future
<
void
>
stop
({
bool
force
=
false
})
async
{
if
(
_textureId
==
null
&&
!
force
)
{
return
;
}
_textureId
=
null
;
_pausing
=
false
;
await
methodChannel
.
invokeMethod
<
void
>(
'stop'
);
await
methodChannel
.
invokeMethod
<
void
>(
'stop'
,
{
'force'
:
force
}
);
}
@override
Future
<
void
>
pause
()
async
{
Future
<
void
>
pause
(
{
bool
force
=
false
}
)
async
{
if
(
_pausing
)
{
return
;
}
_pausing
=
true
;
await
methodChannel
.
invokeMethod
<
void
>(
'pause'
);
await
methodChannel
.
invokeMethod
<
void
>(
'pause'
,
{
'force'
:
force
}
);
}
@override
...
...
lib/src/mobile_scanner.dart
View file @
f5cfe45
import
'dart:async'
;
import
'package:flutter/foundation.dart'
;
import
'package:flutter/material.dart'
;
import
'package:mobile_scanner/src/method_channel/mobile_scanner_method_channel.dart'
;
import
'package:mobile_scanner/src/mobile_scanner_controller.dart'
;
import
'package:mobile_scanner/src/mobile_scanner_exception.dart'
;
import
'package:mobile_scanner/src/mobile_scanner_platform_interface.dart'
;
...
...
@@ -259,19 +261,21 @@ class _MobileScannerState extends State<MobileScanner>
StreamSubscription
?
_subscription
;
Future
<
void
>
initController
()
async
{
// TODO: This will be fixed in another PR
// If debug mode is enabled, stop the controller first before starting it.
// If a hot-restart is initiated, the controller won't be stopped, and because
// there is no way of knowing if a hot-restart has happened, we must assume
// every start is a hot-restart.
// if (kDebugMode) {
// try {
// await controller.stop();
// } catch (e) {
// // Don't do anything if the controller is already stopped.
// debugPrint('$e');
// }
// }
if
(
kDebugMode
)
{
final
platformInterface
=
MobileScannerPlatform
.
instance
;
if
(
platformInterface
is
MethodChannelMobileScanner
)
{
try
{
await
platformInterface
.
stop
(
force:
true
);
}
catch
(
e
)
{
// Don't do anything if the controller is already stopped.
debugPrint
(
'
$e
'
);
}
}
}
if
(
widget
.
onDetect
!=
null
)
{
WidgetsBinding
.
instance
.
addObserver
(
this
);
...
...
macos/mobile_scanner/Sources/mobile_scanner/MobileScannerPlugin.swift
View file @
f5cfe45
...
...
@@ -76,9 +76,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
case
"resetScale"
:
resetScale
(
call
,
result
)
case
"pause"
:
pause
(
result
)
pause
(
call
,
result
)
case
"stop"
:
stop
(
result
)
stop
(
call
,
result
)
case
"updateScanWindow"
:
updateScanWindow
(
call
,
result
)
case
"analyzeImage"
:
...
...
@@ -448,19 +448,24 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
result
(
nil
)
}
func
pause
(
_
result
:
FlutterResult
)
{
func
pause
(
_
call
:
FlutterMethodCall
,
_
result
:
FlutterResult
)
{
let
force
=
(
call
.
arguments
as?
Bool
)
??
false
if
(
!
force
)
{
if
(
paused
||
stopped
)
{
result
(
nil
)
return
}
}
releaseCamera
()
result
(
nil
)
}
func
stop
(
_
result
:
FlutterResult
)
{
if
(
!
paused
&&
stopped
)
{
func
stop
(
_
call
:
FlutterMethodCall
,
_
result
:
FlutterResult
)
{
let
force
=
(
call
.
arguments
as?
Bool
)
??
false
if
(
!
paused
&&
stopped
&&
!
force
)
{
result
(
nil
)
return
...
...
Please
register
or
login
to post a comment