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

Migrating qr_code_scanner to mobile_scanner in talawa mobile #2719

Closed
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
4 changes: 4 additions & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Please add these rules to your existing keep rules in order to suppress warnings.
# This is generated automatically by the Android Gradle plugin.
-dontwarn org.bouncycastle.jce.provider.BouncyCastleProvider
-dontwarn org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip
2 changes: 1 addition & 1 deletion android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.2"
id "com.android.application" version '7.3.0' apply false
id "com.android.application" version '8.1.1' apply false
id "org.jetbrains.kotlin.android" version "1.8.10" apply false
id("com.google.gms.google-services") version "4.4.1" apply false

Expand Down
3 changes: 3 additions & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:
1 change: 1 addition & 0 deletions lib/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ final actionHandlerService = locator<ActionHandlerService>();
///
/// **returns**:
/// None

Future<void> setupLocator() async {
locator.registerSingleton(DataBaseMutationFunctions());

Expand Down
16 changes: 12 additions & 4 deletions lib/splash_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ import 'package:talawa/utils/app_localization.dart';

/// This widget return the SplashScreen. Splash Screen is the first screen that we see when we run our application. It is also known as Launch Screen.
class SplashScreen extends StatefulWidget {
const SplashScreen({required Key key, this.mainScreenIndex = 0})
: super(key: key);
const SplashScreen({
required Key key,
this.mainScreenIndex = 0,
this.isTesting = false,
}) : super(key: key);

/// This is required if url requires us to push different Screen to Home Screen.
final int mainScreenIndex;

/// for testing purpose.
final bool isTesting;

@override
_SplashScreenState createState() => _SplashScreenState();
}
Expand Down Expand Up @@ -206,6 +212,8 @@ class _SplashScreenState extends State<SplashScreen> {
/// **returns**:
/// None
void _handleUserLogIn(bool userLoggedIn) {
print(widget.isTesting);
if (widget.isTesting) return;
Future.delayed(const Duration(milliseconds: 750)).then((value) async {
final pushReplacementScreen = navigationService.pushReplacementScreen;
if (!userLoggedIn) {
Expand Down Expand Up @@ -239,9 +247,9 @@ class _SplashScreenState extends State<SplashScreen> {
}

@override
void dispose() {
_sub.cancel();
Future<void> dispose() async {
super.dispose();
await _sub.cancel();
Comment on lines +250 to +252
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure proper disposal sequence.
Always cancel streams before calling super.dispose() to avoid possible late calls after the State is unmounted.

-  Future<void> dispose() async {
-    super.dispose();
-    await _sub.cancel();
+  @override
+  Future<void> dispose() async {
+    await _sub.cancel();
+    super.dispose();
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Future<void> dispose() async {
super.dispose();
await _sub.cancel();
@override
Future<void> dispose() async {
await _sub.cancel();
super.dispose();
}

}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
// import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:talawa/constants/routing_constants.dart';
import 'package:talawa/enums/enums.dart';
import 'package:talawa/locator.dart';
Expand Down
172 changes: 114 additions & 58 deletions lib/view_model/pre_auth_view_models/set_url_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:talawa/constants/app_strings.dart';
import 'package:talawa/enums/enums.dart';
Expand Down Expand Up @@ -56,6 +56,9 @@
/// qrValidator.
AutovalidateMode validate = AutovalidateMode.disabled;

/// qrController.
final MobileScannerController controller = MobileScannerController();

/// This function initialises the variables.
///
/// **params**:
Expand Down Expand Up @@ -208,11 +211,13 @@
///
/// **params**:
/// * `context`: BuildContext
/// * `mockController`: MobileScannerController
///
/// **returns**:
/// None

void scanQR(BuildContext context) {
void scanQR(BuildContext context, {MobileScannerController? mockController}) {
debugPrint('Scan QR called');
showModalBottomSheet(
context: context,
barrierColor: Colors.transparent,
Expand All @@ -239,16 +244,30 @@
SizedBox(
height: 250,
width: 250,
child: QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
overlay: QrScannerOverlayShape(
borderRadius: 10,
borderLength: 20,
borderWidth: 10,
cutOutSize: 250,
),
/*overlayMargin: EdgeInsets.all(50)*/
child: MobileScanner(
fit: BoxFit.contain,
controller: controller,
errorBuilder: (ctx, error, _) {
final String errorMessage = onDetectError(error);
debugPrint(errorMessage);

Check warning on line 252 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L250-L252

Added lines #L250 - L252 were not covered by tests

WidgetsBinding.instance.addPostFrameCallback((_) {
navigationService.showTalawaErrorSnackBar(

Check warning on line 255 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L254-L255

Added lines #L254 - L255 were not covered by tests
errorMessage,
MessageType.error,
);
});

return Center(
child: Text(

Check warning on line 262 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L261-L262

Added lines #L261 - L262 were not covered by tests
errorMessage,
),
);
},
onDetect: (capture) {
debugPrint('onDetect called with capture: $capture');
onQRViewCreated(capture, controller);

Check warning on line 269 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L267-L269

Added lines #L267 - L269 were not covered by tests
},
),
),
SizedBox(
Expand All @@ -269,56 +288,93 @@
/// This is the helper function which execute when the on QR view created.
///
/// **params**:
/// * `controller`: QRViewController
/// * `scanData`: BarcodeCapture
/// * `controller`: MobileScannerController
///
/// **returns**:
/// None

void _onQRViewCreated(QRViewController controller) {
controller.scannedDataStream.listen((scanData) {
/// if the scanData is not empty.
if (scanData.code!.isNotEmpty) {
print(scanData.code);
try {
final List<String> data = scanData.code!.split('?');
url.text = data[0];
final List<String> queries = data[1].split('&');
orgId = queries[0].split('=')[1];
Vibration.vibrate(duration: 100);
controller.stopCamera();
controller.dispose();
final box = Hive.box('url');
box.put(urlKey, url.text);
box.put(imageUrlKey, "${url.text}/talawa/");
graphqlConfig.getOrgUrl();
Navigator.pop(navigationService.navigatorKey.currentContext!);
navigationService.pushScreen('/selectOrg', arguments: orgId);
} on CameraException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorSnackBar(
"The Camera is not working",
MessageType.error,
);
} on QrEmbeddedImageException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorDialog(
"The QR is not Working",
MessageType.error,
);
} on QrUnsupportedVersionException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorDialog(
"This QR version is not Supported.",
MessageType.error,
);
} on Exception catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorSnackBar(
"This QR is not for the App",
MessageType.error,
);
}
void onQRViewCreated(

Check warning on line 297 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L297

Added line #L297 was not covered by tests
BarcodeCapture scanData,
MobileScannerController? controller,
) {
if (scanData.barcodes.isNotEmpty) {
debugPrint("Barcode: ${scanData.barcodes.first.displayValue}");

Check warning on line 302 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L301-L302

Added lines #L301 - L302 were not covered by tests
try {
final String code = scanData.barcodes.first.displayValue!;

Check warning on line 304 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L304

Added line #L304 was not covered by tests

final List<String> data = code.split('?');
url.text = data[0];
final List<String> queries = data[1].split('&');
orgId = queries[0].split('=')[1];
Vibration.vibrate(duration: 100);
controller!.stop();
controller.dispose();

Check warning on line 312 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L306-L312

Added lines #L306 - L312 were not covered by tests

final box = Hive.box('url');
box.put(urlKey, url.text);
box.put(imageUrlKey, "${url.text}/talawa/");
graphqlConfig.getOrgUrl();
Navigator.pop(navigationService.navigatorKey.currentContext!);
navigationService.pushScreen('/selectOrg', arguments: orgId);
} on QrEmbeddedImageException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorDialog(

Check warning on line 322 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L314-L322

Added lines #L314 - L322 were not covered by tests
"The QR is not Working",
MessageType.error,
);
} on QrUnsupportedVersionException catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorDialog(

Check warning on line 328 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L326-L328

Added lines #L326 - L328 were not covered by tests
"This QR version is not Supported.",
MessageType.error,
);
} on Exception catch (e) {
debugPrint(e.toString());
navigationService.showTalawaErrorSnackBar(

Check warning on line 334 in lib/view_model/pre_auth_view_models/set_url_view_model.dart

View check run for this annotation

Codecov / codecov/patch

lib/view_model/pre_auth_view_models/set_url_view_model.dart#L332-L334

Added lines #L332 - L334 were not covered by tests
"This QR is not for the App",
MessageType.error,
);
}
}
}

/// This function is used to handle the error.
///
/// **params**:
/// * `error`: Object
///
/// **returns**:
/// * `String`: The error message generated.

String onDetectError(Object error) {
String errorMsg = 'An unknown error occurred';

if (error is MobileScannerException) {
debugPrint(error.errorCode.toString());
switch (error.errorCode) {
case MobileScannerErrorCode.controllerDisposed:
errorMsg = 'Camera is disposed';
break;
case MobileScannerErrorCode.controllerAlreadyInitialized:
errorMsg = 'Camera is already in use';
break;
case MobileScannerErrorCode.controllerUninitialized:
errorMsg = 'Camera is not ready';
break;
case MobileScannerErrorCode.permissionDenied:
errorMsg = 'Please provide camera permission to scan QR code';
break;
case MobileScannerErrorCode.unsupported:
errorMsg = 'This device does not support scanning';
break;
case MobileScannerErrorCode.genericError:
errorMsg = 'Something went wrong while detecting the QR';
break;
default:
errorMsg = 'An unknown error occurred';
}
});
}
return errorMsg;
}
}
Loading
Loading