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

fix: #1139 - carousel spinning issues #1604

Merged
Merged
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
15 changes: 14 additions & 1 deletion packages/smooth_app/lib/data_models/continuous_scan_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ContinuousScanModel with ChangeNotifier {

String? _latestScannedBarcode;
String? _latestFoundBarcode;
String? _latestConsultedBarcode;
String? _barcodeTrustCheck; // TODO(monsieurtanuki): could probably be removed
late DaoProduct _daoProduct;
late DaoProductList _daoProductList;
Expand All @@ -37,6 +38,15 @@ class ContinuousScanModel with ChangeNotifier {

List<String> getBarcodes() => _barcodes;

String? get latestConsultedBarcode => _latestConsultedBarcode;

set lastConsultedBarcode(String? barcode) {
_latestConsultedBarcode = barcode;
if (barcode != null) {
notifyListeners();
}
}

Future<ContinuousScanModel?> load(final LocalDatabase localDatabase) async {
try {
_daoProduct = DaoProduct(localDatabase);
Expand Down Expand Up @@ -96,7 +106,8 @@ class ContinuousScanModel with ChangeNotifier {
_barcodeTrustCheck = code;
return;
}
if (_latestScannedBarcode == code) {
if (_latestScannedBarcode == code || _barcodes.contains(code)) {
lastConsultedBarcode = code;
return;
}
AnalyticsHelper.trackScannedProduct(barcode: code);
Expand Down Expand Up @@ -125,6 +136,7 @@ class ContinuousScanModel with ChangeNotifier {
}
_setBarcodeState(barcode, ScannedProductState.LOADING);
_cacheOrLoadBarcode(barcode);
lastConsultedBarcode = barcode;
return true;
}
if (state == ScannedProductState.FOUND ||
Expand All @@ -137,6 +149,7 @@ class ContinuousScanModel with ChangeNotifier {
if (state == ScannedProductState.CACHED) {
_updateBarcode(barcode);
}
lastConsultedBarcode = barcode;
return true;
}
return false;
Expand Down
10 changes: 8 additions & 2 deletions packages/smooth_app/lib/pages/page_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ class PageManagerState extends State<PageManager> {
selectedItemColor: Colors.white,
backgroundColor: Theme.of(context).appBarTheme.backgroundColor,
onTap: (int index) {
final InheritedDataManagerState inheritedDataManager =
InheritedDataManager.of(context);
if (_currentPage == BottomNavigationTab.Scan &&
_pageKeys[index] == BottomNavigationTab.Scan) {
InheritedDataManager.of(context).resetShowSearchCard(true);
if (!inheritedDataManager.showSearchCard) {
inheritedDataManager.resetShowSearchCard(true);
}
_selectTab(_pageKeys[index], index);
} else {
InheritedDataManager.of(context).resetShowSearchCard(false);
if (inheritedDataManager.showSearchCard) {
inheritedDataManager.resetShowSearchCard(false);
}
_selectTab(_pageKeys[index], index);
}
},
Expand Down
32 changes: 28 additions & 4 deletions packages/smooth_app/lib/widgets/smooth_product_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,34 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
final CarouselController _controller = CarouselController();
List<String> barcodes = <String>[];
bool _returnToSearchCard = false;
int _lastIndex = 0;

int get _searchCardAdjustment => widget.containSearchCard ? 1 : 0;
late ContinuousScanModel _model;

@override
void initState() {
super.initState();
_lastIndex = _searchCardAdjustment;
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
_model = context.watch<ContinuousScanModel>();
barcodes = _model.getBarcodes();
_returnToSearchCard = InheritedDataManager.of(context).showSearchCard;
if (_controller.ready) {
if (_returnToSearchCard && widget.containSearchCard) {
if (_returnToSearchCard && widget.containSearchCard && _lastIndex > 0) {
_controller.animateToPage(0);
} else if (_model.latestConsultedBarcode != null &&
_model.latestConsultedBarcode!.isNotEmpty) {
final int indexBarcode =
barcodes.indexOf(_model.latestConsultedBarcode!);
final int indexCarousel = indexBarcode + _searchCardAdjustment;
_controller.animateToPage(indexCarousel);
} else {
_controller.animateToPage(barcodes.length - 1 + _searchCardAdjustment);
_controller.animateToPage(0);
}
}
}
Expand All @@ -70,8 +83,19 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
height: widget.height,
enableInfiniteScroll: false,
onPageChanged: (int index, CarouselPageChangedReason reason) {
if (index > 0 && InheritedDataManager.of(context).showSearchCard) {
InheritedDataManager.of(context).resetShowSearchCard(false);
_lastIndex = index;
final InheritedDataManagerState inheritedDataManager =
InheritedDataManager.of(context);
if (inheritedDataManager.showSearchCard) {
inheritedDataManager.resetShowSearchCard(false);
}
if (index > 0) {
if (reason == CarouselPageChangedReason.manual) {
_model.lastConsultedBarcode =
barcodes[index - _searchCardAdjustment];
}
} else if (index == 0) {
_model.lastConsultedBarcode = null;
}
},
),
Expand Down