Skip to content

Use novel refactor#27

Closed
CD-Z wants to merge 40 commits into
masterfrom
useNovel-Refactor
Closed

Use novel refactor#27
CD-Z wants to merge 40 commits into
masterfrom
useNovel-Refactor

Conversation

@CD-Z
Copy link
Copy Markdown
Owner

@CD-Z CD-Z commented Mar 25, 2026

Summary by CodeRabbit

  • New Features

    • Persistent chapter-text cache, batch chapter loading with automatic deduplication, and a new persisted novel store for faster, more reliable reading/navigation
  • Bug Fixes

    • More reliable chapter operations and incremental novel statistics; safer recovery from corrupted persisted data
  • Documentation

    • Added reader settings string for volume-button scroll offset
  • Chores

    • Updated dependencies and optimized database/query flows
  • Tests

    • Expanded test coverage across persistence, bootstrapping, and UI flows

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 25, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4bae9f99-4fcb-4a56-89ac-c3d752239a38

📥 Commits

Reviewing files that changed from the base of the PR and between 035bbd0 and 89be147.

📒 Files selected for processing (2)
  • src/database/queries/NovelQueries.ts
  • tsconfig.tsbuildinfo

📝 Walkthrough

Walkthrough

Replaces the hook-based novel layer with a Zustand-backed persistent store, introduces persistence/key contracts and bootstrap services, updates consumers to selector/action hooks, adds MMKV/Zustand adapters, synchronous DB helpers, and extensive tests and mocks. Also updates package dependencies.

Zustand-backed Novel Store & Persistence

