DEV Community

Cover image for I Decomposed a 3,000-Line God Screen Into 11 Modular Composables — Here's How
DEVANSHU PATIL
DEVANSHU PATIL

Posted on

I Decomposed a 3,000-Line God Screen Into 11 Modular Composables — Here's How

I Decomposed a 3,000-Line God Screen Into 11 Modular Composables — Here's How

In modern Android development with Jetpack Compose, UI code is declarative, concise, and elegant.

Until it isn't.

If you don't enforce strict architectural boundaries, Jetpack Compose screens have a notorious tendency to metastasize into God Objects.

Nowhere is this temptation stronger than the Settings Screen.

Whenever a new feature is built, it needs a toggle, a dialog, or a configuration slider:

  • Added biometric lock? Add a toggle to Settings.
  • Added SMS transaction parsing? Add bank selection chips to Settings.
  • Added CSV export and Google Drive backup? Add file pickers to Settings.
  • Added secret vault geofencing? Add a map radius slider to Settings.

Before we knew it, FinLedger's SettingsScreen.kt had bloated into a staggering 3,140-line monolithic file.

During Hardening Sprint H8, we performed major surgery on this screen, decomposing it into 11 modular, single-responsibility units.

Here is why Compose god screens destroy app performance, and how we refactored the screen into a clean, reactive architecture.

The Architectural Costs of a 3,000-Line Composable

A 3,000-line composable file is not just an aesthetic eyesore; it introduces severe technical liabilities:

flowchart TD
    subgraph GodScreenNightmare["The Recomposition Cascade in a God Screen"]
        User["User Toggles Dark Theme Switch"] --> State["SettingsUiState Emits Update"]

        State --> MonoScreen["SettingsScreen (3,140 Lines of Compose):
        Observes monolithic SettingsUiState holding 40+ properties!"]

        MonoScreen --> RecompCascade["Unnecessary Recomposition Cascades:
        - Recomposes SMS Bank Checkboxes
        - Recomposes Biometric Auth State
        - Recomposes Backup Progress Indicators
        - Recomposes Geofence Map Sliders"]

        RecompCascade --> FrameJank["Severe Main-Thread Frame Drops:
        Switch toggle animation stutters!"]
    end

1. Recomposition Cascades and UI Jank

When a single screen composable observes a massive SettingsUiState data class containing 40 different properties, any state change—such as toggling dark mode or updating a reminder time—forces Compose's runtime to evaluate dozens of unrelated child composables. This causes dropped frames and micro-stutter during animations.

2. Constructor Bloat in ViewModels

To support all 40 settings, SettingsViewModel required 14 different repository and manager dependencies injected into its constructor: SecurityManager, BackupRestoreManager, SmsInboxScanner, VaultRepository, PreferencesRepository, and more. Testing such a ViewModel required writing hundreds of lines of mock boilerplate.

3. The Android Studio Preview Breakdown

An oversized Compose file completely breaks Android Studio's @Preview tooling. Rendering a preview for a small toggle button required the IDE to compile 3,000 lines of complex UI, resulting in frequent IDE crashes and ten-second preview refresh latencies.

The 11-Unit Modular Architecture

In Sprint H8, we broke the monolithic screen into 7 domain setting cards and 4 isolated dialog modules:

flowchart TD
    subgraph DecomposedArchitecture["The 11-Unit Modular Architecture"]
        direction TB
        SettingsScreen["SettingsScreen.kt (Scaffold & TopBar: < 120 Lines)"]

        SettingsScreen --> C1["1. SecuritySettingsCard.kt:
        PIN, Biometrics, Dynamic FLAG_SECURE"]
        SettingsScreen --> C2["2. DataManagementCard.kt:
        Backup Vaults, CSV Export, Restore Checkpoints"]
        SettingsScreen --> C3["3. SmsAutomationCard.kt:
        Sender Header Filters, Historical Inbox Scanner"]
        SettingsScreen --> C4["4. PreferencesCard.kt:
        Currency Symbol, Theme, Number Formatting"]
        SettingsScreen --> C5["5. VaultSettingsCard.kt:
        Auto-Lock Inactivity Timer, Geofence Radius"]
        SettingsScreen --> C6["6. NotificationSettingsCard.kt:
        Daily Review Reminders, Budget Alerts"]
        SettingsScreen --> C7["7. AboutCard.kt:
        App Version, Build Number, Open Source Licenses"]

        C1 -.-> D1["Dialog 1: PinSetupDialog.kt"]
        C2 -.-> D2["Dialog 2: BackupRestoreDialog.kt"]
        C5 -.-> D3["Dialog 3: GeofenceRadiusDialog.kt"]
        C2 -.-> D4["Dialog 4: ResetWipeDialog.kt"]
    end

Let's examine the rules that govern this new architecture:

Rule 1: Granular, Immutable State Sub-Models

Instead of a single god-state class, SettingsUiState was refactored into focused sub-interfaces:

  • SecurityUiState (only PIN, biometric, and screen lock properties)
  • BackupUiState (only export paths, last backup date, and progress)
  • SmsAutomationUiState (only permission statuses and scanned counts)

When the user toggles dark mode, only PreferencesCard recomposes. The biometric cards, SMS filters, and backup buttons are skipped completely by the Compose compiler (Skipped Recomposition).

Rule 2: Event Hoisting to Scoped Lambdas

Setting cards do not talk directly to ViewModels.

Each modular card accepts only plain Kotlin data classes and emits plain lambdas:

  • SecuritySettingsCard(state = securityState, onToggleBiometrics = { ... }, onOpenPinDialog = { ... }) This allows each card to have its own instantaneous @Preview in Android Studio with zero mocks or Hilt dependencies required.

Rule 3: Isolated Ephemeral Dialogs

Dialogs are notoriously heavy in Compose. If a dialog composable is defined inside the parent screen body, its internal state lingers in memory even when closed.

We extracted dialogs into dedicated standalone files (PinSetupDialog.kt, BackupRestoreDialog.kt). The dialog composables are only composed into the composition tree when their explicit boolean flags evaluate to true, and they are completely unmounted and garbage-collected the moment they are dismissed.

Real-World Performance Improvements

Measuring the screen before and after Sprint H8 on an entry-level physical Android test device showed dramatic gains:

Dimension Monolithic Settings (Pre-Audit) Modular Architecture (Post-H8)
File Line Count 3,140 lines (1 file) < 280 lines per file (11 files)
Compose Recomposition Time 18.4 ms (Dropped frames) < 1.8 ms (Zero frame drops)
Android Studio Preview Time 9.2 seconds / Crashes 0.6 seconds (Instant)
ViewModel Injected Dependencies 14 God-Dependencies Decoupled into 3 domain use cases

Key Takeaway

In Jetpack Compose, modularity is not just an organization preference; it is a performance necessity.

Decomposing a 3,000-line god composable into isolated domain cards and ephemeral dialogs restores 60 FPS fluidity, enables instant IDE previews, and ensures that adding new configuration options never drags down the responsiveness of your app.


FinLedger is a privacy-first, offline-first personal finance application for Android that models bank accounts, credit cards, and cash without double-counting.

Explore FinLedger on Google Play: https://play.google.com/store/apps/details?id=com.devanshufinance.app

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

Rule 2 is the one I would steal first — cards that take plain data classes and emit lambdas, with previews that need zero mocks or Hilt. That is the part that actually changes how fast you iterate. One question on the numbers: the 18.4ms to 1.8ms recomposition drop — was that measured on a dark-mode toggle specifically, or across a few different state changes? I am curious how much of the win came from the granular state split versus the dialogs being unmounted, since those are two very different fixes.