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
| Component | File | Role |
|---|---|---|
| Application | ScanlyApplication.kt | @HiltAndroidApp; custom WorkManager + HiltWorkerFactory |
| Activity | MainActivity.kt | Onboarding gate, theme, NavHost, update dialog, widget/shortcut launch actions, Open in Scanly PDF intents (IncomingPdf) |
| Theme | ui/theme/ | ScanlyTheme, colors, typography |
| Navigation | navigation/ScanlyNavHost.kt | Route registration, bottom nav / rail |
MainActivity hosts four top-level ViewModels:
AppSettingsViewModel— observes theme mode and pure black preferenceOnboardingViewModel— first-run gateAppUpdateViewModel— update checks and dialog stateLaunchActionViewModel— 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/:
| Package | Responsibility |
|---|---|
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
- Offline-first — core flows need no network.
- Non-destructive captures —
raw/files are immutable. - Derived processing — processed images and thumbnails regenerate on edit.
- Manual fallback — AI Detect, crop, rotate, filters, and fine adjustments always available;
NEEDS_REVIEWstate for failures. - Testable boundaries — use cases encapsulate business rules; repositories encapsulate I/O.
Layer rules
| Layer | May call | Must not call |
|---|---|---|
feature/ | domain/usecase/, core/ui/ | Room DAOs, DataStore, filesystem |
domain/ | Repository interfaces | Android framework, Room, Compose |
data/ | Room, files, network, core/ | Compose, ViewModels |
core/ | Other core/ utilities | Feature screens, ViewModels |
Dependency injection
Hilt modules in di/ install into SingletonComponent:
| Module | Provides / binds |
|---|---|
DatabaseModule | ScanlyDatabase, DAOs, migrations |
DocumentDataModule | Document, page, group repos; storage manager |
ExportModule | DocumentExportRepository, ExportStorageRepository |
PageTextModule | PageTextRepository, SearchHistoryRepository |
SignatureModule | SignatureRepository, PageMarkupRepository |
RatingModule | Rating-prompt repository |
PipModule | PipRepository → DefaultPipRepository (DataStore pip_* keys) |
SettingsModule | SettingsRepository |
AppDataModule | AppDataRepository |
ProcessingModule | PageImageProcessor |
PdfToolkitModule | PDF toolkit repository |
MlModule | DocumentCornerDetector → LiteRtDocumentCornerDetector; DocumentGateDetector → LiteRtDocumentGateDetector |
AppUpdateModule | Shared update notes, Play coordinator, and prompt storage |
DistributionAppUpdateModule | Build-type-specific AppUpdateRepository binding |
CoroutineModule | ScanlyDispatchers |
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
Page text search
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
| Permission | Purpose |
|---|---|
CAMERA | Document capture |
INTERNET | Google Play update check, GitHub release notes |
Camera hardware is optional (android:required="false"). No REQUEST_INSTALL_PACKAGES.
FileProvider: ${applicationId}.fileprovider for export/share URIs.
Related docs
- navigation.md — routes and user flows
- ocr-engines.md — page text engines, fallback ladder and crash guard
- screens.md — screen and ViewModel table
- ../data/database.md — Room schema
- ../processing/ — capture and image processing