Skip to content

Commit

Permalink
fix: #1139 - carousel spinning issues (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
cli1005 authored Apr 25, 2022
1 parent a3a02d1 commit e08e813
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
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 @@ -37,21 +37,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 @@ -76,8 +89,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

0 comments on commit e08e813

Please sign in to comment.