diff --git a/src/Controller/Kobo/KoboStateController.php b/src/Controller/Kobo/KoboStateController.php index 95f11d3f..83f743f7 100644 --- a/src/Controller/Kobo/KoboStateController.php +++ b/src/Controller/Kobo/KoboStateController.php @@ -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; diff --git a/src/Service/BookProgressionService.php b/src/Service/BookProgressionService.php index 18da61b2..124390ec 100644 --- a/src/Service/BookProgressionService.php +++ b/src/Service/BookProgressionService.php @@ -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; } diff --git a/tests/Service/BookProgressionServiceTest.php b/tests/Service/BookProgressionServiceTest.php index 718b9d2d..b07ce6f2 100644 --- a/tests/Service/BookProgressionServiceTest.php +++ b/tests/Service/BookProgressionServiceTest.php @@ -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; @@ -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 {