Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9 - showing current tile #12

Merged
merged 8 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
.composer
.phpunit.result.cache
vendor
composer.lock
index.php
krzysztofrewak marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 30 additions & 12 deletions src/Decorators/OpacityDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class OpacityDecorator implements Decorator
{
public function __construct(
public readonly string $color,
public readonly string $futureTileClass = "opacity: .25",
public readonly string $todayTileClass = "border-color: rgb(156, 163, 175)",
public readonly string $defaultSizeClass = "width: 1rem; height: 1rem",
public readonly string $defaultBorderClass = "border-width: 1px; border-color: rgb(209, 213, 219)",
public readonly string $defaultRoundedClass = "border-radius: 0.25rem",
) {}

public function decorate(array $bucket): array
Expand All @@ -19,25 +24,38 @@ public function decorate(array $bucket): array

/** @var Tile $tile */
foreach ($bucket as $tile) {
$tile->description = match (true) {
$description = $this->getDefaultAttributes();

$description[] = match (true) {
$tile->count === 0 => "background: White",
$tile->count === $max => "background: {$this->color}; opacity: .9;",
$tile->count >= $max * .9 => "background: {$this->color}; opacity: .8;",
$tile->count >= $max * .8 => "background: {$this->color}; opacity: .7;",
$tile->count >= $max * .7 => "background: {$this->color}; opacity: .6;",
$tile->count >= $max * .6 => "background: {$this->color}; opacity: .5;",
$tile->count >= $max * .5 => "background: {$this->color}; opacity: .4;",
$tile->count >= $max * .4 => "background: {$this->color}; opacity: .3;",
$tile->count >= $max * .3 => "background: {$this->color}; opacity: .2;",
$tile->count >= $max * .2 => "background: {$this->color}; opacity: .1;",
default => "background: {$this->color}; opacity: .05;",
$tile->count === $max => "background: {$this->color};",
$tile->count >= $max * .9 => "background: {$this->color}; filter: brightness(90%);",
$tile->count >= $max * .8 => "background: {$this->color}; filter: brightness(80%);",
$tile->count >= $max * .7 => "background: {$this->color}; filter: brightness(70%);",
$tile->count >= $max * .6 => "background: {$this->color}; filter: brightness(60%);",
$tile->count >= $max * .5 => "background: {$this->color}; filter: brightness(50%);",
$tile->count >= $max * .4 => "background: {$this->color}; filter: brightness(40%);",
$tile->count >= $max * .3 => "background: {$this->color}; filter: brightness(30%);",
$tile->count >= $max * .2 => "background: {$this->color}; filter: brightness(20%);",
default => "background: {$this->color}; filter: brightness(10%);",
};

if ($tile->inFuture) {
$tile->description = " opacity: .25;";
$description[] = $this->futureTileClass;
}

if ($tile->isToday) {
$description[] = $this->todayTileClass;
}

$tile->description = implode("; ", $description);
}

return $bucket;
}

protected function getDefaultAttributes(): array
{
return [$this->defaultSizeClass, $this->defaultBorderClass, $this->defaultRoundedClass];
}
}
22 changes: 20 additions & 2 deletions src/Decorators/TailwindDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class TailwindDecorator implements Decorator
{
public function __construct(
public readonly string $theme,
public readonly string $futureTileClass = "opacity-25",
public readonly string $todayTileClass = "border-gray-400 shadow",
public readonly string $defaultSizeClass = "size-4",
public readonly string $defaultBorderClass = "border border-gray-300",
public readonly string $defaultRoundedClass = "rounded",
) {}

public function decorate(array $bucket): array
Expand All @@ -19,7 +24,9 @@ public function decorate(array $bucket): array

/** @var Tile $tile */
foreach ($bucket as $tile) {
$tile->description = match (true) {
$description = $this->getDefaultAttributes();

$description[] = match (true) {
$tile->count === 0 => "bg-white",
$tile->count === $max => "bg-{$this->theme}-900",
$tile->count >= $max * .9 => "bg-{$this->theme}-800",
Expand All @@ -34,10 +41,21 @@ public function decorate(array $bucket): array
};

if ($tile->inFuture) {
$tile->description = " opacity-25";
$description[] = $this->futureTileClass;
}

if ($tile->isToday) {
$description[] = $this->todayTileClass;
}

$tile->description = implode(" ", $description);
}

return $bucket;
}

protected function getDefaultAttributes(): array
{
return [$this->defaultSizeClass, $this->defaultBorderClass, $this->defaultRoundedClass];
}
}
5 changes: 5 additions & 0 deletions src/HeatmapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function build(iterable $data): array
PeriodInterval::Daily => $item->isSameDay($date),
}),
),
isToday: match ($this->periodInterval) {
PeriodInterval::Monthly => $date->isCurrentMonth(),
PeriodInterval::Weekly => $date->isCurrentWeek(),
PeriodInterval::Daily => $date->isCurrentDay(),
},
inFuture: $date->isFuture(),
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Tile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function __construct(
public readonly string $label,
public readonly int $count,
public string $description = "",
public bool $isToday = false,
public bool $inFuture = false,
) {}

Expand Down
7 changes: 6 additions & 1 deletion tests/DecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public function testBasicArrayAccessItems(): void
actual: array_map(fn(Tile $item): int => $item->count, $result),
);

$defaultClasses = "size-4 border border-gray-300 rounded";

$this->assertSame(
expected: ["bg-white", "bg-white", "bg-green-100", "bg-green-600", "bg-white", "bg-green-900", "bg-white", "bg-green-50"],
expected: array_map(
fn(string $class): string => "$defaultClasses $class",
["bg-white", "bg-white", "bg-green-100", "bg-green-600", "bg-white", "bg-green-900", "bg-white", "bg-green-50"],
),
actual: array_map(fn(Tile $item): string => $item->description, $result),
);
}
Expand Down