Files
smooth-app/packages/smooth_app/lib/data_models/continuous_scan_model.dart
monsieurtanuki 342c5f18dc feat: #1501 - split of the preference page into detailed pages (#1797)
Deleted files:
* `faq_handle_view.dart`
* `list_helper.dart`
* `smooth_list_tile.dart`
* `social_handle_view.dart`
* `user_contribution_view.dart`

New files:
* `abstract_collapsible_user_preferences.dart`: Abstraction of a collapsed/expanded display for the preference pages.
* `all_user_product_list_page.dart`: Page that lists all user product lists.
* `user_preferences_connect.dart`: Display of "Connect" for the preferences page.
* `user_preferences_contribute.dart`: Display of "Contribute" for the preferences page.
* `user_preferences_faq.dart`: Display of "FAQ" for the preferences page.
* `user_preferences_list_tile.dart`: Custom `ListTile` for preferences.

Impacted files:
* `abstract_user_preferences.dart`: refactoring around the fact that now we display pages instead of collapsible List<Widget>
* `continuous_scan_model.dart`: refactored around `DaoProductList` being seldom `async`
* `dao_product_list.dart`: new method `getLength`; refactored with less `async` methods
* `new_product_page.dart`: refactored around `DaoProductList` being seldom `async`
* `preferences_page.dart`: minor refactoring
* `product_list_import_export.dart`: refactored around `DaoProductList` being seldom `async`
* `product_list_page.dart`: refactored around `DaoProductList` being seldom `async`
* `product_list_supplier.dart`: refactored around `DaoProductList` being seldom `async`
* `product_list_user_dialog_helper.dart`: new "delete list?" dialog; refactored around `DaoProductList` being seldom `async`
* `query_product_list_supplier.dart`: refactored around `DaoProductList` being seldom `async`
* `user_preferences_attribute_group.dart`: minor refactoring
* `user_preferences_dev_mode.dart`: minor refactoring
* `user_preferences_food.dart`: minor refactoring
* `user_preferences_page.dart`: now we use the same `StatefulWidget` for a root page with only headers and detailed pages with bodies; added item "Lists"; created 3 new "top paragraphs" extracted from "settings".
* `user_preferences_page-blue-dark.dart`: impacted by new page design
* `user_preferences_page-blue-light.dart`: impacted by new page design
* `user_preferences_page-brown-dark.dart`: impacted by new page design
* `user_preferences_page-brown-light.dart`: impacted by new page design
* `user_preferences_page-green-dark.dart`: impacted by new page design
* `user_preferences_page-green-light.dart`: impacted by new page design
* `user_preferences_profile.dart`: minor refactoring
* `user_preferences_settings.dart`: minor refactoring
2022-05-10 20:53:34 +02:00

