Skip to content

Commit

Permalink
feat: 5700 - no click for KP title without additional elements (#5709)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
monsieurtanuki authored Oct 20, 2024
1 parent 3185c72 commit 29f433c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Widget>(
context,
Expand All @@ -64,7 +71,7 @@ class KnowledgePanelCard extends StatelessWidget {
),
child: KnowledgePanelsBuilder.getPanelSummaryWidget(
panel,
isClickable: isClickable,
isClickable: improvedIsClickable,
margin: EdgeInsets.zero,
) ??
const SizedBox(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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,
Expand Down Expand Up @@ -180,6 +198,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,
Expand Down Expand Up @@ -244,10 +264,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;
}
}

Expand Down

0 comments on commit 29f433c

Please sign in to comment.