Architecture Overview

How Scanly is structured at v1.0.22. For navigation detail see navigation.md. For screen inventory see screens.md.

Layer diagram

┌─────────────────────────────────────────────────────────┐
│  feature/          Compose screens + ViewModels         │
├─────────────────────────────────────────────────────────┤
│  domain/           Use cases + repository interfaces    │
│                    + domain models                      │
├─────────────────────────────────────────────────────────┤
│  data/             Room, files, export, settings,     │
│                    update implementations               │
├─────────────────────────────────────────────────────────┤
│  core/             ML, OpenCV, editing math, UI utils  │
└─────────────────────────────────────────────────────────┘
         ▲                              ▲
         │         di/ (Hilt)           │
         └──────────────────────────────┘

Call direction: feature → domain → data → core. Never skip layers.

Application shell

ComponentFileRole
ApplicationScanlyApplication.kt@HiltAndroidApp; custom WorkManager + HiltWorkerFactory
ActivityMainActivity.ktOnboarding gate, theme, NavHost, update dialog, widget/shortcut launch actions, Open in Scanly PDF intents (IncomingPdf)
Themeui/theme/ScanlyTheme, colors, typography
Navigationnavigation/ScanlyNavHost.ktRoute registration, bottom nav / rail

MainActivity hosts four top-level ViewModels:

  • AppSettingsViewModel — observes theme mode and pure black preference
  • OnboardingViewModel — first-run gate
  • AppUpdateViewModel — update checks and dialog state
  • LaunchActionViewModel — widget/shortcut actions (scan, import, QR, library)

AppLocaleProvider supplies the saved language’s context, resources, configuration and layout direction to the existing composition. Welcome applies a selection immediately. SettingsLanguageRoute keeps the selection in screen state and calls AppLocales.setLanguage when the user taps Apply. Applying a language preserves the activity, window, navigation and scroll state. AppLocales uses the platform per-app locale on Android 13+ and a private preference on Android 10–12. MainActivity handles locale|layoutDirection|screenLayout changes without recreation. Application configuration callbacks refresh Compose resources, the non-Compose AppStrings cache and widget bindings.

Package layout

All code under app/src/main/java/in/c1ph3rj/scanly/:

PackageResponsibility
ui/theme/Material 3 theming
navigation/Destinations and NavHost
feature/Screen UI and ViewModels
feature/library/Library and combined name/page-text search
feature/text/Extracted page-text reading and correction
feature/sign/PDF and scan signing, signature creation and management
feature/pip/Pip mascot: art, speech bubble, lines, PipViewModel, LocalPipPreferences
feature/tools/pdf/PDF reader and tools; IncomingPdf (Open in Scanly), PdfReaderMenu (hand-off to tools)
feature/launch/Widget/shortcut launch action parsing and redirects
feature/widget/App widget providers (actions, scan, QR)
domain/model/Business models
domain/repository/Repository contracts
domain/usecase/Business operations
domain/processing/PageImageProcessor interface
data/local/db/Room database, entities, DAOs
data/document/Document repository
data/page/Page repository and capture finalize
data/group/Group repository
data/export/PDF/ZIP export
data/text/On-device OCR, page-text persistence/search, background indexing
data/sign/Saved signatures, editable page markups, image compositing
data/storage/App-private file manager
data/settings/DataStore and bundled assets
data/update/Build-selected GitHub or Google Play update checks
data/processing/PageImageProcessor implementation
core/ml/LiteRT corner models, document gate, book/quad policies, auto model selection
core/processing/Perspective warp, OpenCV filter engine (filter/), filter adjustments
core/editing/Crop quad editor and signature ink stroke processing
core/ui/Thumbnail cache, adaptive layout helpers
core/common/Result types, formatters
di/Hilt modules

Design principles

  1. Offline-first — core flows need no network.
  2. Non-destructive captures — raw/ files are immutable.
  3. Derived processing — processed images and thumbnails regenerate on edit.
  4. Manual fallback — AI Detect, crop, rotate, filters, and fine adjustments always available; NEEDS_REVIEW state for failures.
  5. Testable boundaries — use cases encapsulate business rules; repositories encapsulate I/O.