257 lines
7.0 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/barcode_product_query.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
enum ScannedProductState {
FOUND,
NOT_FOUND,
LOADING,
THANKS,
CACHED,
ERROR,
}
class ContinuousScanModel with ChangeNotifier {
ContinuousScanModel();
final Map<String, ScannedProductState> _states =
<String, ScannedProductState>{};
final List<String> _barcodes = <String>[];
final ProductList _productList = ProductList.scanSession();
final ProductList _history = ProductList.history();
String? _latestScannedBarcode;
String? _latestFoundBarcode;
String? _latestConsultedBarcode;
String? _barcodeTrustCheck; // TODO(monsieurtanuki): could probably be removed
late DaoProduct _daoProduct;
late DaoProductList _daoProductList;
ProductList get productList => _productList;
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);
_daoProductList = DaoProductList(localDatabase);
if (!await _refresh()) {
return null;
}
return this;
} catch (e) {
debugPrint('exception: $e');
}
return null;
}
Future<bool> _refresh() async {
try {
_latestScannedBarcode = null;
_latestFoundBarcode = null;
_barcodeTrustCheck = null;
_barcodes.clear();
_states.clear();
_latestScannedBarcode = null;
await refreshProductList();
for (final String barcode in _productList.barcodes) {
_barcodes.add(barcode);
_states[barcode] = ScannedProductState.CACHED;
_latestScannedBarcode = barcode;
}
return true;
} catch (e) {
debugPrint('exception: $e');
}
return false;
}
Future<void> refreshProductList() async => _daoProductList.get(_productList);
void _setBarcodeState(
final String barcode,
final ScannedProductState state,
) {
_states[barcode] = state;
notifyListeners();
}
ScannedProductState? getBarcodeState(final String barcode) =>
_states[barcode];
Product getProduct(final String barcode) => _productList.getProduct(barcode);
Future<void> onScan(String? code) async {
if (code == null) {
return;
}
if (_barcodeTrustCheck != code) {
_barcodeTrustCheck = code;
return;
}
if (_latestScannedBarcode == code || _barcodes.contains(code)) {
lastConsultedBarcode = code;
return;
}
AnalyticsHelper.trackScannedProduct(barcode: code);
_latestScannedBarcode = code;
_addBarcode(code);
}
Future<bool> onCreateProduct(String? barcode) async {
if (barcode == null) {
return false;
}
return _addBarcode(barcode);
}
Future<void> retryBarcodeFetch(String barcode) async {
_setBarcodeState(barcode, ScannedProductState.LOADING);
await _updateBarcode(barcode);
}
Future<bool> _addBarcode(final String barcode) async {
final ScannedProductState? state = getBarcodeState(barcode);
if (state == null) {
if (!_barcodes.contains(barcode)) {
_barcodes.add(barcode);
}
_setBarcodeState(barcode, ScannedProductState.LOADING);
_cacheOrLoadBarcode(barcode);
lastConsultedBarcode = barcode;
return true;
}
if (state == ScannedProductState.FOUND ||
state == ScannedProductState.CACHED) {
final Product product = getProduct(barcode);
_barcodes.remove(barcode);
_barcodes.add(barcode);
_addProduct(product, state);
if (state == ScannedProductState.CACHED) {
_updateBarcode(barcode);
}
lastConsultedBarcode = barcode;
return true;
}
return false;
}
Future<void> _cacheOrLoadBarcode(final String barcode) async {
final bool cached = await _cachedBarcode(barcode);
if (!cached) {
_loadBarcode(barcode);
}
}
Future<bool> _cachedBarcode(final String barcode) async {
final Product? product = await _daoProduct.get(barcode);
if (product != null) {
_addProduct(product, ScannedProductState.CACHED);
return true;
}
return false;
}
Future<FetchedProduct> _queryBarcode(
final String barcode,
) async =>
BarcodeProductQuery(
barcode: barcode,
daoProduct: _daoProduct,
).getFetchedProduct();
Future<void> _loadBarcode(
final String barcode,
) async {
final FetchedProduct fetchedProduct = await _queryBarcode(barcode);
switch (fetchedProduct.status) {
case FetchedProductStatus.ok:
_addProduct(fetchedProduct.product!, ScannedProductState.FOUND);
return;
case FetchedProductStatus.internetNotFound:
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
return;
}
}
Future<void> _updateBarcode(
final String barcode,
) async {
final FetchedProduct fetchedProduct = await _queryBarcode(barcode);
switch (fetchedProduct.status) {
case FetchedProductStatus.ok:
_addProduct(fetchedProduct.product!, ScannedProductState.FOUND);
return;
case FetchedProductStatus.internetNotFound:
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
return;
}
}
Future<void> _addProduct(
final Product product,
final ScannedProductState state,
) async {
_productList.refresh(product);
if (_latestFoundBarcode != product.barcode!) {
_latestFoundBarcode = product.barcode;
_daoProductList.push(productList, _latestFoundBarcode!);
_daoProductList.push(_history, _latestFoundBarcode!);
}
_setBarcodeState(product.barcode!, state);
}
Future<void> clearScanSession() async {
_daoProductList.clear(productList);
await refresh();
}
Future<void> removeBarcode(
final String barcode,
) async {
_daoProductList.set(
productList,
barcode,
false,
);
await refresh();
notifyListeners();
}
Future<void> refresh() async {
await _refresh();
notifyListeners();
}
}