Files
smooth-app/packages/smooth_app/lib/data_models/continuous_scan_model.dart
Aman Raj cf4fa6aebe fix: Background image upload (#2433)
* work manager used for bg schedule

* removed swith case

* remove unwanted contrains and randomize the taskid
as image.others might fail

* added translations

* new product image upload working

* removed debug for work manager

* delete photo when uploaded

* use await keywords while initializing

* refactor the code

* fix show image after queed

* serialize and deserialze input data seperately

* add an instant product in the loacl db to
let the user not confuse

* added smooth random class

* use smooth random to generate 8 digits

* remove unneccesary await

* update the local db with a product if not yet
uploaded

* update local databse after sucessful
update into server

* don't refresh automatically after upload queed

* supply language code to work manager

* import app localizations

* small refactoring

* refactor the code for work manager

* iOS Config for WorkManager

* formatting

* suggestion by tolemon

Co-authored-by: Pierre Slamich <pierre.slamich@gmail.com>

* suggestion by tolemon

Co-authored-by: Pierre Slamich <pierre.slamich@gmail.com>

* suggestion by tolemon

Co-authored-by: Pierre Slamich <pierre.slamich@gmail.com>

* small fix

* add swith case to the workmanager

* efficient switch case

* Update packages/smooth_app/lib/l10n/app_en.arb

Suggested by tolemon

Co-authored-by: Pierre Slamich <pierre.slamich@gmail.com>

* minor fix

* Basic Input details add are now run in background

* basic details are now updated immediatly
even without internet

* nutrient edit task are now background
and also conflict when recheduling task is managed

* menu screen for background tasks and
nutrition edits are now backgrounded

* button for retry now works in the bg tasks screen

* formatting

* genaralised function for non image tasks

* should retry set default to false

* better mechanism for retries and refactor

* refactor and consts duration of 3s for snackbars

* now language country and user are properly dropped

* Update packages/smooth_app/ios/Runner/Info.plist

* Update packages/smooth_app/ios/Runner/AppDelegate.swift

* Error messages fixed

* custom workmanager dependencies added

* migrated basic detail task from work manager to
taskmanager

* chanages to do the product edit changes in bg

* dart format

* cleaned the function calls when doing bg task

* dao_task edited

* unnecessary methods removed

* use int as id instead of string

* added comments and made fields final

* used const variable instead of hardcoded ones

* get pending tasks from taskManager class

* refactor

* handled exceptions

* string instead of int

* remove unused plugin

* removed unnessary files

* update to localdatabase working without islotes

* suggestions left as comments on PR

* deleted unncessary Random class

* doc added

* Remove WorkManager iOS code

* reset publock

* reset publock

* remove harcoded color

* used duration constant class instead of harcoded

* make methods to generate taskId

* dart frmt

* changes as per the code review

* string buffer , var documentation and rename

* error fix

* refactor and spelling corrections

* /// instead of //

* dart ff formater

* more refactorings

* Update background_task_helper.dart

* dart ff fix

Co-authored-by: monsieurtanuki <fabrice_fontaine@hotmail.com>
Co-authored-by: Edouard Marquez <g123k@users.noreply.github.com>
Co-authored-by: Pierre Slamich <pierre.slamich@gmail.com>
Co-authored-by: Pierre Slamich <pierre@openfoodfacts.org>
2022-09-01 12:15:09 +02:00

287 lines
8.2 KiB
Dart

import 'dart:async';
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/dao_product.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/query/barcode_product_query.dart';
import 'package:smooth_app/services/smooth_services.dart';
enum ScannedProductState {
FOUND,
NOT_FOUND,
LOADING,
THANKS,
CACHED,
ERROR_INTERNET,
ERROR_INVALID_CODE,
}
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) {
Logs.e('Load database error', ex: 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) {
Logs.e('Refresh database error', ex: 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];
/// Adds a barcode
/// Will return [true] if this barcode is successfully added
Future<bool> onScan(String? code) async {
if (code == null) {
return false;
}
if (_barcodeTrustCheck != code) {
_barcodeTrustCheck = code;
return false;
}
if (_latestScannedBarcode == code || _barcodes.contains(code)) {
lastConsultedBarcode = code;
return false;
}
AnalyticsHelper.trackScannedProduct(barcode: code);
_latestScannedBarcode = code;
return _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 || state == ScannedProductState.NOT_FOUND) {
if (!_barcodes.contains(barcode)) {
_barcodes.add(barcode);
}
_setBarcodeState(barcode, ScannedProductState.LOADING);
_cacheOrLoadBarcode(barcode);
lastConsultedBarcode = barcode;
return true;
}
if (state == ScannedProductState.FOUND ||
state == ScannedProductState.CACHED) {
_barcodes.remove(barcode);
_barcodes.add(barcode);
_addProduct(barcode, 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) {
try {
// We try to load the fresh copy of product from the server
final FetchedProduct fetchedProduct =
await _queryBarcode(barcode).timeout(SnackBarDuration.long);
if (fetchedProduct.product != null) {
_addProduct(barcode, ScannedProductState.CACHED);
return true;
}
} on TimeoutException {
// We tried to load the product from the server,
// but it was taking more than 5 seconds.
// So we'll just show the already cached product.
_addProduct(barcode, ScannedProductState.CACHED);
return true;
}
_addProduct(barcode, ScannedProductState.CACHED);
return true;
}
return false;
}
Future<FetchedProduct> _queryBarcode(
final String barcode,
) async =>
BarcodeProductQuery(
barcode: barcode,
daoProduct: _daoProduct,
isScanned: true,
).getFetchedProduct();
Future<void> _loadBarcode(
final String barcode,
) async {
final FetchedProduct fetchedProduct = await _queryBarcode(barcode);
switch (fetchedProduct.status) {
case FetchedProductStatus.ok:
_addProduct(barcode, ScannedProductState.FOUND);
return;
case FetchedProductStatus.internetNotFound:
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR_INTERNET);
return;
case FetchedProductStatus.codeInvalid:
_setBarcodeState(barcode, ScannedProductState.ERROR_INVALID_CODE);
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(barcode, ScannedProductState.FOUND);
return;
case FetchedProductStatus.internetNotFound:
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR_INTERNET);
return;
case FetchedProductStatus.codeInvalid:
_setBarcodeState(barcode, ScannedProductState.ERROR_INVALID_CODE);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
return;
}
}
Future<void> _addProduct(
final String barcode,
final ScannedProductState state,
) async {
if (_latestFoundBarcode != barcode) {
_latestFoundBarcode = barcode;
await _daoProductList.push(productList, _latestFoundBarcode!);
await _daoProductList.push(_history, _latestFoundBarcode!);
_daoProductList.localDatabase.notifyListeners();
}
_setBarcodeState(barcode, state);
}
Future<void> clearScanSession() async {
await _daoProductList.clear(productList);
await refresh();
}
Future<void> removeBarcode(
final String barcode,
) async {
await _daoProductList.set(
productList,
barcode,
false,
);
_barcodes.remove(barcode);
if (barcode == _latestScannedBarcode) {
_latestScannedBarcode = null;
}
notifyListeners();
}
Future<void> refresh() async {
await _refresh();
notifyListeners();
}
}