add social battery sorting engine
This commit is contained in:
@@ -44,6 +44,78 @@ body {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.social-battery {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1.5rem;
|
||||
padding: 1rem;
|
||||
margin-bottom: 1.5rem;
|
||||
color: #07384d;
|
||||
background: #d9f4ff;
|
||||
border-radius: 1rem;
|
||||
}
|
||||
|
||||
.social-battery h2,
|
||||
.social-battery p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.social-battery p {
|
||||
margin-top: .25rem;
|
||||
}
|
||||
|
||||
.social-battery__levels {
|
||||
display: flex;
|
||||
gap: .25rem;
|
||||
}
|
||||
|
||||
.social-battery__input {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.social-battery__levels label {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 2.25rem;
|
||||
height: 2.25rem;
|
||||
cursor: pointer;
|
||||
color: #4f7280;
|
||||
border-radius: .5rem;
|
||||
transition: color .15s ease, background-color .15s ease, transform .15s ease;
|
||||
}
|
||||
|
||||
.social-battery__levels label:hover {
|
||||
color: #07384d;
|
||||
background: #fff;
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.social-battery__input:checked + label {
|
||||
color: #fff;
|
||||
background: #087aa9;
|
||||
}
|
||||
|
||||
.social-battery__input:focus-visible + label {
|
||||
outline: 3px solid #07384d;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.social-battery {
|
||||
align-items: flex-start;
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
|
||||
.task-list__item form {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Controller;
|
||||
use App\Entity\Task;
|
||||
use App\Form\TaskType;
|
||||
use App\Repository\TaskRepository;
|
||||
use App\Service\TaskRecommendationService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -15,13 +16,30 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
final class TaskController extends AbstractController
|
||||
{
|
||||
#[Route(name: 'app_task_index', methods: ['GET'])]
|
||||
public function index(TaskRepository $taskRepository): Response
|
||||
public function index(Request $request, TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService): Response
|
||||
{
|
||||
$batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getSession()->get('social_battery', 3));
|
||||
|
||||
return $this->render('task/index.html.twig', [
|
||||
'tasks' => $taskRepository->findAll(),
|
||||
'tasks' => $taskRecommendationService->sort($taskRepository->findAll(), $batteryLevel),
|
||||
'battery_level' => $batteryLevel,
|
||||
'battery_budget' => $taskRecommendationService->budgetFor($batteryLevel),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/social-battery', name: 'app_task_social_battery', methods: ['POST'])]
|
||||
public function socialBattery(Request $request, TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService): Response
|
||||
{
|
||||
if (!$this->isCsrfTokenValid('social_battery', $request->getPayload()->getString('_token'))) {
|
||||
throw $this->createAccessDeniedException('Invalid CSRF token.');
|
||||
}
|
||||
|
||||
$batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getPayload()->getInt('social_battery'));
|
||||
$request->getSession()->set('social_battery', $batteryLevel);
|
||||
|
||||
return $this->renderTaskRecommendations($taskRepository, $taskRecommendationService, $batteryLevel);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_task_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
@@ -69,7 +87,7 @@ final class TaskController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/{id}/completion', name: 'app_task_toggle_completion', methods: ['POST'])]
|
||||
public function toggleCompletion(Request $request, Task $task, EntityManagerInterface $entityManager): Response
|
||||
public function toggleCompletion(Request $request, Task $task, EntityManagerInterface $entityManager, TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService): Response
|
||||
{
|
||||
if (!$this->isCsrfTokenValid('completion'.$task->getId(), $request->getPayload()->getString('_token'))) {
|
||||
throw $this->createAccessDeniedException('Invalid CSRF token.');
|
||||
@@ -81,9 +99,9 @@ final class TaskController extends AbstractController
|
||||
->setUpdatedAt(new \DateTimeImmutable());
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->render('task/_task_item.html.twig', [
|
||||
'task' => $task,
|
||||
]);
|
||||
$batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getSession()->get('social_battery', 3));
|
||||
|
||||
return $this->renderTaskList($taskRepository, $taskRecommendationService, $batteryLevel);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_task_delete', methods: ['POST'])]
|
||||
@@ -96,4 +114,20 @@ final class TaskController extends AbstractController
|
||||
|
||||
return $this->redirectToRoute('app_task_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
private function renderTaskList(TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService, int $batteryLevel): Response
|
||||
{
|
||||
return $this->render('task/_task_list.html.twig', [
|
||||
'tasks' => $taskRecommendationService->sort($taskRepository->findAll(), $batteryLevel),
|
||||
]);
|
||||
}
|
||||
|
||||
private function renderTaskRecommendations(TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService, int $batteryLevel): Response
|
||||
{
|
||||
return $this->render('task/_task_recommendations.html.twig', [
|
||||
'tasks' => $taskRecommendationService->sort($taskRepository->findAll(), $batteryLevel),
|
||||
'battery_level' => $batteryLevel,
|
||||
'battery_budget' => $taskRecommendationService->budgetFor($batteryLevel),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Task;
|
||||
|
||||
/**
|
||||
* Central home for recommendation rules. Add future signals such as priority,
|
||||
* category, or recurring tasks here without changing the controller or view.
|
||||
*/
|
||||
final class TaskRecommendationService
|
||||
{
|
||||
/** @var array<int, int> */
|
||||
private const ENERGY_BUDGET_MINUTES = [
|
||||
1 => 15,
|
||||
2 => 30,
|
||||
3 => 60,
|
||||
4 => 90,
|
||||
5 => 180,
|
||||
];
|
||||
|
||||
public function normaliseBatteryLevel(int $batteryLevel): int
|
||||
{
|
||||
return max(1, min(5, $batteryLevel));
|
||||
}
|
||||
|
||||
public function budgetFor(int $batteryLevel): int
|
||||
{
|
||||
return self::ENERGY_BUDGET_MINUTES[$this->normaliseBatteryLevel($batteryLevel)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task[] $tasks
|
||||
*
|
||||
* @return Task[] Active tasks that fit the selected energy budget are first.
|
||||
*/
|
||||
public function sort(array $tasks, int $batteryLevel): array
|
||||
{
|
||||
$batteryLevel = $this->normaliseBatteryLevel($batteryLevel);
|
||||
$budget = $this->budgetFor($batteryLevel);
|
||||
|
||||
usort($tasks, function (Task $left, Task $right) use ($batteryLevel, $budget): int {
|
||||
return $this->sortKey($left, $batteryLevel, $budget) <=> $this->sortKey($right, $batteryLevel, $budget);
|
||||
});
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
/** @return array<int, int|string> */
|
||||
private function sortKey(Task $task, int $batteryLevel, int $budget): array
|
||||
{
|
||||
$duration = $task->getEstimatedDurationMinutes();
|
||||
$hasDuration = null !== $duration;
|
||||
$duration ??= PHP_INT_MAX;
|
||||
$fitsBudget = $hasDuration && $duration <= $budget;
|
||||
|
||||
// Low-energy days start with the smallest task. With more energy,
|
||||
// prefer a task that makes good use of the available time budget.
|
||||
$durationScore = $batteryLevel <= 2 ? $duration : abs($budget - $duration);
|
||||
|
||||
return [
|
||||
$task->isActive() ? 0 : 1,
|
||||
$hasDuration ? 0 : 1,
|
||||
$fitsBudget ? 0 : 1,
|
||||
$durationScore,
|
||||
$duration,
|
||||
strtolower($task->getTitle() ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
aria-label="Mark {{ task.title }} as {{ task.isActive ? 'completed' : 'active' }}"
|
||||
hx-post="{{ path('app_task_toggle_completion', {'id': task.id}) }}"
|
||||
hx-include="closest form"
|
||||
hx-target="closest .task-list__item"
|
||||
hx-target="closest .task-list"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label class="task-list__toggle" for="task-completion-{{ task.id }}" title="Mark {{ task.title }} as {{ task.isActive ? 'completed' : 'active' }}">
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<ul id="task-list" class="task-list" aria-label="Tasks">
|
||||
{% for task in tasks %}
|
||||
{{ include('task/_task_item.html.twig', {task: task}) }}
|
||||
{% else %}
|
||||
<li class="task-list__empty">No tasks yet.</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
@@ -0,0 +1,39 @@
|
||||
<div id="task-recommendations">
|
||||
{% set battery_levels = {
|
||||
1: ['Low', 'battery_0_bar'],
|
||||
2: ['Steady', 'battery_2_bar'],
|
||||
3: ['Okay', 'battery_3_bar'],
|
||||
4: ['Good', 'battery_5_bar'],
|
||||
5: ['Full', 'battery_full']
|
||||
} %}
|
||||
<section class="social-battery" aria-labelledby="social-battery-title">
|
||||
<div>
|
||||
<h2 id="social-battery-title">Social battery</h2>
|
||||
<p>Choose today’s energy. We’ll put tasks that fit your {{ battery_budget }}-minute budget first.</p>
|
||||
</div>
|
||||
<form class="social-battery__levels">
|
||||
<input type="hidden" name="_token" value="{{ csrf_token('social_battery') }}">
|
||||
{% for level, option in battery_levels %}
|
||||
<input
|
||||
id="social-battery-{{ level }}"
|
||||
class="social-battery__input"
|
||||
type="radio"
|
||||
name="social_battery"
|
||||
value="{{ level }}"
|
||||
{{ battery_level == level ? 'checked' : '' }}
|
||||
aria-label="{{ option[0] }} energy"
|
||||
hx-post="{{ path('app_task_social_battery') }}"
|
||||
hx-include="closest form"
|
||||
hx-target="#task-recommendations"
|
||||
hx-swap="outerHTML"
|
||||
>
|
||||
<label for="social-battery-{{ level }}" title="{{ option[0] }} energy">
|
||||
<span class="material-symbols-outlined" aria-hidden="true">{{ option[1] }}</span>
|
||||
<span class="visually-hidden">{{ option[0] }} energy</span>
|
||||
</label>
|
||||
{% endfor %}
|
||||
</form>
|
||||
</section>
|
||||
|
||||
{{ include('task/_task_list.html.twig', {tasks: tasks}) }}
|
||||
</div>
|
||||
@@ -11,13 +11,11 @@ add
|
||||
</span></a>
|
||||
</div>
|
||||
|
||||
<ul class="task-list" aria-label="Tasks">
|
||||
{% for task in tasks %}
|
||||
{{ include('task/_task_item.html.twig', {task: task}) }}
|
||||
{% else %}
|
||||
<li class="task-list__empty">No tasks yet.</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{{ include('task/_task_recommendations.html.twig', {
|
||||
tasks: tasks,
|
||||
battery_level: battery_level,
|
||||
battery_budget: battery_budget
|
||||
}) }}
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user