Skip to content

Commit

Permalink
fix: 5634 - refresh of price lazy counters when accessing lists
Browse files Browse the repository at this point in the history
Impacted files:
* `get_prices_model.dart`: added a `LazyCounterPrices?` parameter
* `lazy_counter.dart`: added a `bool notify` parameter
* `lazy_counter_widget.dart`: now refreshed by "external" new count
* `price_user_button.dart`: added a `LazyCounterPrices` parameter to have the counter refreshed
* `product_prices_list.dart`: now setting the lazy counter when displaying the list
* `user_preferences.dart`: added a `bool notify` parameter for the lazy counter setter
* `user_preferences_account.dart`: added a `LazyCounterPrices` parameter to have the counter refreshed
  • Loading branch information
monsieurtanuki committed Oct 9, 2024
1 parent 2c411c0 commit a121e3d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 32 deletions.
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
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 @@ -276,6 +276,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 a121e3d

Please sign in to comment.