Skip to content

Commit

Permalink
Merge branch 'develop' into fix/5665
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurtanuki authored Oct 13, 2024
2 parents a8fbc69 + db080d2 commit dd573a9
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 53 deletions.
18 changes: 14 additions & 4 deletions packages/smooth_app/lib/cards/category_cards/svg_safe_network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@ class _SvgSafeNetworkState extends State<SvgSafeNetwork> {
}
}
if (snapshot.error != null) {
final bool serverOrConnectionIssue =
snapshot.error.toString().contains("Failed host lookup: '");
if (!serverOrConnectionIssue) {
if (snapshot.error.toString().contains("Failed host lookup: '")) {
Logs.w(
'Failed host lookup for "$_url"',
ex: snapshot.error,
);
} else if (snapshot.error
.toString()
.contains('Connection timed out')) {
Logs.w(
'Connection timed out for "$_url"',
ex: snapshot.error,
);
} else {
Logs.e(
'Could not download "$_url"',
'Could really not download "$_url"',
ex: snapshot.error,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,20 @@ class UserPreferences extends ChangeNotifier {

String _getLazyCountTag(final String tag) => '$_TAG_LAZY_COUNT_PREFIX$tag';

Future<void> setLazyCount(final int value, final String suffixTag) async =>
_sharedPreferences.setInt(_getLazyCountTag(suffixTag), value);
Future<void> setLazyCount(
final int value,
final String suffixTag, {
required final bool notify,
}) async {
final int? oldValue = getLazyCount(suffixTag);
if (value == oldValue) {
return;
}
await _sharedPreferences.setInt(_getLazyCountTag(suffixTag), value);
if (notify) {
notifyListeners();
}
}

int? getLazyCount(final String suffixTag) =>
_sharedPreferences.getInt(_getLazyCountTag(suffixTag));
Expand Down
53 changes: 36 additions & 17 deletions packages/smooth_app/lib/database/dao_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,42 @@ class DaoProduct extends AbstractSqlDao implements BulkDeletable {
required final int limit,
required final List<String> excludeBarcodes,
}) async {
final List<Map<String, dynamic>> queryResults =
await localDatabase.database.rawQuery(
'select p.$_TABLE_PRODUCT_COLUMN_BARCODE '
'from'
' $_TABLE_PRODUCT p'
' left outer join ${DaoProductLastAccess.TABLE} a'
' on p.$_TABLE_PRODUCT_COLUMN_BARCODE = a.${DaoProductLastAccess.COLUMN_BARCODE} '
'where'
' p.$_TABLE_PRODUCT_COLUMN_LANGUAGE is null'
' or p.$_TABLE_PRODUCT_COLUMN_LANGUAGE != ? '
'order by a.${DaoProductLastAccess.COLUMN_LAST_ACCESS} desc nulls last '
'limit ?',
<Object>[
language.offTag,
limit + excludeBarcodes.length,
],
);
/// Unfortunately, some SQFlite implementations don't support "nulls last"
String getRawQuery(final bool withNullsLast) =>
'select p.$_TABLE_PRODUCT_COLUMN_BARCODE '
'from'
' $_TABLE_PRODUCT p'
' left outer join ${DaoProductLastAccess.TABLE} a'
' on p.$_TABLE_PRODUCT_COLUMN_BARCODE = a.${DaoProductLastAccess.COLUMN_BARCODE} '
'where'
' p.$_TABLE_PRODUCT_COLUMN_LANGUAGE is null'
' or p.$_TABLE_PRODUCT_COLUMN_LANGUAGE != ? '
'order by a.${DaoProductLastAccess.COLUMN_LAST_ACCESS} desc ${withNullsLast ? 'nulls last' : ''} '
'limit ?';

List<Map<String, dynamic>> queryResults = <Map<String, dynamic>>[];
try {
queryResults = await localDatabase.database.rawQuery(
getRawQuery(true),
<Object>[
language.offTag,
limit + excludeBarcodes.length,
],
);
} catch (e) {
if (!e.toString().startsWith(
'DatabaseException(near "nulls": syntax error (code 1 SQLITE_ERROR[1])',
)) {
rethrow;
}
queryResults = await localDatabase.database.rawQuery(
getRawQuery(false),
<Object>[
language.offTag,
limit + excludeBarcodes.length,
],
);
}

final List<String> result = <String>[];

Expand Down
11 changes: 8 additions & 3 deletions packages/smooth_app/lib/pages/preferences/lazy_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ abstract class LazyCounter {
/// Sets the value cached locally;
Future<void> setLocalCount(
final int value,
final UserPreferences userPreferences,
) =>
userPreferences.setLazyCount(value, getSuffixTag());
final UserPreferences userPreferences, {
required final bool notify,
}) =>
userPreferences.setLazyCount(
value,
getSuffixTag(),
notify: notify,
);

/// Returns the suffix tag used to cache the value locally;
@protected
Expand Down
62 changes: 35 additions & 27 deletions packages/smooth_app/lib/pages/preferences/lazy_counter_widget.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
Expand All @@ -15,41 +17,44 @@ class LazyCounterWidget extends StatefulWidget {

class _LazyCounterWidgetState extends State<LazyCounterWidget> {
bool _loading = false;
int? _count;

@override
void initState() {
super.initState();
final UserPreferences userPreferences = context.read<UserPreferences>();
_count = widget.lazyCounter.getLocalCount(userPreferences);
if (_count == null) {
_asyncLoad();
final int? count = widget.lazyCounter.getLocalCount(userPreferences);
if (count == null) {
unawaited(_asyncLoad());
}
}

@override
Widget build(BuildContext context) => Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
if (_count != null) Text(_count.toString()),
if (_loading)
const Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator.adaptive(),
),
)
else
IconButton(
onPressed: () => _asyncLoad(),
icon: const Icon(Icons.refresh),
Widget build(BuildContext context) {
final UserPreferences userPreferences = context.watch<UserPreferences>();
final int? count = widget.lazyCounter.getLocalCount(userPreferences);
return Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
if (count != null) Text(count.toString()),
if (_loading)
const Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator.adaptive(),
),
],
);
)
else
IconButton(
onPressed: () => _asyncLoad(),
icon: const Icon(Icons.refresh),
),
],
);
}

Future<void> _asyncLoad() async {
if (_loading) {
Expand All @@ -63,8 +68,11 @@ class _LazyCounterWidgetState extends State<LazyCounterWidget> {
try {
final int? value = await widget.lazyCounter.getServerCount();
if (value != null) {
await widget.lazyCounter.setLocalCount(value, userPreferences);
_count = value;
await widget.lazyCounter.setLocalCount(
value,
userPreferences,
notify: false,
);
}
} catch (e) {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ class UserPreferencesAccount extends AbstractUserPreferences {
uriHelper: ProductQuery.uriPricesHelper,
),
title: appLocalizations.all_search_prices_latest_title,
lazyCounterPrices: const LazyCounterPrices(null),
),
),
),
Expand Down
5 changes: 5 additions & 0 deletions packages/smooth_app/lib/pages/prices/get_prices_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/pages/preferences/lazy_counter.dart';
import 'package:smooth_app/pages/prices/price_meta_product.dart';
import 'package:smooth_app/pages/prices/product_price_add_page.dart';
import 'package:smooth_app/query/product_query.dart';
Expand All @@ -13,6 +14,7 @@ class GetPricesModel {
required this.displayProduct,
required this.uri,
required this.title,
this.lazyCounterPrices,
this.enableCountButton = true,
this.subtitle,
this.addButton,
Expand Down Expand Up @@ -84,5 +86,8 @@ class GetPricesModel {
/// "Enable the count button?". Typically "false" for product price pages.
final bool enableCountButton;

/// Lazy Counter to refresh.
final LazyCounterPrices? lazyCounterPrices;

static const int pageSize = 10;
}
2 changes: 2 additions & 0 deletions packages/smooth_app/lib/pages/prices/price_user_button.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/pages/preferences/lazy_counter.dart';
import 'package:smooth_app/pages/prices/get_prices_model.dart';
import 'package:smooth_app/pages/prices/price_button.dart';
import 'package:smooth_app/pages/prices/prices_page.dart';
Expand Down Expand Up @@ -46,6 +47,7 @@ class PriceUserButton extends StatelessWidget {
),
title: showUserTitle(user: user, context: context),
subtitle: user,
lazyCounterPrices: LazyCounterPrices(user),
),
),
),
Expand Down
13 changes: 13 additions & 0 deletions packages/smooth_app/lib/pages/prices/product_prices_list.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:matomo_tracker/matomo_tracker.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart';
import 'package:smooth_app/pages/prices/get_prices_model.dart';
Expand Down Expand Up @@ -50,6 +54,15 @@ class _ProductPricesListState extends State<ProductPricesList>
return Text(snapshot.data!.error!);
}
final GetPricesResult result = snapshot.data!.value;
if (widget.model.lazyCounterPrices != null && result.total != null) {
unawaited(
widget.model.lazyCounterPrices!.setLocalCount(
result.total!,
context.read<UserPreferences>(),
notify: true,
),
);
}
// highly improbable
if (result.items == null) {
return const Text('empty list');
Expand Down

0 comments on commit dd573a9

Please sign in to comment.