From d2fa54725779fdbebef1fc9030688a7b22a129c0 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Tue, 19 Dec 2023 17:16:57 +0100 Subject: [PATCH] PhotoMosaic: keep queue ordered by date captured --- models/AssetIterator.php | 19 ++++++++++++++++--- models/PhotoMosaic.php | 11 +++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/models/AssetIterator.php b/models/AssetIterator.php index 3a50cf5..94132aa 100644 --- a/models/AssetIterator.php +++ b/models/AssetIterator.php @@ -8,15 +8,18 @@ class AssetIterator extends Asset { + private Database $db; + private $direction; + private $return_format; private $res_assets; private $res_meta; private $res_thumbs; - private Database $db; - protected function __construct($res_assets, $res_meta, $res_thumbs, $return_format) + protected function __construct($res_assets, $res_meta, $res_thumbs, $return_format, $direction) { $this->db = Registry::get('db'); + $this->direction = $direction; $this->res_assets = $res_assets; $this->res_meta = $res_meta; $this->res_thumbs = $res_thumbs; @@ -174,7 +177,7 @@ class AssetIterator extends Asset '_' => '_', ]); - $iterator = new self($res_assets, $res_meta, $res_thumbs, $return_format); + $iterator = new self($res_assets, $res_meta, $res_thumbs, $return_format, $params['direction']); // Returning total count, too? if ($return_count) @@ -190,4 +193,14 @@ class AssetIterator extends Asset else return $iterator; } + + public function isAscending() + { + return $this->direction === 'asc'; + } + + public function isDescending() + { + return $this->direction === 'desc'; + } } diff --git a/models/PhotoMosaic.php b/models/PhotoMosaic.php index 68089ba..949c52c 100644 --- a/models/PhotoMosaic.php +++ b/models/PhotoMosaic.php @@ -8,6 +8,7 @@ class PhotoMosaic { + private $descending; private AssetIterator $iterator; private $layouts; private $processedImages = 0; @@ -19,6 +20,7 @@ class PhotoMosaic { $this->iterator = $iterator; $this->layouts = $this->availableLayouts(); + $this->descending = $iterator->isDescending(); } public function __destruct() @@ -143,8 +145,17 @@ class PhotoMosaic return -$priority_diff; } + private function orderQueueByDate() + { + usort($this->queue, function($a, $b) { + $score = $a->getDateCaptured() <=> $b->getDateCaptured(); + return $score * ($this->descending ? -1 : 1); + }); + } + private function pushToQueue(Image $image) { $this->queue[] = $image; + $this->orderQueueByDate(); } }