Add menu items for history, add manga reading history
All checks were successful
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-04-22 11:39:44 -04:00
parent f2991cd416
commit 4d6c15b030
14 changed files with 415 additions and 89 deletions

View File

@ -1,19 +1,21 @@
[anime_list]
route_prefix = "/anime"
route_prefix = ""
[anime_list.items]
watching = '/watching'
plan_to_watch = '/plan_to_watch'
on_hold = '/on_hold'
dropped = '/dropped'
completed = '/completed'
all = '/all'
watch_history = '/history/anime'
watching = '/anime/watching'
plan_to_watch = '/anime/plan_to_watch'
on_hold = '/anime/on_hold'
dropped = '/anime/dropped'
completed = '/anime/completed'
all = '/anime/all'
[manga_list]
route_prefix = "/manga"
route_prefix = ""
[manga_list.items]
reading = '/reading'
plan_to_read = '/plan_to_read'
on_hold = '/on_hold'
dropped = '/dropped'
completed = '/completed'
all = '/all'
reading_history = '/history/manga'
reading = '/manga/reading'
plan_to_read = '/manga/plan_to_read'
on_hold = '/manga/on_hold'
dropped = '/manga/dropped'
completed = '/manga/completed'
all = '/manga/all'

View File

@ -193,16 +193,6 @@ $routes = [
'username' => '.*?'
]
],
'anime_history' => [
'controller' => 'history',
'path' => '/history/anime',
'action' => 'anime',
],
'manga_history' => [
'controller' => 'history',
'path' => '/history/manga',
'action' => 'manga',
],
// ---------------------------------------------------------------------
// Default / Shared routes
// ---------------------------------------------------------------------
@ -289,6 +279,13 @@ $routes = [
'view' => ALPHA_SLUG_PATTERN,
],
],
'history' => [
'controller' => 'history',
'path' => '/history/{type}',
'tokens' => [
'type' => SLUG_PATTERN
]
],
'index_redirect' => [
'path' => '/',
'action' => 'redirectToDefaultRoute',

45
app/views/history.php Normal file
View File

@ -0,0 +1,45 @@
<main class="details fixed">
<?php if (empty($items)): ?>
<h3>No recent history.</h3>
<?php else: ?>
<section>
<?php foreach ($items as $name => $item): ?>
<article class="flex flex-no-wrap flex-justify-start">
<section class="flex-self-center history-img"><?= $helper->picture(
$item['coverImg'],
'jpg',
['width' => '110px', 'height' => '156px'],
['width' => '110px', 'height' => '156px']
) ?></section>
<section class="flex-self-center">
<?= $item['title'] ?>
<br />
<br />
<?= $item['action'] ?>
<br />
<small>
<?php if ( ! empty($item['dateRange'])):
[$startDate, $endDate] = array_map(
fn ($date) => $date->format('l, F d'),
$item['dateRange']
);
[$startTime, $endTime] = array_map(
fn ($date) => $date->format('h:i:s A'),
$item['dateRange']
);
?>
<?php if ($startDate === $endDate): ?>
<?= "{$startDate}, {$startTime} &ndash; {$endTime}" ?>
<?php else: ?>
<?= "{$startDate} {$startTime} &ndash; {$endDate} {$endTime}" ?>
<?php endif ?>
<?php else: ?>
<?= $item['updated']->format('l, F d h:i:s A') ?>
<?php endif ?>
</small>
</section>
</article>
<?php endforeach ?>
</section>
<?php endif ?>
</main>

View File

@ -1,19 +0,0 @@
<main class="details fixed">
<?php if (empty($items)): ?>
<h3>No recent watch history.</h3>
<?php else: ?>
<section>
<?php foreach ($items as $name => $item): ?>
<article class="flex flex-no-wrap flex-justify-start">
<section class="flex-self-center history-img"><?= $helper->picture(
$item['coverImg'],
'jpg',
['width' => '110px', 'height' => '156px'],
['width' => '110px', 'height' => '156px']
) ?></section>
<section class="flex-self-center"><?= $item['action'] ?></section>
</article>
<?php endforeach ?>
</section>
<?php endif ?>
</main>

View File

@ -5,8 +5,8 @@ namespace Aviat\AnimeClient;
$whose = $config->get('whose_list') . "'s ";
$lastSegment = $urlGenerator->lastSegment();
$extraSegment = $lastSegment === 'list' ? '/list' : '';
$hasAnime = stripos($_SERVER['REQUEST_URI'], 'anime') === 1;
$hasManga = stripos($_SERVER['REQUEST_URI'], 'manga') === 1;
$hasAnime = stripos($_SERVER['REQUEST_URI'], 'anime') !== FALSE;
$hasManga = stripos($_SERVER['REQUEST_URI'], 'manga') !== FALSE;
?>
<div id="main-nav" class="flex flex-align-end flex-wrap">
@ -79,10 +79,12 @@ $hasManga = stripos($_SERVER['REQUEST_URI'], 'manga') === 1;
<nav>
<?php if ($container->get('util')->isViewPage() && ($hasAnime || $hasManga)): ?>
<?= $helper->menu($menu_name) ?>
<?php if (stripos($_SERVER['REQUEST_URI'], 'history') === FALSE): ?>
<br />
<ul>
<li class="<?= Util::isNotSelected('list', $lastSegment) ?>"><a href="<?= $urlGenerator->url($route_path) ?>">Cover View</a></li>
<li class="<?= Util::isSelected('list', $lastSegment) ?>"><a href="<?= $urlGenerator->url("{$route_path}/list") ?>">List View</a></li>
</ul>
<?php endif ?>
<?php endif ?>
</nav>

View File

@ -24,17 +24,10 @@ use function Aviat\Ion\_dir;
setlocale(LC_CTYPE, 'en_US');
// Work around the silly timezone error
$timezone = ini_get('date.timezone');
if ($timezone === '' || $timezone === FALSE)
{
ini_set('date.timezone', 'GMT');
}
// Load composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
// if (array_key_exists('ENV', $_ENV) && $_ENV['ENV'] === 'development')
if (array_key_exists('ENV', $_SERVER) && $_SERVER['ENV'] === 'development')
{
$whoops = new Run;
$whoops->pushHandler(new PrettyPageHandler);
@ -62,6 +55,24 @@ $overrideConfig = file_exists($overrideFile)
$configArray = array_replace_recursive($baseConfig, $config, $overrideConfig);
$checkedConfig = ConfigType::check($configArray);
// Set the timezone for date display
// First look in app config, then PHP config, and at last
// resort, just set to UTC.
$timezone = ini_get('date.timezone');
if (array_key_exists('timezone', $checkedConfig) && ! empty($checkedConfig['timezone']))
{
date_default_timezone_set($checkedConfig['timezone']);
}
else if ($timezone !== '')
{
date_default_timezone_set($timezone);
}
else
{
date_default_timezone_set('UTC');
}
$container = $di($checkedConfig);
// Unset 'constants'

View File

@ -34,6 +34,7 @@ use Aviat\AnimeClient\API\Kitsu\Transformer\{
AnimeHistoryTransformer,
AnimeTransformer,
AnimeListTransformer,
MangaHistoryTransformer,
MangaTransformer,
MangaListTransformer
};
@ -184,7 +185,7 @@ final class Model {
public function getAnimeHistory(): array
{
$raw = $this->getRawHistoryList('anime');
$organized = (array)JsonAPI::organizeData($raw);
$organized = JsonAPI::organizeData($raw);
$organized = array_filter($organized, fn ($item) => array_key_exists('relationships', $item));
@ -204,11 +205,14 @@ final class Model {
public function getMangaHistory(): array
{
$raw = $this->getRawHistoryList('manga');
$organized = (array)JsonAPI::organizeData($raw);
$organized = JsonAPI::organizeData($raw);
$organized = array_filter($organized, fn ($item) => array_key_exists('relationships', $item));
return $organized;
$transformer = new MangaHistoryTransformer();
$transformer->setContainer($this->getContainer());
return $transformer->transform($organized);
}
/**
@ -989,7 +993,7 @@ final class Model {
* @throws InvalidArgumentException
* @throws Throwable
*/
protected function getRawHistoryList(string $type = 'anime', int $entries = 60): array
protected function getRawHistoryList(string $type = 'anime', int $entries = 120): array
{
$size = 20;
$pages = ceil($entries / $size);

View File

@ -19,6 +19,9 @@ namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
use Aviat\AnimeClient\Types\HistoryItem;
use Aviat\Ion\Di\ContainerAware;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
class AnimeHistoryTransformer {
use ContainerAware;
@ -49,7 +52,7 @@ class AnimeHistoryTransformer {
$kind = $entry['attributes']['kind'];
if ($kind === 'progressed')
if ($kind === 'progressed' && ! empty($entry['attributes']['changedData']['progress']))
{
$output[] = $this->transformProgress($entry);
}
@ -100,25 +103,30 @@ class AnimeHistoryTransformer {
if (count($entries) > 1)
{
$episodes = [];
$updated = [];
foreach ($entries as $e)
{
$episodes[] = max($e['original']['attributes']['changedData']['progress']);
$updated[] = $e['updated'];
}
$firstEpisode = min($episodes);
$lastEpisode = max($episodes);
$firstUpdate = min($updated);
$lastUpdate = max($updated);
$title = $entries[0]['title'];
$action = (count($entries) > 3)
? "Marathoned episodes {$firstEpisode}-{$lastEpisode} of {$title}"
: "Watched episodes {$firstEpisode}-{$lastEpisode} of {$title}";
? "Marathoned episodes {$firstEpisode}-{$lastEpisode}"
: "Watched episodes {$firstEpisode}-{$lastEpisode}";
$output[] = HistoryItem::check([
'title' => $title,
$output[] = HistoryItem::from([
'action' => $action,
'coverImg' => $entries[0]['coverImg'],
'dateRange' => [$firstUpdate, $lastUpdate],
'isAggregate' => true,
'title' => $title,
'updated' => $entries[0]['updated'],
]);
@ -126,16 +134,14 @@ class AnimeHistoryTransformer {
$i += count($entries) - 1;
continue;
}
else
{
$output[] = $entry;
}
$output[] = $entry;
}
return $output;
}
protected function transformProgress ($entry): array
protected function transformProgress ($entry): HistoryItem
{
$animeId = array_keys($entry['relationships']['anime'])[0];
$animeData = $entry['relationships']['anime'][$animeId]['attributes'];
@ -143,17 +149,17 @@ class AnimeHistoryTransformer {
$imgUrl = 'images/anime/' . $animeId . '.webp';
$episode = max($entry['attributes']['changedData']['progress']);
return HistoryItem::check([
'action' => "Watched episode {$episode} of {$title}",
return HistoryItem::from([
'action' => "Watched episode {$episode}",
'coverImg' => $imgUrl,
'kind' => 'progressed',
'original' => $entry,
'title' => $title,
'updated' => $entry['attributes']['updatedAt'],
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
]);
}
protected function transformUpdated($entry): array
protected function transformUpdated($entry): HistoryItem
{
$animeId = array_keys($entry['relationships']['anime'])[0];
$animeData = $entry['relationships']['anime'][$animeId]['attributes'];
@ -169,23 +175,23 @@ class AnimeHistoryTransformer {
if ($statusName === 'Completed')
{
return HistoryItem::check([
'action' => "Completed {$title}",
return HistoryItem::from([
'action' => 'Completed',
'coverImg' => $imgUrl,
'kind' => 'updated',
'original' => $entry,
'title' => $title,
'updated' => $entry['attributes']['updatedAt'],
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
]);
}
return HistoryItem::check([
'action' => "Set status of {$title} to {$statusName}",
return HistoryItem::from([
'action' => "Set status to {$statusName}",
'coverImg' => $imgUrl,
'kind' => 'updated',
'original' => $entry,
'title' => $title,
'updated' => $entry['attributes']['updatedAt'],
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
]);
}
@ -199,4 +205,14 @@ class AnimeHistoryTransformer {
$helper = $this->getContainer()->get('html-helper');
return $helper->a($url, $animeData['canonicalTitle'], ['id' => $animeData['slug']]);
}
protected function parseDate (string $date): DateTimeImmutable
{
$dateTime = DateTimeImmutable::createFromFormat(
DateTimeInterface::RFC3339_EXTENDED,
$date
);
return $dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
}
}

View File

@ -0,0 +1,218 @@
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
* PHP version 7.4
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2020 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 5
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Mapping\MangaReadingStatus;
use Aviat\AnimeClient\Types\HistoryItem;
use Aviat\Ion\Di\ContainerAware;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
class MangaHistoryTransformer {
use ContainerAware;
protected array $skipList = [];
/**
* Convert raw history
*
* @param array $data
* @return array
*/
public function transform(array $data): array
{
$output = [];
foreach ($data as $id => $entry)
{
if ( ! isset($entry['relationships']['manga']))
{
continue;
}
if (in_array($id, $this->skipList, FALSE))
{
continue;
}
$kind = $entry['attributes']['kind'];
if ($kind === 'progressed' && ! empty($entry['attributes']['changedData']['progress']))
{
$output[] = $this->transformProgress($entry);
}
else if ($kind === 'updated')
{
$output[] = $this->transformUpdated($entry);
}
}
return $this->aggregate($output);
}
/**
* Combine consecutive 'progressed' events
*
* @param array $singles
* @return array
*/
protected function aggregate (array $singles): array
{
$output = [];
$count = count($singles);
for ($i = 0; $i < $count; $i++)
{
$entries = [];
$entry = $singles[$i];
$prevTitle = $entry['title'];
$nextId = $i;
$next = $singles[$nextId];
while (
$next['kind'] === 'progressed' &&
$next['title'] === $prevTitle
) {
$entries[] = $next;
$prevTitle = $next['title'];
if ($nextId + 1 < $count)
{
$nextId++;
$next = $singles[$nextId];
continue;
}
break;
}
if (count($entries) > 1)
{
$chapters = [];
$updated = [];
foreach ($entries as $e)
{
$chapters[] = max($e['original']['attributes']['changedData']['progress']);
$updated[] = $e['updated'];
}
$firstChapter = min($chapters);
$lastChapter = max($chapters);
$firstUpdate = min($updated);
$lastUpdate = max($updated);
$title = $entries[0]['title'];
$action = (count($entries) > 3)
? "Marathoned chapters {$firstChapter}-{$lastChapter}"
: "Watched chapters {$firstChapter}-{$lastChapter}";
$output[] = HistoryItem::from([
'action' => $action,
'coverImg' => $entries[0]['coverImg'],
'dateRange' => [$firstUpdate, $lastUpdate],
'isAggregate' => true,
'title' => $title,
'updated' => $entries[0]['updated'],
]);
// Skip the rest of the aggregate in the main loop
$i += count($entries) - 1;
continue;
}
$output[] = $entry;
}
return $output;
}
protected function transformProgress ($entry): HistoryItem
{
$mangaId = array_keys($entry['relationships']['manga'])[0];
$mangaData = $entry['relationships']['manga'][$mangaId]['attributes'];
$title = $this->linkTitle($mangaData);
$imgUrl = 'images/manga/' . $mangaId . '.webp';
$chapter = max($entry['attributes']['changedData']['progress']);
return HistoryItem::from([
'action' => "Watched chapter {$chapter}",
'coverImg' => $imgUrl,
'kind' => 'progressed',
'original' => $entry,
'title' => $title,
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
]);
}
protected function transformUpdated($entry): HistoryItem
{
$mangaId = array_keys($entry['relationships']['manga'])[0];
$mangaData = $entry['relationships']['manga'][$mangaId]['attributes'];
$title = $this->linkTitle($mangaData);
$imgUrl = 'images/manga/' . $mangaId . '.webp';
$kind = array_key_first($entry['attributes']['changedData']);
if ($kind === 'status')
{
$status = array_pop($entry['attributes']['changedData']['status']);
$statusName = MangaReadingStatus::KITSU_TO_TITLE[$status];
if ($statusName === 'Completed')
{
return HistoryItem::from([
'action' => 'Completed',
'coverImg' => $imgUrl,
'kind' => 'updated',
'original' => $entry,
'title' => $title,
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
]);
}
return HistoryItem::from([
'action' => "Set status to {$statusName}",
'coverImg' => $imgUrl,
'kind' => 'updated',
'original' => $entry,
'title' => $title,
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
]);
}
return $entry;
}
protected function linkTitle (array $mangaData): string
{
$url = '/manga/details/' . $mangaData['slug'];
$helper = $this->getContainer()->get('html-helper');
return $helper->a($url, $mangaData['canonicalTitle'], ['id' => $mangaData['slug']]);
}
protected function parseDate (string $date): DateTimeImmutable
{
$dateTime = DateTimeImmutable::createFromFormat(
DateTimeInterface::RFC3339_EXTENDED,
$date
);
return $dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
}
}

View File

@ -54,11 +54,33 @@ final class History extends BaseController {
$this->mangaModel = $container->get('manga-model');
}
public function anime(): void
public function index(string $type = 'anime'): void
{
if (method_exists($this, $type))
{
$this->$type();
return;
}
$this->notFound(
$this->config->get('whose_list') .
"'s List &middot; History &middot; " .
'History Not Found',
'History Not Found'
);
}
private function anime(): void
{
$this->baseData = array_merge($this->baseData, [
'menu_name' => 'anime_list',
'other_type' => 'manga',
'url_type' => 'anime',
]);
// $this->outputJSON($this->animeModel->getHistory());
// return;
$this->outputHTML('history/anime', [
$this->outputHTML('history', [
'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Anime List",
'Anime',
@ -68,11 +90,17 @@ final class History extends BaseController {
]);
}
public function manga(): void
private function manga(): void
{
$this->outputJSON($this->mangaModel->getHistory());
return;
$this->outputHTML('history/manga', [
$this->baseData = array_merge($this->baseData, [
'menu_name' => 'manga_list',
'other_type' => 'anime',
'url_type' => 'manga',
]);
// $this->outputJSON($this->mangaModel->getHistory());
// return;
$this->outputHTML('history', [
'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Manga List",
'Manga',

View File

@ -16,6 +16,7 @@
namespace Aviat\AnimeClient\Model;
use function is_array;
use const Aviat\AnimeClient\SETTINGS_MAP;
use function Aviat\AnimeClient\arrayToToml;
@ -124,14 +125,14 @@ final class Settings {
public function validateSettings(array $settings): array
{
$config = (new Config($settings))->toArray();
$cfg = Config::check($settings);
$looseConfig = [];
$keyedConfig = [];
// Convert 'boolean' values to true and false
// Also order keys so they can be saved properly
foreach ($config as $key => $val)
foreach ($cfg as $key => $val)
{
if (is_scalar($val))
{
@ -148,7 +149,7 @@ final class Settings {
$looseConfig[$key] = $val;
}
}
elseif (\is_array($val) && ! empty($val))
elseif (is_array($val) && ! empty($val))
{
foreach($val as $k => $v)
{

View File

@ -52,6 +52,11 @@ class Config extends AbstractType {
*/
public $dark_theme; /* Deprecated */
/**
* @var string The PHP timezone
*/
public string $timezone = '';
/**
* Default Anime list status page, values are listed in
* Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Title

View File

@ -16,6 +16,8 @@
namespace Aviat\AnimeClient\Types;
use DateTimeImmutable;
class HistoryItem extends AbstractType {
/**
* @var string Title of the anime/manga
@ -43,9 +45,17 @@ class HistoryItem extends AbstractType {
public string $kind = '';
/**
* @var string When the item was last updated
* @var DateTimeImmutable When the item was last updated
*/
public string $updated = '';
public ?DateTimeImmutable $updated = NULL;
public $original;
/**
* @var array Range of updated times for the aggregated item
*/
public array $dateRange = [];
/**
* @var array The item before transformation
*/
public array $original = [];
}

View File

@ -147,6 +147,12 @@ const SETTINGS_MAP = [
'default' => 'Somebody',
'description' => 'Name of the owner of the list data.',
],
'timezone' => [
'type' => 'string',
'title' => 'Timezone',
'default' => 'America/Detroit',
'description' => 'See https://www.php.net/manual/en/timezones.php for options'
],
'theme' => [
'type' => 'select',
'title' => 'Theme',