Layer rules

LayerMay callMust not call
feature/domain/usecase/, core/ui/Room DAOs, DataStore, filesystem
domain/Repository interfacesAndroid framework, Room, Compose
data/Room, files, network, core/Compose, ViewModels
core/Other core/ utilitiesFeature screens, ViewModels

Dependency injection

Hilt modules in di/ install into SingletonComponent:

ModuleProvides / binds
DatabaseModuleScanlyDatabase, DAOs, migrations
DocumentDataModuleDocument, page, group repos; storage manager
ExportModuleDocumentExportRepository, ExportStorageRepository
PageTextModulePageTextRepository, SearchHistoryRepository
SignatureModuleSignatureRepository, PageMarkupRepository
RatingModuleRating-prompt repository
PipModulePipRepository → DefaultPipRepository (DataStore pip_* keys)
SettingsModuleSettingsRepository
AppDataModuleAppDataRepository
ProcessingModulePageImageProcessor
PdfToolkitModulePDF toolkit repository
MlModuleDocumentCornerDetector → LiteRtDocumentCornerDetector; DocumentGateDetector → LiteRtDocumentGateDetector
AppUpdateModuleShared update notes, Play coordinator, and prompt storage
DistributionAppUpdateModuleBuild-type-specific AppUpdateRepository binding
CoroutineModuleScanlyDispatchers

Connection maps

Camera capture

ScanSessionViewModel
  → live path: DocumentGateDetector → DocumentCornerDetector(live model)
    → DocumentQuadPolicy / StableCornerSelector → overlay + auto-capture
  → PreparePageCaptureUseCase → PageRepository.prepareCapture
  → CameraX writes raw JPEG
  → FinalizeCapturedPageUseCase → PageRepository.finalizeCapture
    → PageImageProcessor → gate + corner (post model) + warp + OpenCV filter (+ adjustments if any)
    → Room + file storage

Page edit

PageEditorViewModel
  ├─ Filters / Adjust overlays (same ViewModel drafts)
  └─ UpdatePageEditsUseCase → PageRepository.updatePageEdits
        → PageImageProcessor.reprocessPage (crop + rotation + filter + adjustments)
        → Room update + thumbnail invalidation

PageCropViewModel
  ├─ DetectDocumentCornersUseCase → PageImageProcessor.detectDocumentCorners
  └─ UpdatePageEditsUseCase (apply crop + rotation; keep filter/adjustments)

Export

DocumentDetailViewModel / GroupDetailViewModel
  → Export*UseCase → DocumentExportRepository
    → Room (page paths) + PdfDocument (+ PdfBox-Android encryption when requested) → cache/exports
  → SaveExportArtifactUseCase → configured Downloads/SAF destination
LibrarySearchViewModel
  → PageTextRepository → Room page_texts + FTS4 index
    → TextIndexScheduler → WorkManager TextIndexWorker → SelectablePageTextRecognizer (ML Kit, or experimental Indra OCR with ML Kit fallback)

Document signing

PDF signing: PdfSignViewModel → open/sign session use cases → PdfToolkitRepository → new signed PDF in export cache
Scan signing: DocumentSignViewModel → PageMarkupRepository → page_markups + composed processed image
Reusable signatures: SignatureRepository → Room signatures + files/signatures/{id}/ink.png

Clear all data

SettingsViewModel
  → ClearAllAppDataUseCase → AppDataRepository
    → Room clear + files wipe + cache wipe + ThumbnailCache.clearAll

Permissions

PermissionPurpose
CAMERADocument capture
INTERNETGoogle Play update check, GitHub release notes

Camera hardware is optional (android:required="false"). No REQUEST_INSTALL_PACKAGES.

FileProvider: ${applicationId}.fileprovider for export/share URIs.

View source on GitHub · Markdown