Skip to content

Commit

Permalink
feat: show exact time remaining for episode
Browse files Browse the repository at this point in the history
  • Loading branch information
danjac committed Jan 12, 2025
1 parent 9909274 commit 8b6a4a3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 36 deletions.
5 changes: 5 additions & 0 deletions radiofeed/episodes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,8 @@ def __str__(self) -> str:
self.listened.isoformat(),
]
)

@cached_property
def time_remaining(self) -> int:
"""Returns the time remaining in the episode in seconds."""
return max(self.episode.duration_in_seconds - self.current_time, 0)
16 changes: 16 additions & 0 deletions radiofeed/episodes/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,19 @@ def test_str(self):
listened=datetime.datetime(year=2024, month=9, day=10),
)
assert str(audio_log) == "user 1 | episode 2 | 2024-09-10T00:00:00"

@pytest.mark.django_db
def test_time_remaining(self):
audio_log = AudioLogFactory(
episode__duration="1:00:00",
current_time=60 * 10,
)
assert audio_log.time_remaining == 50 * 60

@pytest.mark.django_db
def test_time_remaining_negative(self):
audio_log = AudioLogFactory(
episode__duration="1:00:00",
current_time=60 * 70,
)
assert audio_log.time_remaining == 0
12 changes: 0 additions & 12 deletions radiofeed/templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,3 @@ def format_duration(total_seconds: int | None) -> str:
rv.append(f"{total_minutes} minute{pluralize(total_minutes)}")

return " ".join(rv)


@register.filter
def percentage(value: float, total: float) -> int:
"""Returns % value.
Example:
{{ value|percentage:total }}% done
"""
if 0 in (value, total):
return 0
return min(math.ceil((value / total) * 100), 100)
22 changes: 1 addition & 21 deletions radiofeed/tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import pytest
from django.contrib.sites.models import Site

from radiofeed.templatetags import (
absolute_uri,
format_duration,
percentage,
)
from radiofeed.templatetags import absolute_uri, format_duration


@pytest.fixture
Expand All @@ -23,22 +19,6 @@ def auth_req(req, user):
return req


class TestPercentage:
@pytest.mark.parametrize(
("value", "total", "expected"),
[
pytest.param(0, 0, 0, id="all zero"),
pytest.param(50, 0, 0, id="total zero"),
pytest.param(0, 50, 0, id="value zero"),
pytest.param(50, 100, 50, id="50%"),
pytest.param(150, 100, 100, id="150%"),
pytest.param(100, 100, 100, id="100%"),
],
)
def test_percentage(self, value, total, expected):
assert percentage(value, total) == expected


class TestFormatDuration:
@pytest.mark.parametrize(
("duration", "expected"),
Expand Down
6 changes: 3 additions & 3 deletions templates/episodes/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ <h2 class="text-lg font-semibold">
Listened
</span>
<span>{{ audio_log.listened|date:"DATE_FORMAT" }}</span>
{% with percent_complete=audio_log.current_time|percentage:episode.duration_in_seconds %}
{% if percent_complete > 0 and percent_complete < 100 %}
<span>{{ percent_complete }}% Complete</span>
{% with time_remaining=audio_log.time_remaining|format_duration %}
{% if audio_log.current_time and time_remaining %}
<span>{{ time_remaining }} left</span>
{% endif %}
{% endwith %}
</div>
Expand Down

0 comments on commit 8b6a4a3

Please sign in to comment.