Skip to content

Commit

Permalink
fix: Camera fix (#946)
Browse files Browse the repository at this point in the history
Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>
  • Loading branch information
M123-dev and monsieurtanuki authored Jan 13, 2022
1 parent 7e1f175 commit 120bc18
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:openfoodfacts/model/KnowledgePanel.dart';
import 'package:openfoodfacts/model/KnowledgePanelElement.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Product.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Product.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/cards/product_cards/product_image_carousel.dart';
import 'package:smooth_app/cards/product_cards/product_title_card.dart';
Expand Down
8 changes: 8 additions & 0 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class UserPreferences extends ChangeNotifier {
'lastVisitedOnboardingPage';
static const String _TAG_PREFIX_FLAG = 'FLAG_PREFIX_';
static const String _TAG_DEV_MODE = 'devMode';
static const String _TAG_CAMERA_DECLINE = 'declined_camera_use_once';

Future<void> init(final ProductPreferences productPreferences) async {
if (_sharedPreferences.getBool(_TAG_INIT) != null) {
Expand Down Expand Up @@ -80,6 +81,13 @@ class UserPreferences extends ChangeNotifier {
: OnboardingPage.values[pageIndex];
}

Future<void> setCameraDecline(final bool declined) async {
_sharedPreferences.setBool(_TAG_CAMERA_DECLINE, declined);
}

bool get cameraDeclinedOnce =>
_sharedPreferences.getBool(_TAG_CAMERA_DECLINE) ?? false;

String _getFlagTag(final String key) => _TAG_PREFIX_FLAG + key;

Future<void> setFlag(
Expand Down
1 change: 0 additions & 1 deletion packages/smooth_app/lib/pages/product/nutrition_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/model/OrderedNutrients.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/database/product_query.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:openfoodfacts/interface/JsonObject.dart';
import 'package:openfoodfacts/model/Nutriments.dart';
import 'package:openfoodfacts/model/OrderedNutrient.dart';
import 'package:openfoodfacts/model/OrderedNutrients.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/UnitHelper.dart';
import 'package:smooth_app/database/product_query.dart';
Expand Down
2 changes: 0 additions & 2 deletions packages/smooth_app/lib/pages/product/summary_card.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/Attribute.dart';
import 'package:openfoodfacts/model/AttributeGroup.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/personalized_search/preference_importance.dart';
import 'package:smooth_app/cards/data_cards/score_card.dart';
Expand Down
50 changes: 28 additions & 22 deletions packages/smooth_app/lib/pages/scan/continuous_scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,37 @@ class _ContinuousScanPageState extends State<ContinuousScanPage> {
final double carouselHeight = constraints.maxHeight /
1.81; // roughly 55% of the available height
final double viewFinderBottomOffset = carouselHeight / 2.0;

final List<Widget> children = getScannerWidgets(
context,
constraints,
_model,
);

//Insert scanner at the right position
children.insert(
1,
SmoothRevealAnimation(
delay: 400,
startOffset: Offset.zero,
animationCurve: Curves.easeInOutBack,
child: QRView(
overlay: QrScannerOverlayShape(
// We use [SmoothViewFinder] instead of the overlay.
overlayColor: Colors.transparent,
// This offset adjusts the scanning area on iOS.
cutOutBottomOffset: viewFinderBottomOffset,
),
key: _scannerViewKey,
onQRViewCreated: setupScanner,
),
),
);

return Scaffold(
appBar: AppBar(toolbarHeight: 0.0),
body: Stack(
children: <Widget>[
SmoothRevealAnimation(
delay: 400,
startOffset: Offset.zero,
animationCurve: Curves.easeInOutBack,
child: QRView(
overlay: QrScannerOverlayShape(
// We use [SmoothViewFinder] instead of the overlay.
overlayColor: Colors.transparent,
// This offset adjusts the scanning area on iOS.
cutOutBottomOffset: viewFinderBottomOffset,
),
key: _scannerViewKey,
onQRViewCreated: setupScanner,
),
),
...getScannerWidgets(
context,
constraints,
_model,
),
],
children: children,
),
);
},
Expand Down
43 changes: 24 additions & 19 deletions packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,33 @@ class MLKitScannerPageState extends State<MLKitScannerPage> {

return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
SmoothRevealAnimation(
delay: 400,
startOffset: Offset.zero,
animationCurve: Curves.easeInOutBack,
child: Transform.scale(
scale: scale,
child: Center(
child: CameraPreview(
_controller!,
),
final List<Widget> children = getScannerWidgets(
context,
constraints,
_model,
);

//Inserting the scanner at the right position
children.insert(
1,
SmoothRevealAnimation(
delay: 400,
startOffset: Offset.zero,
animationCurve: Curves.easeInOutBack,
child: Transform.scale(
scale: scale,
child: Center(
child: CameraPreview(
_controller!,
),
),
),
...getScannerWidgets(
context,
constraints,
_model,
),
],
),
);

return Stack(
fit: StackFit.expand,
children: children,
);
},
);
Expand Down
66 changes: 54 additions & 12 deletions packages/smooth_app/lib/pages/scan/scan_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/continuous_scan_model.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
Expand Down Expand Up @@ -33,27 +34,68 @@ class _ScanPageState extends State<ScanPage> {
setState(() {});
}

Future<PermissionStatus> _permissionCheck(
UserPreferences userPreferences) async {
final PermissionStatus status = await Permission.camera.status;

//If is denied, is not restricted by for example parental control and is not already declined once
if (status.isDenied &&
!status.isRestricted &&
!userPreferences.cameraDeclinedOnce) {
final PermissionStatus newStatus = await Permission.camera.request();
if (!newStatus.isGranted && !newStatus.isLimited) {
userPreferences.setCameraDecline(true);
}
return newStatus;
} else {
return status;
}
}

@override
Widget build(BuildContext context) {
final UserPreferences userPreferences = context.read<UserPreferences>();
if (_model == null) {
return const Center(child: CircularProgressIndicator());
}

final Widget child;
return FutureBuilder<PermissionStatus>(
future: _permissionCheck(userPreferences),
builder: (
BuildContext context,
AsyncSnapshot<PermissionStatus> snapshot,
) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return const Center(child: Text('Error'));
}

if (userPreferences.getFlag(
UserPreferencesDevMode.userPreferencesFlagUseMLKit,
) ??
true) {
child = const MLKitScannerPage();
} else {
child = const ContinuousScanPage();
}
// TODO(M123): show no camera access screen
if (snapshot.data!.isDenied ||
snapshot.data!.isPermanentlyDenied ||
snapshot.data!.isRestricted) {
const Center(
child: Text('No camera access granted'),
);
}

final Widget child;

if (userPreferences.getFlag(
UserPreferencesDevMode.userPreferencesFlagUseMLKit,
) ??
true) {
child = const MLKitScannerPage();
} else {
child = const ContinuousScanPage();
}

return ChangeNotifierProvider<ContinuousScanModel>(
create: (BuildContext context) => _model!,
child: child,
return ChangeNotifierProvider<ContinuousScanModel>(
create: (BuildContext context) => _model!,
child: child,
);
},
);
}
}
4 changes: 2 additions & 2 deletions packages/smooth_app/lib/pages/scan/scan_page_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ List<Widget> getScannerWidgets(
padding: qrScannerPadding,
child: SvgPicture.asset(
'assets/actions/scanner_alt_2.svg',
width: 60.0,
height: 6,
width: scannerSize.width,
height: scannerSize.height,
color: Colors.white,
),
),
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ dependencies:
matomo: ^1.1.0
modal_bottom_sheet: ^2.0.0
openfoodfacts: ^1.10.0
# Uncomment those lines if you want to use a local version of the openfoodfacts package
# openfoodfacts:
# path: ../../../openfoodfacts-dart
package_info_plus: ^1.3.0
permission_handler: ^8.3.0
photo_view: ^0.13.0
provider: ^6.0.2
qr_code_scanner: ^0.6.1
Expand Down

0 comments on commit 120bc18

Please sign in to comment.