KPdfLoadState is the observable load lifecycle for a single KPDF viewer instance. Collect it from KPdfViewerState.loadState (a StateFlow) to drive loading indicators, show page counts, surface error messages, and gate actions that require a fully decoded document. The state machine progresses linearly from Idle → Loading → Ready on success, or Idle → Loading → Error on failure. Transitioning the viewer to a new source resets the state back to Idle.
Variants
data object
No document is loaded and no load is in progress. This is the initial state of every viewer instance before
open() is called, and the state the viewer returns to after close().data object
The SDK is actively fetching (for URL sources) or decoding the PDF bytes. Long-running network requests remain in
Loading for their entire duration. Display a progress indicator while in this state.data class
data class
Document loading failed. Inspect
reason to determine the failure type and decide whether to surface a retry action or a specific error message.KPdfError
required
A sealed
KPdfError value describing the failure. See KPdfError variants below.KPdfError variants
KPdfError is a sealed interface with a message: String property on every variant. The following concrete types are available:
Collecting load state in Compose
CallcollectAsState() on KPdfViewerState.loadState inside your composable, then branch on each variant with a when expression.
pageCount is only available on the Ready variant. Guard any page-count logic with a smart cast or (loadState as? KPdfLoadState.Ready)?.pageCount to avoid a null or default value while the document is still loading.
Notes
KPdfViewerState.loadStateis aStateFlow<KPdfLoadState>, so it always has a current value and never suspends on the first collection.- Calling
viewerState.open(newSource)while inReadyorErrorresetsloadStatetoIdlebefore beginning the next load cycle. - If you only need
pageCountfor toolbar display,KPdfViewerToolbarreads it internally — you do not need to observeloadStateyourself in that case.