From 71b8881a381fa2a3d61f82f3c7e2f128e2a26492 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Fri, 18 Oct 2024 17:00:24 +0200 Subject: [PATCH] feat: 5700 - no click for KP title without additional elements Impacted files: * `knowledge_panel_card.dart`: overrides the "isClickable" parameter in case there are eventually no further elements to display * `knowledge_panels_builder.dart`: new helper methods --- .../knowledge_panel_card.dart | 11 ++++- .../knowledge_panels_builder.dart | 49 +++++++++++++++++-- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart index b40ff5a410a..82f53fef727 100644 --- a/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart +++ b/packages/smooth_app/lib/knowledge_panel/knowledge_panels/knowledge_panel_card.dart @@ -43,11 +43,18 @@ class KnowledgePanelCard extends StatelessWidget { ); } + // in some cases there's nothing to click about. + // cf. https://github.com/openfoodfacts/smooth-app/issues/5700 + final bool improvedIsClickable = isClickable && + KnowledgePanelsBuilder.hasSomethingToDisplay( + product, + panelId, + ); return Padding( padding: const EdgeInsets.symmetric(vertical: SMALL_SPACE), child: InkWell( borderRadius: ANGULAR_BORDER_RADIUS, - onTap: !isClickable + onTap: !improvedIsClickable ? null : () async => Navigator.push( context, @@ -64,7 +71,7 @@ class KnowledgePanelCard extends StatelessWidget { ), child: KnowledgePanelsBuilder.getPanelSummaryWidget( panel, - isClickable: isClickable, + isClickable: improvedIsClickable, margin: EdgeInsets.zero, ) ?? const SizedBox(), diff --git a/packages/smooth_app/lib/knowledge_panel/knowledge_panels_builder.dart b/packages/smooth_app/lib/knowledge_panel/knowledge_panels_builder.dart index feaff5bacb7..4c9e7fd34a1 100644 --- a/packages/smooth_app/lib/knowledge_panel/knowledge_panels_builder.dart +++ b/packages/smooth_app/lib/knowledge_panel/knowledge_panels_builder.dart @@ -149,6 +149,24 @@ class KnowledgePanelsBuilder { return elements.first; } + /// Returns true if there are elements to display for that panel. + static bool hasSomethingToDisplay( + final Product product, + final String panelId, + ) { + final KnowledgePanel panel = + KnowledgePanelsBuilder.getKnowledgePanel(product, panelId)!; + if (panel.elements == null) { + return false; + } + for (final KnowledgePanelElement element in panel.elements!) { + if (_hasSomethingToDisplay(element: element, product: product)) { + return true; + } + } + return false; + } + /// Returns a padded widget that displays the KP element, or rarely null. static Widget? getElementWidget({ required final KnowledgePanelElement knowledgePanelElement, @@ -178,6 +196,8 @@ class KnowledgePanelsBuilder { } /// Returns the widget that displays the KP element, or rarely null. + /// + /// cf. [_hasSomethingToDisplay]. static Widget? _getElementWidget({ required final KnowledgePanelElement element, required final Product product, @@ -242,10 +262,33 @@ class KnowledgePanelsBuilder { element.actionElement!, product, ); + } + } - default: - Logs.e('unexpected element type: ${element.elementType}'); - return null; + /// Returns true if the element has something to display. + /// + /// cf. [_getElementWidget]. + static bool _hasSomethingToDisplay({ + required final KnowledgePanelElement element, + required final Product product, + }) { + switch (element.elementType) { + case KnowledgePanelElementType.TEXT: + case KnowledgePanelElementType.IMAGE: + case KnowledgePanelElementType.PANEL_GROUP: + case KnowledgePanelElementType.TABLE: + case KnowledgePanelElementType.MAP: + case KnowledgePanelElementType.ACTION: + return true; + case KnowledgePanelElementType.UNKNOWN: + return false; + case KnowledgePanelElementType.PANEL: + final String panelId = element.panelElement!.panelId; + final KnowledgePanel? panel = getKnowledgePanel(product, panelId); + if (panel == null) { + return false; + } + return true; } }