QR scanning fixes for Huawei

main_production_upgrade_ios
WaseemAbbasi22 1 day ago
parent aab2a557c8
commit 887217646a

@ -152,35 +152,86 @@ class SwipeGeneralUtils {
} }
//huawei permission part.... //huawei permission part....
void getHuaweiCurrentLocation({SwipeTypeEnum? attendanceType, required BuildContext context}) async { Future<void> getHuaweiCurrentLocation({
SwipeTypeEnum? attendanceType,
required BuildContext context,
}) async {
try { try {
showLoading(context); showLoading(context);
FusedLocationProviderClient locationService = FusedLocationProviderClient()..initFusedLocationService();
LocationRequest locationRequest = LocationRequest(); final locationService = FusedLocationProviderClient()..initFusedLocationService();
locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY; final locationRequest = LocationRequest()
locationRequest.interval = 500; ..priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
List<LocationRequest> locationRequestList = <LocationRequest>[locationRequest]; ..interval = 500;
LocationSettingsRequest locationSettingsRequest = LocationSettingsRequest(requests: locationRequestList);
final requestCode = await locationService.requestLocationUpdates(locationRequest) ?? 0;
late StreamSubscription<Location> _streamSubscription;
int requestCode = (await (locationService.requestLocationUpdates(locationRequest)))!; final streamSubscription = locationService.onLocationData?.listen(
_streamSubscription = locationService.onLocationData!.listen( (location) async {
(Location location) async { if (location == null) {
hideLoading(context);
debugPrint("Location data is null");
return;
}
hideLoading(context); hideLoading(context);
await locationService.removeLocationUpdates(requestCode); await locationService.removeLocationUpdates(requestCode);
handleSwipeOperation(swipeType: attendanceType!, context: context, lat: location.latitude ?? 0, long: location.longitude ?? 0); handleSwipeOperation(
requestCode = 0; swipeType: attendanceType!,
_streamSubscription.cancel(); context: context,
lat: location.latitude ?? 0,
long: location.longitude ?? 0,
);
}, },
); );
streamSubscription?.onDone(() {
streamSubscription.cancel();
});
} catch (error) { } catch (error) {
log("HUAWEI LOCATION ERROR!!!!!"); debugPrint("HUAWEI LOCATION ERROR!!!!!");
log('$error'); debugPrint('$error');
hideLoading(context); hideLoading(context);
// handleException(error, context, null); showDialog(
context: context,
builder: (BuildContext cxt) => ConfirmDialog(
message: "Unable to fetch location. Please try again.",
onTap: () {
Navigator.pop(context);
},
),
);
} }
} }
// void getHuaweiCurrentLocation({SwipeTypeEnum? attendanceType, required BuildContext context}) async {
// try {
// showLoading(context);
// FusedLocationProviderClient locationService = FusedLocationProviderClient()..initFusedLocationService();
// LocationRequest locationRequest = LocationRequest();
// locationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
// locationRequest.interval = 500;
// List<LocationRequest> locationRequestList = <LocationRequest>[locationRequest];
// LocationSettingsRequest locationSettingsRequest = LocationSettingsRequest(requests: locationRequestList);
//
// late StreamSubscription<Location> _streamSubscription;
// int requestCode = (await (locationService.requestLocationUpdates(locationRequest)))!;
// _streamSubscription = locationService.onLocationData!.listen(
// (Location location) async {
// hideLoading(context);
// await locationService.removeLocationUpdates(requestCode);
// handleSwipeOperation(swipeType: attendanceType!, context: context, lat: location.latitude ?? 0, long: location.longitude ?? 0);
// requestCode = 0;
// _streamSubscription.cancel();
// },
// );
// } catch (error) {
// log("HUAWEI LOCATION ERROR!!!!!");
// log('$error');
// hideLoading(context);
// // handleException(error, context, null);
// }
// }
Future<bool> requestPermissions() async { Future<bool> requestPermissions() async {
var result = await [ var result = await [
Permission.location, Permission.location,
@ -188,31 +239,71 @@ class SwipeGeneralUtils {
return (result[Permission.location] == PermissionStatus.granted || result[Permission.locationAlways] == PermissionStatus.granted); return (result[Permission.location] == PermissionStatus.granted || result[Permission.locationAlways] == PermissionStatus.granted);
} }
void checkHuaweiLocationPermission({required SwipeTypeEnum attendanceType, required BuildContext context}) async { //TODO delete older code after confirm the new one is working fine in huawei devices
// Permission_Handler permissionHandler = PermissionHandler(); // void checkHuaweiLocationPermission({required SwipeTypeEnum attendanceType, required BuildContext context}) async {
LocationUtilities.isEnabled((bool isEnabled) async { // // Permission_Handler permissionHandler = PermissionHandler();
if (isEnabled) { // LocationUtilities.isEnabled((bool isEnabled) async {
LocationUtilities.havePermission((bool permission) async { // if (isEnabled) {
if (permission) { // LocationUtilities.havePermission((bool permission) async {
getHuaweiCurrentLocation(attendanceType: attendanceType, context: context); // if (permission) {
} else { // getHuaweiCurrentLocation(attendanceType: attendanceType, context: context);
bool has = await requestPermissions(); // } else {
if (has) { // bool has = await requestPermissions();
getHuaweiCurrentLocation(attendanceType: attendanceType, context: context); // if (has) {
} else { // getHuaweiCurrentLocation(attendanceType: attendanceType, context: context);
showDialog( // } else {
context: context, // showDialog(
builder: (BuildContext cxt) => ConfirmDialog( // context: context,
message: "You need to give location permission to mark attendance", // builder: (BuildContext cxt) => ConfirmDialog(
onTap: () { // message: "You need to give location permission to mark attendance",
Navigator.pop(context); // onTap: () {
}, // Navigator.pop(context);
), // },
); // ),
} // );
} // }
}); // }
} else { // });
// } else {
// showDialog(
// context: context,
// builder: (BuildContext cxt) => ConfirmDialog(
// message: "You need to enable location services to mark attendance",
// onTap: () async {
// Navigator.pop(context);
// await Geolocator.openLocationSettings();
// },
// ),
// );
// }
// });
//
// // if (await permissionHandler.hasLocationPermission()) {
// // getHuaweiCurrentLocation(attendanceType);
// // } else {
// // bool has = await requestPermissions();
// // if (has) {
// // getHuaweiCurrentLocation(attendanceType);
// // } else {
// // showDialog(
// // context: context,
// // builder: (BuildContext cxt) => ConfirmDialog(
// // message: "You need to give location permission to mark attendance",
// // onTap: () {
// // Navigator.pop(context);
// // },
// // ),
// // );
// // }
// // }
// }
Future<void> checkHuaweiLocationPermission({
required SwipeTypeEnum attendanceType,
required BuildContext context,
}) async {
try {
final isEnabled = await LocationUtilities.isEnabledAsync();
if (!isEnabled) {
showDialog( showDialog(
context: context, context: context,
builder: (BuildContext cxt) => ConfirmDialog( builder: (BuildContext cxt) => ConfirmDialog(
@ -223,27 +314,40 @@ class SwipeGeneralUtils {
}, },
), ),
); );
return;
} }
});
// if (await permissionHandler.hasLocationPermission()) { final hasPermission = await LocationUtilities.havePermissionAsync();
// getHuaweiCurrentLocation(attendanceType); if (!hasPermission) {
// } else { final granted = await requestPermissions();
// bool has = await requestPermissions(); if (!granted) {
// if (has) { showDialog(
// getHuaweiCurrentLocation(attendanceType); context: context,
// } else { builder: (BuildContext cxt) => ConfirmDialog(
// showDialog( message: "You need to give location permission to mark attendance",
// context: context, onTap: () {
// builder: (BuildContext cxt) => ConfirmDialog( Navigator.pop(context);
// message: "You need to give location permission to mark attendance", },
// onTap: () { ),
// Navigator.pop(context); );
// }, return;
// ), }
// ); }
// }
// } // Proceed to get location
await getHuaweiCurrentLocation(attendanceType: attendanceType, context: context);
} catch (error) {
debugPrint("Error in checkHuaweiLocationPermission: $error");
showDialog(
context: context,
builder: (BuildContext cxt) => ConfirmDialog(
message: "An unexpected error occurred. Please try again.",
onTap: () {
Navigator.pop(context);
},
),
);
}
} }
void handleSwipeOperation({required SwipeTypeEnum swipeType, required double lat, required double long, required BuildContext context}) { void handleSwipeOperation({required SwipeTypeEnum swipeType, required double lat, required double long, required BuildContext context}) {
@ -362,7 +466,6 @@ class SwipeGeneralUtils {
); );
return; return;
} }
// Check if location is enabled
final isEnabled = await LocationUtilities.isEnabledAsync(); final isEnabled = await LocationUtilities.isEnabledAsync();
if (!isEnabled) { if (!isEnabled) {
showInfoDialog( showInfoDialog(
@ -371,8 +474,6 @@ class SwipeGeneralUtils {
); );
return; return;
} }
// Check permission
final hasPermission = await LocationUtilities.havePermissionAsync(); final hasPermission = await LocationUtilities.havePermissionAsync();
if (!hasPermission) { if (!hasPermission) {
showInfoDialog( showInfoDialog(
@ -381,11 +482,7 @@ class SwipeGeneralUtils {
); );
return; return;
} }
// Show loader
showLoading(context); showLoading(context);
// Get location
await LocationUtilities.getCurrentLocation( await LocationUtilities.getCurrentLocation(
(Position position, bool isMocked) { (Position position, bool isMocked) {
hideLoading(context); hideLoading(context);
@ -426,7 +523,7 @@ class SwipeGeneralUtils {
} }
} }
//older code.. //TODO delete older code..
// void handleSwipe({required SwipeTypeEnum swipeType, required bool isEnable, required BuildContext context}) async { // void handleSwipe({required SwipeTypeEnum swipeType, required bool isEnable, required BuildContext context}) async {
// if (Platform.isAndroid && !(await isGoogleServicesAvailable())) { // if (Platform.isAndroid && !(await isGoogleServicesAvailable())) {
// checkHuaweiLocationPermission(attendanceType: swipeType, context: context); // checkHuaweiLocationPermission(attendanceType: swipeType, context: context);

@ -1,87 +1,176 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:google_api_availability/google_api_availability.dart';
import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart'; import 'package:qr_code_scanner_plus/qr_code_scanner_plus.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import '../buttons/app_icon_button.dart'; import '../buttons/app_icon_button.dart';
class ScanQr extends StatefulWidget { class ScanQr extends StatefulWidget {
const ScanQr({Key? key}) : super(key: key); const ScanQr({Key? key}) : super(key: key);
@override @override
_ScanQrState createState() => _ScanQrState(); State<ScanQr> createState() => _ScanQrState();
} }
class _ScanQrState extends State<ScanQr> { class _ScanQrState extends State<ScanQr> {
// Barcode result; MobileScannerController? mobileController;
// QRViewController? _controller; QRViewController? qrController;
MobileScannerController? controller;
bool _scanDone = false;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR_scanner'); final GlobalKey qrKey = GlobalKey(debugLabel: 'QR_scanner');
// In order to get hot reload to work we need to pause the camera if the platform bool _scanDone = false;
// is android, or resume the camera if the platform is iOS. bool _isHuawei = false;
bool _isLoading = true;
@override @override
void initState() { void initState() {
// TODO: implement initState
super.initState(); super.initState();
controller = MobileScannerController( _initScanner();
detectionSpeed: DetectionSpeed.noDuplicates,
formats: [],
returnImage: false,
torchEnabled: false,
invertImage: false,
autoZoom: true,
);
} }
// Future<void> _initScanner() async {
// @override _isHuawei = Platform.isAndroid && !(await isGoogleServicesAvailable());
// void reassemble() {
// super.reassemble(); if (!_isHuawei) {
// if (Platform.isAndroid) { mobileController = MobileScannerController(
// controller?.pauseCamera(); detectionSpeed: DetectionSpeed.noDuplicates,
// } else if (Platform.isIOS) { formats: [],
// _controller?.resumeCamera(); returnImage: false,
// } torchEnabled: false,
// } invertImage: false,
autoZoom: true,
);
}
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
Future<bool> isGoogleServicesAvailable() async {
GooglePlayServicesAvailability availability = await GoogleApiAvailability.instance.checkGooglePlayServicesAvailability();
String status = availability.toString().split('.').last;
return status == "success";
}
void _onQrViewCreated(QRViewController controller) {
qrController = controller;
controller.scannedDataStream.listen((scanData) {
if (_scanDone) return;
if (scanData.code == null || scanData.code!.isEmpty) return;
_scanDone = true;
Navigator.of(context).pop(scanData.code);
});
}
@override @override
void dispose() { void dispose() {
mobileController?.dispose();
qrController?.dispose();
super.dispose(); super.dispose();
// _controller?.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_isLoading) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return Scaffold( return Scaffold(
body: Stack( body: Stack(
children: [ children: [
MobileScanner( _isHuawei
tapToFocus: true, ? QRView(
controller: controller, key: qrKey,
onDetect: (result) { onQRViewCreated: _onQrViewCreated,
if (!_scanDone) { overlay: QrScannerOverlayShape(
_scanDone = true; borderColor: Colors.white,
Navigator.of(context).pop(result.barcodes.first.rawValue); borderRadius: 12,
} borderLength: 30,
borderWidth: 4,
cutOutSize: MediaQuery.of(context).size.width * 0.7,
),
)
: MobileScanner(
controller: mobileController,
onDetect: (capture) {
if (_scanDone) return;
final barcodes = capture.barcodes;
if (barcodes.isEmpty) return;
final value = barcodes.first.rawValue;
if (value == null || value.isEmpty) return;
_scanDone = true;
Navigator.of(context).pop(value);
},
),
_ScannerOverlay(
// scanDone: _scanDone,
onBack: () {
if (!_scanDone) _scanDone = true;
Navigator.of(context).pop();
}, },
showSvgOverlay: !_isHuawei,
),
],
),
);
}
}
class _ScannerOverlay extends StatelessWidget {
final VoidCallback onBack;
final bool showSvgOverlay;
const _ScannerOverlay({
Key? key,
required this.onBack,
this.showSvgOverlay = true,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox.expand(
child: Stack(
children: [
if (showSvgOverlay)
Center(
child: 'scan'.toSvgAsset(
height: 283.toScreenHeight.toInt(),
width: 283.toScreenWidth.toInt(),
fit: BoxFit.fitHeight,
),
),
Positioned(
bottom: 80, // adjust if needed
left: 0,
right: 0,
child: Center(
child: Text(
"Point camera at a QR code to scan",
style: TextStyle(
color: Colors.white.withOpacity(0.8),
fontSize: 16,
),
),
),
), ),
SafeArea( SafeArea(
child: Padding( child: Padding(
padding: const EdgeInsets.all(12.0), padding: const EdgeInsets.all(12.0),
child: AIconButton( child: AIconButton(
iconData: Icons.arrow_back, iconData: Icons.arrow_back,
onPressed: () { onPressed: onBack,
Navigator.of(context).pop();
},
), ),
), ),
) ),
], ],
), ),
); );

@ -750,6 +750,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.14.0+300" version: "6.14.0+300"
huawei_scan:
dependency: "direct main"
description:
name: huawei_scan
sha256: "71bc8ce2e90fa07c2d82829ce928ebe3e69b3326fb55d2e7a68f62d9efdef6dd"
url: "https://pub.dev"
source: hosted
version: "2.12.0+304"
image: image:
dependency: transitive dependency: transitive
description: description:

@ -106,6 +106,7 @@ dependencies:
audio_waveforms: ^1.3.0 audio_waveforms: ^1.3.0
signalr_netcore: ^1.4.4 signalr_netcore: ^1.4.4
ellipsized_text: ^2.0.0 ellipsized_text: ^2.0.0
huawei_scan: ^2.12.0+304
local_auth_darwin: ^1.4.0 local_auth_darwin: ^1.4.0
dev_dependencies: dev_dependencies:

Loading…
Cancel
Save