Layer / File(s) Summary
Data Shape / Types
src/hooks/persisted/useNovel/types.ts, src/hooks/persisted/useNovel/store/novelStore.types.ts
Introduces persisted prefixes, defaults, BatchInfo, and comprehensive NovelStore/Chapter slice TypeScript types and interfaces.
Persistence Contracts & Bridge
src/hooks/persisted/useNovel/store-helper/keyContract.ts, src/hooks/persisted/useNovel/store-helper/persistence.ts, src/hooks/persisted/useNovel/store-helper/contracts.ts
Adds key-building contract, MMKV-backed novelPersistence bridge with read/write/copy semantics, and a contracts barrel re-exporting persistence APIs/constants.
Storage Adapter
src/utils/mmkv/zustand-adapter.ts
Adds mmkvZustandAdapter implementing getItem/setItem/removeItem for Zustand persist middleware.
Store Core / Factory
src/hooks/persisted/useNovel/store/createStore.ts, src/hooks/persisted/useNovel/store/novelStore.ts, src/hooks/persisted/useNovel/store/novelStore.chapterState.ts
Adds createStore factory and createNovelSlice with initial chapter slice, wiring persisted settings/pageIndex/lastRead into initial state.
Action Factories
src/hooks/persisted/useNovel/store/novelStore.actions.ts, src/hooks/persisted/useNovel/store/novelStore.chapterActions.ts, src/hooks/persisted/useNovel/store/chapterActions.ts
Provides novel bootstrapping, chapter batch loading, mutation actions (bookmark/read/progress/delete), persistence hooks, and dependency-injected DB/UX side-effect wiring.
Bootstrap Service
src/hooks/persisted/useNovel/store-helper/bootstrapService.ts
New bootstrap service with async/sync flows, batch loading, deduplication of inflight bootstraps, and helpers for next-batch / load-up-to-target.
Public Hook Surface / Deprecation
src/hooks/persisted/useNovel.ts, src/hooks/persisted/index.ts
Replaces previous useNovel implementation with a throwing stub; re-exports persistence constants via contracts; updates deleteCachedNovels to use novelPersistence and await DB cleanup.
Context → Selector Layer
src/screens/novel/NovelContext.tsx, src/hooks/persisted/useNovel/useChapterOperations.ts, src/hooks/persisted/useNovelSettings.ts
Reworks provider into NovelStoreContext/NovelLayoutContext; adds selector hooks (useNovelStore, useNovelState, useNovelValue, useNovelActions, useNovelAction, useNovelLayout) and a chapter-operations hook; settings hook now uses store selectors/actions.
Consumers Updated
src/screens/novel/**/*, src/screens/reader/**/*, src/screens/settings/**/*
Updates UI components and reader hooks to consume useNovelValue/useNovelActions/useNovelLayout instead of legacy context; adapts props and callback wiring (removed batch props where store provides them).
Mocks & Tests
src/hooks/__tests__/mocks.ts, src/hooks/persisted/__mocks__/*, src/hooks/persisted/useNovel/__tests__/*, src/screens/**/__tests__/*, src/screens/reader/**/__tests__/*
Adds comprehensive mock store factories, extends Jest mocks, and introduces many test suites covering contracts, persistence, bootstrap, store actions, component behavior, and hook unit tests.

Database & Query Changes

Layer / File(s) Summary
DB Manager API & Sync Helpers
src/database/manager/manager.ts, src/database/manager/manager.d.ts
Adds allSync/getSync synchronous query variants, batch implementation, __resetDbManagerForTests, and useLiveQuery callback support.
Query Shape & Sync Variants
src/database/queries/ChapterQueries.ts, src/database/queries/NovelQueries.ts, src/database/queries/__tests__/*
insertChapters accepts options; adds sync variants (getNovelChaptersSync, getChapterCountSync, getFirstUnreadChapter sync); getNovelByPath/getNovelById return DB-backed shapes; tests updated/added.
Query Utilities & Triggers
src/database/utils/filter.ts, src/database/utils/parser.ts, src/database/queryStrings/triggers.ts
Adds FilterObject and toFilterObject, makes chapter order optional, and converts trigger SQL to incremental updates.
Test DB Backend & Test Helpers
src/database/__tests__/db.test.ts, src/database/queries/__tests__/testDb.ts, src/database/queries/__tests__/testDbManager.ts (removed)
Switches tests from better-sqlite3 to @op-engineering/op-sqlite, removes custom test DB manager, and updates Drizzle initialization and test helpers.

Misc / Packaging

Layer / File(s) Summary
Dependencies
package.json
Adds zustand (^5.0.12) and bumps better-sqlite3 devDependency to ^12.9.0.
MMKV-based cleanup & migration
src/services/migrate/migrateNovel.ts, src/services/updates/LibraryUpdateQueries.ts
Migration now uses novelPersistence bridge; Library update batching simplified and new scheduling flow for downloads.
Localization / Small Fixes
strings/languages/en/strings.json, strings/types/index.ts, various minor files
Adds readerSettings.volumeButtonOffset localization key and small logic/type adjustments across UI and hooks.

Sequence Diagram(s)

sequenceDiagram
    participant UI as UI Component
    participant Selector as Selector Hook
    participant Store as Zustand Store
    participant Persist as novelPersistence (MMKV)
    participant Bootstrap as Bootstrap Service
    participant DB as Database

    UI->>Selector: read state (chapters/novel/etc.)
    Selector->>Store: selector query
    Store-->>Selector: state slice
    Selector-->>UI: returns state

    UI->>Selector: dispatch action (e.g., bootstrapNovel / getNextChapterBatch)
    Selector->>Store: invoke action
    Store->>Persist: read/write pageIndex/settings/lastRead
    Store->>Bootstrap: request chapters/novel data
    Bootstrap->>DB: query novel/chapters (sync or async)
    DB-->>Bootstrap: data
    Bootstrap-->>Store: update chapters/pages/batchInformation
    Store-->>UI: state updated

    UI->>Selector: request chapter text
    Selector->>Store: chapterTextCache.read(id)
    alt cache hit
        Store-->>UI: return cached text
    else cache miss
        Store->>DB: (or fetch) obtain chapter text
        Store->>Persist: optional persist lastRead/progress
        Store-->>UI: text available
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • CD-Z/lnreader#20: Modifies persisted novel/settings export surface and overlaps with this PR's re-export and persistence refactoring.

Poem

🐰 I hopped through hooks to build a store,
MMKV burrows keep pages and more,
Bootstrap batched chapters, caches held tight,
Selectors fetch gently, UI sleeps at night,
A rabbit's small cheer for state done right.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch useNovel-Refactor
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch useNovel-Refactor

coderabbitai[bot]

This comment was marked as outdated.

coderabbitai[bot]

This comment was marked as outdated.

@CD-Z CD-Z force-pushed the master branch 2 times, most recently from 2547835 to 06f0f66 Compare April 6, 2026 12:01
@CD-Z CD-Z force-pushed the useNovel-Refactor branch from 29de6d4 to db25722 Compare April 10, 2026 20:47
coderabbitai[bot]

This comment was marked as outdated.

coderabbitai[bot]

This comment was marked as outdated.

coderabbitai[bot]

This comment was marked as outdated.

@CD-Z

This comment was marked as resolved.

@CD-Z

This comment was marked as resolved.

@CD-Z
Copy link
Copy Markdown
Owner Author

CD-Z commented Apr 17, 2026

I want to change the pagination ui, but this is for another pr

@Palloxin

This comment was marked as resolved.

@Palloxin
Copy link
Copy Markdown

..19
(not this pr fault)

since you are dealing with novelscreen
lnreader#1229

@Palloxin
Copy link
Copy Markdown

..20
(not this pr fault)

Maybe this feature too.
lnreader#1171

@Palloxin
Copy link
Copy Markdown

..21

(dunno if this pr fault)

start-reading isnt fixed to the very first chap but the first chap of the single page.

YouCut_20260419_062501713.mp4

@Palloxin

This comment was marked as resolved.

@Palloxin
Copy link
Copy Markdown

Palloxin commented Apr 19, 2026

..23

Flawed pagination implementation ui (i know you said u will remake the pag ui, this is a suggestion for the remaking)

It is counter-intuitive to put the 1st chapter list in the Last page. If tge users sorts descending, the displayed paging should simply invert:

Current behaviour

Ascending:
•••Page-1
-chap 1
-chap 2
-chap 3
-chap 4
-chap 5
•••Page-2
-chap 6
-chap 7
-chap 8
-chap 9
-chap 10

DESCENDING:
•••Page-1
-chap 470
-chap 469
-chap 468
-chap 467
-chap 466
•••Page-2
-chap 465
-chap 464
-chap 463
-chap 462
-chap 461

Expected behaviour

DESCENDING:
•••Page-94 (1st page from left)
-chap 470
-chap 469
-chap 468
-chap 467
-chap 466
•••Page-93 (2nd page from left)
-chap 465
-chap 464
-chap 463
-chap 462
-chap 461

@CD-Z
Copy link
Copy Markdown
Owner Author

CD-Z commented Apr 19, 2026

why was this marked as offtopic #27 (comment) If i click the sort button the scroll position must stay fixed, not move at the bottom.

This is some issue with legendlist. It doesn't happen with more chapters. Since it isn't an easy fix I don't want to include it here.

@CD-Z

This comment was marked as resolved.

@CD-Z
Copy link
Copy Markdown
Owner Author

CD-Z commented Apr 19, 2026

Did you build the new version yourself, if not then you didn't update. As this isn't the master branch I need to trigger builds manually and I didn't after I pushed. I did now

@Palloxin
Copy link
Copy Markdown

This is some issue with legendlist. It doesn't happen with more chapters. Since it isn't an easy fix I don't want to include it here.

Could you report it on their repo? The dev is very responsive.

@Palloxin

This comment was marked as resolved.

@CD-Z
Copy link
Copy Markdown
Owner Author

CD-Z commented Apr 19, 2026

This is some issue with legendlist. It doesn't happen with more chapters. Since it isn't an easy fix I don't want to include it here.

Could you report it on their repo? The dev is very responsive.

Not yet, as I first need to check if it really is the problem and then I need to check if it is already solved in the beta version.

coderabbitai[bot]

This comment was marked as resolved.

@CD-Z CD-Z closed this May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants