Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/pub/packages/smooth_app/flutte…
Browse files Browse the repository at this point in the history
…r_email_sender-6.0.2
  • Loading branch information
monsieurtanuki authored Oct 31, 2023
2 parents 6880539 + 8261f92 commit 1703648
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 49 deletions.
47 changes: 45 additions & 2 deletions packages/smooth_app/lib/background/background_task_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
return result;
}

static const String _invalidUserError = 'invalid_user_id_and_password';

/// Uploads the product changes.
@override
Future<void> upload() async {
Expand All @@ -145,7 +147,19 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
);
if (result.status != ProductResultV3.statusSuccess &&
result.status != ProductResultV3.statusWarning) {
throw Exception('Could not save product - ${result.errors}');
bool isInvalidUser = false;
if (result.errors != null) {
for (final ProductResultFieldAnswer answer in result.errors!) {
if (answer.message?.id == _invalidUserError) {
isInvalidUser = true;
}
}
}
throw Exception(
'Could not save product'
' - '
'${result.errors}${isInvalidUser ? _getIncompleteUserData() : ''}',
);
}
return;
}
Expand All @@ -157,7 +171,36 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
uriHelper: uriProductHelper,
);
if (status.status != 1) {
throw Exception('Could not save product - ${status.error}');
bool isInvalidUser = false;
if (status.error != null) {
if (status.error!.contains(_invalidUserError)) {
isInvalidUser = true;
}
}
throw Exception(
'Could not save product'
' - '
'${status.error}${isInvalidUser ? _getIncompleteUserData() : ''}',
);
}
}

String _getIncompleteUserData() {
final User user = getUser();
final StringBuffer result = StringBuffer();
result.write(' [user:');
result.write(user.userId);
final int length = user.password.length;
result.write(' (');
if (length >= 8) {
result.write(user.password.substring(0, 2));
result.write('*' * (length - 4));
result.write(user.password.substring(length - 2));
} else {
result.write('passwordLength:$length');
}
result.write(')');
result.write('] ');
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ abstract class AbstractCache extends StatelessWidget {
this.iconUrl, {
this.width,
this.height,
this.displayAssetWhileWaiting = true,
});

/// Returns the best cache possibility: none, svg or png/jpeg
Expand All @@ -33,7 +32,6 @@ abstract class AbstractCache extends StatelessWidget {
final String? iconUrl;
final double? width;
final double? height;
final bool displayAssetWhileWaiting;

/// Returns a list of possible related cached filenames.
@protected
Expand Down Expand Up @@ -75,11 +73,4 @@ abstract class AbstractCache extends StatelessWidget {
size: width ?? height,
color: Colors.red,
);

@protected
Widget getCircularProgressIndicator() => SizedBox(
width: width ?? height,
height: height ?? width,
child: const CircularProgressIndicator.adaptive(),
);
}
19 changes: 8 additions & 11 deletions packages/smooth_app/lib/cards/category_cards/raster_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class RasterCache extends AbstractCache {
super.iconUrl, {
super.width,
super.height,
super.displayAssetWhileWaiting = true,
});

@override
Expand All @@ -31,16 +30,14 @@ class RasterCache extends AbstractCache {
if (loadingProgress == null) {
return child;
}
return displayAssetWhileWaiting
? RasterAsyncAsset(
AssetCacheHelper(
fullFilenames,
iconUrl!,
width: width,
height: height,
),
)
: getCircularProgressIndicator();
return RasterAsyncAsset(
AssetCacheHelper(
fullFilenames,
iconUrl!,
width: width,
height: height,
),
);
},
);
}
Expand Down
34 changes: 9 additions & 25 deletions packages/smooth_app/lib/cards/category_cards/svg_cache.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:smooth_app/cards/category_cards/abstract_cache.dart';
import 'package:smooth_app/cards/category_cards/asset_cache_helper.dart';
import 'package:smooth_app/cards/category_cards/svg_async_asset.dart';
import 'package:smooth_app/cards/category_cards/svg_safe_network.dart';

/// Widget that displays a svg from network (and cache while waiting).
class SvgCache extends AbstractCache {
Expand All @@ -14,7 +11,6 @@ class SvgCache extends AbstractCache {
super.width,
super.height,
this.color,
super.displayAssetWhileWaiting = true,
});

final Color? color;
Expand Down Expand Up @@ -55,26 +51,14 @@ class SvgCache extends AbstractCache {
? Colors.white
: Colors.black;
}
return SvgPicture.network(
iconUrl!,
colorFilter: forcedColor == null
? null
: ui.ColorFilter.mode(forcedColor, ui.BlendMode.srcIn),
width: width,
height: height,
fit: BoxFit.contain,
semanticsLabel: getSemanticsLabel(context, iconUrl!),
placeholderBuilder: (BuildContext context) => displayAssetWhileWaiting
? SvgAsyncAsset(
AssetCacheHelper(
cachedFilenames,
iconUrl!,
width: width,
height: height,
color: forcedColor,
),
)
: getCircularProgressIndicator(),
return SvgSafeNetwork(
AssetCacheHelper(
cachedFilenames,
iconUrl!,
width: width,
height: height,
color: forcedColor,
),
);
}

Expand Down
79 changes: 79 additions & 0 deletions packages/smooth_app/lib/cards/category_cards/svg_safe_network.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_svg/svg.dart';
import 'package:http/http.dart' as http;
import 'package:smooth_app/cards/category_cards/asset_cache_helper.dart';
import 'package:smooth_app/cards/category_cards/svg_async_asset.dart';
import 'package:smooth_app/cards/category_cards/svg_cache.dart';
import 'package:smooth_app/services/smooth_services.dart';

/// Widget with async load of SVG network file.
///
/// We could use SvgPicture.network, but it sends tons of errors if there in no
/// internet connection. That's why we download the data ourselves.
class SvgSafeNetwork extends StatefulWidget {
const SvgSafeNetwork(this.helper);

final AssetCacheHelper helper;

@override
State<SvgSafeNetwork> createState() => _SvgSafeNetworkState();
}

class _SvgSafeNetworkState extends State<SvgSafeNetwork> {
late final Future<String> _loading = _load();

String get _url => widget.helper.url;

Future<String> _load() async {
String? cached = _networkCache[_url];
if (cached != null) {
return cached;
}
final http.Response response = await http.get(Uri.parse(_url));
_networkCache[_url] = cached = response.body;
return cached;
}

@override
Widget build(BuildContext context) => FutureBuilder<String>(
future: _loading,
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data != null) {
return SvgPicture.string(
snapshot.data!,
width: widget.helper.width,
height: widget.helper.height,
colorFilter: widget.helper.color == null
? null
: ui.ColorFilter.mode(
widget.helper.color!,
ui.BlendMode.srcIn,
),
fit: BoxFit.contain,
semanticsLabel: SvgCache.getSemanticsLabel(context, _url),
placeholderBuilder: (BuildContext context) =>
SvgAsyncAsset(widget.helper),
);
}
}
if (snapshot.error != null) {
final bool serverOrConnectionIssue = snapshot.error.toString() ==
"Failed host lookup: 'static.openfoodfacts.org'";
if (!serverOrConnectionIssue) {
Logs.e(
'Could not download "$_url"',
ex: snapshot.error,
);
}
}
return SvgAsyncAsset(widget.helper);
},
);
}

/// Network cache, with url as key and SVG data as value.
Map<String, String> _networkCache = <String, String>{};
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SmoothLargeButtonWithIcon extends StatelessWidget {
final ThemeData themeData = Theme.of(context);
TextStyle style = textStyle ?? themeData.textTheme.bodyMedium!;

if (style.color == null) {
if (foregroundColor != null) {
style = style.copyWith(color: _getForegroundColor(themeData));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ class AddNewProductButton extends StatelessWidget {
this.iconData,
this.onPressed, {
required this.done,
this.trailingIconData,
});

final String label;
final IconData iconData;
final VoidCallback? onPressed;
final bool done;
final IconData? trailingIconData;

static const IconData doneIconData = Icons.check;
static const IconData todoIconData = Icons.add;
Expand All @@ -95,7 +97,7 @@ class AddNewProductButton extends StatelessWidget {
text: label,
icon: iconData,
onPressed: onPressed,
trailing: Icons.edit,
trailing: trailingIconData ?? Icons.edit,
backgroundColor: onPressed == null
? (dark ? darkGrey : lightGrey)
: done
Expand Down Expand Up @@ -144,6 +146,7 @@ class AddNewProductEditorButton extends StatelessWidget {
isLoggedInMandatory: isLoggedInMandatory,
),
done: done,
trailingIconData: done ? AddNewProductButton.doneIconData : null,
);
}
}
Expand Down

0 comments on commit 1703648

Please sign in to comment.