Skip to content

Commit

Permalink
utils: Fix isMobileFriendly calculation from display length
Browse files Browse the repository at this point in the history
The ranges are calculated from pre-existing behaviour exhibited by
other frontends such as gnome software and appstreamcli subcommands and
generally matches historical recommendation and current usage.

To reduce complexity,not all comparison operators and relationship
between multiple comparisons are supported. It's calculated on
first-found basis of a single valid tag matching the criteria.
  • Loading branch information
bbhtt committed Jan 17, 2025
1 parent 38b1f5a commit 376bcbf
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions backend/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import re
from datetime import datetime, timedelta
from typing import Any, Literal
from typing import Any

import gi
import jwt
Expand Down Expand Up @@ -99,26 +99,15 @@ def appstream2dict(appstream_url=None) -> dict[str, dict]:
app["type"] = component.attrib.get("type", "generic")
app["locales"] = {}

isMobileFriendly: list[bool] = []
isMobileFriendly = False
requires = component.find("requires")
if requires is not None:
if display_lengths := requires.findall("display_length"):
for display_length in display_lengths:
compare: Literal[
"eq",
"ne",
"lt",
"gt",
"le",
"ge",
] = display_length.attrib.get("compare") or "ge"

isMobileFriendly.append(
compare_requires_is_between_mobile_target(
int(display_length.text),
compare,
)
)
if any(
display_length_supports_mobile(display_length)
for display_length in display_lengths
):
isMobileFriendly = True

hasTouch = False
supports = component.find("supports")
Expand All @@ -135,9 +124,7 @@ def appstream2dict(appstream_url=None) -> dict[str, dict]:
if control.text == "touch":
hasTouch = True

app["isMobileFriendly"] = (
all(isMobileFriendly) and any(isMobileFriendly) and hasTouch
)
app["isMobileFriendly"] = isMobileFriendly and hasTouch

descriptions = component.findall("description")
if len(descriptions):
Expand Down Expand Up @@ -472,18 +459,24 @@ def appstream2dict(appstream_url=None) -> dict[str, dict]:
return apps


def compare_requires_is_between_mobile_target(
display_length: int,
compare: Literal["ge", "lt", "gt", "le", "eq", "ne"],
):
if compare == "ge" and display_length <= mobile_max_size:
return True
if compare == "lt" and display_length > mobile_max_size:
return True
if compare == "gt" and display_length < mobile_min_size:
return True
if compare == "le" and display_length >= mobile_min_size:
return True
def display_length_supports_mobile(display_length: etree.Element) -> bool:
conditions = {
"ge": lambda value: 1 <= value <= 360,
"gt": lambda value: 1 <= value <= 359,
"le": lambda value: 480 <= value <= 1279,
"lt": lambda value: 481 <= value <= 1280,
}
compare = display_length.attrib.get("compare", "ge")
displaylen_value = display_length.text
if displaylen_value is None:
return False

try:
value = int(displaylen_value)
return conditions.get(compare, lambda x: False)(value)
except ValueError:
pass

return False


Expand Down

0 comments on commit 376bcbf

Please sign in to comment.