Skip to content

Commit

Permalink
fix(Kobo): Mark as unread remove readpages from interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusa87 committed Aug 12, 2024
1 parent aeadabd commit 7d45a1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controller/Kobo/KoboStateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function state(KoboDevice $kobo, string $uuid, Request $request): Respons
$this->bookProgressionService->setProgression($book, $kobo->getUser(), 1.0);
break;
case ReadingStateStatusInfo::STATUS_READY_TO_READ:
$this->bookProgressionService->setProgression($book, $kobo->getUser(), 0.0);
$this->bookProgressionService->setProgression($book, $kobo->getUser(), null);
break;
case ReadingStateStatusInfo::STATUS_READING:
$progress = $state->currentBookmark?->progressPercent;
Expand Down
6 changes: 6 additions & 0 deletions src/Service/BookProgressionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function getProgression(Book $book, User $user): ?float
public function setProgression(Book $book, User $user, ?float $progress): self
{
if ($progress === null) {
$interaction = $book->getLastInteraction($user);
if ($interaction instanceof BookInteraction) {
$interaction->setReadPages(null);
$interaction->setFinished(false);
}

return $this;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Service/BookProgressionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\DataFixtures\BookFixture;
use App\DataFixtures\UserFixture;
use App\Entity\Book;
use App\Entity\BookInteraction;
use App\Entity\User;
use App\Repository\BookRepository;
use App\Repository\UserRepository;
Expand Down Expand Up @@ -80,6 +81,24 @@ public function testProgressReading(): void{
self::assertSame(15, $lastInteraction->getReadPages(), 'Book should have half page read');
}

public function testMarkAsUnread(): void{
$service = $this->getProgressionService();
$book = $this->getBook();
// Make sure we have 0 interactions
$interaction = new BookInteraction();
$interaction->setBook($book);
$interaction->setUser($this->getUser());
$interaction->setReadPages(12);

$book->getBookInteractions()->add($interaction);
$service->setProgression($book, $this->getUser(), null);
$lastInteraction = $book->getLastInteraction($this->getUser());
self::assertNotNull($lastInteraction, 'Interaction should be created');

self::assertFalse($lastInteraction->isFinished(), 'Book should not be finished');
self::assertNull($lastInteraction->getReadPages(), 'Book should have null page read');
}


private function getBook(): Book
{
Expand Down

0 comments on commit 7d45a1f

Please sign in to comment.