<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shiva Thapa</title>
    <description>The latest articles on DEV Community by Shiva Thapa (@shivathapaa).</description>
    <link>https://dev.to/shivathapaa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1463198%2F757c0615-69d8-40e0-97da-0717158d6a5d.png</url>
      <title>DEV Community: Shiva Thapa</title>
      <link>https://dev.to/shivathapaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivathapaa"/>
    <language>en</language>
    <item>
      <title>One Codebase, Six Apps on the Play Store: An Architecture Deep Dive</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Wed, 18 Nov 2026 11:50:19 +0000</pubDate>
      <link>https://dev.to/shivathapaa/one-codebase-six-apps-on-the-play-store-an-architecture-deep-dive-3io7</link>
      <guid>https://dev.to/shivathapaa/one-codebase-six-apps-on-the-play-store-an-architecture-deep-dive-3io7</guid>
      <description>&lt;h4&gt;
  
  
  &lt;em&gt;How a Kotlin Multiplatform monorepo ships an entire app portfolio — features as libraries, apps as thin shells — and what that actually costs to run.&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4tbsa6zyo6ing7p5ymuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4tbsa6zyo6ing7p5ymuo.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Six of my apps ship to Google Play: a score-keeping toolbox, a chess clock, a tally counter, a spin-the-bottle game, a Call Break scorer, and a Truth or Dare game. Six separate listings, six package names, six signing keys, six independent release streams, six sets of store screenshots.&lt;/p&gt;

&lt;p&gt;They are also one Git repository. One Gradle sync. Any of the six release bundles is a single ./gradlew invocation away, and the iOS builds of the same apps come out of the same tree - the App Store versions are next.&lt;/p&gt;

&lt;p&gt;This article is a tour of the architecture that makes that sustainable for a solo developer: the module topology, the small set of rules everything else follows from, the release pipeline that gives every app its own version stream, and — because any honest write-up owes you this — the parts that hurt.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why six apps at all
&lt;/h3&gt;

&lt;p&gt;The flagship app, &lt;strong&gt;Tallyo&lt;/strong&gt; , is a game-night toolbox: a generic round-based score calculator, a Call Break scorer, a chess timer, a multi-counter tally, a stopwatch, a dice roller, a coin flip, a spinner wheel, spin the bottle, truth or dare, and a few more party utilities behind one home screen.&lt;/p&gt;

&lt;p&gt;The product insight that produced the other five is simple: &lt;strong&gt;app-store search is tool-shaped.&lt;/strong&gt; Somebody who needs a chess clock types “chess clock” into the store. They will not install a party toolbox to get one, and the store’s ranking will not show them one. So the tools that can stand on their own get listings of their own, five of them so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://play.google.com/store/apps/details?id=com.tallyo.tallyo&amp;amp;utm_source=medium&amp;amp;utm_campaign=architecture-deep-dive" rel="noopener noreferrer"&gt;&lt;strong&gt;Tallyo&lt;/strong&gt;&lt;/a&gt; — com.tallyo.tallyo, built from everything&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://play.google.com/store/apps/details?id=com.tallyo.chess&amp;amp;utm_source=medium&amp;amp;utm_campaign=architecture-deep-dive" rel="noopener noreferrer"&gt;&lt;strong&gt;Chess Clock&lt;/strong&gt;&lt;/a&gt; — com.tallyo.chess, built from :feature:chesstimer:*&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://play.google.com/store/apps/details?id=com.tallyo.counter&amp;amp;utm_source=medium&amp;amp;utm_campaign=architecture-deep-dive" rel="noopener noreferrer"&gt;&lt;strong&gt;Tally Counter&lt;/strong&gt;&lt;/a&gt; — com.tallyo.counter, built from :feature:counter:*&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://play.google.com/store/apps/details?id=com.tallyo.spinbottle&amp;amp;utm_source=medium&amp;amp;utm_campaign=architecture-deep-dive" rel="noopener noreferrer"&gt;&lt;strong&gt;Spin the Bottle&lt;/strong&gt;&lt;/a&gt; — com.tallyo.spinbottle, built from :feature:spinbottle:*&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://play.google.com/store/apps/details?id=com.tallyo.callbreak&amp;amp;utm_source=medium&amp;amp;utm_campaign=architecture-deep-dive" rel="noopener noreferrer"&gt;&lt;strong&gt;Call Break Scorer&lt;/strong&gt;&lt;/a&gt; — com.tallyo.callbreak, built from :feature:callbreak:*&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://play.google.com/store/apps/details?id=com.tallyo.truthordare&amp;amp;utm_source=medium&amp;amp;utm_campaign=architecture-deep-dive" rel="noopener noreferrer"&gt;&lt;strong&gt;Truth or Dare&lt;/strong&gt;&lt;/a&gt; — com.tallyo.truthordare, built from :feature:truthordare&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The naive reading of that list is “sixfold maintenance.” The entire point of the architecture is that it isn’t. Each standalone app is a thin shell (three small Gradle modules and an Xcode project, plus store assets and a keystore) around feature code that already existed. Counter, Call Break, and Truth or Dare all began life as screens inside the flagship and were then promoted to their own listings. Two of those promotions &lt;strong&gt;changed no feature code at all&lt;/strong&gt; ; the third added one optional slot to Call Break’s settings screen. That promotion path is the proof that the architecture works, and the rest of this article explains what makes it possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  The one rule everything follows from
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Features are libraries. Apps are shells.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A feature module knows nothing about which app is hosting it. It exposes screens, a navigation graph fragment, a dependency-injection module, and (if it needs them) its own database and settings store. It does not know its application ID, its theme defaults, whether ads exist, or whether it is one tool among ten or the only tool in the app.&lt;/p&gt;

&lt;p&gt;An app shell owns identity and assembly, and nothing else: the application ID, the icon, the splash screen, the navigation graph it composes out of feature fragments, the DI tree it assembles, the monetization configuration, and the signing setup.&lt;/p&gt;

&lt;p&gt;Even the flagship follows this rule. :composeApp, Tallyo's shared entry point, has no privileged status; it is simply the largest shell in the repository. Once that is true, "ship this feature as its own app" stops being a project and becomes an afternoon of shell wiring.&lt;/p&gt;

&lt;h3&gt;
  
  
  The topology
&lt;/h3&gt;

&lt;p&gt;The repository is currently &lt;strong&gt;99 Gradle modules&lt;/strong&gt; , organized in three strict layers plus the build logic that configures them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build-logic/ convention plugins (the boilerplate killer)
│
core/ 15 modules - shared platform
│ ui, resources, presentation, platformSpecific, permission,
│ datastore, utils, logger, observability, review,
│ billing, ads, adsui, adsremoteconfig,
│ appshell (Android only: the base Application and Activity)
│
feature/ 21 feature roots - nearly every screen in the portfolio
│ scoreCalculator/ callbreak/ counter/ (split features)
│ chesstimer/ spinbottle/ diceroller/ stopwatch/ (lighter split features)
│ flipacoin magic8ball truthordare spinnerwheel … (single-module)
│ rewards devmessage donate settings/
│
composeApp/ + androidApp/ the flagship shell (Tallyo)
baselineprofile/ the flagship's baseline-profile generator
apps/
    chess/ composeApp + androidApp + baselineprofile
    counter/ 〃
    spinbottle/ 〃
    callbreak/ 〃
    truthordare/ 〃
    shared/ About + Theme &amp;amp; Colors, shared by the five standalone apps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dependency direction is non-negotiable: &lt;strong&gt;apps → features → core&lt;/strong&gt; , never sideways, never up. A feature’s screen modules may depend on that same feature’s domain/database/datastore modules, and everything may depend on :core:*. Nothing in feature/ may reference another feature, and nothing in core/ or feature/ may reference a shell.&lt;/p&gt;

&lt;p&gt;Two modules are Android-only by design: :core:appshell, which only the six app shells link, and :feature:counter:widget, the Counter home-screen widget, which a shell opts into with a single flag.&lt;/p&gt;

&lt;p&gt;settings.gradle.kts reads like a manifest of the whole strategy. Each standalone app is three lines and a comment stating the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Call Break Scorer - separate Play / App Store listing (com.tallyo.callbreak).&lt;/span&gt;
&lt;span class="c1"&gt;// Shares :core:* and :feature:callbreak:* with Tallyo; only the app shell lives here.&lt;/span&gt;
&lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":apps:callbreak:composeApp"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":apps:callbreak:androidApp"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":apps:callbreak:baselineprofile"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Convention plugins: how 99 modules stay cheap
&lt;/h3&gt;

&lt;p&gt;Ninety-nine modules are only viable if a module costs almost nothing to declare. Every shared module in this repo applies a locally built convention plugin instead of carrying its own kotlin {} and android {} blocks, and the 60 with UI apply a second one for Compose. Here is the first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;KotlinMultiplatformConventionPlugin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Plugin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pluginManager&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kotlinMultiplatform"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pluginId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"android.kotlin.multiplatform.library"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pluginId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;libs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kotlinSerialization"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pluginId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;KotlinMultiplatformExtension&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;configureKotlinMultiplatform&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;KotlinMultiplatformAndroidComponentsExtension&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;configureKotlinMultiplatformAndroidLibrary&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The convention configures the iOS targets (iosArm64, iosSimulatorArm64), pins the JVM target, applies the default source-set hierarchy, wires the baseline dependencies (Koin, coroutines), and derives the Android namespace mechanically from the module path, so no module ever declares one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;moduleName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removePrefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;androidExtension&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.tallyo.tallyo.$moduleName"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result: a typical feature module’s build.gradle.kts is under twenty lines - two plugin aliases, a dependency list, done. When a policy changes (a new compile SDK, a packaging exclusion, consumer ProGuard rules), it changes in one place and every module inherits it. This is the same idea Google's Now in Android popularized, applied to a KMP library codebase; at this module count it is not a nicety, it is the load-bearing wall.&lt;/p&gt;

&lt;p&gt;There are four of these plugins now: the KMP library and Compose conventions above, one for the Android app shells (more on that below), and one for the baseline-profile generators.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two shapes of feature, chosen deliberately
&lt;/h3&gt;

&lt;p&gt;Not every feature deserves a dozen modules. The repo recognizes exactly two shapes and forces a choice up front:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The split feature&lt;/strong&gt;  — for anything with multiple screens, per-round state, or persistence. Call Break has the full set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;feature/callbreak/
    domain/ pure models + the score engine (no Android, no Compose)
    database/ Room DB + DAOs + entities + migrations
    datastore/ feature-scoped settings (Preferences DataStore)
    presentation/ feature-specific shared composables
    feature/
        home/ gamesetup/ addplayer/ currentgameplay/
        gamedetail/ playerprofile/ history/ settings/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lighter split features take only the modules they need; the chess timer is a settings store and two screens.&lt;/p&gt;

&lt;p&gt;The eight screen names are a canonical template, and every split feature draws from it. Call Break uses all eight and Score Calculator seven; Counter, which is not a game, keeps the layout and gives its setup and play screens names of their own (countersetup, counting). That sameness is deliberate: when Score Calculator, Call Break, and Counter all decompose the same way, navigating an unfamiliar feature costs nothing, and the next feature is scaffolding rather than design work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The single-module mini-feature&lt;/strong&gt;  — for self-contained toys: one module, one screen, one ViewModel, one Koin module (:feature:flipacoin, :feature:magic8ball, :feature:truthordare, and friends).&lt;/p&gt;

&lt;p&gt;Two details make features &lt;em&gt;extractable&lt;/em&gt; into standalone apps later, and both are about ownership:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Each feature owns its persistence outright.&lt;/strong&gt; Call Break has its own Room database module; Counter has its own; nothing shares tables. There is no app-wide database that would need to be sliced apart when a feature moves out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Each feature owns its settings keys.&lt;/strong&gt; DataStore Preferences.Key constants live in the feature's own module, never in :core:datastore. The core module owns only genuinely app-wide state (theme, dark mode).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When Truth or Dare got its own listing, the feature module was not touched. That is the standard I hold the structure to.&lt;/p&gt;

&lt;h3&gt;
  
  
  Anatomy of an app shell
&lt;/h3&gt;

&lt;p&gt;Here is the entire Truth or Dare app, minus store assets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apps/truthordare/
    composeApp/ MainApp() + NavHost + Koin assembly (+ iOS entry point)
    androidApp/ one build call, a 40-line Application and a 20-line Activity
    baselineprofile/ records this app's own baseline profile on an emulator
    iosApp/ the Xcode project: a SwiftUI host plus Firebase and ads bridges
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The About and Theme &amp;amp; Colors screens (the latter doubles as Truth or Dare’s settings hub) come from apps/shared/, which the five standalone apps link instead of each forking their own.&lt;/p&gt;

&lt;p&gt;The most honest description of “what is this app?” in this codebase is the shell’s Koin assembly. It is short enough to read whole:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** The RevenueCat entitlement that grants Truth or Dare Pro. */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="no"&gt;TRUTHORDARE_ENTITLEMENT_PRO&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Truth or Dare Pro"&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;listOfDiModules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dataStoreModules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;platformSpecificModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;observabilityModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Analytics / Crashlytics / Performance&lt;/span&gt;
        &lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MoreAppsVisibility&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;billingModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;entitlementId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TRUTHORDARE_ENTITLEMENT_PRO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;tipsOfferingId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;TRUTHORDARE_TIPS_OFFERING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;adsModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;reviewModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// the store's own rating card&lt;/span&gt;
        &lt;span class="n"&gt;donateUiModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;rewardsModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RewardScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TRUTH_OR_DARE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;devMessageModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="n"&gt;themeAndColorsUiModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;aboutUiModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="n"&gt;truthOrDareUiModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// the actual feature&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;An app, in this architecture, is a list of DI modules and a navigation graph.&lt;/strong&gt; The flagship’s version of this same file assembles nearly seventy modules; Truth or Dare’s assembles fourteen. Same shape, different length.&lt;/p&gt;

&lt;p&gt;Navigation composes the same way. Every feature exports a typed @Serializable route and a NavGraphBuilder extension for each of its screens; every shell owns its own NavHost and stitches together exactly the screens it wants. Tallyo and Call Break Scorer each assemble their own callBreakGraph() from the same eight screen builders. Chess Clock has no such graph and no dependency on Call Break at all, so the Call Break screens - and their Room database, and its bundled SQLite driver - do not exist in that APK.&lt;/p&gt;

&lt;p&gt;The Android half of a shell is just as short. Its build file states only what makes it its own Play app, in one call to the shell convention plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;tallyoAppShell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;applicationId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.tallyo.truthordare"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;keystore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"truthordare"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;signingEnvPrefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"TRUTHORDARE_"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requireKeystore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;versionKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"truthordare-app"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;admobAppIdSecret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ADMOB_APP_ID_ANDROID_TRUTHORDARE"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;composeApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;":apps:truthordare:composeApp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The flagship’s call differs in two telling places: requireKeystore = true, so Tallyo's release fails without its keystore instead of shipping unsigned, and counterWidget = true (Tally Counter sets it too), which links the home-screen widget module and merges its receiver into the manifest.&lt;/p&gt;

&lt;p&gt;The shell’s Application and Activity extend base classes in :core:appshell and override only what differs: the RevenueCat key, the ad unit IDs, the Koin entry point and the root composable. Splash, edge-to-edge, consent, in-app updates, crash reporting and the ad session live once, in the base classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainActivity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TallyoShellActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Composable&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;MainApp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onExitApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One shell detail worth stealing: platform-specific DI is a &lt;strong&gt;typed slot&lt;/strong&gt; , not a convention.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlatformModules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;ads&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;initializeKoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PlatformModules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PlatformModules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Android, the base Application builds the AdMob module from the shell's own ad unit IDs and hands it to the shell's initializeKoin; on iOS, the entry point passes iosAdsModule(host), where host is a Swift implementation of the ads bridge, or null, which makes it inert. Because the slot is a data class parameter rather than "remember to also load these modules," a new slot declared without a default is a compile error at every entry point instead of a NoBeanDefFoundException on one platform's cold start.&lt;/p&gt;

&lt;p&gt;The third module records the app’s own baseline profile. The siblings used to ship Tallyo’s, whose recorded journeys never open Spin the Bottle or Truth or Dare, so those apps were precompiling somebody else’s hot path. Each app now has a generator that plays its own journeys, built from one shared set of steps. Copy one detail from it: the benchmark build type declares itself a debug build, with test ad units and billing switched off, because an emulator requesting a live ad is invalid traffic, and AdMob enforces that across the whole account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why not product flavors?
&lt;/h3&gt;

&lt;p&gt;The question every Android engineer asks at this point: isn’t this what build flavors are for?&lt;/p&gt;

&lt;p&gt;No. Flavors solve a different problem. Flavors are &lt;em&gt;one app, several configurations&lt;/em&gt;: brand A vs. brand B, free vs. paid, staging vs. production. What I have is &lt;em&gt;six different apps with six different feature sets&lt;/em&gt; that happen to share parts.&lt;/p&gt;

&lt;p&gt;Concretely, flavors would have cost me the two properties I care most about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Classpath isolation.&lt;/strong&gt; With flavors, unused features are excluded by configuration you must maintain (per-flavor source sets and dependency declarations), and one mistake silently ships the Call Break engine inside the chess clock. With shells, exclusion is structural: the chess shell has no dependency edge to :feature:callbreak:*, so the compiler and the dependency graph enforce what a flavor config only promises. The feature toggle &lt;em&gt;is&lt;/em&gt; the dependency graph.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Independent release identity.&lt;/strong&gt; Six flavors of one module would still share one Gradle module’s version plumbing, one manifest merge tree, and one increasingly baroque build.gradle.kts. Six shells give each app its own signing config, its own Firebase project, its own version line, and its own release cadence with no interaction between them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trade-off is real: a shell is a physical thing you maintain, not a flag. The shell convention plugin now shrinks that thing to one function call and wins back the one advantage flavors had: configuration defined in a single place. It took me six shells to write it, and I price that honestly below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keeping money out of shared screens
&lt;/h3&gt;

&lt;p&gt;Monetization is where “features are libraries” gets stress-tested, because ads and billing SDKs are the heaviest, most platform-bound dependencies in the app, and the easiest to leak everywhere.&lt;/p&gt;

&lt;p&gt;Three structural decisions keep them contained:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Billing and ads are separate modules with a one-way street between them.&lt;/strong&gt;  :core:billing owns RevenueCat and nothing else. :core:ads owns the ads engine and nothing else; it depends on billing, never the reverse, and learns about premium status through a single Flow signal, so it never sees a RevenueCat type. A screen that only needs to know whether the user is premium (Theme &amp;amp; Colors, for one) links billing and never an ad SDK, and the "is this user premium?" logic has exactly one owner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Compose ad surfaces live in their own module.&lt;/strong&gt;  :core:adsui (the native ad card, the banner slots) is deliberately &lt;em&gt;not&lt;/em&gt; part of :core:presentation, the module every screen already depends on. A module gets ad UI only by depending on it explicitly, never as a side effect of using the shared components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Shared screens take slots; shells fill them.&lt;/strong&gt; A feature screen that ships in more than one app must not reference an ad component, because each app decides its own monetization. So screens expose a slot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// :feature:counter:feature:counting - shared by Tallyo and Tally Counter&lt;/span&gt;
&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;CountingScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;bottomSlot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and each shell decides what goes in it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Tallyo's nav graph
CounterCounting(
    ...,
    bottomSlot = { TallyoGameplayBannerSlot() },
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This rule was learned, not designed. An early version referenced ad UI directly from the shared Counter screens, which pulled the ads and billing SDKs into the standalone Counter APK — measured at &lt;strong&gt;more than 7,000 extra classes&lt;/strong&gt; (6,551 from RevenueCat alone) in an app that, at the time, showed no ads at all. Hoisting the surface into the shell via a slot removed the entire subtree. Dependency hygiene here is not aesthetic; it shows up in APK size and method counts. (A few shared screens, Call Break’s and Truth or Dare’s, still place their own banner. That holds only because both apps that ship each of them show it in the same place, and it is exactly the kind of exception that turns into the next 7,000 classes.)&lt;/p&gt;

&lt;p&gt;Slots carry ad surfaces in; callbacks carry facts out. A full-screen ad must never land on a game that is still running, and the chess clock keeps ticking while the app is in the background, so an app-open ad on the way back could cost a player their time. The shared chess screen cannot ask the ads engine whether that is allowed, because it does not link one. Instead it reports the one fact only it knows, and the shell turns it into an ads signal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Tallyo's chess graph: the board reports a live game, the shell tells the ads engine&lt;/span&gt;
&lt;span class="n"&gt;onGameActiveChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;live&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;gameplayAdState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setInGameplay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameplayAdState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CHESS_BOARD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;live&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The board reports it rather than the shell deriving it from the route, because being on the board is not the same as having a game on it. The feature stays ignorant of ads, and the policy stays in one place.&lt;/p&gt;

&lt;p&gt;The same parameterization handles per-app billing: billingModule(entitlementId = ...). Each app is its own RevenueCat project with its own entitlement ("Call Break Pro", "Truth or Dare Pro"), and the shared billing code neither knows nor cares which app it is running in.&lt;/p&gt;

&lt;h3&gt;
  
  
  One version catalog, six release trains
&lt;/h3&gt;

&lt;p&gt;Sharing a repository must not mean sharing a release cadence. Each app has its own version line, declared side by side in the single gradle/libs.versions.toml:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="na"&gt;app-version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"3.3.2"&lt;/span&gt; &lt;span class="c1"&gt;# Tallyo&lt;/span&gt;
&lt;span class="na"&gt;chess-app-version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1.1.1"&lt;/span&gt;
&lt;span class="na"&gt;counter-app-version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1.1.1"&lt;/span&gt;
&lt;span class="na"&gt;spinbottle-app-version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1.1.1"&lt;/span&gt;
&lt;span class="na"&gt;callbreak-app-version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1.2.2"&lt;/span&gt;
&lt;span class="na"&gt;truthordare-app-version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1.1.1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each of those lines has two siblings, -versionCode and -updatePriority. The last one goes to Play with each release as its in-app update priority, so a critical fix can ask users to update immediately without a code change.&lt;/p&gt;

&lt;p&gt;(One hard-won convention hides in the naming: CI reads that priority with an anchored grep such as ^counter-app-updatePriority, and because the app name always comes first, Tallyo's bare ^app-updatePriority can never match a sibling's key. Prefix, never suffix.)&lt;/p&gt;

&lt;p&gt;Releases are tag-driven, one prefix per app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v3.3.2+18-production → Tallyo, versionCode 18, production
callbreak-v1.2.2+6-production → Call Break Scorer, versionCode 6, production
truthordare-v1.1.1+3-internal → Truth or Dare, versionCode 3, internal testing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tag suffix selects the Play track (internal → alpha → beta → production), so promoting a build is a tag, not a YAML edit. The optional +&lt;code&gt; pins the version code; more on why below.&lt;/code&gt;&lt;/p&gt;
&lt;code&gt;

&lt;p&gt;On the CI side there is exactly &lt;strong&gt;one&lt;/strong&gt; release pipeline, a reusable GitHub Actions workflow, and six thin callers that parameterize it:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# TruthOrDarePublish.yml (tag trigger and secrets omitted)
&lt;/span&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./.github/workflows/_publish.yml&lt;/span&gt;
    &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;app_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Truth or Dare&lt;/span&gt;
      &lt;span class="na"&gt;tag_prefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'truthordare-'&lt;/span&gt;
      &lt;span class="na"&gt;module_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;':apps:truthordare:androidApp'&lt;/span&gt;
      &lt;span class="na"&gt;package_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;com.tallyo.truthordare&lt;/span&gt;
      &lt;span class="na"&gt;keystore_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;keystores/truthordare.jks&lt;/span&gt;
      &lt;span class="na"&gt;env_prefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'TRUTHORDARE_'&lt;/span&gt;
      &lt;span class="na"&gt;google_services_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;apps/truthordare/androidApp/google-services.json&lt;/span&gt;
      &lt;span class="na"&gt;whatsnew_dir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;apps/truthordare/distribution/whatsnew&lt;/span&gt;
      &lt;span class="na"&gt;update_priority_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;truthordare-app-updatePriority&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;_publish.yml does everything once: extracts version and track from the tag, decodes the app's keystore and google-services.json from secrets, builds the bundle, and uploads it to Play with its release notes and in-app update priority. Fix the release flow once and all six apps inherit the fix: the same convention-plugin philosophy, applied to CI. Secrets follow a mechanical _ prefix scheme (CHESS_STORE_PASSWORD, TRUTHORDARE_KEYSTORE_BASE64), so provisioning app number seven is filling in a template.&lt;/p&gt;

&lt;p&gt;Two operational details worth writing down before you copy this design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pin the version code in the tag.&lt;/strong&gt; Without the +&lt;code&gt; segment, the code falls back to github.run_number, which is scoped to the caller workflow file and knows nothing about Play's history: renaming ChessPublish.yml resets it to 1, and any upload made outside CI leaves it behind. Play rejects a code that is not higher than the last one it saw. I now pin every release (v3.3.2+18-production); the run number is only the fallback, and the caller files still carry do-not-rename warnings.&lt;/code&gt;
&lt;/li&gt;
&lt;code&gt;
&lt;li&gt;
&lt;strong&gt;Decide what the merge gate covers.&lt;/strong&gt; CI on every pull request runs the unit tests, assembles all six app shells and lints the flagship. All six, because each shell has its own NavHost, Koin graph and manifest, and a shared-module change can break one of them while the flagship still builds. The iOS framework link runs only on merges to main, because macOS minutes cost about ten times as much. That is a deliberate cost/coverage trade, and you should make yours consciously.&lt;/li&gt;
&lt;/code&gt;
&lt;/ul&gt;
&lt;code&gt;
&lt;h3&gt;
  
  
  Testing what six apps share
&lt;/h3&gt;

&lt;p&gt;A bug in a shared module can ship in six apps at once, so the tests sit where that blast would start. There are 373 of them across 15 modules today, most in commonTest, all running on the JVM host, and CI runs them on every pull request. Three things made that work in a KMP library codebase:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prove the suite runs.&lt;/strong&gt; AGP’s KMP library plugin creates no host-test compilation unless the module asks for one (withHostTest {}). Forget it and the suite compiles, never runs, and the build stays green. After adding a suite, I check that its XML report exists with a non-zero test count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run Room on the host through a test-only&lt;/strong&gt; &lt;strong&gt;jvm() target.&lt;/strong&gt; Each database module carries one that nothing ships, so the tests build the &lt;em&gt;production&lt;/em&gt; database in memory. Migration tests build the old database from its schema and let Room run the real migration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let a model check the engine.&lt;/strong&gt; For the three databases, fixed seeds of random operations run against the real database and are compared at every step with a small model written from the game rules, not from the engine. A failure names its seed and step, so it replays exactly. To test the tests, I broke the code under test on purpose 24 times; every break turned the suites red.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The multiplatform dividend
&lt;/h3&gt;

&lt;p&gt;Everything above would hold for a pure Android monorepo. Kotlin Multiplatform is what turns six Play Store apps into (soon) twelve store listings without a second codebase.&lt;/p&gt;

&lt;p&gt;Apart from the two Android-only modules, every shared module compiles to Android and iOS (iosArm64, iosSimulatorArm64, configured once, in the convention plugin). Each shell's composeApp produces that app's iOS framework, consumed by a minimal SwiftUI host. The discipline that makes this cheap is boring and constant rather than clever:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;commonMain is the default; platform source sets are the exception.&lt;/strong&gt; Anything touching Context, UIKit, or java.* goes behind an expect/actual seam. The seams are few and deliberate (only 12 of the 81 shared modules declare one), and most are what you would guess: platform info, haptics and sound, permissions and files, the theme entry point, keeping the screen awake, sharing an image, the store's review card, and the plumbing that Room, DataStore and Firebase need.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Platform truths get encoded as rules, not tribal knowledge.&lt;/strong&gt; When something compiles for Android and breaks only on the iOS side, the fix ships together with a written rule, so nobody pays for the same lesson twice. The iOS link on every merge to main exists to catch exactly that class of bug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Firebase stays out of shared code, even though KMP wrappers for it exist.&lt;/strong&gt; Observability is shared behind provider-agnostic interfaces (AnalyticsHelper, CrashReporter), with the Firebase implementation wired per platform - on iOS via a small Swift bridge - so shared code never sees a Firebase type.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Even business posture is a per-platform seam.&lt;/strong&gt; Pro is a paid upgrade on iOS but granted free on Android (a payments-availability constraint in my market). That is a single expect/actual value in the billing module - not a fork, not a flavor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The compounding effect is the real payoff: the theme engine, the reward-unlock system, the remote-config kill switches, the crash reporting, the in-app review prompts. Each was built once, and every app in the portfolio gets it in the next release. A feature’s cost is amortized over six apps today and twelve listings tomorrow.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it costs (the honest section)
&lt;/h3&gt;

&lt;p&gt;No architecture write-up is complete without its bill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The shells were near-duplicates for far too long.&lt;/strong&gt; Every new app was, truthfully, “copy the nearest sibling and sed the identifiers." By app six that meant six near-identical build files, six copies of the Application and Activity (nearly 6,000 lines that differed in a billing key, the ad unit IDs and two deep-link hooks), and ten forked settings modules (about 8,500 lines that differed in a name, a tagline and a logo). The copies had started to drift, too: the Call Break app's feedback form said "Help us improve Tallyo Counter", inherited from the Counter shell it was cloned from. All of it is now one convention plugin, an Android-only :core:appshell with the base classes, and two shared settings modules. The safety net was mechanical. Before and after the shell plugin, each app's resolved build configuration, merged manifest, BuildConfig and runtime classpaths were compared and came out identical, and the move to the base classes left all six merged manifests unchanged. If you adopt this architecture, extract the shell plugin around app three, not app six.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shared screens have to be told who they are.&lt;/strong&gt; Sharing a screen instead of forking it has its own failure mode: a screen that quietly assumes it is running in the flagship. The Support screen, shared by all six apps from the start, did exactly that. It said “Support Tallyo” in all six apps, and in the other five its “Leave a review” button opened Tallyo’s Play listing and “Share” sent Tallyo’s link. The fix is a rule: anything that names the app is a &lt;em&gt;required&lt;/em&gt; parameter the shell passes in (AboutAppInfo, DonateAppInfo), and store links use the running app's own package. With a required parameter, the next shell fails to compile instead of impersonating the flagship.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Manual DI assembly is a runtime failure class.&lt;/strong&gt; One Koin tree per app, composed by hand, means a forgotten module is a crash on first resolution, not a compile error. The typed PlatformModules slot removes one category of that mistake; the rest is covered by the dullest possible mitigation: a written checklist for "adding a feature," applied every time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A monorepo concentrates blast radius.&lt;/strong&gt; One careless change in :core:ui ships in six apps' next releases. The strict layering, the conventional structure, the host-side test suite and the CI gate exist precisely because of this; so does a healthy respect for boring, incremental change in :core:*.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gradle at 99 modules must be managed, not endured.&lt;/strong&gt; Configuration cache, build cache, typesafe project accessors, and thin per-module build scripts are what keep sync and build times civilized. The convention plugins matter as much for build performance as for consistency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaways
&lt;/h3&gt;

&lt;p&gt;If you are considering this shape for your own portfolio:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Write the shell rule down on day one.&lt;/strong&gt; &lt;em&gt;Features are libraries; apps are shells.&lt;/em&gt; Every dependency edge either respects it or gets refactored. The first leak (mine cost more than 7,000 classes) is much cheaper to fix than the fifth.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make features own their persistence and settings.&lt;/strong&gt; Feature-scoped databases and preference keys are what make “promote this feature to its own app” a wiring task instead of a surgery.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standardize the feature template.&lt;/strong&gt; Two shapes, one canonical screen set. Sameness is a feature; it is what makes the tenth module free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefer shells over flavors when feature sets differ.&lt;/strong&gt; Flavors configure exclusion; dependency edges enforce it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invert dependencies at every money boundary.&lt;/strong&gt; Billing and ads in separate modules, a one-way signal between them, UI slots so shared screens stay ignorant of monetization, and callbacks for the facts only a screen knows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build the release pipeline once, parameterize per app.&lt;/strong&gt; One reusable workflow plus thin callers, prefixed tags, prefixed secrets, per-app version keys in one catalog, and the version code pinned in the tag.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conventions are the enabling technology:&lt;/strong&gt; convention plugins for modules and app shells, one reusable workflow for CI. At portfolio scale, anything configured in more than one place will drift. Extract the shell plugin at app three; I waited until app six.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make app identity a required parameter.&lt;/strong&gt; A shared screen with a default name will eventually ship the flagship’s name inside another app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the shared layer on the host, and prove the tests run.&lt;/strong&gt; A suite that silently never runs is worse than none: it buys confidence you have not earned.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One codebase, six apps, one person maintaining it. The architecture is not what makes that impressive — it is what makes it &lt;em&gt;unremarkable&lt;/em&gt;, week after week. That is the highest compliment I can pay a codebase.&lt;/p&gt;

&lt;/code&gt;&lt;/code&gt;

</description>
      <category>androiddevelopment</category>
      <category>kotlin</category>
      <category>kotlinmultiplatform</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Publishing one Kotlin Multiplatform library to six places: Maven Central, SPM, pub.dev,</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Tue, 06 Oct 2026 04:06:23 +0000</pubDate>
      <link>https://dev.to/shivathapaa/publishing-one-kotlin-multiplatform-library-to-six-places-maven-central-spm-pubdev-4jgb</link>
      <guid>https://dev.to/shivathapaa/publishing-one-kotlin-multiplatform-library-to-six-places-maven-central-spm-pubdev-4jgb</guid>
      <description>&lt;h3&gt;
  
  
  Publishing one Kotlin Multiplatform library to six places: Maven Central, SPM, pub.dev, npm, PyPI, and the web
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftwq4a2ute7engm3gof6a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftwq4a2ute7engm3gof6a.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I started Nepali Date Picker to solve one problem: give Android and iOS a Bikram Sambat date picker that feels exactly like the Material 3 one. I did not plan to end up publishing the same code to six different package ecosystems. But a calendar is not an Android problem or an iOS problem. It is a Nepali problem, and Nepali dates show up on backends, in scripts, on websites, and in Python notebooks just as often as they show up in a mobile UI.&lt;/p&gt;

&lt;p&gt;So over time the library grew a second life as a publishing exercise: one set of conversion tables, shipped everywhere a developer might need them. This is the map of how that works, what each destination actually wants from you, and the mistakes I made so you do not have to.&lt;/p&gt;

&lt;h3&gt;
  
  
  The one rule that made everything else possible
&lt;/h3&gt;

&lt;p&gt;Before any of the publishing, there is one decision that pays for itself over and over: &lt;strong&gt;the calendar math lives in exactly one place, and it has zero UI dependencies.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this project that place is the :core module. It depends only on kotlinx-datetime. No Compose, no Android, nothing that would stop it compiling for a backend or a browser. The Bikram Sambat to Gregorian conversion is table driven: a map from each BS year to the number of days in each of its twelve months. That table is the source of truth for the entire ecosystem. If it is correct in :core, every downstream target is correct.&lt;/p&gt;

&lt;p&gt;Keeping :core Compose free took one trick worth knowing. The core data classes still want to advertise Compose stability (@Immutable, &lt;a class="mentioned-user" href="https://dev.to/stable"&gt;@stable&lt;/a&gt;) on the targets that have Compose, without dragging Compose onto the targets that do not. I declare those annotations in commonMain as @OptionalExpectation expect annotation class, and a Compose-only source set provides the actual typealias to androidx.compose.runtime.*. On a plain JVM backend or a Linux native target, the annotation simply compiles away to nothing. That is what lets one module target a strict superset of platforms while the UI module targets only the ones Compose supports.&lt;/p&gt;

&lt;p&gt;The library ends up as three artifacts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;:core, conversion, formatting, calendar utilities. Pure Kotlin.&lt;/li&gt;
&lt;li&gt;:ui, the Compose Multiplatform pickers. Depends on :core.&lt;/li&gt;
&lt;li&gt;:serialization, optional kotlinx-serialization support, kept separate so no serialization dependency leaks into projects that do not want it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything below is just teaching a different ecosystem how to consume that core.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destination 1: Maven Central (the Kotlin Multiplatform home)
&lt;/h3&gt;

&lt;p&gt;This is the primary target and the one most KMP guides already cover, so I will keep it to what actually matters.&lt;/p&gt;

&lt;p&gt;A multiplatform publication is not one artifact. It is a root module plus one per target (-android, -jvm, -js, -iosarm64, and so on), tied together by Gradle metadata. The Kotlin Gradle plugin generates all of it. Your job is three things: coordinates, signing, and the target list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Coordinates and signing.&lt;/strong&gt; Maven Central requires a group you own (mine is io.github.shivathapaa, verified through the GitHub namespace), plus GPG-signed artifacts. The modern Central Portal flow is smoother than the old OSSRH staging dance, but budget an afternoon the first time for the account, the namespace verification, and the signing key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The target matrix belongs in one place.&lt;/strong&gt; I keep target declarations and the JVM toolchain in convention plugins under build-logic, not scattered across modules. Because :core is Compose free, it targets a strict superset of :ui: :core adds linuxX64, mingwX64, wasmWasi, the watch and TV targets, and more, while :ui covers only what Compose Multiplatform supports.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Split the artifacts on real boundaries.&lt;/strong&gt; Splitting :core out from :ui is what makes the library usable on a server at all. A backend that only needs to convert a date should not have to pull in Compose. (When I moved from the single umbrella artifact to the split in 3.0.0, the only migration most people needed was changing the dependency coordinate from nepali-date-picker to nepali-date-picker-ui.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consumers add one line to commonMain and get every platform:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gradle"&gt;&lt;code&gt;&lt;span class="n"&gt;commonMain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"io.github.shivathapaa:nepali-date-picker-ui:&amp;lt;version&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Destination 2: iOS through Swift Package Manager
&lt;/h3&gt;

&lt;p&gt;KMP already produces an iOS framework, but a Swift developer expects to add a package by URL in Xcode, not to reason about your Gradle output. Swift Package Manager wants a Git repository with a Package.swift that points at a binary XCFramework.&lt;/p&gt;

&lt;p&gt;The pattern that works well: build the XCFramework from the :ui module in the main repo, then publish it through a &lt;strong&gt;separate, thin SPM repository&lt;/strong&gt; (mine is Nepali-Date-Picker-SPM). That repo holds the Package.swift and a release pointing at the built framework. The separation keeps the huge binary and the SPM release tags out of the main source repo, and it gives Swift users the exact "Add package dependencies, paste URL, done" flow they expect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;nepali_date_picker&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The release workflow rewrites that Package.swift with the new asset URL and its checksum, so the manifest and the binary can never drift apart. Nobody edits it by hand.&lt;/p&gt;

&lt;p&gt;That principle is worth pushing further than the manifest, because a thin repository still has a README, and a README states facts that go stale the moment you cut the next release. So the SPM repository now carries no prose of its own either. Its page is rendered from a template in the main repo, and every fact on it (the version, the date, both archive sizes, both checksums, the platform floors, the supported year range) is substituted from the outputs of the build that produced the release. The year range is read out of the Kotlin source that defines it rather than restated. A placeholder with no value behind it fails the release instead of publishing literal braces, which is the property that makes the whole thing trustworthy: the page cannot quietly describe a release that never happened.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shipping a framework is not the same as shipping an API.&lt;/strong&gt; This is the same lesson the npm section below tells, and I had to learn it twice. Kotlin/Native will happily export your module to Objective-C, but a Swift developer calling a Compose composable is not a thing. So the pickers are exposed as UIViewController factories: one function per picker, each returning a controller a SwiftUI view wraps in a UIViewControllerRepresentable. Two details that only show up once you try it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Kotlin default arguments do not survive the Objective-C bridge.&lt;/strong&gt; A composable with eighteen optional parameters becomes an eighteen-argument Swift call with no defaults. The fix is a plain options object per picker (NepaliCalendarOptions, NepaliDockedOptions, and so on) that the Swift side mutates before handing it over.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compose cannot tell SwiftUI how tall it is.&lt;/strong&gt; A hosted controller has no intrinsic content size, so the frame collapses or clips. Every factory takes an onHeightChange callback and the SwiftUI wrapper drives its own frame from it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That first point has a consequence that took me a release to appreciate, and it is the one I would warn people about hardest: &lt;strong&gt;on the Swift side, adding a parameter with a default value is a source-breaking change.&lt;/strong&gt; In Kotlin, giving a state factory a new initialCalendarSystem parameter with a default is purely additive; every existing call site keeps compiling. Across the bridge the default vanishes, so the generated initializer grows an argument and every Swift call site stops compiling. That is exactly what 3.2.0 did to NepaliDatePickerState and NepaliDateRangePickerState, and it is why the documented Swift path goes through the UIViewController factories and their options objects rather than the state classes: an options object absorbs a new field without changing anyone's call. If you expose Kotlin classes directly to Swift, your Kotlin notion of "additive" and your Swift consumers' notion of it are not the same thing, and semantic versioning has to follow the stricter one.&lt;/p&gt;

&lt;p&gt;I also learned to treat that exported surface as something that can regress silently, so it is snapshot-tested: one task dumps the framework’s export list to a checked-in file, another diffs it on CI, and a third fails the build if a picker exists in Compose with no iOS factory behind it. Linking a framework takes minutes, so those run on the macOS leg only, not in the fast local loop.&lt;/p&gt;

&lt;p&gt;Two more things worth planning for. First, &lt;strong&gt;offer a UI-free product too.&lt;/strong&gt; Not every Swift consumer wants Compose, and the numbers make the case on their own: the picker XCFramework is an 83.9 MB download, while :core assembles its own Compose-free XCFramework at 5.9 MB zipped, published as a second product. They are deliberately mutually exclusive, since the UI framework statically embeds the core module and linking both would duplicate the Kotlin runtime. The Compose-free one also reaches further: it carries an Apple silicon macOS slice alongside the two iOS ones, which the picker framework cannot, because Compose Multiplatform publishes no embeddable AppKit host. A macOS app gets the calendar math and brings its own SwiftUI. Second, &lt;strong&gt;Apple architectures.&lt;/strong&gt; Compose Multiplatform dropped Apple x86_64 in a recent release, so :ui cannot target the iOS simulator on Intel Macs even though :core still can. Document that consumers must exclude x86_64 for the simulator, or you will get confused issue reports from CI machines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destination 3: Flutter through pub.dev
&lt;/h3&gt;

&lt;p&gt;This is the newest door and the one I put off longest, because there is no official bridge between Kotlin Multiplatform and Flutter. JetBrains documents the two as alternatives rather than neighbours, and the community projects that tried to marry them have gone quiet. That leaves two honest options: port the calendar to Dart, or bridge the binaries you are already publishing.&lt;/p&gt;

&lt;p&gt;I bridged. A Dart port would have been a second implementation of the conversion table, and the PyPI section below is about what that costs even when you do it carefully. So nepali_date_picker_kmp is a thin Dart surface over the artifacts the two sections above already produce: on Android it resolves the ordinary Maven artifact, and on iOS it links the exact XCFramework the SPM release publishes. Nothing calendar-shaped is compiled twice. The engine that answers in Dart is the engine that answered in Kotlin and in Swift.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;nepali_date_picker_kmp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;^3.3.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://pub.dev/packages/pigeon" rel="noopener noreferrer"&gt;Pigeon&lt;/a&gt; generates the channel contract. One schema file describes the messages and the types, and it emits the Dart, Kotlin and Swift ends together; CI regenerates them and fails if the committed files differ, so the bridge cannot drift from its schema. Pin the generator to an exact version, because codegen has to be byte stable across machines for that diff to mean anything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The bridge was only affordable because of a property **&lt;/strong&gt; :core already had.** Its public API is entirely synchronous, has no coroutines or Flow anywhere in it, and does not mention a kotlinx-datetime type in a single public signature. It is ints, strings, booleans and the library's own flat value types. Every one of those absences would have been an argument at the channel boundary, and it is the same property that made the JavaScript facade possible a section later. If your core traffics in suspend functions or in types from a library the other runtime has never heard of, price the bridge before you promise it.&lt;/p&gt;

&lt;p&gt;The honest tax is that &lt;strong&gt;everything is a&lt;/strong&gt;  &lt;strong&gt;Future&lt;/strong&gt;. Platform channels are asynchronous by nature, so a table lookup that takes microseconds arrives in Dart as an awaited call. There is no way around it short of FFI, and I would rather say so in the README than let a consumer discover it while writing a build method.&lt;/p&gt;

&lt;p&gt;Then the parts that only show up once the pickers themselves have to render.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compose has opinions about the Activity it is hosted in.&lt;/strong&gt; A FlutterActivity is not a ComponentActivity, so a ComposeView placed inside one finds no lifecycle owner and no saved-state owner in its view tree and refuses to compose. Supply those and it still dies, because Compose's shared window recomposer reads the lifecycle off the window's content view, which in a Flutter app is the FlutterView and has none. The fix is a synthetic owner driven by the view's own attach and detach, plus a view-local recomposer installed through setParentCompositionContext so the window is never consulted at all. It installs itself only when the view tree provides nothing, so a normal Android app keeps its own owners.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The height problem, for the third time.&lt;/strong&gt; A platform view has no intrinsic content size either, exactly like a hosted UIViewController. Same contract, then: the native side reports its measured height, the Dart widget sizes itself from it. Three ecosystems, one lesson, which is how you know it is not an ecosystem's fault.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The packaging problem is the one I found most interesting, because it changed how releases work. A pub package is capped at roughly 100 MB and the iOS XCFramework unpacks to 265 MB, so vendoring it inside the package is simply not available. The plugin therefore keeps a local binary target for development and the release pipeline swaps in the released download URL and checksum, read from the SPM repository’s manifest at that tag. That is a good outcome, and it has a consequence: &lt;strong&gt;the SPM release must land before the pub release.&lt;/strong&gt; My distributions now have an ordering, and the script that does the swap waits for the upstream manifest rather than racing it.&lt;/p&gt;

&lt;p&gt;One more trap, and it rhymes with the npm one below. &lt;strong&gt;pub.dev’s trusted publishing cannot create a package.&lt;/strong&gt; The OIDC exchange authenticates a release of a package that already exists and has GitHub Actions publishing enabled on its admin page, so on a first release it has nothing to authenticate against and falls back to an interactive prompt that no CI job can answer. The first version goes out by hand; every later one is automatic. Budget for that instead of debugging a workflow that is behaving correctly.&lt;/p&gt;

&lt;p&gt;Two smaller things worth copying. Enums cross a generated bridge &lt;strong&gt;by index&lt;/strong&gt; , so both ends pin the ordinals in tests; that contract is invisible in the source and breaks silently when someone inserts a case. And Kotlin/Native renames as it exports, so a Swift host sees short_, long_ and governmentpublic where Kotlin sees something tidier. Neither is hard. Both are only found by running the thing.&lt;/p&gt;

&lt;p&gt;Finally, the bridge paid rent upstream. Android had no View-based wrapper around the composables, because nothing had needed one, so the work added androidMain view factories and dialog presenters mirroring the iOS ones, behind the same kind of coverage check that guards the iOS surface. Any View-based Android app gets that now, for free, and the new surface cannot quietly fall behind the pickers any more than the iOS one can. Work done for a bridge is rarely only for that bridge. The plugin covers Android and iOS; Flutter web and desktop consumers are pointed at the npm engine or the Maven artifact, which is the ecosystem map doing its job rather than an apology.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destination 4: the web, and the honest npm story
&lt;/h3&gt;

&lt;p&gt;This is the part I learned the most from, so I will tell it straight, including the wrong turn.&lt;/p&gt;

&lt;p&gt;Kotlin/JS will happily produce an npm package. When you build the JS target, you get a publishable artifact, and it is tempting to think “great, the web is done.” &lt;strong&gt;It is not, and here is why.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That generated package is the Kotlin/JS runtime for your module. Its entry file only exports what you explicitly annotate with @JsExport. If you have not exported anything, a JavaScript developer who runs npm install and imports your package gets almost nothing usable: no convertAdToBs, no picker, just internal Kotlin symbols with mangled names. It bundles the whole Kotlin standard library (and, for a Compose app, Skiko and a WebAssembly canvas renderer) as its runtime. It is meant to be consumed by other Kotlin/JS code through the Kotlin toolchain, not hand written from JavaScript.&lt;/p&gt;

&lt;p&gt;I confirmed this the blunt way: the published Kotlin/JS package’s entry module exported exactly one symbol, a timezone helper. That is correct for Kotlin interop and useless as a JavaScript date library.&lt;/p&gt;

&lt;p&gt;So for the web I built two purpose-made packages instead, and treated JavaScript as a first-class consumer rather than a byproduct:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;@nepali-date-picker/core&lt;/strong&gt; is the headless engine with a real, hand-designed JavaScript API: convertAdToBs, getTodayBs, formatBsDateByPattern, and so on, with TypeScript types and zero runtime dependencies. It is compiled from the same Kotlin :core, so the conversion tables match, but the public surface is designed for JavaScript, not leaked from Kotlin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@nepali-date-picker/web-component&lt;/strong&gt; is a framework-agnostic  custom element built on Lit, plus its variants (dialog, docked, range, text field, range field, wheel, and the browsable month calendar). Because custom elements are standard DOM, it drops into React, Vue, Angular, Svelte, or plain HTML with no wrapper.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"module"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s1"&gt;'@nepali-date-picker/web-component'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;nepali-date-picker&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"2081-05-24"&lt;/span&gt; &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"ne"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/nepali-date-picker&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That snippet, though, assumes a bundler. &lt;strong&gt;A library build and a drop-in build are not the same artifact,&lt;/strong&gt; and I shipped only the first one at first. The library build externalizes its dependencies so a consumer’s bundler can deduplicate them, which means the published files import the bare specifiers lit and @nepali-date-picker/core. A browser loading that file from a CDN has no resolver for those names, so it fails. Correct for webpack and Vite, useless for a plain HTML page, and that second audience is large: a lot of the software that needs a Nepali calendar is a PHP or Django template with a  tag and no build step at all.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;The fix is a second pass that inlines everything into one file, advertised through the exports map and the unpkg / jsdelivr fields. It is one request, currently 317 kB over the wire and 82 kB gzipped, and it registers all eight elements. Two details I would not have guessed:&amp;lt;/p&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Ship it as an ES module, not UMD.&amp;lt;/strong&amp;gt; The instinct is to reach for a global build for maximum compatibility, but every browser that implements custom elements and shadow DOM also supports module scripts. A UMD build would only serve browsers that cannot render the component anyway.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Bundle the built output, not the sources.&amp;lt;/strong&amp;gt; My first attempt bundled from src/, and the bundler silently dropped the side-effect imports that register the elements, because the sideEffects field in package.json is declared against dist/. The registrations survived only by accident, through unrelated named re-exports in the barrel file. Bundling dist/ fixes it and guarantees the CDN file is the same code the registry serves.&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;The other thing worth writing down is what happened when I tried to type the elements for React. Custom elements are standard DOM, so the usual claim is that React 19 “just works” with them now. That is half true, and the half that is false costs real debugging time. &amp;lt;strong&amp;gt;React 19 fixed custom element properties, not events.&amp;lt;/strong&amp;gt; I verified it rather than trusting the write-ups: onChange does fire, but React wraps the dispatched CustomEvent in its own synthetic event, so event.detail is undefined and the payload lives at event.nativeEvent.detail. Worse, any event not already in React&amp;amp;#39;s synthetic set never reaches an on-prefixed prop at all. Two of this library&amp;amp;#39;s events, invalid and cancel, simply never fire that way, no matter how they are dispatched.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;So the shipped TypeScript definitions encode the real behaviour: the change handler is typed to expose its payload only through nativeEvent, and no onInvalid or onCancel props are generated at all, because a prop that type-checks and silently does nothing is worse than no prop. Those definitions are generated from a custom-elements.json manifest rather than hand-written, so the element stays the single source of truth and a new property cannot drift out of the types.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;The lesson generalizes to any KMP library: &amp;lt;strong&amp;gt;exposing your code to an ecosystem is an API design task, not a build flag.&amp;lt;/strong&amp;gt; The Kotlin/JS artifact is a transport mechanism. The developer experience is something you still have to author, whether that is @JsExport annotations with a clean facade or, as here, a small dedicated package that presents an idiomatic surface. (If you want the raw Kotlin/JS route instead, the work is the same shape: design and @JsExport a facade, do not ship the internals and hope.)&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;One more npm-specific trap: &amp;lt;strong&amp;gt;your dev setup is not what consumers install.&amp;lt;/strong&amp;gt; My showcase site loads the packages through local aliases, so it cannot catch a broken exports map, a missing files entry, or a dist that was never rebuilt. Those only surface after someone runs npm install. The cheap fix is a pre-publish check that packs both packages into tarballs, installs them into a throwaway project, imports them the way a consumer would, and bundles the element. It runs as a required gate in the publish workflow, so a package that cannot be installed cannot be released.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;If you publish from CI, prefer npm’s OIDC trusted publishing over a long-lived automation token. The workflow proves its identity to the registry per run and there is no secret sitting in the repository settings waiting to leak. It also buys you provenance: the versions I published by hand before setting it up carry no provenance badge, and the ones published from CI do, which is a visible difference on the package page.&amp;lt;/p&amp;gt;
&amp;lt;h3&amp;gt;
  &amp;lt;a name="destination-5-python-on-pypi" href="#destination-5-python-on-pypi" class="anchor"&amp;gt;
  &amp;lt;/a&amp;gt;
  Destination 5: Python on PyPI
&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;Some consumers are not on the JVM at all. Backends and data work in Nepal lean heavily on Python, so there is a sibling package, nepali_calendar_utils, published to PyPI.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;This one is a genuine port, not a compile target. Kotlin does not transpile to Python, so the utility logic is reimplemented. The one non-negotiable rule: &amp;lt;strong&amp;gt;the conversion tables must stay byte identical to the Kotlin **&amp;lt;/strong&amp;gt; :core.** A Bikram Sambat date that converts to one Gregorian date in the Android app and a different one in the Python report is worse than having no Python package at all. I treat the Kotlin table as canonical and diff the Python table against it whenever calendar logic changes.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;A port also gets its own version track, and that is fine. The Python package sits a minor version behind the Kotlin coordinates right now, because the releases in between were picker and bridge work that a Python consumer has no equivalent of. What must never drift is the table, not the version number. Say that out loud in the README so nobody reads the gap as neglect.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;If you go this route, write the parity check as a test, not as a habit. Habits forget. Tests do not.&amp;lt;/p&amp;gt;
&amp;lt;h3&amp;gt;
  &amp;lt;a name="destination-6-the-web-demo-and-api-docs-on-github-pages" href="#destination-6-the-web-demo-and-api-docs-on-github-pages" class="anchor"&amp;gt;
  &amp;lt;/a&amp;gt;
  Destination 6: the web demo and API docs on GitHub Pages
&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;The last destination is not a package registry, it is discoverability. A library people cannot see running is a hard sell, and API docs behind a Gradle command do not get read.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;A single GitHub Actions workflow builds three things on every push to main and deploys them as one Pages site:&amp;lt;/p&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;the Compose wasmJs sample at the site root,&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;the web-component showcase under /demo/,&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;the Dokka API reference under /api/.&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;Two things worth copying. First, GitHub Pages has a single live environment, so pull requests must not deploy: they would fight over the deploy lock and burn quota. PRs run tests through the CI workflow and nothing else; only main deploys. Second, trim the payload. The Compose wasmJs bundle is large because Skiko is large, so the workflow strips the JS source map before upload, which saves every visitor a download they never needed.&amp;lt;/p&amp;gt;
&amp;lt;h3&amp;gt;
  &amp;lt;a name="keeping-six-destinations-honest" href="#keeping-six-destinations-honest" class="anchor"&amp;gt;
  &amp;lt;/a&amp;gt;
  Keeping six destinations honest
&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;Publishing to six places is only sustainable if they cannot silently disagree. The things that keep this from becoming six subtly different libraries:&amp;lt;/p&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;One source of truth for the calendar.&amp;lt;/strong&amp;gt; Every target either compiles from :core, links a binary built from it, or is diffed against it. Nothing reimplements the conversion table casually.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Tests that assert parity&amp;lt;/strong&amp;gt; , not documentation that requests it. That includes the boundaries between ecosystems: the iOS export snapshot, the two bridge-coverage checks, the regenerate-and-diff of the Flutter channel schema, the enum ordinals pinned on both ends of it, and the packed-tarball npm install are all just parity tests wearing different clothes.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;A sample per consumption path.&amp;lt;/strong&amp;gt; There is a Compose sample, a SwiftUI sample that links the XCFramework the way a real Swift app does, a Flutter showcase that mirrors the SwiftUI one screen for screen, and a web showcase. Each one is the first thing to break when a distribution regresses, which is exactly what you want, and CI builds the Swift and Flutter ones on every run.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Semantic versioning taken seriously&amp;lt;/strong&amp;gt; , because a breaking change now ripples across six ecosystems with six different migration cultures. The 3.1.0 release was deliberately additive: every existing symbol kept working, and every new API was opt-in. 3.1.1 added two entire distributions without touching the Kotlin API, so Kotlin consumers upgraded by changing a version string. 3.2.0 put a whole second calendar into every picker with both new knobs defaulting to off, so an existing screen looks identical until it opts in, and the only break in the release was the Swift initializer one described above, which is the case where “additive in Kotlin” and “additive to consumers” come apart.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;strong&amp;gt;Centralized build configuration.&amp;lt;/strong&amp;gt; Targets and toolchain versions live in convention plugins, so I change them once.&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;h3&amp;gt;
  &amp;lt;a name="takeaways" href="#takeaways" class="anchor"&amp;gt;
  &amp;lt;/a&amp;gt;
  Takeaways
&amp;lt;/h3&amp;gt;

&amp;lt;p&amp;gt;If you are about to publish a KMP library beyond Maven Central, this is what I would tell past me:&amp;lt;/p&amp;gt;

&amp;lt;ol&amp;gt;
&amp;lt;li&amp;gt;Make your core logic UI free and dependency light on day one. Every other destination depends on this being true, and retrofitting it is painful.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Split artifacts along real dependency boundaries (core, UI, optional extras), not for tidiness. Offer a UI-free build to every ecosystem, not just the JVM one.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Swift Package Manager wants a thin, separate repo with a binary framework. Plan for it, and generate the manifest from the release workflow so the checksum cannot drift.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Every bridge is an API design task, not a build flag. The npm artifact Kotlin/JS hands you is a runtime, and the Objective-C export Kotlin/Native hands you is not a Swift API. Design the facade: options objects instead of lost default arguments, explicit size callbacks instead of intrinsic sizing you do not get.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Bridging the binary you already ship beats porting, but only if your public API can survive the crossing. Synchronous, no runtime-specific types in signatures, plain values in and out. Audit that before you promise a second runtime anything.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Check what “additive” means on the far side of each bridge before you promise it. A defaulted Kotlin parameter is a breaking change in Swift, and your version number has to answer to the strictest consumer, not the friendliest one.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Snapshot the exported surface of every bridge and diff it on CI. Bridges rot quietly, because the compiler on your side stays happy.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Verify what consumers actually install, not what your dev setup links. Pack it, install it in a throwaway project, import it.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;When one distribution pins another distribution’s artifact, your releases have an order. Encode the order in the pipeline and make it wait, rather than leaving it in a runbook nobody reads at 11pm.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;A registry’s automated publishing usually cannot create the package, only release it. Expect the first version of anything to go out by hand, and do not spend an evening debugging a workflow that is behaving correctly.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;The build a bundler wants is not the build a browser wants. If any of your audience has no build step, and on the web a lot of it does not, ship a second self-contained artifact and let a CDN serve it.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Test the platform claims you are about to encode, rather than trusting the write-ups. “React 19 supports custom elements” is true about properties and false about events, and I would have shipped types describing behaviour that does not exist if I had not checked.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;A non-JVM port lives or dies on data parity. Enforce it with a test, and let its version number drift if it needs to.&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Ship a running demo and hosted API docs. Discoverability is a feature.&amp;lt;/li&amp;gt;
&amp;lt;/ol&amp;gt;

&amp;lt;p&amp;gt;One library, six front doors, one calendar that agrees with itself everywhere. That was the whole point.&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Links&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;

&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;Source: &amp;lt;a href="https://github.com/shivathapaa/Nepali-Date-Picker"&amp;gt;https://github.com/shivathapaa/Nepali-Date-Picker&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Live demo: &amp;lt;a href="https://shivathapaa.github.io/Nepali-Date-Picker/demo/"&amp;gt;https://shivathapaa.github.io/Nepali-Date-Picker/demo/&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;API reference: &amp;lt;a href="https://shivathapaa.github.io/Nepali-Date-Picker/api/"&amp;gt;https://shivathapaa.github.io/Nepali-Date-Picker/api/&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Swift guide: &amp;lt;a href="https://github.com/shivathapaa/Nepali-Date-Picker/blob/main/README-spm.md"&amp;gt;https://github.com/shivathapaa/Nepali-Date-Picker/blob/main/README-spm.md&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Flutter guide: &amp;lt;a href="https://github.com/shivathapaa/Nepali-Date-Picker/blob/main/README-flutter.md"&amp;gt;https://github.com/shivathapaa/Nepali-Date-Picker/blob/main/README-flutter.md&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Web guide: &amp;lt;a href="https://github.com/shivathapaa/Nepali-Date-Picker/blob/main/README-js.md"&amp;gt;https://github.com/shivathapaa/Nepali-Date-Picker/blob/main/README-js.md&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Maven Central: &amp;lt;a href="https://central.sonatype.com/namespace/io.github.shivathapaa"&amp;gt;https://central.sonatype.com/namespace/io.github.shivathapaa&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Swift package: &amp;lt;a href="https://github.com/shivathapaa/Nepali-Date-Picker-SPM"&amp;gt;https://github.com/shivathapaa/Nepali-Date-Picker-SPM&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Flutter package: &amp;lt;a href="https://pub.dev/packages/nepali_date_picker_kmp"&amp;gt;https://pub.dev/packages/nepali_date_picker_kmp&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;npm: &amp;lt;a href="https://www.npmjs.com/package/@nepali-date-picker/web-component"&amp;gt;https://www.npmjs.com/package/@nepali-date-picker/web-component&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;Python sibling: &amp;lt;a href="https://pypi.org/project/nepali_calendar_utils/"&amp;gt;https://pypi.org/project/nepali_calendar_utils/&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;p&amp;gt;Drop a ❤️ if this was useful — it helps more developers find it. And follow for more upcoming contents (single codebase, six apps in playstore).&amp;lt;/p&amp;gt;
&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>kotlin</category>
      <category>kotlinmultiplatform</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Your Stack Trace Says Where It Died. It Won’t Say Why.</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sat, 08 Aug 2026 06:26:01 +0000</pubDate>
      <link>https://dev.to/shivathapaa/your-stack-trace-says-where-it-died-it-wont-say-why-334l</link>
      <guid>https://dev.to/shivathapaa/your-stack-trace-says-where-it-died-it-wont-say-why-334l</guid>
      <description>&lt;h4&gt;
  
  
  Building one logging, crash, and performance pipeline for a Kotlin Multiplatform app — one that scrubs its own secrets, is forbidden from crashing the app, and behaves the same on Android and iOS.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftjghehin59so09k2q4tc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftjghehin59so09k2q4tc.png" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a crash report I stared at for twenty minutes and learned nothing from.&lt;/p&gt;

&lt;p&gt;A NullPointerException, six frames deep, all of them inside a coroutine dispatcher. No app class in the stack. No indication of which screen the user was on, whether they were signed in, what language the UI was in, or which of our two backend tiers the build was pointed at. The stack told me, with total precision, the one thing I could already guess: a value was null when the code expected it not to be.&lt;/p&gt;

&lt;p&gt;It did not tell me why. And “why,” on a phone you will never hold, in the hands of a farmer on a 3G connection in a district you have never visited, is the entire job.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A stack trace is an autopsy. Observability is the medical history.&lt;/strong&gt; The autopsy tells you the organ that failed. The history tells you the patient skipped a dose, that they were on their third screen in a flow, offline, mid-way through a payment, on the app’s oldest supported OS. One of those is actionable. This is a piece about building the second one — for a Kotlin Multiplatform app, where “the second one” has to work identically on two platforms that share none of the same tooling underneath.&lt;/p&gt;

&lt;p&gt;I’ll build it up in layers, because that’s how it should be built: a structured logger at the bottom, then the pipeline that carries its output to the places that matter, then the policies — redaction, noise control, privacy, environment gating — that decide what’s allowed through.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structured Logging Is Not “printf With Levels”
&lt;/h3&gt;

&lt;p&gt;Most logging in most apps is string concatenation with a severity attached:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;d&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"TAG"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"User $userId loaded $count plans in ${elapsed}ms"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That line is fine for a human reading Logcat at their desk. It is nearly useless everywhere else. You cannot query it. You cannot aggregate “loaded plans” events across ten thousand devices to get a p95. You cannot redact $userId without a regex that also mangles the rest. The moment the log leaves the developer's eyeballs and heads toward a backend, a file, or a crash report, its shape stops being prose and starts being &lt;em&gt;data&lt;/em&gt; — and prose-shaped data is the thing you will regret.&lt;/p&gt;

&lt;p&gt;Structured logging separates the stable message from the variable fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;attrs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"plan_count"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"duration_ms"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;elapsed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"plans loaded"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message "plans loaded" is now a low-cardinality key you can group by. plan_count and duration_ms are typed attributes you can chart. And — this matters more than it looks — the message is a &lt;em&gt;lambda&lt;/em&gt;. It is only ever built if the log level is actually enabled. Every senior codebase eventually finds the log line that dominates a hot path because someone wrote Log.d("state = ${expensiveSerialize(state)}") and shipped it; lazy message evaluation makes that mistake structurally impossible, not merely discouraged.&lt;/p&gt;

&lt;p&gt;For the Kotlin Multiplatform app in this story, the logger at the bottom of the stack is &lt;a href="https://github.com/shivathapaa/KMP-Logger" rel="noopener noreferrer"&gt;KMP-Logger&lt;/a&gt; a small library I maintain on Maven Central. I’m going to use it as the concrete example because I know every corner of it, but nothing here is specific to it — the ideas port to Timber, to SLF4J, to whatever you already have. Its model is the one you want from any structured logger:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// One-liner API for casual call sites - picks up a default tag.&lt;/span&gt;
&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;i&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;e&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"token refresh failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Named logger with structured attributes for the paths you'll query later.&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LoggerFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PlanScreen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;attrs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"retry_count"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"plan load failed"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Under the hood every call becomes a LogEvent — a level, a logger name (the tag), an optional message, a map of attributes, an ambient context, and an optional throwable. That object is the currency of the whole pipeline. Everything downstream — Logcat, the crash backend, the redactor — operates on LogEvent, not on a formatted string. Hold onto that; it's the hinge the entire design turns on.&lt;/p&gt;

&lt;p&gt;Two Kotlin Multiplatform details worth naming, because they’re the difference between a logger that works in commonMain and one that only pretends to. First, the native output is per-platform under one API: the same Log.i lands in Logcat on Android, NSLog on iOS, console on web, stdout on JVM/native — resolved through expect/actual, invisible to your shared code. Second, there is no reflection and no Throwable::class.java in the shared path, because neither exists on Kotlin/Native. A logger built Android-first quietly assumes the JVM; one built multiplatform-first cannot, and that constraint turns out to make it cleaner everywhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  One Pipe, Many Destinations — and the Insight Most Teams Miss
&lt;/h3&gt;

&lt;p&gt;Here is the architectural claim this whole article is organized around, and the one I’d defend hardest in a review:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logging and crash reporting are not two systems. They are one pipeline with two of its outputs wired to different backends.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Almost everyone builds them separately. There’s a logging call (Log.e(...)) and, somewhere else, a crash call (Crashlytics.recordException(...)), and the two drift. Logs have context the crash report lacks; crash reports fire on paths that never logged. You end up correlating them by timestamp at 2 a.m.&lt;/p&gt;

&lt;p&gt;The fix is a &lt;em&gt;sink&lt;/em&gt;. A structured logger’s output isn’t a destination, it’s a stream, and a sink is anything that consumes the stream:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;LogSink&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You install however many you want. In this app, three:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;LoggerFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;install&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;LoggerConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;minLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isDebug&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERBOSE&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DefaultLogSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Logcat / NSLog / stdout&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SanitizingSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;crashSink&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WARN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// crash backend&lt;/span&gt;
        &lt;span class="c1"&gt;// (debug builds also add a pretty console sink)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second sink is the payoff. It takes the &lt;em&gt;same&lt;/em&gt; LogEvent stream Logcat sees and routes it into the crash backend by severity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Below WARN:&lt;/strong&gt; dropped. The crash backend does not care that a screen loaded.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WARN and above:&lt;/strong&gt; written as a &lt;strong&gt;breadcrumb&lt;/strong&gt;  — a timestamped line appended to a rolling buffer that the crash backend replays, in order, on the next crash. This is the “medical history”: the trail of steps that led to the fall.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ERROR and above &lt;em&gt;with a throwable&lt;/em&gt;:&lt;/strong&gt; additionally recorded as a &lt;strong&gt;non-fatal&lt;/strong&gt;  — a handled exception, grouped and counted like a crash but without taking the process down, carrying the logger’s tag, message, and attributes as custom keys so the report knows its own identity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Write one log line. It shows up in Logcat for the developer at their desk, &lt;em&gt;and&lt;/em&gt; — if it’s serious enough — it becomes crash context for the incident you’ll debug next week, assembled automatically. You stop maintaining two mental models. The log you already write is the observability you were going to bolt on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CrashReporterLogSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CrashReporter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogSink&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;WARN&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"[${event.level}] ${event.loggerName}: ${event.message}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recordNonFatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;throwable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;keysFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CrashReporter here is a deliberately small interface — log, setKey, recordNonFatal, setUserId, setCollectionEnabled — that maps exactly onto a crash backend's customization surface without leaking the SDK into feature code. That indirection is what lets the same sink target Crashlytics today and something else tomorrow, and it's what lets the sink exist in commonMain at all. More on why that matters when we get to the platform boundary.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Observability Layer Is the One Layer Allowed to Fail Silently
&lt;/h3&gt;

&lt;p&gt;Read that sink again and notice what’s missing: it has no error handling. Now think about where it runs. emit fires on &lt;em&gt;whatever thread was trying to log&lt;/em&gt; — a background dispatcher, the main thread, a coroutine mid-flight. If recordNonFatal throws — a malformed key, an SDK in a bad state, an OOM at exactly the wrong moment — that exception surfaces on a thread whose only crime was trying to write a log line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your telemetry must never be the cause of the incident it exists to record.&lt;/strong&gt; So the real sink wraps its body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;WARN&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="n"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"[${event.level}] ${event.loggerName}: ${event.message}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// ... record non-fatal ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the inversion that separates observability code from feature code. Everywhere else in the app, a swallowed exception is a bug — you want failures loud. Here, a swallowed exception is the &lt;em&gt;contract&lt;/em&gt;. The logging layer is infrastructure the rest of the app leans on precisely because it can never push back. A logger that can crash the app is worse than no logger, because it fails in production, under load, on the exact devices you can’t reproduce.&lt;/p&gt;

&lt;p&gt;The same principle shows up one level down, in startup. The pipeline is installed in Application.onCreate — the single worst place in the entire app to throw, because a crash there crash-loops every launch and there is no screen from which the user can escape it. So the installation, the SDK bring-up, everything in that window is defensive by default. Infrastructure that runs before the app has a face has to assume it's the last line standing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Redaction Belongs in the Pipeline, Not at the Call Site
&lt;/h3&gt;

&lt;p&gt;Now the uncomfortable part. That crash sink transmits log payloads &lt;em&gt;off the device&lt;/em&gt;. And developers log everything — request bodies, headers, auth flows — especially the developer debugging the auth flow at 6 p.m. who logs the full Authorization header "just for a second" and forgets to delete it.&lt;/p&gt;

&lt;p&gt;You cannot solve this with a code-review rule. “Don’t log secrets” is a rule every team has and every team violates, because the violation is invisible until it’s a breach. The person who leaks the token is never the person who wrote the policy.&lt;/p&gt;

&lt;p&gt;So redaction moves into the pipeline, as a middleware sink that wraps the real one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SanitizingSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="k"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogSink&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;minLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERBOSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogSink&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;minLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="k"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scrubbed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// JWTs, Bearer tokens, password=…, otp=…, attr keys named "token"/"secret"/…&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The point isn’t the regexes — those are the boring, conservative part (three-segment base64 for JWTs, Bearer …, name=value for the usual sensitive names, plus attribute keys that &lt;em&gt;look&lt;/em&gt; sensitive get replaced wholesale). The point is &lt;em&gt;where it sits&lt;/em&gt;. Because it wraps the sink, it doesn't matter which of the app's forty modules produced the event, or whether that module's author had ever heard of the redaction policy. Every payload bound for a backend passes through one chokepoint. Redaction stops being a discipline and becomes a property of the wire.&lt;/p&gt;

&lt;p&gt;Two senior touches on top. It wraps &lt;em&gt;only&lt;/em&gt; the off-device sink — Logcat on a developer’s own machine doesn’t need scrubbing and shouldn’t pay for it. And it carries a minLevel gate that short-circuits &lt;em&gt;before&lt;/em&gt; the regex work: the crash sink ignores anything below WARN anyway, so scrubbing the INFO firehose would burn CPU on events that get dropped one layer later. Put the redactor where the secrets can escape, and make it free where they can't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Not Every Exception Is a Bug
&lt;/h3&gt;

&lt;p&gt;The first week the crash pipeline was live, the non-fatals dashboard lit up. Thousands of them. Almost all the same shape: a farmer’s phone lost signal mid-request, the HTTP call threw, the code logged it at ERROR with the throwable attached — and the sink dutifully filed it as a handled exception. UnknownHostException, SocketTimeoutException, connection reset, TLS handshake failure, over and over, from every corner of the app.&lt;/p&gt;

&lt;p&gt;None of it was a bug. &lt;strong&gt;An observability system that reports everything reports nothing&lt;/strong&gt;  — the signal you need drowns in the noise you can’t act on. A user in a valley with one bar is not an app defect. There is no fix. There is no code change. It is weather.&lt;/p&gt;

&lt;p&gt;So the pipeline filters transport failures out of the non-fatal stream — while still leaving them as breadcrumbs, because “the last three calls timed out” is genuinely useful context for whatever &lt;em&gt;does&lt;/em&gt; eventually crash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;throwable&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ordinal&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="n"&gt;throwable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasTransportFailure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// walks the cause chain, bounded, self-cause-guarded&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recordNonFatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;throwable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;keysFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interesting decision is &lt;em&gt;how&lt;/em&gt; you identify a transport failure across a multiplatform network stack, and it’s a small masterclass in the gap between what you’d like to match and what you can. The clean answer is “match these specific exception types.” You can’t. The timeout and stream-reset types inside a modern HTTP client are library-internal and get renamed by the release-mode optimizer, so neither an is SpecificType check nor a name match survives to production. The answer that &lt;em&gt;does&lt;/em&gt; survive is to match their common ancestor — on Android, every offline/DNS/reset/timeout/TLS failure is ultimately a java.io.IOException, one supertype, no dependency on the network library, obfuscation-proof. The trade-off is honest and worth stating out loud: a genuine local-disk IOException gets suppressed too. It's rare, it's usually environmental, the breadcrumb still fires — accepted. Good filters come with a footnote, and if yours doesn't, you haven't found its edge yet.&lt;/p&gt;

&lt;p&gt;On iOS the same predicate returns a flat false, because that platform's HTTP engine doesn't raise IOException and the network layer already demotes its transport errors to a warning at the source — so nothing reaches the filter to begin with. One expect/actual function, two honest platform-specific answers. Which is the whole texture of KMP: the &lt;em&gt;policy&lt;/em&gt; is shared, the &lt;em&gt;mechanism&lt;/em&gt; is not, and pretending otherwise is how you ship a filter that works on one platform and silently fails on the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make the Report Answer “Why,” Not “What”
&lt;/h3&gt;

&lt;p&gt;Back to the crash report I opened with — the one that told me nothing. The fix for that report is not more stack frames. It’s the two things a stack can never carry: &lt;strong&gt;the state the app was in, and the path the user took to get there.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Crash backends give you two instruments for exactly this, and most teams use neither:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom keys&lt;/strong&gt; are session-global key/values pinned to the top of every subsequent report. They answer &lt;em&gt;what state was the app in when it died.&lt;/em&gt; You set them once, centrally, from the app’s identity and navigation, and every crash that follows inherits them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"current_screen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;screenName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// updated on every navigation&lt;/span&gt;
&lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"is_logged_in"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userToken&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app_language"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// nepali / english&lt;/span&gt;
&lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app_environment"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// production / staging / local&lt;/span&gt;
&lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"district_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;districtId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// coarse region - never GPS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Breadcrumbs&lt;/strong&gt; are the timestamped, ordered trail — &lt;em&gt;the path.&lt;/em&gt; Wire them into one place, the navigation observer, and every screen the user visited replays in sequence on the next crash. One listener, a flat graph, total coverage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;navController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addOnDestinationChangedListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toScreenName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;@&lt;span class="n"&gt;listener&lt;/span&gt;
    &lt;span class="n"&gt;analyticsHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logScreenView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// analytics gets it&lt;/span&gt;
    &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"current_screen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// crash state gets it&lt;/span&gt;
    &lt;span class="n"&gt;crashReporter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"screen: $screen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// crash trail gets it&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that one navigation event feeds three consumers — analytics, crash state, crash trail — from a single centralized site. No screen calls this itself. Centralizing telemetry is not tidiness; it’s the only way it stays &lt;em&gt;complete&lt;/em&gt;, because the screen someone adds next month gets covered whether or not they remember to instrument it. The telemetry a developer has to remember to add is the telemetry that’s missing on the screen where it matters.&lt;/p&gt;

&lt;p&gt;And a genuinely non-obvious trap while we’re here: when you record a handled exception, the backend captures &lt;em&gt;the throwable’s own stack&lt;/em&gt; — where it was thrown — not where you called recordNonFatal. For a network error thrown deep in the HTTP client's dispatcher, that stack is all library frames, none of yours. You cannot tell the source from the stack. The only way to attribute it is the custom keys the sink attaches — the logger tag, the URL, the method. The keys aren't decoration; for a whole class of non-fatals they're the &lt;em&gt;only&lt;/em&gt; thing that says where the failure came from.&lt;/p&gt;

&lt;h3&gt;
  
  
  Privacy Is an Architecture Decision, Not a Review Comment
&lt;/h3&gt;

&lt;p&gt;Every method that reaches a backend in this app carries the same warning in its contract, and it is load-bearing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Never pass PII — phone numbers, names, free text, exact GPS, account numbers, amounts tied to identity — through any method here. Use coarse, derived signals only.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is not boilerplate. For this app it’s existential in a way western analytics guides rarely feel. The user is a farmer. Their phone number &lt;em&gt;is&lt;/em&gt; their identity, their login, and — in a country where mobile money rides on that number — a key to their money. An exact GPS coordinate isn’t a “location,” it’s &lt;em&gt;their house and their field&lt;/em&gt;. The cost of a leak here is not a GDPR fine; it’s a specific person, findable.&lt;/p&gt;

&lt;p&gt;So the privacy posture is built into the &lt;em&gt;shape&lt;/em&gt; of the telemetry, not left to whoever’s writing the event:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Coarse over exact, always.&lt;/strong&gt; district_id, not latitude/longitude. is_company, not the company name. A price bucket, not the amount. You cannot leak what you never collected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pseudonymous identity, one-way.&lt;/strong&gt; To stitch a user’s events across sessions you need a stable id, but it must never be the phone number. It’s a hash — computed on-device, one-directional by design, the raw number never leaving the phone. (Choose that hash carefully: a naive hash of a value from a small, guessable keyspace — like a national phone-number range — is reversible by brute force, which quietly turns your “pseudonymous” id back into PII. Salt it, or accept and document the risk deliberately; don’t stumble into it.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The interface is where the rule lives.&lt;/strong&gt; Because every call site depends on one small AnalyticsHelper / CrashReporter interface whose documented contract bans PII, the constraint is visible at the exact moment an engineer is about to log something. A rule in a wiki is a rule nobody reads. A rule in the KDoc of the method you're calling is a rule you can't miss.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The general principle: minimize at the source. Every downstream policy — retention, access control, redaction — is a mitigation for data you chose to collect. The data you decline to collect needs no policy at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  Free in Development, Silent in Production
&lt;/h3&gt;

&lt;p&gt;There’s a tension in everything above. In development you want the full firehose — every event, pretty-printed, in your face. In production you want almost none of it leaving the device, and what does leave sanitized and gated. The same code has to do both.&lt;/p&gt;

&lt;p&gt;The pattern is a debug/release swap at the dependency-injection seam. Every observability abstraction has a real implementation and a no-op (or log-only) one, and the container picks by build type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AnalyticsHelper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isDebug&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;StubAnalyticsHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;FirebaseAnalyticsHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CrashReporter&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isDebug&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;StubCrashReporter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;FirebaseCrashReporter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PerformanceTracer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isDebug&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;NoOpPerformanceTracer&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;FirebasePerformanceTracer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In debug, every call is real code that goes nowhere near a backend — the stub logs to the console so you can &lt;em&gt;see&lt;/em&gt; what would have been sent, and nothing ships. In release, the SDK-backed implementations report. Feature code is identical in both; it depends on the interface and never learns which body it got. This is belt-and-suspenders with a second buckle: the SDKs’ own collection flags are &lt;em&gt;also&lt;/em&gt; gated off in debug at the manifest/startup level, so even the automatic events (app-start traces, screen renders) stay quiet. Two independent guards, because “we didn’t mean to send that from a debug build” is a mistake you get to make exactly once before it’s real users’ data.&lt;/p&gt;

&lt;p&gt;A subtle bonus: because the debug path is &lt;em&gt;real objects&lt;/em&gt;, not null and not a compile-time strip, the observability code runs in every test and every developer session. It can't rot. The no-op that never executes is the no-op that's silently broken when you finally need it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Traces: Measure What the SDK Can’t See — and Don’t Double-Count What It Can
&lt;/h3&gt;

&lt;p&gt;The third pillar is performance, and here the senior move is mostly about &lt;em&gt;restraint&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Modern performance SDKs auto-instrument a lot for free: app start, foreground/background, screen rendering, and — critically — network requests, because the HTTP client runs over the platform’s native stack (OkHttp on Android, NSURLSession on iOS) which the SDK already hooks. The rookie instinct is to wrap every network call in a custom trace. Don't. &lt;strong&gt;You'll double-count what the SDK already measures, and now you have two numbers that disagree and no idea which to trust.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Custom traces earn their place only for spans the SDK &lt;em&gt;cannot&lt;/em&gt; see — app-level operations that span several calls plus local work: a feed assembling from three sources, a plan list loading and rendering, a chat message with an image and audio payload being prepared and sent. Those are real user-perceived durations that no automatic hook captures.&lt;/p&gt;

&lt;p&gt;And when you do trace, add the dimension that makes the number mean something:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;performanceTracer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"plan_load"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isSuccess&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"success"&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"empty"&lt;/span&gt; &lt;span class="c1"&gt;// no plans in this category - normal, NOT an error&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"error"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putMetric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"plan_count"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That empty bucket is the whole lesson. An empty result is a &lt;em&gt;normal&lt;/em&gt; outcome — this category has no plans yet — but the naive two-way success/error split files it under error, and now your error rate is fiction and your alerting cries wolf. The distinction between "failed" and "succeeded with nothing to show" is invisible to any automatic instrument and invisible to a boolean; you have to name it. A trace's segments are only as honest as the person who bucketed them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Part That’s Actually Hard: Two Platforms, One Pipeline
&lt;/h3&gt;

&lt;p&gt;Everything so far would be true on a single-platform app. Kotlin Multiplatform adds one constraint that reshapes the whole design: &lt;strong&gt;the shared code in&lt;/strong&gt;  &lt;strong&gt;commonMain cannot see a platform SDK.&lt;/strong&gt; No Firebase types, no android.util.Log, no NSError. The pipeline lives in shared code; the backends live on the platforms. Something has to cross that gap without dragging the SDK across with it.&lt;/p&gt;

&lt;p&gt;Three moves make it work, and they’re the transferable part for anyone doing native-SDK work in KMP:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The SDK type never crosses the boundary — a handle does.&lt;/strong&gt; A performance trace is a live SDK object with a lifecycle. It must not leak into Kotlin. So the Swift side owns the real Trace objects in a map and hands Kotlin an opaque token string; Kotlin passes the token back on putAttribute/stop, Swift resolves it to the real trace. Kotlin holds a String; the SDK object stays on its own side of the wall, its type never named in shared code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Impedance mismatches get translated at the edge.&lt;/strong&gt; A crash backend cannot ingest a Kotlin Throwable. So the bridge reconstitutes it as a native error — domain set to the Kotlin class name (so issues still group correctly), the message as the description, the Kotlin/Native stack tucked into the error's userInfo. A handled KMP failure surfaces as a grouped non-fatal with its real stack attached, even though the crash SDK has never heard of Kotlin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Watch the module graph, or it’ll bite you.&lt;/strong&gt; The logging module cannot depend on the analytics/crash module — but the crash &lt;em&gt;sink&lt;/em&gt; needs the crash reporter. If you wire the sink inside the logger, you get a dependency cycle, and on iOS the added twist that the crash sink needs a native bridge that lives in the analytics module, not the logging one. The resolution is dependency inversion at the seam: the logger’s installer accepts a sink from &lt;em&gt;outside&lt;/em&gt; — install(isDebug, crashSink) — and startup, which sits above both modules and can see everything, constructs the crash sink and injects it, release builds only. The logging module stays SDK-agnostic and reusable; the wiring happens at the one place that's allowed to know about both. When two low-level modules need to cooperate, the cooperation usually belongs to a third module above them, not to a new edge between them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What We’ve Built
&lt;/h3&gt;

&lt;p&gt;Step back and the whole thing is one pipeline with policies bolted to it at the right layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;structured logger&lt;/strong&gt; (&lt;a href="https://github.com/shivathapaa/KMP-Logger" rel="noopener noreferrer"&gt;KMP-Logger&lt;/a&gt; at the base — typed events, lazy messages, per-tag levels, native output on every platform — so the app logs &lt;em&gt;data&lt;/em&gt;, not prose.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One stream, many sinks:&lt;/strong&gt; Logcat for the developer, a crash sink for incidents, a performance tracer for durations. The log you write once becomes the crash context you read later, assembled for free.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A sink that can’t crash the app,&lt;/strong&gt; because the observability layer is the one layer whose failures must be silent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redaction in the pipeline,&lt;/strong&gt; not at the call site, so no future engineer can leak a token by forgetting a rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A non-fatal filter&lt;/strong&gt; that drops the weather and keeps the bugs, with its trade-off written down.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crash reports that answer “why”:&lt;/strong&gt; centralized custom keys for the state, breadcrumbs for the path, keys-as-attribution for the stacks that lie.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Privacy in the shape of the data&lt;/strong&gt;  — coarse, derived, pseudonymous — because the cheapest data to protect is the data you never collected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A debug/release swap&lt;/strong&gt; that’s a firehose in development and silent in production, guarded twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traces for what the SDK can’t see,&lt;/strong&gt; with an honest success/empty/error dimension, and the discipline not to double-count what it can.&lt;/li&gt;
&lt;li&gt;All of it &lt;strong&gt;shared across Android and iOS&lt;/strong&gt; , with the SDK objects held firmly on their own side of the cinterop wall.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these are exotic. Each is one small decision made at the layer where it belongs. That’s the actual thesis: &lt;strong&gt;observability is not a product you install, it’s a set of boundaries you choose&lt;/strong&gt;  — what fails silently, what gets scrubbed, what gets dropped, what never gets collected. The SDK gives you a pipe. Where you put the valves is the engineering.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next
&lt;/h3&gt;

&lt;p&gt;A few edges I’m still working, in case you’re ahead of me on any: sampling high-volume breadcrumbs on the very lowest-memory devices, where even the rolling buffer has a cost; a structured-event schema shared between the analytics catalog and the crash keys so the two vocabularies can’t drift; and richer offline buffering, since a meaningful share of our users are disconnected at the exact moment something interesting happens and the report has to survive until they reconnect.&lt;/p&gt;

&lt;p&gt;If you’re building observability for a multiplatform app — or arguing with someone about whether logs and crashes are one system or two — I’d genuinely like to hear how you drew the lines. And if you want the structured-logging layer underneath all of this, &lt;a href="https://github.com/shivathapaa/KMP-Logger" rel="noopener noreferrer"&gt;KMP-Logger&lt;/a&gt; is open source and on Maven Central. Tell me what breaks. ❤️&lt;/p&gt;

</description>
      <category>logging</category>
      <category>kotlinmultiplatform</category>
      <category>iosdevelopment</category>
      <category>androidappdevelopmen</category>
    </item>
    <item>
      <title>The 7% You Can’t Share</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Tue, 04 Aug 2026 09:51:46 +0000</pubDate>
      <link>https://dev.to/shivathapaa/the-7-you-cant-share-556g</link>
      <guid>https://dev.to/shivathapaa/the-7-you-cant-share-556g</guid>
      <description>&lt;h4&gt;
  
  
  A Compose Multiplatform app can share 93% of its code and still meet the platform in two places nothing abstracts away — native vendor SDKs, and the physical device. The finale is about engineering that edge so it fails loudly instead of rotting in silence.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2jnsr18cv65qqw8msml8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2jnsr18cv65qqw8msml8.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Production Compose Multiplatform — Part 5 of 5, the finale.&lt;/em&gt; &lt;a href="https://medium.com/@shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-6c2f43011939?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;&lt;em&gt;Part 1 — Liquid Glass&lt;/em&gt;&lt;/a&gt; &lt;em&gt;·&lt;/em&gt; &lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;&lt;em&gt;Part 2 — the case study&lt;/em&gt;&lt;/a&gt; &lt;em&gt;·&lt;/em&gt; &lt;a href="https://medium.com/@shivathapaa/inside-a-93-shared-codebase-the-architecture-of-a-production-compose-multiplatform-app-fecf7b58246e?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;&lt;em&gt;Part 4 — the architecture tour.&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The number that sells a multiplatform rebuild is the shared-code percentage. Ours is 93% — measured, both codebases, counted in &lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt;. It’s the figure I’d lead a conference talk with, because it’s true and it’s rare.&lt;/p&gt;

&lt;p&gt;It’s also not where the engineering is.&lt;/p&gt;

&lt;p&gt;The 93% is the part Compose Multiplatform gives you: write a screen once, it renders on both platforms. The interesting work — the work that decides whether the app is a demo or a product — lives in the other 7%. And that 7% is not scattered leftovers. It clusters at exactly two boundaries, the two places a shared codebase still has to touch something it cannot abstract:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The vendor boundary&lt;/strong&gt;  — native SDKs (Firebase, Google Maps, a payment provider) that ship no Kotlin bindings and have to be reached from shared code that must not know the difference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The device boundary&lt;/strong&gt;  — the physical phone the app actually runs on, whose first-launch performance no amount of shared code improves, and which on our median device is a four-year-old budget Android.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both are irreducible. And both share a failure mode that makes them dangerous out of proportion to their size: &lt;strong&gt;they degrade in silence.&lt;/strong&gt; A vendor bridge keeps compiling long after its two sides have drifted apart. A performance win keeps “feeling fast” on your flagship long after it’s decayed on the phone your users own. Neither shows up in a green build.&lt;/p&gt;

&lt;p&gt;This finale is about the one discipline that beats that, applied twice: make the seam narrow, and give it a &lt;em&gt;tripwire&lt;/em&gt; — a failure loud enough that drift becomes a build error or a benchmark regression instead of a bug report six months late.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Vendor Boundary: Two Ways to Bridge a Native SDK
&lt;/h3&gt;

&lt;p&gt;Compose Multiplatform gives you shared UI. It does not give you shared vendors. Each native SDK arrives as a platform library with no iOS-side Kotlin bindings, and each has to be reachable from commonMain. The Android half is never the problem — the SDKs are Kotlin/Java-native, an injected implementation or an expect/actual seam covers it. The iOS half is where the real decision lives. In our codebase it resolved two different ways, and the contrast &lt;em&gt;is&lt;/em&gt; the lesson.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategy 1 — header-only cinterop, when Kotlin is the caller
&lt;/h3&gt;

&lt;p&gt;Firebase’s iOS SDK is Swift-first, ships through Swift Package Manager, and has no Kotlin bindings. The path of least resistance is to pull the whole SDK into the Kotlin framework through a CocoaPods integration and generate bindings against it.&lt;/p&gt;

&lt;p&gt;We didn’t — for one reason that generalizes to every SDK you’ll ever bridge: &lt;strong&gt;that couples your Kotlin build to the entire vendor dependency graph&lt;/strong&gt;  — its version, its transitive weight, its update cadence, its next CVE — for the sake of a handful of calls.&lt;/p&gt;

&lt;p&gt;Instead, the Kotlin side binds a small header we wrote ourselves. It declares the surface Kotlin is allowed to see — nothing more:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight objective_c"&gt;&lt;code&gt;&lt;span class="nd"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;FirebaseAnalyticsService&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NSObject&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FirebaseAnalyticsService&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;logEvent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NSDictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;setUserId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;setUserProperty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;forName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;setAnalyticsCollectionEnabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s one of four such blocks — analytics, messaging, crash reporting, performance — in a single 62+ line header. Swift &lt;em&gt;implements&lt;/em&gt; each declared class with @objc(FirebaseAnalyticsService) annotations, and the &lt;strong&gt;Swift&lt;/strong&gt; compiler, not the Kotlin one, resolves the real Firebase SDK from SPM.&lt;/p&gt;

&lt;p&gt;The payoff is the whole reason to do it this way: &lt;strong&gt;the compiled Kotlin library carries no Firebase dependency.&lt;/strong&gt; Kotlin calls FirebaseAnalyticsService.shared() as if it were any other object, and what stands behind that selector is entirely Swift's business. Bump Firebase's version, patch a Firebase CVE, or swap Firebase for something else — the Kotlin build doesn't recompile, because nothing it depends on changed. The blast radius of a vendor change stops at the Swift file. In a codebase whose thesis is &lt;em&gt;shared&lt;/em&gt; code, that isolation is worth more than the few lines it costs.&lt;/p&gt;

&lt;p&gt;And here is the tripwire. This strategy has exactly one discipline: &lt;strong&gt;the Swift selectors must match the header, exactly.&lt;/strong&gt; Get one wrong and it doesn’t misbehave at runtime on one platform — it fails to link. The drift becomes a build error, caught on the machine that introduced it, not a null that surfaces in a farmer’s hands. A boundary whose only failure mode is a compile error is a boundary you can trust. (Full .def and multi-module mechanics are in &lt;a href="https://medium.com/@shivathapaa/bridging-swift-to-kotlin-multiplatform-a-complete-guide-to-cinterop-in-multi-module-projects-af54d88f4b4f" rel="noopener noreferrer"&gt;my CInterop guide&lt;/a&gt;; this piece is about &lt;em&gt;when&lt;/em&gt;, not &lt;em&gt;how&lt;/em&gt;.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategy 2 — a Kotlin interface implemented in Swift, when the SDK calls back
&lt;/h3&gt;

&lt;p&gt;Then Google Maps refused to play that game.&lt;/p&gt;

&lt;p&gt;The Maps SDK doesn’t want to receive fire-and-forget calls — it wants to &lt;em&gt;hand you things&lt;/em&gt;: a map view to embed, camera callbacks, lifecycle obligations. A C-shaped “call this selector” binding is the wrong tool when the SDK’s real surface is object lifecycle plus a platform view.&lt;/p&gt;

&lt;p&gt;So the bridge inverts. iosMain defines a plain Kotlin interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;GoogleMapBridge&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;GoogleMapHandle&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;GoogleMapHandle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setCamera&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lng&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zoom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setMarker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lng&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;cameraTargetLat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="c1"&gt;// camera state read back out&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;cameraZoom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;dispose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Swift implements both, wrapping GMSMapView, and injects the implementation at the one moment both worlds sit in a single hand — when Swift constructs the Compose host:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;MainViewControllerKt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MainViewController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;mapBridge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IosGoogleMapBridge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;placesBridge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IosGooglePlacesBridge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The shared map composable hosts the returned view through UIKitView and drives it through the handle. No header, no cinterop at all — the Kotlin/Native compiler already knows how to let Swift implement a Kotlin interface; we just hand the object across.&lt;/p&gt;

&lt;p&gt;This inversion buys something cinterop can’t: &lt;strong&gt;testability.&lt;/strong&gt; A Kotlin interface has a fake. Shared code that depends on GoogleMapBridge can be exercised in commonTest against a no-op or a scripted implementation, on any machine, no device, no SDK. The cinterop surface has no such seam — it's only real on a linked iOS binary. When the SDK sits behind an interface you defined, the SDK stops being a thing your tests have to reach.&lt;/p&gt;

&lt;h3&gt;
  
  
  The rule, and the corollaries that matter
&lt;/h3&gt;

&lt;p&gt;One codebase, two strategies. After shipping both, the rule is clean:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Kotlin is the caller and the SDK’s surface is stable and Objective-C-representable?&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;Header-only cinterop. Keep the SDK out of your Kotlin library entirely.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The SDK hands you views, delegates, or lifecycle callbacks?&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;Define a Kotlin interface, implement it in Swift, inject at startup.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Shorter: &lt;strong&gt;cinterop when Kotlin calls; interface-injection when the SDK calls back.&lt;/strong&gt; Firebase is fire-and-forget — cinterop. Maps is a conversation with a view in the middle — injection.&lt;/p&gt;

&lt;p&gt;Three corollaries earned in production, the kind nobody writes on the box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Don’t reach for a bridge first.&lt;/strong&gt; Most iOS platform APIs need neither strategy: Kotlin/Native’s built-in platform.* bindings already cover LocalAuthentication, Security, CoreLocation, AVFoundation, WebKit. Across 46+ modules there is a &lt;em&gt;single&lt;/em&gt; cinterop binding, wired in one module. Bridges are for &lt;em&gt;vendor&lt;/em&gt; SDKs, not the OS — if you're writing a header to reach something Apple ships, check for the platform.* binding first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep every dependency where its package manager lives.&lt;/strong&gt; Firebase stays in SPM. Maps stays in SPM. The payment SDK stays an .xcframework behind a Swift factory on iOS and an .aar on Android. The Kotlin build never becomes the place iOS dependencies are managed — that job already has a tool, and taking it over is how KMP builds turn slow and brittle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mind what you own across the line.&lt;/strong&gt; The object you hand across is retained by the other runtime; writes that land in Compose snapshot state must happen on the main thread (UIKit already obliges); and an exception thrown Swift-side is not a Kotlin Throwable until you make it one. The bridge is narrow on purpose, but "narrow" is not "free" — every method on it is a small contract about threading and ownership.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last pattern — a Kotlin interface a native runtime implements, with state crossing back into Compose — turned out to be the most general tool in the whole app. Its furthest use isn’t an SDK at all: it’s the iOS 26 Liquid Glass UITabBar from &lt;a href="https://medium.com/@shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-6c2f43011939?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt;, a Swift ObservableObject implementing a Kotlin interface, writing its height back into Compose snapshot state. The bridge you build for Maps is the bridge you'll reuse for everything the platform owns and Kotlin can't hold.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Device Boundary: Performance Born on the Phone Your Users Own
&lt;/h3&gt;

&lt;p&gt;The second place shared code runs out is not a vendor — it’s the metal. There’s a moment when performance work stops being abstract: the first cold start of your app on the cheapest phone your users actually own. Not the emulator. Not your flagship. The four-year-old budget device with the storage nearly full.&lt;/p&gt;

&lt;p&gt;GeoKrishi Farm serves farmers across Nepal, and that phone &lt;em&gt;is&lt;/em&gt; our median device — minSdk 24 is a deliberate choice, not an oversight. Compose Multiplatform runs beautifully on it, after the first launch. But the &lt;em&gt;first&lt;/em&gt; launch is where a JIT-compiled UI framework pays its tax: ART interprets and JIT-compiles the hot paths — all of Compose's — as they first run. On low-end hardware, that tax is very visible, and it lands on the worst possible moment, the one where a new user decides whether the app is worth keeping.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Baseline Profile&lt;/strong&gt; is the fix: a list of methods and classes, shipped inside the app, that ART ahead-of-time compiles at install time, so the first launch skips interpretation for everything on the list. A &lt;strong&gt;Startup Profile&lt;/strong&gt; additionally informs how that code is laid out in the DEX. Google documents the effect at roughly &lt;strong&gt;30% faster code execution from first launch&lt;/strong&gt; , and the startup profile at around &lt;strong&gt;another 15%&lt;/strong&gt;. We ship both. Turning them on is trivial; the engineering is in two decisions the tutorials skip — &lt;em&gt;what goes in the profile&lt;/em&gt;, and &lt;em&gt;how you keep it honest.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The journey is the profile
&lt;/h3&gt;

&lt;p&gt;A profile is only as good as the code paths its generator exercises. Ours drives the app’s genuine first session, in order — dismiss onboarding, scroll the public feed with real flings so image decode is captured, reach login, visit all five bottom-nav tabs — and one line of it is the whole philosophy in miniature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// at the login step, the generator waits for the title a real user sees&lt;/span&gt;
&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Until&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"आफ्नो खातामा लग इन गर्नुहोस्"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It asserts on the &lt;strong&gt;Nepali&lt;/strong&gt; login title. The profile should capture the app that ships, and our users see Nepali; profiling an English debug variant would ahead-of-time-compile a UI that never runs. includeInStartupProfile = true means this single journey feeds &lt;em&gt;both&lt;/em&gt; profiles — the code that matters for startup is, definitionally, the code the startup journey touches.&lt;/p&gt;

&lt;p&gt;And the generator runs on a &lt;strong&gt;physically connected budget device&lt;/strong&gt; , not a Gradle-managed emulator. A profile is a bet about which code paths matter; place the bet on hardware that resembles the audience. An emulator on a development machine has never once behaved like a four-year-old phone with a full disk.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shipping it, and the trap that eats an afternoon
&lt;/h3&gt;

&lt;p&gt;The generator writes a 64,265+ line profile into the release sources; the app module consumes it and adds profileinstaller, which applies the packaged profile on first run — before Play's cloud-distilled profile has arrived, which is exactly when a new install is most fragile.&lt;/p&gt;

&lt;p&gt;Here’s the senior half nobody warns you about: &lt;strong&gt;verify the profile actually installed.&lt;/strong&gt; A baseline profile that silently failed to apply is indistinguishable from a working one until you measure — same app, same green build, no AOT benefit, no error. A performance optimization you didn't verify is a performance optimization you don't have.&lt;/p&gt;

&lt;h3&gt;
  
  
  The harness is the tripwire
&lt;/h3&gt;

&lt;p&gt;A profile is not a one-time win; it’s a claim that decays. Every release moves code, and a profile describing last quarter’s hot paths slowly stops describing yours. So next to the generator lives a macrobenchmark — the device-boundary equivalent of the compile error that guards the vendor boundary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;startupCompilationNone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;benchmark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CompilationMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Test&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;startupCompilationBaselineProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nf"&gt;benchmark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CompilationMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Partial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BaselineProfileMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Require&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// StartupTimingMetric, COLD, 10 iterations each&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details in there are load-bearing. BaselineProfileMode.Require — not Enable — means the benchmark &lt;em&gt;fails&lt;/em&gt; if the profile is missing, rather than quietly measuring an uncompiled build and reporting a flattering non-result. And ten cold iterations, compared by &lt;strong&gt;median&lt;/strong&gt; , because a single cold start is noise and a mean is hostage to one slow outlier. The delta between the two modes &lt;em&gt;is&lt;/em&gt; the profile's value, measured — and re-measurable on demand, on the same class of device the profile was born on. On our reference device, the current medians are &lt;strong&gt;1,518 ms without the profile and 1,108 ms with it&lt;/strong&gt;  — a 410ms cold-start win, about 27%, on the hardware that needs it most.&lt;/p&gt;

&lt;p&gt;Two honest limits, because a finale that only sells is a finale you shouldn’t trust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The number is time-to-initial-display, not time-to-full-display.&lt;/strong&gt; We measure the first frame; we don’t yet call reportFullyDrawn(), so the async settling &lt;em&gt;after&lt;/em&gt; first paint — the feed's images resolving, the first data landing — is outside the measurement. The number is real and it's the right first metric, but it's honest about the frame and blind to what fills it. reportFullyDrawn() is the next instrument, not a box already ticked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A profile compiles your startup code; it doesn’t remove it.&lt;/strong&gt; AOT can’t help a cold start that’s slow because Application.onCreate is doing too much — SDK initialisation, strict-mode setup, dependency-graph construction all land there, on the main thread, before the first frame. The profile makes that work run compiled instead of interpreted; doing &lt;em&gt;less&lt;/em&gt; of it is a separate discipline the profile can't substitute for. Measure before you assume the profile is the whole answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Through-Line: Build a Tripwire, Not a Rule
&lt;/h3&gt;

&lt;p&gt;Step back and the two halves of this piece are the same move made twice.&lt;/p&gt;

&lt;p&gt;The vendor boundary and the device boundary are where a multiplatform app quietly stops being what its shared-code percentage claims. A bridge drifts and keeps compiling. A profile decays and keeps feeling fast on the wrong phone. In both cases the danger isn’t the difficulty — it’s the &lt;em&gt;silence&lt;/em&gt;. Neither failure raises its hand.&lt;/p&gt;

&lt;p&gt;You don’t fix silent decay with discipline, because discipline doesn’t survive deadlines and new teammates. You fix it with a &lt;strong&gt;loud failure wired into the seam itself&lt;/strong&gt; : on the vendor side, a selector mismatch that won’t link; on the device side, a benchmark whose delta collapses when the profile drifts and a Require mode that refuses to measure a build without one. The rule you have to remember is worth nothing at 6pm on a Friday. The build error and the red benchmark remember for you.&lt;/p&gt;

&lt;p&gt;That’s the shape of every durable decision in this series, now that I line them up: the module boundary that makes a forbidden import &lt;em&gt;fail to resolve&lt;/em&gt; rather than merely be discouraged; the redaction that lives in the logging pipeline so no future caller &lt;em&gt;can&lt;/em&gt; leak a token; the Liquid Glass shell that reverts by deleting one if-branch. Good architecture isn't a set of rules people follow. It's a set of mistakes the system won't let them make.&lt;/p&gt;

&lt;h3&gt;
  
  
  What We’ve Built — the Series, Closed
&lt;/h3&gt;

&lt;p&gt;Five parts, one production app, live on both stores, serving farmers across Nepal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A real iOS 26 UITabBar rendering Apple's Liquid Glass over an unchanged shared Compose host (&lt;a href="https://medium.com/@shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-6c2f43011939?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt;) — native chrome, shared behavior, no forked navigation.&lt;/li&gt;
&lt;li&gt;A KMP app that shared 848 lines with iOS, rebuilt to 93% shared — every number counted from both codebases (&lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;The rewrite shipped as an ordinary store update — credential migration under R8 obfuscation, a new encrypted cache layer, deep-link archaeology.(&lt;a href="https://medium.com/@shivathapaa/shipping-a-full-rewrite-as-an-ordinary-app-update-698a963d5509?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;Part 3&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;The 46-module architecture, with boundaries the compiler enforces and a dependency graph rendered straight from the build (&lt;a href="https://medium.com/@shivathapaa/inside-a-93-shared-codebase-the-architecture-of-a-production-compose-multiplatform-app-fecf7b58246e?sharedUserId=shivathapaa" rel="noopener noreferrer"&gt;Part 4)&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The 7% that can’t be shared — native-SDK bridges chosen by who calls whom, and baseline profiles born on the hardware our users actually own — each with a tripwire against silent decay.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The thread through all five was never “how to share more code.” It was &lt;em&gt;where to draw the boundary, and how to make the boundary defend itself.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next
&lt;/h3&gt;

&lt;p&gt;The practice continues — coverage growing over the framework-free domain layers, reportFullyDrawn() to close the gap between first frame and first &lt;em&gt;useful&lt;/em&gt; frame, the release pipeline hardening, and a quarterly re-run of every number this series quoted. If the numbers move interestingly, you'll read about it.&lt;/p&gt;

&lt;p&gt;If you ship to low-end devices and profile on flagships — run one cold start on the phone your median user owns, and confirm the profile actually installed while you’re there. Then come tell me what you found, in the comments.&lt;/p&gt;

&lt;p&gt;Drop a ❤️ if this series was useful — it helps more developers find it.&lt;/p&gt;

</description>
      <category>androidappdevelopmen</category>
      <category>composemultiplatform</category>
      <category>performance</category>
      <category>kotlinmultiplatform</category>
    </item>
    <item>
      <title>Inside a 93% Shared Codebase: The Architecture of a Production Compose Multiplatform App</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sun, 26 Jul 2026 20:46:01 +0000</pubDate>
      <link>https://dev.to/shivathapaa/inside-a-93-shared-codebase-the-architecture-of-a-production-compose-multiplatform-app-45ii</link>
      <guid>https://dev.to/shivathapaa/inside-a-93-shared-codebase-the-architecture-of-a-production-compose-multiplatform-app-45ii</guid>
      <description>&lt;h4&gt;
  
  
  46+ modules, 37+ expect/actual seams, and the dependency graph rendered straight from the build.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxk64l5cqleeniq12gyqp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxk64l5cqleeniq12gyqp.gif" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Production Compose Multiplatform — Part 4 of 5.&lt;/em&gt; &lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f" rel="noopener noreferrer"&gt;&lt;em&gt;Part 2 — the case study&lt;/em&gt;&lt;/a&gt;&lt;em&gt;·&lt;/em&gt; &lt;a href="https://medium.com/@shivathapaa/shipping-a-full-rewrite-as-an-ordinary-app-update-698a963d5509" rel="noopener noreferrer"&gt;&lt;em&gt;Part 3 — the invisible store swap&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There’s a moment in every KMP project where you stop asking whether code &lt;em&gt;can&lt;/em&gt; be shared and start asking what genuinely can’t be. Getting to that question took us one full rewrite (&lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt; has the before/after numbers). Answering it produced a codebase where 93% of 128,801+ lines live in commonMain — every screen included — and the 7% that doesn't is an enumerable list I can walk you through.&lt;/p&gt;

&lt;p&gt;This part is the architecture tour of GeoKrishi Farm as it runs in production today: the module structure, the DI seams, and the platform-bridge patterns for analytics, crash reporting, push, and biometrics. I’ve written before about individual layers — &lt;a href="https://medium.com/@shivathapaa/kmp-networking-in-production-part-1-setting-up-ktor-the-right-way-91e7fdfd3c5b" rel="noopener noreferrer"&gt;the Ktor networking stack,&lt;/a&gt; &lt;a href="https://medium.com/@shivathapaa/convention-plugins-in-kotlin-multiplatform-the-complete-production-guide-cf61a109a73b" rel="noopener noreferrer"&gt;convention plugins&lt;/a&gt;, &lt;a href="https://medium.com/@shivathapaa/debugging-kotlin-multiplatform-apps-with-better-observability-b285d9dc0754" rel="noopener noreferrer"&gt;observability &lt;/a&gt;— so this piece is about how the whole thing hangs together.&lt;/p&gt;

&lt;h3&gt;
  
  
  46+ Modules, Two Kinds
&lt;/h3&gt;

&lt;p&gt;The project is 46+ Gradle modules: 12 core modules, 31+ feature modules, two app shells, and a baseline-profile module, with a build-logic composite build holding two convention plugins (one for plain KMP libraries, one that layers Compose on top).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;core/ resources, cache, models, logger, analytics/firebase,
               network, platformspecific, utils, ui, appui, permission, video
feature/ login, signup, biometric, weather, farmland, plan, market,
               diary, calculators, notification, settings, ... (31 total,
               some nested: home/{private,public,shared})
composeApp navigation graph, Koin aggregation, App() root
androidApp 5 files: Activity, Application, FCM service, in-app updates
iosApp 8 Swift files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three rules make the structure hold instead of decaying:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Features never depend on features.&lt;/strong&gt; A feature module may depend on core:*, never on a sibling. Cross-feature flows happen through navigation, not imports. However there are some constraints to this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain layers are framework-free.&lt;/strong&gt; No Ktor, no Compose, no platform types inside domain/. Repositories are interfaces there; implementations live in data/.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;expect declarations live in core modules, organized by concern&lt;/strong&gt;  — not scattered wherever someone first needed a platform API. The pragmatic exception: a platform-split &lt;em&gt;composable&lt;/em&gt; that only one feature uses (a map component, a WebView wrapper) stays inside that feature.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of these rules is novel. What’s novel, in my experience, is that module boundaries &lt;em&gt;enforce&lt;/em&gt; them. Convention doesn’t survive deadlines. implementation(projects.core.network) does.&lt;/p&gt;

&lt;p&gt;And you don’t have to take my word for the graph’s shape. The dependency report published alongside this article was generated straight from the build by &lt;a href="https://github.com/shivathapaa/Aalekh" rel="noopener noreferrer"&gt;Aalekh &lt;/a&gt;— the architecture-visualization plugin I built after getting tired of reverse-engineering my own architecture. The premise there applies double here: the real architecture of a multi-module project lives in the Gradle dependency graph, not in any diagram or doc. Diagrams drift. Builds don’t.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the 7% Actually Buys
&lt;/h3&gt;

&lt;p&gt;The platform code is 37+ expect declarations plus the two app shells. Grouped by concern — the shared abstraction first, then how each platform fulfills it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Analytics &amp;amp; push&lt;/strong&gt;  — shared service objects behind an expect/actual seam; the Firebase SDK directly on Android, a Swift @objc bridge on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crash reporting&lt;/strong&gt;  — a sink on the shared logger, wired to Crashlytics (NDK) on Android.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Biometrics&lt;/strong&gt;  — a shared authenticator + credential vault; BiometricPrompt + Keystore on Android, LAContext + Keychain on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secure storage&lt;/strong&gt;  — one preference-storage abstraction; KVault over the Keystore on Android, over the Keychain on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maps &amp;amp; Places&lt;/strong&gt;  — a shared composable + bridge interface; the Maps Compose SDK on Android, a Swift-implemented Kotlin interface on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Media&lt;/strong&gt;  — shared audio and video players; ExoPlayer and a YouTube view on Android, AVPlayer and a WKWebView shim on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device&lt;/strong&gt;  — camera, gallery, contacts, location, share, dial; ActivityResult contracts + FusedLocation on Android, UIImagePickerController + CLLocationManager on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Payment&lt;/strong&gt;  — a shared launcher seam; the payment service provider SDK as an .aar on Android and an .xcframework behind a Swift factory on iOS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DI&lt;/strong&gt;  — an expect val platformModule per core module; context-carrying registrations on Android, context-free on iOS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last item is the quiet backbone. Every platform-touching core module ships its own expect val platformModule, and the app aggregates them: Android boots Koin in the Application with an Android context; iOS boots it idempotently inside the Compose host — right after wiping stale Keychain state (&lt;a href="https://medium.com/@shivathapaa/shipping-a-full-rewrite-as-an-ordinary-app-update-698a963d5509" rel="noopener noreferrer"&gt;Part 3&lt;/a&gt;explains why that wipe exists). Same graph, two doorways in.&lt;/p&gt;

&lt;p&gt;One thing is missing from that list because it isn’t platform &lt;em&gt;code&lt;/em&gt; so much as platform &lt;em&gt;chrome&lt;/em&gt;: on iOS 26 the bottom navigation is a native UITabBar overlaid on this same shared container, switched on by a single CompositionLocal that defaults to off, so the shared code renders its normal bottom bar everywhere else and never learns the native bar exists. It's the sharpest test of whether these seams actually hold, and it opened this series in &lt;a href="https://medium.com/@shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-6c2f43011939" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  AnalyticsHelper — An Interface First, expect/actual Second
&lt;/h3&gt;

&lt;p&gt;The mistake I see in most KMP analytics setups is jumping straight to expect fun logEvent(...). That welds every feature to one vendor and makes debug builds pollute production dashboards.&lt;/p&gt;

&lt;p&gt;We layered it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain -the only thing features ever see&lt;/span&gt;
&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;AnalyticsHelper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;logEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AnalyticsEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setUserId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setUserProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Koin decides the implementation per build type&lt;/span&gt;
&lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AnalyticsHelper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isDebugBuild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;StubAnalyticsHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;FirebaseAnalyticsHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;StubAnalyticsHelper just logs locally — debug sessions never touch GA4. FirebaseAnalyticsHelper does something more valuable than forwarding: it enforces GA4's silent truncation limits (40-character event names, 100-character param values, 24/36 for user properties) in &lt;em&gt;common&lt;/em&gt; code, once, before anything reaches a platform SDK. Get that wrong per-platform and your dashboards drift apart in ways you only notice months later.&lt;/p&gt;

&lt;p&gt;Two more decisions paid off:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Screen views are logged from one place&lt;/strong&gt;  — a single OnDestinationChangedListener on the navigation controller. No screen composable calls analytics for its own existence. Adding a screen automatically instruments it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User IDs are hashed before they’re set.&lt;/strong&gt; The analytics layer never sees a raw phone number or account ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Crash Reporting Rides the Logger
&lt;/h3&gt;

&lt;p&gt;There is no CrashlyticsHelper. There's a logging pipeline (the same one from &lt;a href="https://medium.com/@shivathapaa/debugging-kotlin-multiplatform-apps-with-better-observability-b285d9dc0754" rel="noopener noreferrer"&gt;my observability post&lt;/a&gt;), and Crashlytics is a &lt;em&gt;sink&lt;/em&gt; on it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain&lt;/span&gt;
&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;crashlyticsSink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LogSink&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Android actual returns a sink that forwards WARN-and-above as breadcrumbs and ERROR-with-throwable as recorded exceptions, wrapped in a sanitizer so nothing sensitive rides along. Features never import a crash-reporting API; they log. Where a report goes is an installation detail decided at startup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Push and Deep Links: Four Doors, One Hallway
&lt;/h3&gt;

&lt;p&gt;FCM is the bridge with the most platform ceremony, so the design goal was to make the ceremony end as early as possible.&lt;/p&gt;

&lt;p&gt;On Android, a FirebaseMessagingService subclass in the app shell receives the message, uploads rotated tokens, and rebuilds the notification. On iOS, the app delegate registers with APNs and receives taps. Two completely different doors — and both immediately hand the raw payload to the same commonMain resolver, which accepts every payload key spelling the backend has ever used and synthesizes routes for legacy payloads that carry only a type. What comes out is one URI, flowing into a small shared handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;ExternalUriHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Holds a URI that arrived before the UI was ready to route it.&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
        &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// replay on attach&lt;/span&gt;
                &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onNewUri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;
        &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cache the URI, replay it the moment the UI attaches. That closes the cold-start race — a notification tapped before Compose exists still navigates correctly once it does. The same cache-then-replay shape turned out to be reusable: it also carries the in-app-update restart prompt from the Android shell into shared UI.&lt;/p&gt;

&lt;p&gt;Push is only two of the four doors. Android registers a custom scheme and verified HTTPS links (with the legacy path variants older shares still use); iOS handles the scheme through onOpenURL plus universal links through NSUserActivityTypeBrowsingWeb. A link tapped in a browser, a chat message, a QR code, or a notification — on either OS — lands in the same handler, resolves through the same rules, and navigates through the same type-safe routes. Adding a linkable screen is one declaration, not a per-platform project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Biometric Login: The Invariant Is Invalidation
&lt;/h3&gt;

&lt;p&gt;Biometric auth is where “share the interface, not the implementation” gets stress-tested, because the security semantics must match across two very different platform APIs.&lt;/p&gt;

&lt;p&gt;The shared surface is two expect classes — an authenticator (can we prompt? what type of sensor?) and a credential vault (seal a secret behind biometrics, unseal it later) — with sealed results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UnsealResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UnsealResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;KeyInvalidated&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UnsealResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// biometrics changed since sealing; vault wiped&lt;/span&gt;
    &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;NoEnrollment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UnsealResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// nothing sealed yet, or the vault was cleared&lt;/span&gt;
    &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Cancelled&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UnsealResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Failed&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UnsealResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;KeyInvalidated is the case that matters. If a fingerprint is added or removed, a stored credential must die — otherwise "biometric login" quietly becomes "whoever enrolls a finger next logs in."&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Android:&lt;/strong&gt; an AES/GCM key in AndroidKeyStore created with setUserAuthenticationRequired(true) and setInvalidatedByBiometricEnrollment(true), and the prompt is &lt;em&gt;cipher-bound&lt;/em&gt; — BiometricPrompt.CryptoObject wraps the cipher, so the OS won't decrypt without a fresh biometric.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;iOS:&lt;/strong&gt; a Keychain item stored with kSecAccessControlBiometryCurrentSet, which is the same contract in Apple's vocabulary — the item is bound to the biometric set that existed when it was written.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two implementations, one invariant. The login screen in commonMain handles KeyInvalidated with a re-enroll dialog and has no idea which platform bricked the key. That's the whole point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where the Series Goes From Here
&lt;/h3&gt;

&lt;p&gt;Two topics earned their own parts. Firebase and Google Maps forced &lt;em&gt;opposite&lt;/em&gt; integration strategies on iOS — header-only cinterop versus a Kotlin interface implemented in Swift — and the decision rule between them is Part 5. The app ships baseline + startup profiles generated from the real first-launch journey on low-end hardware; that harness is Part 6, and it closes the series.&lt;/p&gt;

&lt;h3&gt;
  
  
  What We’ve Built
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;46+ modules whose boundaries enforce the architecture instead of suggesting it — with the graph rendered from the build to prove it&lt;/li&gt;
&lt;li&gt;93% of 128,801 lines in commonMain, UI included, with the 7% enumerable: 37 expect seams and two thin app shells&lt;/li&gt;
&lt;li&gt;Analytics behind an interface, vendor limits enforced once in common code&lt;/li&gt;
&lt;li&gt;Crash reporting as a logger sink, not an API features know about&lt;/li&gt;
&lt;li&gt;Push and deep-link routing that’s pure Kotlin behind thin platform doors — custom scheme, universal links, and notification payloads all speaking one grammar&lt;/li&gt;
&lt;li&gt;A biometric vault with matching invalidation semantics on Keystore and Keychain&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What’s Next
&lt;/h3&gt;

&lt;p&gt;Part 5: two ways to bridge a native SDK, and how to pick. If you’re weighing Compose Multiplatform for production and something here doesn’t add up — or your shared-code number is counted differently — I’d genuinely like to hear it in the comments.&lt;/p&gt;

&lt;p&gt;Drop a ❤️ if this was useful — it helps more developers find the series.&lt;/p&gt;

</description>
      <category>kotlinmultiplatform</category>
      <category>jetpackcompose</category>
      <category>architecture</category>
      <category>iosdevelopment</category>
    </item>
    <item>
      <title>Shipping a Full Rewrite as an Ordinary App Update</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Thu, 23 Jul 2026 09:15:55 +0000</pubDate>
      <link>https://dev.to/shivathapaa/shipping-a-full-rewrite-as-an-ordinary-app-update-3k8h</link>
      <guid>https://dev.to/shivathapaa/shipping-a-full-rewrite-as-an-ordinary-app-update-3k8h</guid>
      <description>&lt;h4&gt;
  
  
  Credential migration under R8 obfuscation, Keychain state that outlives uninstalls, and deep links from three years ago — the swap nobody blogs about.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frzvpm2usli9tnuewj5w0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frzvpm2usli9tnuewj5w0.png" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Production Compose Multiplatform — Part 3 of 5.&lt;/em&gt; &lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f" rel="noopener noreferrer"&gt;&lt;em&gt;Part 2 — the case study.&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There’s a moment in every rewrite where the engineering stops being about the new codebase. Ours arrived near the end, with a simple question: what exactly happens to the people who already have the app installed?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-01d95643cb3f" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt; told the story of rebuilding GeoKrishi Farm from 848 shared lines to a 93%-shared Compose Multiplatform codebase. This part is about the step that made the rebuild real: shipping it. Users had the old app on their phones. They had sessions, saved credentials, notification tokens, share links in old chat threads. “Install our new app” was never on the table — the new codebase had to arrive as an ordinary update on the same store listing, and a farmer opening the app the next morning had to notice nothing at all.&lt;/p&gt;

&lt;p&gt;A rewrite the user notices is a failed rewrite. Here’s everything that took.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Baseline Mechanics
&lt;/h3&gt;

&lt;p&gt;The boring parts first, because getting them wrong ends the story early:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Same application ID&lt;/strong&gt; , both platforms. The store treats the new codebase as version N+1 of the existing app, nothing else.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The next versionCode.&lt;/strong&gt; The old app’s final release was versionCode 716; the new codebase shipped 717 and 718. One numbering line, two codebases — the store never knew a rewrite happened.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signing continuity&lt;/strong&gt; on Android and the same bundle identity on iOS. Obvious, and worth stating because everything below assumes it: an update only reads its predecessor’s data if the platform believes it &lt;em&gt;is&lt;/em&gt; its predecessor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that, every existing install upgrades in place — and inherits the old app’s on-device state. Which is where it gets interesting.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Storage Layer Changed Underneath
&lt;/h3&gt;

&lt;p&gt;The migrations in this section are only necessary because the rewrite didn’t reuse the old app’s storage — it replaced it.&lt;/p&gt;

&lt;p&gt;The previous build persisted state ad hoc — scattered across preference files, each concern keying its own entries however it happened to. The new codebase routes everything through one abstraction — a CacheStore interface over encrypted vault — with typed state stores layered on top. An AuthStateStore exposes the access token and login state as observable flows; a SessionStateStore carries expiry and login-required signals; an app-language store handles locale. The session token, the user's subscription details, a half-finished signup draft, saved market and weather filters, theme, onboarding state, the biometric opt-in — all of it now lives behind that one door, under explicit, stable key names declared in a single registry.&lt;/p&gt;

&lt;p&gt;That registry is the quiet foundation of the whole layer, and — as the next migration shows — the reason a piece of this was hard at all. Keys are plain string constants, "userToken", "loginCredentials", "userSubscriptionDetails", chosen once and never derived from a class name, a type, or anything a build tool is free to rewrite. A persisted key is a contract with every future release; the new layer treats it as one.&lt;/p&gt;

&lt;p&gt;So “upgrade in place” meant bridging two entirely different storage schemes. For each piece of the old app’s state, one decision: carry it forward into the new keys, or drop it and let the new system re-establish it cleanly. Most of it is the second — a session is better re-authenticated than resurrected, stale preferences better re-derived than restored. Exactly one thing is worth carrying so the user never feels the seam: their saved sign-in.&lt;/p&gt;

&lt;h3&gt;
  
  
  Migration 1: Carrying the Saved Sign-In Across an Obfuscated Key
&lt;/h3&gt;

&lt;p&gt;Carrying those credentials forward should have been a one-line read. It wasn’t, and the reason earns the section.&lt;/p&gt;

&lt;p&gt;The last released build stored the saved username and password under a key derived from a class’s simple name. That is harmless in a debug build. In a minified release build it isn’t: R8 rewrites that class name — no keep rule pinned it — so on the shipped Android binary the on-disk key is an obfuscated, build-specific string. Read back the original name and you get nothing; the data is sitting under a single minified letter. Nothing is minified on iOS or in debug builds, so the read succeeds everywhere except the one place that ships to users.&lt;/p&gt;

&lt;p&gt;So the migration reads defensively: try the known key first, and when that misses, identify the entry by the &lt;em&gt;shape of its value&lt;/em&gt; — the one blob in the legacy credential store that decodes into a username and password — then write it into the new store under the new, explicit key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Known key first (correct on iOS and unminified builds); otherwise&lt;/span&gt;
&lt;span class="c1"&gt;// recover the entry by value shape, then write it under the NEW key name.&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;migrateLoginCredentials&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;legacy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;legacyStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LEGACY_FILE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;LEGACY_KEY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;legacyStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readAllValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LEGACY_FILE&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;firstNotNullOfOrNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;// nothing to carry - a clean outcome&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cacheStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PreferenceKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LOGIN_CREDENTIALS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;legacy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three rules kept it safe, and I would carry all three into any migration:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recover by value, not by key, when a key can move.&lt;/strong&gt; The key was a casualty of minification; the JSON shape of the value was not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Never overwrite fresher data.&lt;/strong&gt; If the new store already holds credentials, the user has signed in since — the legacy entry is stale and is wiped unread, not restored over the top.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Destructive steps last, and only on a definite outcome.&lt;/strong&gt; The source wipe and the one-shot “migration done” flag commit together, and only when the step either carried something forward or confirmed there was nothing to carry. If a blob is found but the write fails, neither commits — the next launch retries rather than destroying the only copy of someone’s saved password. The whole path is wrapped so it can never throw into launch; the worst case is a single manual sign-in, which is exactly what a fresh install asks for anyway.&lt;/p&gt;

&lt;p&gt;The forward-looking half is the real point. This is precisely why the new layer keys every value off an explicit string constant rather than a class identifier: the class a value is modeled by can be renamed, minified, or moved between modules, but the string it was persisted under cannot follow it without a migration like this one. Decouple the two, and every release after this one reads its own data without ceremony.&lt;/p&gt;

&lt;h3&gt;
  
  
  Migration 2: The Keychain That Outlives the App
&lt;/h3&gt;

&lt;p&gt;iOS has the inverse problem. Android’s app data dies with uninstall; the iOS Keychain does not. Delete the app, reinstall a year later — your Keychain items are still there.&lt;/p&gt;

&lt;p&gt;For a rewrite arriving at the same bundle identity, that means a &lt;em&gt;fresh install&lt;/em&gt; of the new app can wake up holding a stale session token from an app that no longer exists. First launch looks logged-in, with a token the backend may have expired — or worse, half-valid state that the new auth layer was never designed to interpret.&lt;/p&gt;

&lt;p&gt;The fix is a fresh-install detector: a marker that lives where data &lt;em&gt;does&lt;/em&gt; die with the app. NSUserDefaults is wiped on uninstall; the Keychain isn't. If the marker is missing, this launch is a fresh install — wipe the leftover session before the auth layer reads anything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;runFreshInstallKeychainWipe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;defaults&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NSUserDefaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;standardUserDefaults&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;boolForKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FRESH_INSTALL_MARKER&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="c1"&gt;// no-op after the first run&lt;/span&gt;

    &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;cacheStore&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KoinPlatform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getKoin&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CacheStore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class="nc"&gt;SESSION_KEYS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cacheStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deleteNoSuspend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setBool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;forKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FRESH_INSTALL_MARKER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;onFailure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;w&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fresh-install session wipe failed; a stale session may persist"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TAG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;throwable&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ordering is the whole point. It runs immediately after Koin starts and before a single screen composes — so the wipe always wins the race against the auth store’s first, lazy token read. The marker is persisted only if the wipe succeeded, so an interrupted first launch retries rather than silently marking a dirty install clean. And because an in-place &lt;em&gt;update&lt;/em&gt; preserves NSUserDefaults, the marker survives, the wipe is skipped, and a logged-in user is never disturbed.&lt;/p&gt;

&lt;p&gt;The subtle part is what it doesn’t clear. It wipes the session-bound keys — the access token, subscription state, a half-finished signup draft, saved filters — and deliberately keeps the device-level ones: theme, locale, onboarding-seen, the saved username, the biometric opt-in flag, and the biometric-sealed credential. Reinstall the app and you’re logged out, as you should be. Then one Face ID tap puts you back, rather than a full re-onboarding. “Wipe the session” and “wipe the user’s relationship with the app” are different operations, and the key list is where you say which one you meant.&lt;/p&gt;

&lt;p&gt;(One more thing that list bought us: bumping the marker’s name forces the wipe once across the entire install base on the next update. It’s a global session-reset lever, so its value is load-bearing — change it only on purpose.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Migration 3: Deep Links and Push Payloads From the Past
&lt;/h3&gt;

&lt;p&gt;The old app had three years of artifacts in the wild: share links in chat threads, notification payloads the backend still emits, and multiple historical spellings of “where should this tap go.”&lt;/p&gt;

&lt;p&gt;The new app’s router treats that history as part of the contract. One shared resolver — pure Kotlin, used by both platforms — accepts every payload key the backend has ever used for a link (deeplink_url, deeplinkUrl, click_action, link, uri, url), and when a legacy payload carries only a type field instead of a URL, the resolver &lt;em&gt;synthesizes&lt;/em&gt; the correct route from it. Android registers both the custom scheme and verified HTTPS links, including the legacy path variants; iOS handles the scheme and universal links and funnels them into the same resolver.&lt;/p&gt;

&lt;p&gt;The result: a notification sent by the backend on migration day routed correctly no matter which app version received it, and a share link from two years ago still opens the right screen today. Nothing about the rewrite was allowed to break a URL that already existed.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Swap Checklist
&lt;/h3&gt;

&lt;p&gt;Everything above, compressed into the list I wish I’d had at the start — designed at the &lt;em&gt;start&lt;/em&gt;, because half of these constrain decisions you make on day one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Same application/bundle ID, same signing, next versionCode. The rewrite is an update, full stop.&lt;/li&gt;
&lt;li&gt;Inventory every piece of on-device state the old app created: credentials, preferences, tokens, files, Keychain items. Each one gets a decision — migrate, wipe, or deliberately ignore.&lt;/li&gt;
&lt;li&gt;Identify migrated data by value shape, not by key, if there is &lt;em&gt;any&lt;/em&gt; chance obfuscation touched the keys.&lt;/li&gt;
&lt;li&gt;Destructive steps last, and only after verified writes. Design every migration to be retryable on next launch.&lt;/li&gt;
&lt;li&gt;On iOS, assume the Keychain contains ghosts. Detect fresh installs and clean before first read.&lt;/li&gt;
&lt;li&gt;Treat historical deep links and push payloads as API contracts. Enumerate every spelling that ever shipped; support all of them.&lt;/li&gt;
&lt;li&gt;Rehearse on a device that genuinely upgraded from the old app — not a clean install. The clean install always works. The upgrade is the product.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What We’ve Built
&lt;/h3&gt;

&lt;p&gt;A rewrite that arrived as versionCode 717 on a listing whose users met it as an ordinary update — back in with a tap on a saved sign-in (a fresh session, not a resurrected token), notifications still routing, old links still working, on a codebase sharing 93% of its lines instead of 848.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next
&lt;/h3&gt;

&lt;p&gt;Part 4 is the architecture tour: the 46+ module structure, the DI seams, and the dependency graph rendered straight from the build. If you’ve shipped a rewrite-in-place and hit a migration I didn’t — I’d genuinely like to hear it in the comments.&lt;/p&gt;

&lt;p&gt;Drop a ❤️ if this was useful — it helps more developers find the series.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>kotlinmultiplatform</category>
      <category>mobiledevelopment</category>
      <category>androidappdevelopmen</category>
    </item>
    <item>
      <title>We Called Our App Multiplatform. 848 Lines Actually Were.</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sun, 19 Jul 2026 05:25:35 +0000</pubDate>
      <link>https://dev.to/shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-19g5</link>
      <guid>https://dev.to/shivathapaa/we-called-our-app-multiplatform-848-lines-actually-were-19g5</guid>
      <description>&lt;h4&gt;
  
  
  The rebuild of a production KMP app to 93% measured shared code — with real numbers from both codebases.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqntadqu6ngu9qsvc7ff3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqntadqu6ngu9qsvc7ff3.png" alt="Representation image" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Production Compose Multiplatform — Part 2 of 5.&lt;/em&gt; &lt;a href="https://dev.to/shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-4hoh"&gt;&lt;em&gt;Part 1 — real iOS 26 Liquid Glass over shared Compose UI&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There’s a moment in every long-lived codebase where you finally run the measurement you’ve been avoiding. Mine was a line count.&lt;/p&gt;

&lt;p&gt;Our Kotlin Multiplatform app — a production farming app, two platforms, years of shipped features — shared 848 lines of Kotlin with iOS. Out of roughly 215,000 lines in the project. Not 848 files. Lines. The rest was an Android app, an iOS app, and a label.&lt;/p&gt;

&lt;p&gt;This series is the story of the rebuild: how GeoKrishi Farm went from that number to 46 Gradle modules with 93% measured shared code, live on both stores, without users ever noticing the swap. This first part is the case study — what the fork looked like, why it happened to a reasonable team, and what the rebuild changed, in numbers I can defend line by line.&lt;/p&gt;

&lt;h3&gt;
  
  
  The App, for Context
&lt;/h3&gt;

&lt;p&gt;GeoKrishi Farm serves farmers in Nepal: crop planning, weather, market prices, a farm diary, expert chat, calculators — on both Android and iOS. The audience shapes the engineering. Most users run low-end Android devices (minSdk is still 24) on intermittent connectivity, in Nepali. Every decision in this series was made for that person.&lt;/p&gt;

&lt;h3&gt;
  
  
  How a Multiplatform App Becomes Two Apps
&lt;/h3&gt;

&lt;p&gt;Nobody decides to build the same app twice. It happens one sprint at a time.&lt;/p&gt;

&lt;p&gt;The first version adopted KMP without adopting shared UI. Android screens were Jetpack Compose; iOS — with nothing to render Compose — rebuilt every screen in SwiftUI. 64,690 lines of Swift grew beside the Kotlin. Our Home screen existed as an 1,138-line Kotlin file and, separately, as a 1,484-line SwiftUI view with its own 1,249-line view model. Two implementations, two sets of bugs, two review cycles for every feature.&lt;/p&gt;

&lt;p&gt;And the “shared” Kotlin module? With no consumer on the iOS side beyond a thin networking seam, code drifted to where it was easiest to write: androidMain. By the end, 82% of the Kotlin in the shared module was Android-only. commonMain held 30,000 lines; androidMain held 119,000.&lt;/p&gt;

&lt;p&gt;Here’s the uncomfortable part: the build never objected. It compiled fine. It shipped fine. There is no compiler error for &lt;em&gt;your architecture quietly forked eight months ago&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Debt Underneath
&lt;/h3&gt;

&lt;p&gt;The fork was the visible problem. Underneath it, a single-module codebase built at delivery speed had accumulated the usual passengers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One god API class.&lt;/strong&gt; 1,151 lines, 78 endpoints, 66 DTO imports — plus sixteen sibling API classes extending it. Inheritance where composition should have been.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No dependency injection.&lt;/strong&gt; Repositories constructed their own HttpClient through a default argument. Every repository, its own connection pool, silently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment switching by comment.&lt;/strong&gt; Which backend a build talked to depended on which constant line was uncommented that day.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigation through a global mutable.&lt;/strong&gt; A singleton held the last child navigator so notification taps could push screens from anywhere — under an all-caps comment warning future readers not to do exactly this. The team knew. Pressure writes code like that anyway.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two state-management idioms at once.&lt;/strong&gt; 73 AndroidX ViewModels and 52 Voyager ScreenModels doing the same job, depending on who wrote the screen and when.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Screens past 2,000 lines&lt;/strong&gt; , and over a thousand commented-out lines kept “just in case.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I can list these freely because I co-built that codebase. This isn’t a review of someone else’s mess — it’s an audit of my own, with two more years of experience. If any of that list feels familiar, that’s the point: none of it is exotic, and none of it trips a compiler. It’s what a single-module codebase becomes under sustained deadline pressure with nobody owning the shape of the whole.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the Old App Got Right
&lt;/h3&gt;

&lt;p&gt;Fairness matters, because the old app wasn’t incompetent — it was unowned.&lt;/p&gt;

&lt;p&gt;The expect/actual boundaries were genuinely tidy. Dependencies were current, with a proper version catalog. The Ktor client was configured correctly in one place. Crashlytics shipped with symbol upload wired for release builds. Individual decisions were fine.&lt;/p&gt;

&lt;p&gt;What no one held was the shape of the whole. Every architectural question — where does this go, what may depend on what — was answered locally, per feature, per deadline. The root cause wasn’t skill. &lt;strong&gt;The architecture never had an owner.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Refactor or Rewrite
&lt;/h3&gt;

&lt;p&gt;We chose a rewrite, and I want to be precise about why, because “rewrite” is usually the wrong answer.&lt;/p&gt;

&lt;p&gt;The deciding factor was UI. As long as iOS rendered SwiftUI, every shared-code improvement had a ceiling: the most expensive duplication — screens — was untouchable. Compose Multiplatform removed that ceiling, but adopting it meant every screen changed anyway. Once every screen changes, the incremental-refactor advantage is gone, and you’re free to fix the module structure, the DI, and the navigation in the same motion.&lt;/p&gt;

&lt;p&gt;The second factor: the old app had to keep shipping. A refactor-in-place would have put the release train and the restructuring in the same repository, fighting. Instead we ran two repos — the old one releasing, the new one growing feature by feature with the old screens as the spec. The rewrite history shows exactly this rhythm: a foundation phase (build system, core modules, design system first), a stretch where the old app took priority, then a long porting push to the finish line.&lt;/p&gt;

&lt;p&gt;Not a big-bang rewrite. A parallel one, with an end date.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Numbers, Honestly Counted
&lt;/h3&gt;

&lt;p&gt;“Shared code percentage” is usually a vibe. Here’s mine, counted with wc -l on source files, build directories excluded — old codebase → new:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gradle modules:&lt;/strong&gt; 1 → 46&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kotlin actually reaching iOS:&lt;/strong&gt; 848 lines → 119,742 lines of commonMain&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;androidMain Kotlin:&lt;/strong&gt; 119,168 lines → 3,941&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Swift:&lt;/strong&gt; 64,690 lines → 993&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared, as a share of the whole codebase:&lt;/strong&gt; ~14% →  &lt;strong&gt;93%&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implementations per screen:&lt;/strong&gt; 2 → 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The new number includes every screen — the UI is Compose Multiplatform, so there is no SwiftUI layer quietly excluded from the math. The entire native iOS footprint is eight Swift files: an app delegate, a Compose host, and a handful of bridges. The 7% that remains platform-specific is deliberate: 37+ explicit expect/actual seams and two thin app shells, each of which earns its existence.&lt;/p&gt;

&lt;p&gt;Structure enforces the rest. Features can’t import features — module boundaries make cross-feature coupling a compile error, not a code-review hope. Domain layers are framework-free. One injected HttpClient behind repository interfaces. Type-safe navigation with no global mutables. Full deep-link support on both platforms — custom scheme, verified universal links, push payloads — resolved by one shared router.&lt;/p&gt;

&lt;p&gt;The usual objection to that number is that a 93%-shared app must feel generic on iOS. It doesn’t have to — &lt;a href="https://dev.to/shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-4hoh"&gt;Part 1&lt;/a&gt; is a real system UITabBar rendering Apple's iOS 26 Liquid Glass, floating above this exact shared host, with the same single NavHost behind it. Native chrome cost us a bounded, deliberate trade, not a fork.&lt;/p&gt;

&lt;p&gt;And it’s fast where it counts: the app ships baseline &lt;em&gt;and&lt;/em&gt; startup profiles, generated from the real first-launch journey on a low-end device like the ones our users own. The app became 18% faster with lots of other improvement within.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Part Nobody Blogs About
&lt;/h3&gt;

&lt;p&gt;The rewrite shipped as an ordinary update on the same store listing — next versionCode, same application ID, every existing install upgraded in place. That required migration engineering that rarely gets written up: recovering stored credentials the old app had saved under an R8-obfuscated key, wiping iOS Keychain state that outlives uninstalls, and keeping years of old push payloads and share links routing correctly.&lt;/p&gt;

&lt;p&gt;Users updated, opened the app, and were still logged in — on a codebase sharing 93% of its lines instead of 848. A rewrite the user notices is a failed rewrite.&lt;/p&gt;

&lt;p&gt;That swap is Part 3 of this series, because it deserves its own piece.&lt;/p&gt;

&lt;h3&gt;
  
  
  What I’d Tell a Team Standing Where We Stood
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Measure real sharing, quarterly.&lt;/strong&gt; Lines compiled for both targets, divided by total. If the number surprises you, the fork already happened. (I later built &lt;a href="https://github.com/shivathapaa/Aalekh" rel="noopener noreferrer"&gt;Aalekh&lt;/a&gt; partly out of this scar — the real architecture lives in the Gradle dependency graph, not in any diagram.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decide the UI question deliberately.&lt;/strong&gt; KMP without shared UI is a legitimate architecture — but it must be a &lt;em&gt;decision&lt;/em&gt;, with the duplication cost priced in. If nobody decided, you’ll pay the cost anyway and get drift for free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Give the architecture an owner before the first feature.&lt;/strong&gt; Written rules, and module boundaries that enforce the important ones. Convention doesn’t survive deadlines; implementation(projects.core.network) does.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep the old app releasable until the day you don’t need it.&lt;/strong&gt; It buys calendar time honestly, and it keeps the pressure off the new codebase to ship before its foundations are done.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/@shivathapaa/shipping-a-full-rewrite-as-an-ordinary-app-update-698a963d5509" rel="noopener noreferrer"&gt;Part 3:&lt;/a&gt; shipping the swap — credential migration under R8 obfuscation, Keychain hygiene, and deep-link backward compatibility. Then the architecture tour, the native-SDK bridge patterns, and baseline profiles for a low-end fleet.&lt;/p&gt;

&lt;p&gt;If you’re staring at your own 848-line number right now — or you’ve run this migration and landed somewhere different — I’d genuinely like to hear how it went in the comments.&lt;/p&gt;

&lt;p&gt;Drop a ❤️ if this was useful — it helps more developers find the series.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>iosappdevelopment</category>
      <category>androidappdevelopmen</category>
      <category>kotlinmultiplatform</category>
    </item>
    <item>
      <title>I Built JetBrains' Official Liquid Glass Setup, Then Deleted It</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Mon, 13 Jul 2026 04:52:47 +0000</pubDate>
      <link>https://dev.to/shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-4hoh</link>
      <guid>https://dev.to/shivathapaa/i-built-jetbrains-official-liquid-glass-setup-then-deleted-it-4hoh</guid>
      <description>&lt;p&gt;There's a moment, if you ship Compose Multiplatform to iOS, when someone opens your app on an iOS 26 device and asks why the tab bar doesn't do the glass thing.&lt;/p&gt;

&lt;p&gt;It's a fair question with an awkward answer. Compose Multiplatform renders its own UI through Skia. It does not draw UIKit. So when Apple ships a new material — Liquid Glass, introduced in iOS 26 — a Compose app doesn't inherit it the way a native app does by recompiling.&lt;/p&gt;

&lt;p&gt;JetBrains says so plainly in &lt;a href="https://kotlinlang.org/docs/multiplatform/ios-liquid-glass.html" rel="noopener noreferrer"&gt;their documentation&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Liquid Glass effects are rendered by the system through native &lt;code&gt;TabView&lt;/code&gt;, &lt;code&gt;NavigationStack&lt;/code&gt;, and toolbar APIs."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you need a native surface. The question is how much of your app you're willing to hand over to get one.&lt;/p&gt;

&lt;p&gt;I write this from a production app — a farming app used across Nepal, live on both stores, 46 Gradle modules, 93% of its code shared in &lt;code&gt;commonMain&lt;/code&gt;, every screen drawn by Compose on both platforms. On iOS 26 its bottom bar is a real &lt;code&gt;UITabBar&lt;/code&gt; rendering Apple's Liquid Glass. It is not a Compose approximation of glass. It's the system's.&lt;/p&gt;

&lt;p&gt;Getting there meant building the officially documented approach first — and then throwing it away.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Official Recipe Does
&lt;/h2&gt;

&lt;p&gt;JetBrains' guidance is coherent and, for most apps, correct. You build a native SwiftUI shell: a &lt;code&gt;TabView&lt;/code&gt; for the tab bar, a &lt;code&gt;NavigationStack&lt;/code&gt; per tab for pushes. iOS 26 applies Liquid Glass to both automatically, with no glass-specific code. Compose stops rendering navigation chrome and renders only screen content, told to do so by a CompositionLocal — theirs is called &lt;code&gt;LocalUseNativeNavigation&lt;/code&gt;. Route state is mirrored across the boundary through a path binding, with callbacks going the other way.&lt;/p&gt;

&lt;p&gt;You get a great deal for that: the glass tab bar, the glass toolbar, the native back-swipe, and the system's own transitions.&lt;/p&gt;

&lt;p&gt;You also get something subtler. &lt;strong&gt;The navigation stack now lives in SwiftUI.&lt;/strong&gt; Compose's routes are mirrored into a &lt;code&gt;NavigationStack&lt;/code&gt; path; SwiftUI owns the presentation; the two stay in sync because you keep them in sync. That's a synchronization surface, and it exists per platform, because Android has no &lt;code&gt;NavigationStack&lt;/code&gt; to mirror into.&lt;/p&gt;

&lt;p&gt;For a fresh app, or one whose navigation is a handful of tabs and pushes, that's a fine price. It wasn't for ours.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Question That Actually Decides It
&lt;/h2&gt;

&lt;p&gt;The official recipe is the right call for a lot of apps. Whether it's right for &lt;em&gt;yours&lt;/em&gt; comes down to one question: &lt;strong&gt;what does your navigation graph carry besides screen transitions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the honest answer is "not much" — a few tabs, some pushes — hand navigation to SwiftUI and enjoy the glass. But most production apps have quietly accreted cross-cutting behavior onto the back stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;deep links from several sources — a custom scheme, verified universal links, push payloads — resolved in shared code,&lt;/li&gt;
&lt;li&gt;an auth guard that redirects an unauthenticated user mid-navigation,&lt;/li&gt;
&lt;li&gt;links that arrive &lt;em&gt;before&lt;/em&gt; the graph is ready and have to be replayed once it is,&lt;/li&gt;
&lt;li&gt;session-expired or re-login dialogs that can interrupt navigation from anywhere,&lt;/li&gt;
&lt;li&gt;a one-time onboarding flow that has to run ahead of all of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If that logic is shared today, mirroring the navigation stack into SwiftUI gives every piece of it a &lt;em&gt;second home&lt;/em&gt; — one that exists on a single platform. And the failure that produces isn't "the glass doesn't render." It's "the auth redirect works on Android and lands one screen too deep on iOS," found six months later by a user rather than a test.&lt;/p&gt;

&lt;p&gt;That was the trade I didn't want. Not &lt;em&gt;native navigation&lt;/em&gt; — &lt;strong&gt;native chrome, shared behavior.&lt;/strong&gt; The bar should be Apple's. The routing behind it shouldn't know the bar exists.&lt;/p&gt;

&lt;p&gt;That reframing is the whole article. Everything below is a consequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Shape: An Overlay, Not a Container
&lt;/h2&gt;

&lt;p&gt;The iOS 26 entry point hosts the &lt;em&gt;unchanged&lt;/em&gt; &lt;code&gt;App()&lt;/code&gt; — the same composable Android calls — and puts a bar on top of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;ZStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;ComposeShell&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                       &lt;span class="c1"&gt;// the whole app, unchanged&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ignoresSafeArea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="kt"&gt;NativeTabBar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="c1"&gt;// a real UITabBar&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;barHeight&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;geo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;safeAreaInsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isBarVisible&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;barHeight&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;geo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;safeAreaInsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two decisions carry the design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's a bare &lt;code&gt;UITabBar&lt;/code&gt;, not a &lt;code&gt;UITabBarController&lt;/code&gt;.&lt;/strong&gt; A controller wants to own the screen and host children. We already have a host. All we need is the bar — a strip at the bottom, roughly 49 points plus the home-indicator inset. (49pt is the documented height of the classic bar; Apple doesn't publish a metric for the iOS 26 floating one, so we pin the classic number and let the bar fill its glass background down to the edge.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The overlay covers only the bar.&lt;/strong&gt; The Compose host is full-bleed beneath it, still visible, still taking touches everywhere the bar isn't. This matters more than it sounds: glass refracts &lt;em&gt;what's behind it&lt;/em&gt;. A bar sitting on an opaque strip is a bar with nothing to sample. The feed has to scroll under it for the material to mean anything.&lt;/p&gt;

&lt;p&gt;And the appearance code? There isn't any:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeUIView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UITabBar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UITabBar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coordinator&lt;/span&gt;
    &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tintColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;NativeTab&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// brand green, that's it&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;UITabBarAppearance&lt;/code&gt;. No &lt;code&gt;selectionIndicatorImage&lt;/code&gt;. Built against the iOS 26 SDK and running on iOS 26, a stock &lt;code&gt;UITabBar&lt;/code&gt; adopts Liquid Glass and the system selection capsule on its own — unless the app opts out with &lt;code&gt;UIDesignRequiresCompatibility&lt;/code&gt; in its Info.plist. The capsule especially is not ours to style: per Apple's DTS engineers, on iOS 26 it's the system indicator and legacy overrides are ignored.&lt;/p&gt;

&lt;p&gt;That's the point. The best code for rendering Apple's material is the code Apple already wrote. Our job was to get out of its way — and to keep Compose from noticing.&lt;/p&gt;

&lt;p&gt;One more decision, easy to get wrong: the bar is mounted &lt;strong&gt;once&lt;/strong&gt; and &lt;em&gt;slides&lt;/em&gt;, never inserted and removed. Look at the &lt;code&gt;.offset&lt;/code&gt; in that &lt;code&gt;ZStack&lt;/code&gt; — when the bar should hide, it slides below the bottom edge instead of leaving the view tree. That isn't stylistic. A &lt;code&gt;UIViewRepresentable&lt;/code&gt; is dismantled the instant it leaves a SwiftUI hierarchy, so a &lt;code&gt;.transition&lt;/code&gt; on it has nothing left to animate out and just snaps. Keep it mounted, animate the offset, and it slides symmetrically in and out — the way &lt;code&gt;hidesBottomBarWhenPushed&lt;/code&gt; feels natively.&lt;/p&gt;

&lt;p&gt;Because it's a permanent view, it also carries two small jobs Compose can't hand it: it hides itself over the keyboard by observing &lt;code&gt;keyboardWillShow&lt;/code&gt;/&lt;code&gt;keyboardWillHide&lt;/code&gt; (Compose's IME state doesn't reach SwiftUI), and it builds its own labels — it lives outside composition, so if your app is localized it needs its own route to the same strings Compose uses.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bridge: One CompositionLocal, One Delegate
&lt;/h2&gt;

&lt;p&gt;Shared code must not import UIKit, and the Swift bar must not import a navigation graph. Between them sit two small contracts in &lt;code&gt;commonMain&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, a switch that is false everywhere except inside the native shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LocalNativeTabBarEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;staticCompositionLocalOf&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The single container that Android and iOS both run reads it. When true, it renders no Compose bottom bar and publishes its bottom-bar state outward instead. When false — Android, iOS 18 — the file behaves exactly as it did before any of this existed.&lt;/p&gt;

&lt;p&gt;(JetBrains' documented shell reaches for the same kind of switch — a &lt;code&gt;CompositionLocal&lt;/code&gt; named &lt;code&gt;LocalUseNativeNavigation&lt;/code&gt; — to tell Compose to stand down. Landing on the same seam is reassuring; where the two designs part ways is how much behavior sits on the other side of it.)&lt;/p&gt;

&lt;p&gt;Second, the bridge itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;NativeTabBarDelegate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onTabsUpdated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tabIds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;selectedTabId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;showTabBar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;NativeTabBarBridge&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bottomInsetState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mutableStateOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bottomInsetDp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Float&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bottomInsetState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;publishTabState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tabIds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;selectedTabId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;showTabBar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onTabTapped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tabId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// native tap -&amp;gt; shared navigation&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setBottomInset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// Swift reports the bar's height&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Swift implements &lt;code&gt;NativeTabBarDelegate&lt;/code&gt; on an &lt;code&gt;ObservableObject&lt;/code&gt;, so the bar's SwiftUI state &lt;em&gt;is&lt;/em&gt; the Compose container's bottom-bar state. Compose publishes; the bar reflects. The bar reports taps; Compose routes them through the same function a Compose tab tap calls, emitting the same analytics event.&lt;/p&gt;

&lt;p&gt;There's no mirrored back stack here, because there's no stack to mirror. The bar knows five strings and which one is selected. That's the entire contract.&lt;/p&gt;

&lt;p&gt;And those are deliberately plain strings — &lt;code&gt;"home"&lt;/code&gt;, &lt;code&gt;"plan"&lt;/code&gt; — not route class names. Anything that crosses a language boundary, lands in an analytics event, or gets written to disk is an API: a class name would shift the moment someone refactors or an obfuscator minifies a release build, silently breaking the contract or fragmenting your analytics. Give the things that cross a name you control.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Native View Can Write Compose State
&lt;/h2&gt;

&lt;p&gt;This is the part worth stealing even if you never touch a tab bar.&lt;/p&gt;

&lt;p&gt;The bar floats, so its height varies — it slides away on a detail screen, it drops when the keyboard opens. Anything Compose lays out near the bottom (a floating action button, the last row of a list) has to know that height or it renders behind the bar. So the height has to travel from a native view &lt;em&gt;into&lt;/em&gt; live Compose layout.&lt;/p&gt;

&lt;p&gt;The move: make the bridge's height field observable — back it with &lt;code&gt;mutableStateOf&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;NativeBarBridge&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;heightState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mutableStateOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Dp&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;heightState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;          &lt;span class="c1"&gt;// read in composition -&amp;gt; subscribes&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setHeight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Dp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;heightState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// the native side calls this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now any composable that reads &lt;code&gt;height&lt;/code&gt; is subscribed to it, and when the native side calls &lt;code&gt;setHeight&lt;/code&gt; the readers recompose — no callbacks, no event bus, no &lt;code&gt;StateFlow&lt;/code&gt; plumbing. The Kotlin/native boundary disappears into Compose's snapshot system; a UIKit view is, in effect, writing Compose state. (One caveat: write from the main thread, which UIKit already does.)&lt;/p&gt;

&lt;p&gt;Expose it to screens as a &lt;code&gt;CompositionLocal&lt;/code&gt; that defaults to zero, so a screen just reads a value it doesn't have to understand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LocalExtraBottomInset&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compositionLocalOf&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A scaffold folds that into its content insets and pads a floating button with it, animated so the button rides the bar in and out instead of jumping. And because the default is zero, every one of those reads is inert on Android and on older iOS — which was the standing rule the whole feature was built to: &lt;strong&gt;shared code must not get worse for the platform that doesn't benefit.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Overlay Actually Cost
&lt;/h2&gt;

&lt;p&gt;Everything above makes the design sound clean. It is — &lt;em&gt;now&lt;/em&gt;. What it cost was three bugs, all of them the same bug wearing different clothes: &lt;strong&gt;a floating bar has a height, and that height is a number the rest of the app has to agree about.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Worth walking through, because if you build this, you will meet all three.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trap 1 — who is allowed to reserve a system-bar inset.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the glass to sample content, the app has to draw all the way to the bottom edge: the root container must &lt;em&gt;stop&lt;/em&gt; reserving the home-indicator inset so content bleeds under the bar.&lt;/p&gt;

&lt;p&gt;The subtlety is what that does to everything below it. Any &lt;em&gt;nested&lt;/em&gt; scaffold that also reserves that inset — "just to be safe" — now either double-counts it, or (when the bar is hidden) reserves an inset that nothing above it did. Either way you get a dead strip of padding across the bottom of the screen. It'll be exactly the height of the home indicator, ~34pt, which is how you'll recognize it on sight.&lt;/p&gt;

&lt;p&gt;The rule that fixes it is worth adopting even without a tab bar:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Only the root container reserves a system-bar inset. A screen-level scaffold never does.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;On a consumption-aware &lt;code&gt;Scaffold&lt;/code&gt; this costs nothing: when an ancestor consumes an inset, a descendant's &lt;code&gt;contentWindowInsets&lt;/code&gt; already resolves it to zero. So excluding the navigation-bar inset in a nested scaffold is a no-op on the platforms where the root handles it, and the fix on the one platform where the root deliberately doesn't. One unconditional rule beats a conditional that has to know whether a bar is currently on screen.&lt;/p&gt;

&lt;p&gt;The last-item clearance then goes where it belongs — a trailing &lt;code&gt;Spacer(Modifier.navigationBarsPadding())&lt;/code&gt;, itself consumption-aware, so it collapses to zero wherever an ancestor already handled the inset and adds nothing on the platforms that don't need it.&lt;/p&gt;

&lt;p&gt;(Trap inside the trap: only the &lt;code&gt;windowInsetsPadding&lt;/code&gt; &lt;em&gt;modifiers&lt;/em&gt; are consumption-aware. Read &lt;code&gt;WindowInsets.navigationBars&lt;/code&gt; as a raw &lt;em&gt;value&lt;/em&gt; and you get the full inset back, regardless of what any ancestor consumed. Hours disappear here.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trap 2 — a runtime value that flips a structural branch.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the best Compose lesson in the whole project, and it has nothing to do with iOS.&lt;/p&gt;

&lt;p&gt;The bar hides over the keyboard, so when the keyboard opens, the height it reports drops to zero. Somewhere, a composable read that height and did the reasonable-looking thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The bug, reduced to its essence.&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extraInset&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bottom&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extraInset&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;content&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;content&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tap a text field. The keyboard starts to open. The inset flips to zero. The branch flips with it — and because Compose keys nodes &lt;em&gt;by position&lt;/em&gt;, those two &lt;code&gt;content()&lt;/code&gt; calls are two different composition groups. Flipping the branch &lt;strong&gt;disposes the whole subtree and rebuilds it&lt;/strong&gt;: the focused text field is destroyed, focus goes with it, and the keyboard closes on the very tap that opened it.&lt;/p&gt;

&lt;p&gt;It only misfires when the value genuinely flips at runtime, so it stays hidden until exactly the wrong moment — a field near something that resizes, a layout that changes when a flag toggles.&lt;/p&gt;

&lt;p&gt;The fix is one line of structure — make the wrapper unconditional and vary only its modifier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bottom&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;extraInset&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;   &lt;span class="c1"&gt;// 0.dp is not a special case&lt;/span&gt;
    &lt;span class="n"&gt;propagateMinConstraints&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// preserve the constraints the bare slot had&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;content&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule, framed to apply anywhere:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A value that changes at runtime must never choose between structural branches that emit the same child.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you catch yourself writing &lt;code&gt;if (x) { Wrapper { content() } } else { content() }&lt;/code&gt;, you've scheduled a subtree teardown for whenever &lt;code&gt;x&lt;/code&gt; flips — and it takes text state, scroll position, focus, and in-flight animations with it. Vary the modifier, not the structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trap 3 — keyboard avoidance runs in opposite directions per platform.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not strictly an overlay bug, but the overlay is how I found it, and it's the one most likely to catch you off guard.&lt;/p&gt;

&lt;p&gt;On Android (edge-to-edge, insets model) the window doesn't resize, so the only thing that lifts a focused field is shrinking its scroll container — it needs &lt;code&gt;imePadding()&lt;/code&gt;. On iOS, Compose Multiplatform lifts the whole scene until the focused element clears the keyboard, so that &lt;em&gt;same&lt;/em&gt; &lt;code&gt;imePadding()&lt;/code&gt; avoids the keys a second time: an empty band above the keyboard, and a flat background where the translucent keyboard should be revealing content.&lt;/p&gt;

&lt;p&gt;Same modifier, correct on exactly one platform, and nothing in the build warns you. The durable fix is a small platform-branching helper instead of a bare &lt;code&gt;imePadding()&lt;/code&gt; on scroll containers, so nobody has to remember the rule at 6pm on a Friday. Two more traps share the room: &lt;code&gt;isImeVisible&lt;/code&gt; is Android-only in Compose Multiplatform (shared code compiles, the iOS build fails at link — derive visibility from the &lt;code&gt;ime&lt;/code&gt; bottom inset instead), and an emulator with a hardware keyboard reports the IME as hidden, so keyboard tests pass for the wrong reason. (That split is a deep enough hole to be its own article — the last part of this series.)&lt;/p&gt;

&lt;p&gt;None of these three are Liquid Glass bugs. They're &lt;em&gt;boundary&lt;/em&gt; bugs — the standing cost of letting a native view own a strip of a screen that Compose is laying out around. The native bar itself is small. Teaching the rest of the app that the bottom edge had become negotiable is what took the work.&lt;/p&gt;

&lt;h2&gt;
  
  
  What This Costs
&lt;/h2&gt;

&lt;p&gt;I gave up real things, and you should price them before copying this. The official native shell against this bar-only overlay, point by point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Glass tab bar&lt;/strong&gt; — both give you it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Glass toolbar and nav bar&lt;/strong&gt; — the shell gets those too; the overlay doesn't, because Compose still draws everything above the tab bar.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Native back-swipe&lt;/strong&gt; — the shell inherits SwiftUI's; the overlay uses Compose's own transitions and predictive back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Where navigation actually lives&lt;/strong&gt; — the shell mirrors it into SwiftUI; the overlay keeps one shared &lt;code&gt;NavHost&lt;/code&gt; and has nothing to mirror.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep links, auth guards, interrupting dialogs&lt;/strong&gt; — the shell gives them a second, per-platform home; the overlay leaves them in shared Kotlin, untouched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost to back out&lt;/strong&gt; — unwind the shell, versus delete one &lt;code&gt;if&lt;/code&gt; branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your app's navigation is simple, take JetBrains' path — you get more native surface for less bridge. If your navigation carries authentication guards, deep-link replay, and interrupting dialogs that you have already made identical across platforms, the overlay buys you the chrome without putting any of that behavior on a second, platform-specific footing.&lt;/p&gt;

&lt;p&gt;And price the inset work honestly. The bridge is small; the consequences of a floating bar are not. Budget for the three bugs above.&lt;/p&gt;

&lt;p&gt;That's the whole decision. It isn't about glass; it's about who owns the back stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  What It Costs to Turn Off
&lt;/h2&gt;

&lt;p&gt;One boolean and an availability check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kt"&gt;FeatureFlags&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;liquidGlassNav&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;#available&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iOS&lt;/span&gt; &lt;span class="mf"&gt;26.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;LiquidGlassRootView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="c1"&gt;// native glass shell&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;ComposeView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;              &lt;span class="c1"&gt;// the shared Compose bottom bar, exactly as before&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Delete the &lt;code&gt;if&lt;/code&gt; branch and the feature is gone, with no archaeology left in shared code. iOS 18 users and Android never entered it. That property was designed in from the first commit, and it's the reason I was willing to ship a platform-specific navigation shell into a codebase whose entire thesis is shared behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  So Is It Still One App?
&lt;/h2&gt;

&lt;p&gt;Yes — and it's the claim I'd defend hardest.&lt;/p&gt;

&lt;p&gt;There is one &lt;code&gt;NavHost&lt;/code&gt;. One container. Deep links resolve through the same shared router on both platforms; the public/private guard, the session-expired dialog, the pending-deep-link replay, and onboarding are the same Kotlin. Adding a screen is one declaration, and it works on iOS 26 with a glass bar, on iOS 18 with a Material bottom bar, and on Android — without anyone opening Xcode.&lt;/p&gt;

&lt;p&gt;What the iOS 26 user gets is Apple's material, Apple's capsule, and Apple's slide behavior, on Apple's schedule — for a couple hundred lines of glue, none of which knows anything about what the app actually does.&lt;/p&gt;

&lt;p&gt;Native feel was never a rendering problem. It's a question of where you draw the boundary — and, having drawn one, how little you're willing to move across it.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This is the opening piece of a six-part series on running Compose Multiplatform in production — next up, the rebuild that got this app from 848 shared lines to 93% shared, with every number counted from both codebases.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you've adopted Liquid Glass in a CMP app — the official way or some third way I haven't thought of — I'd genuinely like to see it in the comments.&lt;/p&gt;

&lt;p&gt;Drop a ❤️ if this was useful — it helps more developers find it.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>ios</category>
      <category>swift</category>
      <category>compose</category>
    </item>
    <item>
      <title>I Got Tired of Reverse-Engineering My Own Architecture. So I Built a Tool to See It.</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sat, 21 Mar 2026 06:40:47 +0000</pubDate>
      <link>https://dev.to/shivathapaa/i-got-tired-of-reverse-engineering-my-own-architecture-so-i-built-a-tool-to-see-it-2f3e</link>
      <guid>https://dev.to/shivathapaa/i-got-tired-of-reverse-engineering-my-own-architecture-so-i-built-a-tool-to-see-it-2f3e</guid>
      <description>&lt;p&gt;There's a moment that every developer on a growing Kotlin or Android project will recognize.&lt;/p&gt;

&lt;p&gt;A new engineer joins the team. They want to understand how the system is structured before they write their first line of code. So they open the project. They see 30 modules. They ask: "Where should I start?"&lt;/p&gt;

&lt;p&gt;And nobody has a good answer.&lt;/p&gt;

&lt;p&gt;The architecture diagram on the wiki is from 18 months ago. The tech spec references modules that were renamed twice. The most honest answer is: "Just dig into the Gradle files and figure it out."&lt;/p&gt;

&lt;p&gt;I've been on both sides of that conversation. And it bothered me enough that I decided to fix it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem Nobody Names Directly
&lt;/h2&gt;

&lt;p&gt;When you're working on a 5-module project, architecture is obvious. Everything fits in your head.&lt;/p&gt;

&lt;p&gt;Past 15–20 modules, something changes. The system becomes too large to hold in working memory. And because Gradle builds succeed even when your dependency graph is a mess, the degradation is &lt;em&gt;silent&lt;/em&gt;. There's no compiler warning for "your data layer now depends on your presentation layer." No test failure for "two feature modules have secretly coupled to each other."&lt;/p&gt;

&lt;p&gt;By the time someone notices - usually during a painful refactoring, a CI build time regression, or an attempt to extract a feature into a separate app - the technical debt has compounded for months.&lt;/p&gt;

&lt;p&gt;The root cause is that the real architecture of a multi-module project lives inside the Gradle dependency graph, not in any diagram, doc, or spec.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Already Exists (And Why It Wasn't Enough)
&lt;/h2&gt;

&lt;p&gt;I looked at every tool in the Kotlin ecosystem before building anything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency Analysis Plugin&lt;/strong&gt; by autonomousapps is excellent - but it focuses on unused dependencies and ABI surface. It tells you what you &lt;em&gt;don't&lt;/em&gt; need. Not what your structure &lt;em&gt;looks like&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Module Graph Assert&lt;/strong&gt; by jraska generates Graphviz graphs and does have a CI enforcement layer - &lt;code&gt;assertModuleGraph&lt;/code&gt; can fail the build using regex-based rules and max tree height checks. The gap is elsewhere: no interactive visualization, no historical tracking, and KMP support requires manually adding source set configuration because the plugin has no native awareness of KMP module boundaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ArchUnit&lt;/strong&gt; is the closest thing to what I wanted. But it's Java-centric, has no visualization layer, and building meaningful tests against a Gradle project structure requires significant boilerplate that feels out of place in a Kotlin codebase.&lt;/p&gt;

&lt;p&gt;None of them do all three things: &lt;strong&gt;visualize&lt;/strong&gt;, &lt;strong&gt;enforce&lt;/strong&gt;, and &lt;strong&gt;track&lt;/strong&gt;. Not together, not in a single tool, not with KMP awareness. That gap is what Aalekh is designed to fill.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Aalekh Does
&lt;/h2&gt;

&lt;p&gt;Two lines to set up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// settings.gradle.kts&lt;/span&gt;
&lt;span class="nf"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"io.github.shivathapaa.aalekh"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;One command to run:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew aalekhReport
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A self-contained HTML file opens in your browser. No server. No CDN. No internet connection required. The entire report - including the D3.js visualization engine and all your graph data - is embedded in a single file you can share over Slack or attach to a PR.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Want to see it before installing anything?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://shivathapaa.github.io/Aalekh/assets/report_samples/nowinandroid.html" rel="noopener noreferrer"&gt;Now in Android - Google's reference Android app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://shivathapaa.github.io/Aalekh/assets/report_samples/nowinandroid_withcyclic.html" rel="noopener noreferrer"&gt;Same project with a cyclic dependency injected&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://shivathapaa.github.io/Aalekh/assets/report_samples/tallyo.html" rel="noopener noreferrer"&gt;Tallyo - a Kotlin Multiplatform project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  The interactive graph
&lt;/h3&gt;

&lt;p&gt;Every module is a node. Every dependency is an edge. Node color tells you the module type at a glance - Android App is blue, KMP is purple, JVM Library is amber.&lt;/p&gt;

&lt;p&gt;The useful part is what gets &lt;em&gt;highlighted&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Circular dependency nodes pulse with a red ring&lt;/li&gt;
&lt;li&gt;God modules (high fan-in AND fan-out) glow orange&lt;/li&gt;
&lt;li&gt;The critical build path is traced in purple&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can filter edges by type, click any node to open the module inspector, and see fan-in, fan-out, instability index, and transitive dependency count per module.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cycle detection that doesn't lie to you
&lt;/h3&gt;

&lt;p&gt;One decision I spent real time on: test dependencies in cycle detection.&lt;/p&gt;

&lt;p&gt;Consider this common pattern:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="py"&gt;core&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;datastore      → (nothing)&lt;/span&gt;
&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="py"&gt;core&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s"&gt;datastore-test → :core:datastore (testImplementation)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Naive cycle detection flags this as a cycle. It isn't - it's a test fixtures module depending on the module it provides fixtures for.&lt;/p&gt;

&lt;p&gt;Aalekh separates &lt;strong&gt;main-code cycles&lt;/strong&gt; (real architectural errors, &lt;code&gt;aalekhCheck&lt;/code&gt; fails on these) from &lt;strong&gt;test-only cycles&lt;/strong&gt; (shown as info, never fail the build). This distinction means you can actually trust the violations you see.&lt;/p&gt;
&lt;h3&gt;
  
  
  Architecture metrics
&lt;/h3&gt;

&lt;p&gt;The Metrics panel computes per-module numbers that turn subjective architectural conversations into objective data:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;What it tells you&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Fan-out&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;How many modules this module directly depends on&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Fan-in&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;How many modules depend on this one&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Instability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;fanOut / (fanIn + fanOut)&lt;/code&gt; - 0 is stable, 1 is unstable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Transitive deps&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Total modules reachable from this one&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Critical path&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Longest dependency chain - constrains build parallelism&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;God modules&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;High fan-in AND fan-out - hardest to change safely&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These aren't vanity metrics. They're signals for where refactoring attention should go first.&lt;/p&gt;
&lt;h3&gt;
  
  
  CI enforcement
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew aalekhCheck
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Fails the build on violations. Automatically wired into &lt;code&gt;./gradlew check&lt;/code&gt;. Writes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;aalekh-results.xml&lt;/code&gt; - JUnit XML, works with every CI system natively&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;aalekh-results.json&lt;/code&gt; - full machine-readable report with graph, summary, violations, and metadata&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GitHub Actions:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run architecture check&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./gradlew aalekhCheck&lt;/span&gt;

&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Upload Aalekh report&lt;/span&gt;
  &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v4&lt;/span&gt;
  &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always()&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aalekh-report&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build/reports/aalekh/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Aalekh Is Built Internally
&lt;/h2&gt;

&lt;p&gt;I tried to apply the same principles to Aalekh's own structure that Aalekh helps you enforce in your project:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aalekh-model      ← Data classes only. Zero dependencies.
aalekh-analysis   ← Pure Kotlin. No Gradle API. Graph algorithms.
aalekh-report     ← Report generators. No Gradle API.
aalekh-gradle     ← Gradle API lives here and nowhere else.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Gradle API only appears in the outermost module. Everything in &lt;code&gt;aalekh-analysis&lt;/code&gt; and &lt;code&gt;aalekh-report&lt;/code&gt; is pure Kotlin - fully unit-testable without Gradle on the classpath. The DFS cycle detection, topological sort, and instability computation all run in milliseconds in plain JUnit 5. Only the Gradle integration tests are slow.&lt;/p&gt;
&lt;h3&gt;
  
  
  Configuration cache compatibility
&lt;/h3&gt;

&lt;p&gt;Gradle 9.x enables configuration cache by default. Task actions run in a separate JVM from project configuration - you cannot capture live &lt;code&gt;Project&lt;/code&gt;, &lt;code&gt;Configuration&lt;/code&gt;, or &lt;code&gt;Dependency&lt;/code&gt; objects inside a task.&lt;/p&gt;

&lt;p&gt;The solution: all dependency data is serialized to plain &lt;code&gt;String&lt;/code&gt; maps during configuration (inside &lt;code&gt;provider { }&lt;/code&gt; lambdas), passed as &lt;code&gt;@Input&lt;/code&gt; primitives to &lt;code&gt;AalekhExtractTask&lt;/code&gt;, which writes &lt;code&gt;graph.json&lt;/code&gt;. Report and check tasks read from that file. No live Gradle objects survive into any task action.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Configuration phase:
  provider { buildSubprojectData(rootProject) }  ← runs at config time
  → Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt;                    ← plain strings, CC-safe

Execution phase:
  AalekhExtractTask  → writes graph.json
  AalekhReportTask   → reads graph.json → writes index.html
  AalekhCheckTask    → reads graph.json → writes results.xml + results.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The intermediate &lt;code&gt;graph.json&lt;/code&gt; is also what makes the tool extensible - anyone can build additional tooling on top of it without touching the plugin.&lt;/p&gt;


&lt;h2&gt;
  
  
  What's Coming in v0.2.0
&lt;/h2&gt;

&lt;p&gt;The current release covers visualization and cycle enforcement. Next up: &lt;strong&gt;layer enforcement via a Kotlin DSL&lt;/strong&gt; - the thing I originally built this for.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;aalekh&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;layers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;layer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"domain"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":core:domain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;":feature:*:domain"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;layer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":core:data"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;":feature:*:data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;canOnlyDependOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"domain"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;layer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"presentation"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":feature:*:ui"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;":app"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;canOnlyDependOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"domain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;featureIsolation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;featurePattern&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;":feature:**"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;When &lt;code&gt;:feature:checkout:data&lt;/code&gt; adds &lt;code&gt;implementation(project(":feature:checkout:ui"))&lt;/code&gt;, the build fails with a message pointing to the exact file and line to fix.&lt;/p&gt;

&lt;p&gt;Teams with existing violations can migrate gradually using severity overrides:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;rules&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"layer-dependency"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;severity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Severity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WARNING&lt;/span&gt;  &lt;span class="c1"&gt;// see violations without blocking CI yet&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Promote to &lt;code&gt;ERROR&lt;/code&gt; when you're ready to enforce.&lt;/p&gt;


&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Gradle 9.0+&lt;/li&gt;
&lt;li&gt;JVM 11+ (v0.1.1 fixes a crash that affected JDK 11–20)&lt;/li&gt;
&lt;li&gt;Kotlin 2.3+&lt;/li&gt;
&lt;li&gt;AGP 9.x+ (if Android)&lt;/li&gt;
&lt;li&gt;Kotlin DSL only — &lt;code&gt;settings.gradle.kts&lt;/code&gt; required (Groovy not supported)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// settings.gradle.kts&lt;/span&gt;
&lt;span class="nf"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"io.github.shivathapaa.aalekh"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew aalekhReport
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/shivathapaa" rel="noopener noreferrer"&gt;
        shivathapaa
      &lt;/a&gt; / &lt;a href="https://github.com/shivathapaa/Aalekh" rel="noopener noreferrer"&gt;
        Aalekh
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Aalekh is a Gradle plugin that extracts, visualizes, and enforces architectural rules across any Gradle multi-module project - Kotlin Multiplatform, Android, JVM, or any Gradle project.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Aalekh&lt;/h1&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;a rel="noopener noreferrer" href="https://github.com/shivathapaa/Aalekh/assets/images/aalekh_banner.png"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fshivathapaa%2FAalekh%2Fassets%2Fimages%2Faalekh_banner.png" alt="Aalekh banner" width="100%"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;a href="https://kotlinlang.org" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/01a2f6748d33c4ed203d72d2bde1be8ce30ff6cf65acdd41c9f9edc9b5e6ca0c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4b6f746c696e2d322e332b2d3746353246462e7376673f6c6f676f3d6b6f746c696e266c6f676f436f6c6f723d7768697465" alt="Kotlin"&gt;&lt;/a&gt;
  &lt;a href="https://gradle.org" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/5233828cd1f2686e9cf18f184a7b04f8f2156bbe39b1e983203c9b2b5f423dbf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f477261646c652d392e782d3032333033412e7376673f6c6f676f3d677261646c65266c6f676f436f6c6f723d7768697465" alt="Gradle"&gt;&lt;/a&gt;
  &lt;a href="https://central.sonatype.com/artifact/io.github.shivathapaa/aalekh-model" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/d3fc20f002e9b209c6fe2b900d8bb3711342517ba33b9aa3ed071b91846a29cc/68747470733a2f2f696d672e736869656c64732e696f2f6d6176656e2d63656e7472616c2f762f696f2e6769746875622e73686976617468617061612f61616c656b682d6d6f64656c3f6c6162656c3d4d6176656e25323043656e7472616c26636f6c6f723d626c7565" alt="Maven Central"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;a href="https://plugins.gradle.org/plugin/io.github.shivathapaa.aalekh" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/056296240fd2379555984ef7efe1c69566e0ff3ec6f4553ae861d61bc24a0c53/68747470733a2f2f696d672e736869656c64732e696f2f677261646c652d706c7567696e2d706f7274616c2f762f696f2e6769746875622e73686976617468617061612e61616c656b683f6c6162656c3d477261646c65253230506c7567696e253230506f7274616c26636f6c6f723d303233303341266c6f676f3d677261646c65" alt="Gradle Plugin Portal"&gt;&lt;/a&gt;
  &lt;a href="https://docs.gradle.org/current/userguide/configuration_cache.html" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/1208f4988f7fd6a62f2d5cfca78460e4f6940eeaf641dc1d7408aa412f22f28b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6e66696775726174696f6e25323043616368652d636f6d70617469626c652d627269676874677265656e" alt="Configuration Cache"&gt;&lt;/a&gt;
  &lt;a href="https://github.com/shivathapaa/Aalekh/LICENSE" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/a549a7a30bacba7bfceebdc207a8e86c3f2c02995a2527640dca30048fd2b64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d417061636865253230322e302d626c75652e737667" alt="License"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Architecture Visualization &amp;amp; Linting for Gradle Multi-Module Projects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Aalekh is a Gradle plugin that extracts, visualizes, and enforces architectural rules across any
Gradle multi-module project - Kotlin Multiplatform, Android, JVM, or any Gradle project. It gives
teams three capabilities that no existing tool provides together: an &lt;strong&gt;interactive module graph&lt;/strong&gt;, a
&lt;strong&gt;Kotlin DSL for architecture rule enforcement&lt;/strong&gt;, and &lt;strong&gt;historical metrics tracking&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Sample Reports&lt;/h3&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Now in Android App&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/shivathapaa/Aalekh/assets/report_samples/nowinandroid.html" rel="noopener noreferrer"&gt;View locally&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://shivathapaa.github.io/Aalekh/assets/report_samples/nowinandroid.html" rel="nofollow noopener noreferrer"&gt;View on GitHub Pages&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now in Android App - with cyclic dependency&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/shivathapaa/Aalekh/assets/report_samples/nowinandroid_withcyclic.html" rel="noopener noreferrer"&gt;View locally&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://shivathapaa.github.io/Aalekh/assets/report_samples/nowinandroid_withcyclic.html" rel="nofollow noopener noreferrer"&gt;View on GitHub Pages&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Tallyo (KMP)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/shivathapaa/Aalekh/assets/report_samples/tallyo.html" rel="noopener noreferrer"&gt;View locally&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://shivathapaa.github.io/Aalekh/assets/report_samples/tallyo.html" rel="nofollow noopener noreferrer"&gt;View on GitHub Pages&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Sample Report Demo&lt;/h3&gt;
&lt;/div&gt;
&lt;p&gt;
  &lt;a href="https://github.com/shivathapaa/Aalekh/assets/report_samples/nowinandroid.html" rel="noopener noreferrer"&gt;
    &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fshivathapaa%2FAalekh%2Fassets%2Fimages%2Fnowinandroid_sample.gif" alt="Now in Android App Demo" width="600"&gt;
  &lt;/a&gt;
&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Why Aalekh?&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;div class="table-wrapper-paragraph"&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;table&gt;

&lt;thead&gt;

&lt;tr&gt;

&lt;th&gt;Tool&lt;/th&gt;

&lt;th&gt;Visualizes&lt;/th&gt;

&lt;th&gt;Enforces rules&lt;/th&gt;

&lt;th&gt;Tracks metrics&lt;/th&gt;

&lt;th&gt;KMP-aware&lt;/th&gt;

&lt;/tr&gt;

&lt;/thead&gt;

&lt;tbody&gt;

&lt;tr&gt;

&lt;td&gt;&lt;strong&gt;Aalekh&lt;/strong&gt;&lt;/td&gt;

&lt;td&gt;&lt;strong&gt;✓&lt;/strong&gt;&lt;/td&gt;

&lt;td&gt;&lt;strong&gt;✓&lt;/strong&gt;&lt;/td&gt;

&lt;td&gt;&lt;strong&gt;✓&lt;/strong&gt;&lt;/td&gt;

&lt;td&gt;&lt;strong&gt;✓&lt;/strong&gt;&lt;/td&gt;

&lt;/tr&gt;

&lt;/tbody&gt;

&lt;/table&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Aalekh &lt;strong&gt;visualizes, enforces, and tracks&lt;/strong&gt; - in a single plugin, with zero external dependencies
beyond the browser.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Quick Start&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;1. Add to &lt;code&gt;settings.gradle.kts&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight highlight-source-kotlin notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;plugins {
    id(&lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;io.github.shivathapaa.aalekh&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;) version &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;0.3.0&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;…
&lt;/div&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/shivathapaa/Aalekh" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Free, open source (Apache 2.0), actively maintained. If you run it on your project and something breaks - or works better than expected - I'd genuinely like to know in the comments.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;The next post will cover how the layer rule engine works under the hood - glob pattern resolution, violation message generation, and the severity migration system. Follow to get notified.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>gradle</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Pagination, WebSockets, and Coordinated Refresh in Kotlin Multiplatform</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sun, 15 Mar 2026 15:53:29 +0000</pubDate>
      <link>https://dev.to/shivathapaa/pagination-websockets-and-coordinated-refresh-in-kotlin-multiplatform-h5</link>
      <guid>https://dev.to/shivathapaa/pagination-websockets-and-coordinated-refresh-in-kotlin-multiplatform-h5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Part 4 of 4&lt;/strong&gt; — The final part of this series. Parts &lt;a href="https://dev.to/shivathapaa/setting-up-ktor-the-right-way-in-kotlin-multiplatform-lg3"&gt;1&lt;/a&gt;, &lt;a href="https://dev.to/shivathapaa/bulletproof-error-handling-in-kotlin-multiplatform-with-responseresult-and-saferequest-387o"&gt;2&lt;/a&gt;, and &lt;a href="https://dev.to/shivathapaa/from-data-to-ui-with-requeststate-in-kotlin-multiplatform-45j8"&gt;3&lt;/a&gt; cover the Ktor client setup, error-handling layer, and &lt;code&gt;RequestState&lt;/code&gt; pattern that this part extends.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;The first three parts built the foundation — a configured Ktor client, a typed error-handling layer, and a state management pattern that carries data cleanly from repository to UI. For a large class of screens, that's everything you need.&lt;/p&gt;

&lt;p&gt;But some screens need more. A feed that loads pages of data as the user scrolls. A chat screen that receives real-time messages over a WebSocket connection while simultaneously loading paginated history. A screen that refreshes six independent data sources at once and needs to wait for all of them before hiding the pull-to-refresh indicator.&lt;/p&gt;

&lt;p&gt;These aren't edge cases — they're common in any serious mobile app. And the good news is that none of them require new primitives. They all extend the same architecture you've already seen.&lt;/p&gt;

&lt;p&gt;This part covers &lt;code&gt;GenericPagingSource&lt;/code&gt; for pagination, &lt;code&gt;PagingStateHandler&lt;/code&gt; for rendering every pagination state cleanly in the UI, &lt;code&gt;WebSocketClientConnection&lt;/code&gt; and &lt;code&gt;observeWebSocketMessages&lt;/code&gt; for real-time data, and &lt;code&gt;RefreshManager&lt;/code&gt; for coordinated multi-source refreshes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pagination with &lt;code&gt;GenericPagingSource&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Writing a &lt;code&gt;PagingSource&lt;/code&gt; for each paginated endpoint is repetitive. The structure is always the same: fetch a page, extract items from the response, determine the next page key, handle errors. The only things that change are the endpoint and the types.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GenericPagingSource&lt;/code&gt; eliminates that repetition entirely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GenericPagingSource&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;fetchPage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;extractItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;getNextPageKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PagingSource&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getRefreshKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PagingState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;anchorPosition&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;anchorPosition&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;closestPageToPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anchorPosition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;prevKey&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;plus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;closestPageToPosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anchorPosition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;nextKey&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;minus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoadParams&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;LoadResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;FIRST_PAGE_INDEX&lt;/span&gt;

            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetchPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;
                        &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;LoadResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Empty response"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                    &lt;span class="nc"&gt;LoadResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extractItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                        &lt;span class="n"&gt;prevKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;FIRST_PAGE_INDEX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;nextKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getNextPageKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="nc"&gt;LoadResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;prevKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nextKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="nc"&gt;LoadResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;LoadResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;FIRST_PAGE_INDEX&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four parameters define the entire pagination behaviour for any endpoint:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;fetchPage&lt;/code&gt;&lt;/strong&gt; — a suspend function that takes a page number and returns a &lt;code&gt;ResponseResult&amp;lt;R&amp;gt;&lt;/code&gt;. This is always a repository call. It returns &lt;code&gt;ResponseResult&lt;/code&gt; — the same type every other repository function in this architecture returns. &lt;code&gt;GenericPagingSource&lt;/code&gt; handles all three outcomes: success with data, empty response, and error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;extractItems&lt;/code&gt;&lt;/strong&gt; — given the full response type &lt;code&gt;R&lt;/code&gt;, pull out the &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; of actual items. This handles the typical API envelope pattern where your response carries &lt;code&gt;{ results: [...], nextPage: 2 }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;getNextPageKey&lt;/code&gt;&lt;/strong&gt; — return the next page number, or null when you've reached the last page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;onError&lt;/code&gt; / &lt;code&gt;onEmpty&lt;/code&gt;&lt;/strong&gt; — optional callbacks for side effects, like showing a snackbar or updating a separate piece of UI state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using It in a ViewModel
&lt;/h3&gt;

&lt;p&gt;A paginated list with type filtering, in about fifteen lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchItemsByType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?):&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PagingData&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ItemDetails&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Pager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PagingConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;pageSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;prefetchDistance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;enablePlaceholders&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;pagingSourceFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;GenericPagingSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;fetchPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchPaginatedItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pageSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="n"&gt;extractItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;details&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="n"&gt;getNextPageKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cachedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;cachedIn(viewModelScope)&lt;/code&gt; is important. Without it, the &lt;code&gt;PagingData&lt;/code&gt; flow recreates from scratch on every subscription — including on recomposition. For a content-heavy feed where the user scrolls quickly, tune &lt;code&gt;prefetchDistance&lt;/code&gt; accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;feedItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PagingData&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FeedItem&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PagingConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;pageSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;prefetchDistance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// start loading next page when 5 items from the end&lt;/span&gt;
        &lt;span class="n"&gt;enablePlaceholders&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;pagingSourceFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GenericPagingSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;fetchPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="n"&gt;feedRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchFeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pageSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;extractItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;getNextPageKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cachedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pagination That Also Updates Other State
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fetchPage&lt;/code&gt; lambda is just a suspend function — there's nothing stopping you from doing more inside it. Consider a chat screen where the first page response also carries metadata about the conversation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Flow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PagingData&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ChatMessage&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PagingConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pageSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefetchDistance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enablePlaceholders&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;pagingSourceFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GenericPagingSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;fetchPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chatId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                &lt;span class="c1"&gt;// On first page only — extract metadata and bootstrap dependent state&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isSuccess&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chatMeta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isOwnChat&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                            &lt;span class="n"&gt;chatMeta&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatMeta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                                &lt;span class="n"&gt;isOwnChat&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;isOwnChat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                &lt;span class="n"&gt;isFeedbackPending&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;isFeedbackPending&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                &lt;span class="n"&gt;isClosed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;isClosed&lt;/span&gt;
                            &lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;isOwnChat&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFeedbackPending&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="nf"&gt;fetchFeedbackOptions&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="nf"&gt;initializeSocketConnection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="n"&gt;response&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;extractItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;getNextPageKey&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextPage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cachedIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard &lt;code&gt;_uiState.value.chatMeta.isOwnChat == null&lt;/code&gt; ensures bootstrapping runs exactly once — on the first page load, not on every subsequent page or refresh. This is a production pattern worth internalising: pagination and dependent state initialisation co-located where they belong, without tangling the ViewModel's &lt;code&gt;init&lt;/code&gt; block with timing dependencies.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rendering Pagination State with &lt;code&gt;PagingStateHandler&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;GenericPagingSource&lt;/code&gt; takes care of fetching pages. But the Paging library produces more than just data — it produces load states. There's an initial load, an append load as the user scrolls, a prepend load for reverse-layout lists, a refresh triggered by pull-to-refresh, and error states for each. Each one needs a corresponding UI.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PagingStateHandler&lt;/code&gt; encapsulates all of that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;PagingStateHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="n"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;initialLoadingContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;DefaultLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;refreshLoadingContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;initialLoadingContent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;appendLoadingContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;refreshLoadingContent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;prependLoadingContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;appendLoadingContent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;initialErrorContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="nc"&gt;BoxScope&lt;/span&gt;&lt;span class="p"&gt;.(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;DefaultInitialError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;appendErrorContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="nc"&gt;ColumnScope&lt;/span&gt;&lt;span class="p"&gt;.(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;DefaultAppendError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;prependErrorContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="nc"&gt;ColumnScope&lt;/span&gt;&lt;span class="p"&gt;.(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;DefaultPrependError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;emptyContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;          &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="nc"&gt;BoxScope&lt;/span&gt;&lt;span class="p"&gt;.(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;DefaultEmptyContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;fullyLoadedContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="nc"&gt;ColumnScope&lt;/span&gt;&lt;span class="p"&gt;.(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;showFullyLoaded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;loadState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadState&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;isInitialLoading&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;isInitialError&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;isEmpty&lt;/span&gt;          &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NotLoading&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;itemCount&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;isFullyLoaded&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;endOfPaginationReached&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isInitialLoading&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;itemCount&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;isInitialError&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;error&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;
                &lt;span class="nf"&gt;initialErrorContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxSize&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;emptyContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxSize&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;isInitialLoading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;initialLoadingContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxSize&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Column&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxSize&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;prependLoadingContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;error&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prepend&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;
                            &lt;span class="nf"&gt;prependErrorContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;

                    &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

                    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                            &lt;span class="nf"&gt;appendLoadingContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                        &lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;error&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;
                            &lt;span class="nf"&gt;appendErrorContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="n"&gt;isFullyLoaded&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;showFullyLoaded&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                            &lt;span class="nf"&gt;fullyLoadedContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Refresh overlay — shown when pull-to-refresh fires while items are already visible&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;LoadState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isInitialLoading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;refreshLoadingContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;align&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Alignment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TopCenter&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The State Machine
&lt;/h3&gt;

&lt;p&gt;Five scenarios, resolved in priority order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initial error&lt;/strong&gt; — the very first load failed. The entire content area is replaced with an error UI and a retry button&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empty&lt;/strong&gt; — first load succeeded but returned nothing. Shows an empty placeholder. This is a &lt;em&gt;successful&lt;/em&gt; state, not an error — the distinction matters for the message you show the user&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Initial loading&lt;/strong&gt; — first load in progress. Shows a full-area shimmer or skeleton&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Active&lt;/strong&gt; — normal operating state. A &lt;code&gt;Column&lt;/code&gt; manages prepend loading/error at the top, the &lt;code&gt;content&lt;/code&gt; lambda in the middle, and append loading/error/fully-loaded at the bottom&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh overlay&lt;/strong&gt; — pull-to-refresh while items are visible. A lightweight indicator overlaid at the top, not replacing the existing content&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;content&lt;/code&gt; Lambda Pattern
&lt;/h3&gt;

&lt;p&gt;In practice the &lt;code&gt;content&lt;/code&gt; lambda is almost always left empty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"PagingFooter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;PagingStateHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;feedItems&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;initialLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;FeedLoadingSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;refreshLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;FeedLoadingSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;emptyContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;EmptyPlaceholder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;stringResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;no_items_found&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;icon&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;painterResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drawable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty_icon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;animateItem&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* items rendered in the LazyColumn above */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PagingStateHandler&lt;/code&gt; is placed as a single item &lt;em&gt;after&lt;/em&gt; all the real list items in the &lt;code&gt;LazyColumn&lt;/code&gt;. The actual items are rendered in the &lt;code&gt;items { }&lt;/code&gt; block above it — as usual. &lt;code&gt;PagingStateHandler&lt;/code&gt; handles only state UI: the shimmer on first load, the error view, the empty placeholder, the append spinner.&lt;/p&gt;

&lt;h3&gt;
  
  
  Per-Screen Customisation
&lt;/h3&gt;

&lt;p&gt;Every parameter except &lt;code&gt;lazyPagingItems&lt;/code&gt; has a default. Override only what's different:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Card-based list — custom skeletons, no "all loaded" footer&lt;/span&gt;
&lt;span class="nc"&gt;PagingStateHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;initialLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;CardListSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cardHeight&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;132&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;appendLoadingContent&lt;/span&gt;  &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;CardListSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;refreshLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;CardListSkeleton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;emptyContent&lt;/span&gt;          &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;EmptyState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;84&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;showFullyLoaded&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;animateItem&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* items rendered above */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Chat screen — message skeletons, prepend handled for reverse layout&lt;/span&gt;
&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ChatPagingFooter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;PagingStateHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;initialLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MessageSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;refreshLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MessageSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;animateItem&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* messages rendered above */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the same principle as &lt;code&gt;displayResultWithDefaults&lt;/code&gt; from Part 3 — convention over configuration, with full override capability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-Time Data with WebSockets
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;WebSocketClientConnection&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;WebSocketClientConnection&lt;/code&gt; is a self-contained WebSocket manager. You give it a URL and a coroutine scope, and it handles the rest — connection establishment, message routing, ping/pong, reconnection on failure, and clean shutdown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WebSocketClientConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;socketUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;autoReconnect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;_connectionState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Disconnected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;connectionState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_connectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;_messages&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableSharedFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SharedFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asSharedFlow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;outgoingMessages&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Channel&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;Channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BUFFERED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Disconnected&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Connecting&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Connected&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="kd"&gt;data class&lt;/span&gt;  &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The public surface is small by design: two observable flows (&lt;code&gt;connectionState&lt;/code&gt;, &lt;code&gt;messages&lt;/code&gt;), one method for sending (&lt;code&gt;sendMessage&lt;/code&gt;), one for shutdown (&lt;code&gt;disconnect&lt;/code&gt;). All the complexity — mutex-protected session, separate coroutines for incoming and outgoing processing, exponential backoff reconnection — stays internal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connection and Reconnection
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_connectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Connected&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt;
        &lt;span class="n"&gt;_connectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Connecting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="n"&gt;reconnectJob&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;retryCount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;retryDelay&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;INITIAL_RETRY_DELAY_MS&lt;/span&gt;  &lt;span class="c1"&gt;// 1000ms&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;_connectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Connecting&lt;/span&gt;

                &lt;span class="n"&gt;httpClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urlString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socketUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;headers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Authorization"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;userRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userTokenData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;accessToken&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Accept-Language"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appLocalizationRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;languagePreference&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;sessionMutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withLock&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="n"&gt;_connectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Connected&lt;/span&gt;
                    &lt;span class="n"&gt;retryCount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
                    &lt;span class="n"&gt;retryDelay&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;INITIAL_RETRY_DELAY_MS&lt;/span&gt;

                    &lt;span class="nf"&gt;startMessageProcessing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

                    &lt;span class="n"&gt;suspendCancellableCoroutine&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;continuation&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                        &lt;span class="n"&gt;continuation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invokeOnCancellation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;handleConnectionError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retryCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retryDelay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="nf"&gt;shouldRetryConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retryCount&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;
                &lt;span class="n"&gt;retryCount&lt;/span&gt;&lt;span class="p"&gt;++&lt;/span&gt;
                &lt;span class="n"&gt;retryDelay&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retryDelay&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;BACKOFF_MULTIPLIER&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toLong&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;coerceAtMost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MAX_RETRY_DELAY_MS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;retryDelay&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;milliseconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;handleConnectionError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retryCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retryDelay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;  &lt;span class="c1"&gt;// Non-IO exceptions are not retried&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key design decisions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;suspendCancellableCoroutine&lt;/code&gt;&lt;/strong&gt; keeps the coroutine and the session alive without busy-waiting. The actual work happens in &lt;code&gt;startMessageProcessing()&lt;/code&gt;, which launches two child coroutines — one for incoming, one for outgoing&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;IOException&lt;/code&gt; is retried; other exceptions are not.&lt;/strong&gt; Network errors are transient. Programming errors, protocol violations, and coroutine cancellations are not. This prevents the reconnection loop from spinning on non-recoverable errors&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponential backoff is capped&lt;/strong&gt; — 1s → 5s max, stops after 5 consecutive failures&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The auth token is read at connection time&lt;/strong&gt;, not construction time — same principle as &lt;code&gt;defaultRequest&lt;/code&gt; in the HTTP client&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Incoming Message Processing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;processIncomingMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DefaultClientWebSocketSession&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;  &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readText&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ping&lt;/span&gt;  &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Pong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt;           &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ignore binary frames */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;EOFException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoReconnect&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;handleMessageProcessingError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CancellationException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Normal shutdown — do nothing&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoReconnect&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;handleMessageProcessingError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ping/pong is handled manually in addition to the plugin-level &lt;code&gt;pingInterval&lt;/code&gt;. Responding to server-initiated pings keeps the connection alive from the server's perspective, while &lt;code&gt;pingInterval&lt;/code&gt; handles client-initiated keepalives. Both together mean the connection survives aggressive network management on Android and iOS.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;observeWebSocketMessages&lt;/code&gt; — Decoding and Routing in One Call
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The message routing here is specific to a backend envelope format with &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt; fields. Your backend may use a different shape — only the envelope parsing would need to change.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;observeWebSocketMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WebSocketClientConnection&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
    &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onProcessing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onConnectionStateChanged&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;WebSocketClientConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ConnectionState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;json&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JsonConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parser&lt;/span&gt;

    &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;rawMessage&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;envelope&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decodeFromString&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;JsonElement&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;rawMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;envelope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;SocketResponseType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DATA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;envelope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                            &lt;span class="nf"&gt;onMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decodeFromJsonElement&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="nc"&gt;SocketResponseType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PROCESSING&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;envelope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                            &lt;span class="nf"&gt;onProcessing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decodeFromJsonElement&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onProcessing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;onProcessing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connectionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onConnectionStateChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function is &lt;code&gt;reified&lt;/code&gt; so it decodes the JSON payload into &lt;code&gt;T&lt;/code&gt; without a class reference. The two-stage decode — first into &lt;code&gt;DefaultResponseDto&amp;lt;JsonElement&amp;gt;&lt;/code&gt;, then from &lt;code&gt;JsonElement&lt;/code&gt; into &lt;code&gt;T&lt;/code&gt; — handles the envelope pattern: the outer wrapper carries a &lt;code&gt;type&lt;/code&gt; field, and the inner &lt;code&gt;data&lt;/code&gt; is the actual payload.&lt;/p&gt;

&lt;p&gt;The function also calls &lt;code&gt;client.connect()&lt;/code&gt;. Observing the messages &lt;em&gt;is&lt;/em&gt; the signal to connect — no separate connect call needed. It's idempotent: &lt;code&gt;connect()&lt;/code&gt; returns early if a connection is already in progress.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using It in a ViewModel
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;initializeSocketConnection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chatMeta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isOwnChat&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="n"&gt;webSocketClient&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createWebSocketConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;chatId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;observeWebSocketMessages&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ChatMessageDto&lt;/span&gt;&lt;span class="p"&gt;?&amp;gt;(&lt;/span&gt;
        &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webSocketClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;onMessage&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dto&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dto&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;appendMessageFromSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;onProcessing&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;dto&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;updateProcessingIndicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dto&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;onError&lt;/span&gt;      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WebSocket error: ${error.message}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;onConnectionStateChanged&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WebSocket state: $state"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCleared&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCleared&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;webSocketClient&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scope cancellation from &lt;code&gt;onCleared()&lt;/code&gt; automatically stops the collecting coroutines, but &lt;code&gt;disconnect()&lt;/code&gt; sends a proper WebSocket close frame to the server. Without it, the server may not know the client left and keeps the session allocated.&lt;/p&gt;

&lt;p&gt;The repository side stays minimal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createWebSocketConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chatId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;WebSocketClientConnection&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"${WSS_URL}ws/chat/$chatId/"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;WebSocketClientConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socketUrl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Coordinated Refresh with &lt;code&gt;RefreshManager&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;typealias&lt;/span&gt; &lt;span class="nc"&gt;RefreshManagerFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onStart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onEnd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RefreshManager&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RefreshManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;coroutineScope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;minDurationMillis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1500L&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;onRefreshStart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;onRefreshEnd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;jobs&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutableListOf&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Job&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;

    &lt;span class="nd"&gt;@Volatile&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isRefreshing&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jobProvider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;jobProvider&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;addSuspending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coroutineScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error in refresh block: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isRefreshing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="n"&gt;isRefreshing&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;

        &lt;span class="n"&gt;coroutineScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;onRefreshStart&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;delayJob&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minDurationMillis&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delayJob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joinAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;onRefreshEnd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="n"&gt;isRefreshing&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Register jobs with &lt;code&gt;add()&lt;/code&gt; or &lt;code&gt;addSuspending()&lt;/code&gt;. Call &lt;code&gt;start()&lt;/code&gt;. &lt;code&gt;RefreshManager&lt;/code&gt; fires &lt;code&gt;onRefreshStart()&lt;/code&gt;, launches all jobs concurrently alongside a minimum-duration delay, waits for all of them to finish, then fires &lt;code&gt;onRefreshEnd()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;minDurationMillis&lt;/code&gt; of 1500ms prevents the jarring flash of a spinner that appears and disappears in 200ms. If all API calls complete in 400ms, the refresh indicator still shows for the full 1.5 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refreshAllData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;refreshManager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;refreshManagerFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;onStart&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isRefreshing&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;onEnd&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isRefreshing&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fetchWeatherData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fetchTaskReminders&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fetchNotificationCount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fetchBanners&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fetchMarketPrices&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addSuspending&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;resetInputState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;refreshManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;refreshManagerFactory&lt;/code&gt; is a &lt;code&gt;typealias&lt;/code&gt; rather than a direct constructor call. In tests, you swap the factory for a synchronous or mock implementation without coroutine timing concerns. The typealias keeps injection clean without needing a full interface.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pagination + WebSockets: How They Coexist on the Same Screen
&lt;/h2&gt;

&lt;p&gt;The real payoff of this architecture is how naturally these pieces compose. A chat screen combining paginated history with live incoming messages is a common requirement — here's how both data paths coexist without interfering.&lt;/p&gt;

&lt;p&gt;Paginated history flows through &lt;code&gt;GenericPagingSource&lt;/code&gt; as &lt;code&gt;LazyPagingItems&lt;/code&gt;. New socket messages are stored in a separate &lt;code&gt;socketMessages&lt;/code&gt; list in &lt;code&gt;UiState&lt;/code&gt;. The typing indicator lives in its own &lt;code&gt;processingMessage&lt;/code&gt; field. The two paths update independent state fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;appendMessageFromSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ChatMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;updated&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;socketMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toMutableList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// prepend for reversed layout&lt;/span&gt;
        &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socketMessages&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;updateProcessingIndicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ChatMessage&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processingMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A single &lt;code&gt;LazyColumn&lt;/code&gt; renders four sections in order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;LazyColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reverseLayout&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;isOwnChat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// 1. Typing indicator — from WebSocket processing state&lt;/span&gt;
    &lt;span class="nf"&gt;item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;AnimatedVisibility&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;visible&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;processingMessage&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;isTyping&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;TypingIndicator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Real-time messages — arrived via WebSocket, not yet in pagination&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socketMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isNotEmpty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;itemsIndexed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socketMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"${msg.id}-socket-$index"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="nc"&gt;MessageBubble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 3. Historical messages — loaded via pagination&lt;/span&gt;
    &lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;itemCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;chatMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;peek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;chatMessages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MessageBubble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// 4. Pagination state — initial loading, append, empty, errors&lt;/span&gt;
    &lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ChatPagingFooter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;PagingStateHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;lazyPagingItems&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chatMessages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;initialLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MessageSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;refreshLoadingContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;MessageSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;reverseLayout = true&lt;/code&gt;, new socket messages prepended to the front appear at the bottom. Historical paged messages appear above as the user scrolls up. The typing indicator sits at the bottom above the newest message.&lt;/p&gt;

&lt;p&gt;The two data sources are completely independent. If the socket drops and reconnects, the paginated history is unaffected. If the user scrolls back through history, new messages keep arriving at the bottom. The paths don't interfere because they never share the same data structure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting It All Together
&lt;/h2&gt;

&lt;p&gt;Looking back across all four parts, here's the complete architecture:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A single, consistently configured &lt;code&gt;HttpClient&lt;/code&gt;&lt;/strong&gt; — auth, token refresh, retries, timeouts, and logging declared once, injected everywhere as a singleton.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A complete error-handling boundary at &lt;code&gt;safeRequest&lt;/code&gt;&lt;/strong&gt; — every failure mode Ktor can produce is caught, classified, and returned as a typed &lt;code&gt;ResponseResult&lt;/code&gt; with user-facing messages and error type enums.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A UI state machine in &lt;code&gt;RequestState&lt;/code&gt;&lt;/strong&gt; — the full lifecycle of any request (idle, loading, success, error, empty) flows through &lt;code&gt;executeNetworkCall&lt;/code&gt; and &lt;code&gt;executeNetworkCallWithState&lt;/code&gt; into &lt;code&gt;UiState&lt;/code&gt;, and renders through &lt;code&gt;displayResult&lt;/code&gt; and &lt;code&gt;displayResultWithDefaults&lt;/code&gt; with animated transitions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Infrastructure for the hard cases&lt;/strong&gt; — &lt;code&gt;GenericPagingSource&lt;/code&gt; for paginated lists with no boilerplate per endpoint, &lt;code&gt;PagingStateHandler&lt;/code&gt; for rendering every load state with per-screen customisable defaults, &lt;code&gt;WebSocketClientConnection&lt;/code&gt; for self-healing real-time connections, &lt;code&gt;observeWebSocketMessages&lt;/code&gt; for typed message decoding and routing, and &lt;code&gt;RefreshManager&lt;/code&gt; for coordinated multi-source refresh with minimum display duration.&lt;/p&gt;

&lt;p&gt;Every screen follows the same pattern. The repository makes a typed call. The ViewModel runs it through &lt;code&gt;executeNetworkCall&lt;/code&gt; or &lt;code&gt;executeNetworkCallWithState&lt;/code&gt;. The UI observes a &lt;code&gt;StateFlow&amp;lt;UiState&amp;gt;&lt;/code&gt; and renders through &lt;code&gt;displayResult&lt;/code&gt; or &lt;code&gt;PagingStateHandler&lt;/code&gt;. No special-casing for loading states, no inconsistent error handling, no try-catch scattered across ViewModel functions.&lt;/p&gt;

&lt;p&gt;When a new feature needs to be added, the developer writes a repository function that returns &lt;code&gt;ResponseResult&amp;lt;T&amp;gt;&lt;/code&gt;, wires it through &lt;code&gt;executeNetworkCallWithState&lt;/code&gt;, stores the result in a &lt;code&gt;RequestState&lt;/code&gt; field in &lt;code&gt;UiState&lt;/code&gt;, and renders it with &lt;code&gt;displayResultWithDefaults&lt;/code&gt;. The path is always the same. The only new code is the code that's actually unique to the feature.&lt;/p&gt;

&lt;p&gt;That's the real value of this architecture — not any single clever piece, but the consistency it creates across a codebase that will grow, be maintained by different developers, and need to absorb new requirements over time.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Thanks for reading the full series. If you found it useful, sharing it with your team or leaving a comment goes a long way. All feedback welcome.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>ktor</category>
      <category>kotlinmultiplatform</category>
    </item>
    <item>
      <title>From Data to UI with RequestState in Kotlin Multiplatform</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sun, 15 Mar 2026 15:52:13 +0000</pubDate>
      <link>https://dev.to/shivathapaa/from-data-to-ui-with-requeststate-in-kotlin-multiplatform-45j8</link>
      <guid>https://dev.to/shivathapaa/from-data-to-ui-with-requeststate-in-kotlin-multiplatform-45j8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Part 3 of 4&lt;/strong&gt; — This part builds on the &lt;code&gt;ResponseResult&lt;/code&gt; contract from &lt;a href="https://dev.to/shivathapaa/bulletproof-error-handling-in-kotlin-multiplatform-with-responseresult-and-saferequest-387o"&gt;Part 2&lt;/a&gt; and the &lt;code&gt;HttpClient&lt;/code&gt; setup from &lt;a href="https://dev.to/shivathapaa/setting-up-ktor-the-right-way-in-kotlin-multiplatform-lg3"&gt;Part 1&lt;/a&gt;. If you're jumping in here, those two parts cover the layers this one sits on top of.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;In Part 2, we built a networking layer where every call returns a &lt;code&gt;ResponseResult&amp;lt;T&amp;gt;&lt;/code&gt;. The repository knows nothing about loading state, error dialogs, or UI updates — it just makes the call and returns a typed result. Clean, testable, consistent.&lt;/p&gt;

&lt;p&gt;But the ViewModel still has to react to those results. It needs to set loading to true before the call, update state when it succeeds, show an error when it fails, and set loading to false when it's done. Across a real app with dozens of screens, this ceremony adds up fast. And if every developer handles it slightly differently, you end up with the same inconsistency problem you had in the repository layer — just one floor higher.&lt;/p&gt;

&lt;p&gt;This part solves that. We'll build &lt;code&gt;RequestState&lt;/code&gt; — the UI-side equivalent of &lt;code&gt;ResponseResult&lt;/code&gt; — and wire it to &lt;code&gt;executeNetworkCall&lt;/code&gt; and &lt;code&gt;executeNetworkCallWithState&lt;/code&gt;, which handle the entire loading/success/error lifecycle in a single call. Finally, we'll look at the Compose extensions that let your UI react to state changes in a single, animated, readable block.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Gap Between Repository and UI
&lt;/h2&gt;

&lt;p&gt;When a ViewModel calls a repository function, it gets a &lt;code&gt;ResponseResult&amp;lt;T&amp;gt;&lt;/code&gt;. But the UI doesn't just need to know whether the data arrived — it needs to know &lt;em&gt;where in the journey it is&lt;/em&gt;. Before the call starts, nothing has happened yet. During the call, something is loading. When it completes, data is available. When it fails, an error needs to be shown. When the server responds with nothing, an empty state needs to be rendered.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ResponseResult&lt;/code&gt; models the &lt;em&gt;outcome&lt;/em&gt; of a call. What we need for the UI is something that models the &lt;em&gt;state over time&lt;/em&gt;. That's &lt;code&gt;RequestState&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;RequestState&lt;/code&gt; — UI State That Tells the Whole Story
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Idle&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;data class&lt;/span&gt;  &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;data class&lt;/span&gt;  &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Empty&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Five states. Each one corresponds to a real moment in the lifecycle of a network call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Idle&lt;/code&gt;&lt;/strong&gt; — the initial state before anything has been requested. A screen that hasn't loaded yet isn't in a loading state; it hasn't started. This distinction matters when you want to avoid flashing a loading indicator on first render, or when you need to show a "tap to load" placeholder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Loading&lt;/code&gt;&lt;/strong&gt; — a request is in flight.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Success&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt; — the request succeeded. Notice that unlike &lt;code&gt;ResponseResult.Success&lt;/code&gt;, the data here is non-nullable. By the time a success state reaches the UI, it should carry real data. If the server returned nothing, that's &lt;code&gt;Empty&lt;/code&gt;, not &lt;code&gt;Success(null)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Error&lt;/code&gt;&lt;/strong&gt; — carries the same fields as &lt;code&gt;ResponseResult.Error&lt;/code&gt;: a user-facing message, the status code, the server message, and the &lt;code&gt;ErrorType&lt;/code&gt; enum for branching on failure kind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Empty&lt;/code&gt;&lt;/strong&gt; — the request succeeded but there's nothing to show. Your UI can render an empty placeholder without having to check &lt;code&gt;Success(emptyList())&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Helper Functions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Idle&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Loading&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isError&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isSuccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Empty&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getSuccessData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;       &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getSuccessDataOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getErrorMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getErrorMessageOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;isLoading()&lt;/code&gt; and &lt;code&gt;isSuccess()&lt;/code&gt; are particularly useful in ViewModels when you need to guard against duplicate requests — for example, not starting a new fetch if one is already running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Transformation
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;RequestState&lt;/code&gt; supports &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;mapNotNull&lt;/code&gt;, which work just like their &lt;code&gt;ResponseResult&lt;/code&gt; counterparts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;    &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;mapNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;?):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;    &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Non-success states pass through untouched. No pattern matching on every branch manually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inline Callbacks
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;onLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;action&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;action&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are chainable. You can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;requestState&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;renderContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onError&lt;/span&gt;   &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;showError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onEmpty&lt;/span&gt;   &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;showEmptyState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;handleResult&lt;/code&gt; — For When You Need All Five Branches
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;handleResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onIdle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;    &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onIdle&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getSuccessData&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All callbacks except &lt;code&gt;onSuccess&lt;/code&gt; are optional. This is used when multiple &lt;code&gt;RequestState&lt;/code&gt; values drive different sections of the same scrollable screen — each section with its own loading skeleton, error state, and content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;weatherForecast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handleResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;forecast&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"WeatherDetail"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;WeatherDetailCard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;forecast&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;forecast&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"WeatherError"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;NetworkErrorCard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;onRetry&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retryWeatherFetch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nf"&gt;stringResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;could_not_load_weather&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onLoading&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"WeatherLoading"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;WeatherLoadingSkeleton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;when&lt;/code&gt; expressions scattered across the screen. No &lt;code&gt;if (isLoading) show else hide&lt;/code&gt; logic. The pattern is the same every time.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;executeNetworkCall&lt;/code&gt; — Wiring the Repository to the ViewModel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;executeNetworkCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nf"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function takes a suspended call that returns a &lt;code&gt;ResponseResult&lt;/code&gt;, fires the loading callback when the call starts, and routes the result to the appropriate callback when it completes. The loading flag is always reset — in every branch, before the relevant callback fires — regardless of how the call ends.&lt;/p&gt;

&lt;p&gt;A simple ViewModel function using it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchBanners&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Job&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;executeNetworkCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;homeRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchBanners&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;banners&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_bannerList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;banners&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onError&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="c1"&gt;// handle silently or show a snackbar&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three lines of intent: what to call, what to do on success, what to do on error. No try-catch. No manual loading flag management. No &lt;code&gt;finally&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;For calls with visible loading state or file upload progress:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;submitQuery&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;executeNetworkCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submitQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;userQuery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;onProgress&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                        &lt;span class="nf"&gt;updateUploadStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UploadStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onLoadingChanged&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSending&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="nf"&gt;resetUserQuery&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="n"&gt;_uiEvents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tryEmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UiEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NavigateToChat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onError&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="n"&gt;dialogStateHolder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;showError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;code&gt;executeNetworkCallWithState&lt;/code&gt; — When the State Itself Lives in the ViewModel
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;executeNetworkCall&lt;/code&gt; is for fire-and-update patterns. But sometimes you want the &lt;code&gt;RequestState&lt;/code&gt; itself stored in &lt;code&gt;UiState&lt;/code&gt;, so the UI can react to every transition — idle, loading, success, error, empty — directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;executeNetworkCallWithState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="k"&gt;crossinline&lt;/span&gt; &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;successState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="nf"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;successState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="nf"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="n"&gt;successState&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;errorState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorType&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;errorState&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;errorState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s"&gt;"An unknown error occurred"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isNullOrBlank&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UNKNOWN&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;onAllEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;errorState&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;onLoadingChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key difference is &lt;code&gt;onAllEvents&lt;/code&gt;. This single callback receives every state transition — &lt;code&gt;Loading&lt;/code&gt;, &lt;code&gt;Success&lt;/code&gt;, &lt;code&gt;Error&lt;/code&gt;, &lt;code&gt;Empty&lt;/code&gt; — making it trivial to pipe all transitions into one ViewModel field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchWeatherForecast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;executeNetworkCallWithState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;weatherRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDailyForecast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onAllEvents&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weatherForecast&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One line in &lt;code&gt;onAllEvents&lt;/code&gt;. Loading state, success data, and any error all flow into the same &lt;code&gt;UiState&lt;/code&gt; field. No separate &lt;code&gt;isLoading&lt;/code&gt; boolean. No separate error string. The state machine &lt;em&gt;is&lt;/em&gt; the state.&lt;/p&gt;

&lt;p&gt;When you need extra logic on success — updating a cache, auto-selecting an item — &lt;code&gt;onSuccess&lt;/code&gt; is there as an opt-in side channel alongside &lt;code&gt;onAllEvents&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchNotificationCategories&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;executeNetworkCallWithState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;notificationRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchCategories&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="c1"&gt;// Auto-select the first category if none is selected yet&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectedCategory&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nf"&gt;updateSelectedCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;firstOrNull&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;onAllEvents&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both callbacks fire independently — they're not mutually exclusive.&lt;/p&gt;




&lt;h2&gt;
  
  
  The &lt;code&gt;UiState&lt;/code&gt; Pattern
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;RequestState&lt;/code&gt; pairs naturally with a &lt;code&gt;data class UiState&lt;/code&gt; exposed as a &lt;code&gt;StateFlow&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;weatherForecast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DailyForecast&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;?&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;marketPrices&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;        &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MarketPrice&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;notificationCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="nc"&gt;NotificationCount&lt;/span&gt;                  &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NotificationCount&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;isRefreshing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;        &lt;span class="nc"&gt;Boolean&lt;/span&gt;                            &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;_uiState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;RequestState&lt;/code&gt; field starts as &lt;code&gt;Idle&lt;/code&gt;. As the ViewModel fires its initial requests in &lt;code&gt;init&lt;/code&gt;, those fields transition through &lt;code&gt;Loading&lt;/code&gt; to &lt;code&gt;Success&lt;/code&gt;, &lt;code&gt;Error&lt;/code&gt;, or &lt;code&gt;Empty&lt;/code&gt;. The UI observes the &lt;code&gt;StateFlow&lt;/code&gt; and renders whatever state it currently holds.&lt;/p&gt;

&lt;p&gt;The ViewModel no longer needs three separate flags (&lt;code&gt;isLoading&lt;/code&gt;, &lt;code&gt;hasError&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt;) per network request. The entire lifecycle lives in one field, in one value, in one state machine.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bringing It to the UI — &lt;code&gt;displayResult&lt;/code&gt; and &lt;code&gt;displayResultWithDefaults&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Two Composable extension functions on &lt;code&gt;RequestState&amp;lt;T&amp;gt;&lt;/code&gt; handle rendering.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;displayResult&lt;/code&gt; — Full Control
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;displayResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parentModifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onIdle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;transitionSpec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ContentTransform&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="nf"&gt;fadeIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tween&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;togetherWith&lt;/span&gt; &lt;span class="nf"&gt;fadeOut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tween&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;contentAlignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Alignment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Alignment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Center&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;AnimatedContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parentModifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;background&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Unspecified&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;targetState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;transitionSpec&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;transitionSpec&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ContentAnimation"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;background&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Unspecified&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;contentAlignment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;contentAlignment&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;    &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onIdle&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorType&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;displayResult&lt;/code&gt; is the flexible version. All callbacks except &lt;code&gt;onSuccess&lt;/code&gt; are optional. If you don't pass &lt;code&gt;onLoading&lt;/code&gt;, nothing renders during loading — useful for non-critical sections where a spinner would be distracting. The whole thing is wrapped in &lt;code&gt;AnimatedContent&lt;/code&gt;, so every state transition cross-fades automatically.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;displayResult&lt;/code&gt; when a screen section has custom loading or error UI that doesn't match the app defaults:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;profileState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;displayResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ProfileContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onLoading&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;ProfileLoadingSkeleton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;NetworkErrorCard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;onRetry&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;onRetry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;displayResultWithDefaults&lt;/code&gt; — Convention Over Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;displayResultWithDefaults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;DefaultLoadingIndicator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onIdle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="nc"&gt;DefaultErrorText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverError&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;DefaultEmptyContent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;transitionSpec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ContentTransform&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="nf"&gt;fadeIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tween&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;togetherWith&lt;/span&gt; &lt;span class="nf"&gt;fadeOut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tween&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="n"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;AnimatedContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;background&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Unspecified&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;targetState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;transitionSpec&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;transitionSpec&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ContentAnimation"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;    &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onIdle&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onLoading&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorType&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Empty&lt;/span&gt;   &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;displayResultWithDefaults&lt;/code&gt; pre-wires app-wide defaults for each state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Loading / Idle&lt;/strong&gt; → &lt;code&gt;DefaultLoadingIndicator()&lt;/code&gt; — your animated loading indicator&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error&lt;/strong&gt; → &lt;code&gt;DefaultErrorText()&lt;/code&gt; — server message if available, falls back to user message&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empty&lt;/strong&gt; → &lt;code&gt;DefaultEmptyContent()&lt;/code&gt; — a "no data found" placeholder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the vast majority of screens, all you write is the success content — and override only what needs to be different:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;displayResultWithDefaults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;CategoryChips&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;selectedItem&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectedCategory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;onChipClick&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateSelectedCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;NetworkErrorCard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;onRetry&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchCategories&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onLoading&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;CategoryChipsLoadingSkeleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxWidth&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The convention handles the boilerplate. You handle the product logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Full Loop
&lt;/h2&gt;

&lt;p&gt;Here's the complete journey of a single data request, from first render to content on screen:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. ViewModel initialises — field starts as &lt;code&gt;Idle&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;uiState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RequestState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. ViewModel fires the request — state transitions to &lt;code&gt;Loading&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;executeNetworkCallWithState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchCategories&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;onAllEvents&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// onAllEvents fires with RequestState.Loading immediately&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. UI observes the &lt;code&gt;StateFlow&lt;/code&gt; — renders loading state&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;uiState&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collectAsState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;displayResultWithDefaults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;CategoryChips&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// AnimatedContent fades in DefaultLoadingIndicator()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Request completes — state transitions to &lt;code&gt;Success&lt;/code&gt; or &lt;code&gt;Error&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// onAllEvents fires with RequestState.Success(categories)&lt;/span&gt;
&lt;span class="c1"&gt;// _uiState updates, StateFlow emits&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. UI re-renders with the new state&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// AnimatedContent cross-fades from loading indicator to category chips&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One &lt;code&gt;StateFlow&lt;/code&gt;. No separate loading boolean. No nullable data field to null-check. No error string to conditionally show. The state machine carries all of it.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Which
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;executeNetworkCall&lt;/code&gt;&lt;/strong&gt; — use when the result updates a simple value or triggers a side effect. The data flows into a regular field, not a &lt;code&gt;RequestState&lt;/code&gt;. Good for background loads where you don't need a loading skeleton: banner lists, notification counts, task reminders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;executeNetworkCallWithState&lt;/code&gt;&lt;/strong&gt; — use when the &lt;code&gt;RequestState&lt;/code&gt; itself should be stored in &lt;code&gt;UiState&lt;/code&gt;. The UI needs to show loading skeletons and error retry buttons for this content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;displayResult&lt;/code&gt;&lt;/strong&gt; — use when you need full control over every state's UI. The loading or error composable is unique to this context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;displayResultWithDefaults&lt;/code&gt;&lt;/strong&gt; — use everywhere else. Consistent loading, error, and empty states across the whole app, with minimal code.&lt;/p&gt;




&lt;h2&gt;
  
  
  What We've Built
&lt;/h2&gt;

&lt;p&gt;Across the first three parts, the full request lifecycle is covered end to end:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;HttpClient&lt;/code&gt; (Part 1) handles auth, retries, and connection concerns&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ResponseResult&lt;/code&gt; and &lt;code&gt;safeRequest&lt;/code&gt; (Part 2) handle every failure mode at the boundary and give repositories a typed return contract&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RequestState&lt;/code&gt;, &lt;code&gt;executeNetworkCall&lt;/code&gt;, &lt;code&gt;executeNetworkCallWithState&lt;/code&gt;, and the &lt;code&gt;displayResult&lt;/code&gt; extensions (this part) carry the result through the ViewModel into the UI with animated state transitions, consistent defaults, and zero boilerplate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A developer joining the team can look at any ViewModel or any screen Composable and immediately understand what's happening. State is always a &lt;code&gt;RequestState&lt;/code&gt;. Network calls always go through &lt;code&gt;executeNetworkCall&lt;/code&gt; or &lt;code&gt;executeNetworkCallWithState&lt;/code&gt;. UI always renders through &lt;code&gt;displayResult&lt;/code&gt; or &lt;code&gt;displayResultWithDefaults&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Part 4&lt;/strong&gt;, we'll extend this architecture into the three scenarios that every production app eventually hits: paginated lists with &lt;code&gt;GenericPagingSource&lt;/code&gt; and &lt;code&gt;PagingStateHandler&lt;/code&gt;, live data streams with &lt;code&gt;WebSocketClientConnection&lt;/code&gt; and &lt;code&gt;observeWebSocketMessages&lt;/code&gt;, and coordinated multi-source refresh with &lt;code&gt;RefreshManager&lt;/code&gt;. None of them require new primitives — they all feed back into the same &lt;code&gt;RequestState&lt;/code&gt;-based pattern you now know.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this useful? Drop a ❤️ or leave a comment — it helps more developers find the series.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>jetpackcompose</category>
      <category>kotlinmultiplatform</category>
    </item>
    <item>
      <title>Bulletproof Error Handling in Kotlin Multiplatform with ResponseResult and safeRequest</title>
      <dc:creator>Shiva Thapa</dc:creator>
      <pubDate>Sat, 14 Mar 2026 14:26:27 +0000</pubDate>
      <link>https://dev.to/shivathapaa/bulletproof-error-handling-in-kotlin-multiplatform-with-responseresult-and-saferequest-387o</link>
      <guid>https://dev.to/shivathapaa/bulletproof-error-handling-in-kotlin-multiplatform-with-responseresult-and-saferequest-387o</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Part 2 of 4&lt;/strong&gt; — This part builds on the Ktor &lt;code&gt;HttpClient&lt;/code&gt; setup from Part 1. If you're jumping in here, the &lt;a href="https://dev.to/shivathapaa/setting-up-ktor-the-right-way-in-kotlin-multiplatform-lg3"&gt;previous article&lt;/a&gt; covers the client configuration this layer sits on top of.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;In Part 1, we built an &lt;code&gt;HttpClient&lt;/code&gt; that handles authentication, retries, timeouts, and logging automatically. Every request leaves the client properly configured. But when the response comes back — what happens then?&lt;/p&gt;

&lt;p&gt;In most codebases I've seen, the answer is: &lt;em&gt;it depends on who wrote the repository&lt;/em&gt;. One repository wraps everything in a try-catch and returns null on failure. Another throws exceptions and lets the ViewModel sort it out. A third returns a &lt;code&gt;Boolean&lt;/code&gt;. Over time, the UI layer ends up with an inconsistent patchwork of null checks, exception handlers, and loading state flags scattered across every screen.&lt;/p&gt;

&lt;p&gt;The architecture in this series solves that problem at the data layer. The goal is simple: every network call, regardless of what it fetches or which repository it lives in, returns the same type. The UI always knows exactly what shape of data to expect. And nobody writes a try-catch in a repository ever again.&lt;/p&gt;

&lt;p&gt;The two pieces that make this work are &lt;code&gt;ResponseResult&lt;/code&gt; and &lt;code&gt;safeRequest&lt;/code&gt;. Let's build them.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem with Raw Exceptions
&lt;/h2&gt;

&lt;p&gt;When you call &lt;code&gt;client.get("some/endpoint")&lt;/code&gt; in Ktor, a lot can go wrong. The network might be down. The server might return a 403. The JSON might have a field with the wrong type. The request might time out. Each of these is a different exception class — &lt;code&gt;IOException&lt;/code&gt;, &lt;code&gt;ClientRequestException&lt;/code&gt;, &lt;code&gt;SerializationException&lt;/code&gt;, &lt;code&gt;HttpRequestTimeoutException&lt;/code&gt;, and more.&lt;/p&gt;

&lt;p&gt;If you let those propagate up as exceptions, your ViewModel has to catch them. But which ones? What does it do with them? How does it distinguish between "no internet" and "user is unauthorized" and "server is down"? How does it get a user-friendly error message?&lt;/p&gt;

&lt;p&gt;The answer shouldn't be: &lt;em&gt;figure it out at call sites&lt;/em&gt;. The answer should be: wrap everything at the boundary, classify it, and give callers structured results.&lt;/p&gt;

&lt;p&gt;That's exactly what &lt;code&gt;ResponseResult&lt;/code&gt; does.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;ResponseResult&lt;/code&gt; — The Return Type for Every Network Call
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Serializable&lt;/span&gt;
&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Serializable&lt;/span&gt;
    &lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;

    &lt;span class="nd"&gt;@Serializable&lt;/span&gt;
    &lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;

    &lt;span class="nd"&gt;@Serializable&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Empty&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;

    &lt;span class="nd"&gt;@Serializable&lt;/span&gt;
    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;NETWORK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;SERVER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;CLIENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;SERIALIZATION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;TIMEOUT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;UNAUTHORIZED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;FORBIDDEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;PAYMENT_NOT_VERIFIED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// domain-specific — adapt to your own app&lt;/span&gt;
        &lt;span class="nc"&gt;TICKET_NOT_GENERATED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// domain-specific — adapt to your own app&lt;/span&gt;
        &lt;span class="nc"&gt;UNKNOWN&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
        &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Empty&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three states. That's all a network call can return.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Success&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt; wraps the deserialized response body. The data is nullable because some successful responses carry no body — a 204 No Content, for instance. We model that with &lt;code&gt;Empty&lt;/code&gt; rather than &lt;code&gt;Success(null)&lt;/code&gt;, but the nullable type gives you flexibility when mapping between layers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Error&lt;/code&gt;&lt;/strong&gt; carries four pieces of information. &lt;code&gt;message&lt;/code&gt; is a user-facing string — something you can show directly on screen. &lt;code&gt;serverMessage&lt;/code&gt; is the raw message from the server's error response, if one exists. &lt;code&gt;code&lt;/code&gt; is the HTTP status code. And &lt;code&gt;errorType&lt;/code&gt; is an enum that lets the UI or ViewModel branch on the &lt;em&gt;kind&lt;/em&gt; of failure without string-matching error messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Empty&lt;/code&gt;&lt;/strong&gt; represents a successful response with no body. This is meaningfully different from an error and deserves its own state.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;ErrorType&lt;/code&gt; Enum
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;ErrorType&lt;/code&gt; enum is one of those things that looks like overkill until you actually need it. Consider what the UI needs to do differently for each of these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;UNAUTHORIZED&lt;/code&gt; — redirect to login&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NETWORK&lt;/code&gt; — show "check your connection"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SERVER&lt;/code&gt; — show "something went wrong on our end, try again"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TIMEOUT&lt;/code&gt; — show "that took too long, try again"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NOT_FOUND&lt;/code&gt; — show "this content doesn't exist anymore"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PAYMENT_NOT_VERIFIED&lt;/code&gt;, &lt;code&gt;TICKET_NOT_GENERATED&lt;/code&gt; — domain-specific flows unique to your app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without &lt;code&gt;ErrorType&lt;/code&gt;, the UI has to match on status codes or parse error strings to drive different behaviours. That's fragile. With &lt;code&gt;ErrorType&lt;/code&gt;, you just &lt;code&gt;when&lt;/code&gt; on it.&lt;/p&gt;

&lt;p&gt;The custom types at the bottom (&lt;code&gt;PAYMENT_NOT_VERIFIED&lt;/code&gt;, &lt;code&gt;TICKET_NOT_GENERATED&lt;/code&gt;) are a production detail worth noticing. When your backend returns non-standard status codes for business logic errors — like &lt;code&gt;402&lt;/code&gt; for an unverified payment or &lt;code&gt;424&lt;/code&gt; for a failed ticket — you map them to domain-specific error types here. Replace these with whatever your own backend uses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Transformation Operators
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ResponseResult&lt;/code&gt; ships with a set of transformation functions that make it composable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Transform success data to another type — errors pass through unchanged&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Chain results — useful when one result feeds into another&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;flatMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Fold into a single value across all three states&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;

&lt;span class="c1"&gt;// Inline callbacks for chaining&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Extraction helpers&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getOrElse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getOrThrow&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;map&lt;/code&gt; function is the one you'll use constantly in repositories. When your API returns a &lt;code&gt;ResponseResult&amp;lt;UserDto&amp;gt;&lt;/code&gt; and you need to return a &lt;code&gt;ResponseResult&amp;lt;User&amp;gt;&lt;/code&gt;, you call &lt;code&gt;.map { it.toDomain() }&lt;/code&gt;. The mapping only runs if the result is a &lt;code&gt;Success&lt;/code&gt; — errors and empty states pass through unchanged. No try-catch, no null check.&lt;/p&gt;

&lt;p&gt;In a repository, this looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchBanners&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Banner&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BannerDto&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;networkClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safeRequest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some/endpoint/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;safeRequest&lt;/code&gt; call returns a &lt;code&gt;ResponseResult&amp;lt;List&amp;lt;BannerDto&amp;gt;&amp;gt;&lt;/code&gt;. The &lt;code&gt;.map&lt;/code&gt; transforms the DTO list into domain models. If the request failed, the &lt;code&gt;Error&lt;/code&gt; passes straight through — the mapper function never executes. The caller gets back clean domain types with zero extra handling.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;safeRequest&lt;/code&gt; — The Safety Net Around Every Request
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;safeRequest&lt;/code&gt; is an extension function on &lt;code&gt;HttpClient&lt;/code&gt;. It's the only way we make HTTP requests in this codebase:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;requestBuilder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpRequestBuilder&lt;/span&gt;&lt;span class="p"&gt;.()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;requestBuilder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;handleResponse&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;handleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three lines. Execute the request, handle the response, or catch any exception that escapes. The caller configures the request through &lt;code&gt;requestBuilder&lt;/code&gt; — URL, method, body, parameters — and gets back a typed &lt;code&gt;ResponseResult&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling the Response
&lt;/h3&gt;

&lt;p&gt;When the request completes without throwing, &lt;code&gt;handleResponse&lt;/code&gt; maps the HTTP status code to the right &lt;code&gt;ResponseResult&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt;

            &lt;span class="mi"&gt;204&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

            &lt;span class="mi"&gt;401&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"AuthError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Token expired or invalid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Your session has expired. Please log in again to continue."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UNAUTHORIZED&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="mi"&gt;403&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ForbiddenError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Access denied for resource"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"You don't have permission to access this."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FORBIDDEN&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="mi"&gt;404&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"NotFoundError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Resource not found at ${response.request.url}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We couldn't find what you're looking for. It may have been moved or removed."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NOT_FOUND&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;retryAfter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Retry-After"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toLongOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="mi"&gt;30L&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"RateLimitError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Rate limit exceeded. Retry after $retryAfter seconds"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"You're doing that too fast. Please wait $retryAfter seconds and try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CLIENT&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="mi"&gt;402&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"PaymentNotVerified"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Payment Not Verified"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We couldn't verify your payment. Please check your payment details or contact support."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PAYMENT_NOT_VERIFIED&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="mi"&gt;424&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"TicketNotGenerated"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Ticket not generated"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Your payment was successful, but we ran into a problem issuing your ticket. Please contact support."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TICKET_NOT_GENERATED&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;499&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ClientError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Client error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Something went wrong with your request. Please try again or contact support."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CLIENT&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;599&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ServerError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Server error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We're experiencing a temporary issue on our end. Please try again in a few moments."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SERVER&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
                &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"UnexpectedStatus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Unexpected response status: ${response.status}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Something unexpected happened. Please try again or contact support."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UNKNOWN&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;handleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few patterns here worth calling out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;serverMessage&lt;/code&gt; extraction uses &lt;code&gt;runCatching&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
&lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;getOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a request fails, the server usually returns a JSON body explaining why — something like &lt;code&gt;{ "message": "This email is already registered." }&lt;/code&gt;. That message is often more informative than a generic "client error" string. But the error response might not always be parseable — the server might be down and return HTML, or the body might be empty. &lt;code&gt;runCatching { }.getOrNull()&lt;/code&gt; extracts the server message silently when it's there, and returns null when it's not.&lt;/p&gt;

&lt;p&gt;This is the purpose of &lt;code&gt;DefaultResponseDto&lt;/code&gt; — a simple wrapper that matches the standard error envelope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Serializable&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="nd"&gt;@SerialName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nd"&gt;@SerialName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nd"&gt;@SerialName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your backend uses a different envelope shape, adjust &lt;code&gt;DefaultResponseDto&lt;/code&gt; accordingly. The pattern stays the same.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Specific status codes are handled before ranges.&lt;/strong&gt; &lt;code&gt;402&lt;/code&gt; and &lt;code&gt;424&lt;/code&gt; are handled before the &lt;code&gt;400..499&lt;/code&gt; catch-all. Kotlin's &lt;code&gt;when&lt;/code&gt; evaluates branches top-to-bottom, so the specific cases win. This is how you map non-standard business logic codes to domain-specific &lt;code&gt;ErrorType&lt;/code&gt; values without breaking the general client error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;Retry-After&lt;/code&gt; header is read on 429:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;retryAfter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Retry-After"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toLongOrNull&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="mi"&gt;30L&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a server enforces rate limits, it often tells you how long to wait. Including that duration in the user-facing message is a small touch that makes a real difference in UX.&lt;/p&gt;




&lt;h3&gt;
  
  
  Handling Exceptions
&lt;/h3&gt;

&lt;p&gt;When the request throws — not a failed HTTP response, but an actual exception — &lt;code&gt;handleException&lt;/code&gt; classifies it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@OptIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ExperimentalSerializationApi&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;SerializationException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"SerializationError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Serialization failed: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We received an unexpected data format. Please try again or update the app."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SERIALIZATION&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;JsonConvertException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"JsonConvertError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"JSON conversion failed: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We had trouble reading the server's response. Please try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SERIALIZATION&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;MissingFieldException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MissingFieldError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Missing required field(s): ${e.missingFields.joinToString()}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We received incomplete information from the server. Please try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SERIALIZATION&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"NetworkIOError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Network I/O error: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Couldn't connect to the server. Please check your internet connection and try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NETWORK&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;SocketTimeoutException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"SocketTimeoutError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Socket timeout: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The server is taking too long to respond. Please check your connection and try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;408&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TIMEOUT&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ConnectTimeoutException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ConnectTimeoutError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Connection timeout: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We couldn't reach the server. Please check your internet connection and try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;408&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TIMEOUT&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;HttpRequestTimeoutException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"RequestTimeoutError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Request timeout: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The request is taking longer than expected. Please try again in a moment."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;408&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TIMEOUT&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;UnresolvedAddressException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"UnresolvedAddressError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Failed to resolve address: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Couldn't reach the server. Please check your internet connection and try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NETWORK&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ClientRequestException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ClientRequestError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Client request failed with status ${e.response.status.value}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Something went wrong with your request. Please try again or contact support."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CLIENT&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;ServerResponseException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ServerResponseError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Server error with status ${e.response.status.value}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"We're experiencing a temporary issue on our end. Please try again shortly."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SERVER&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;CancellationException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"CancellationError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Request was cancelled: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The request was cancelled. Please try again if this wasn't intentional."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UNKNOWN&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;WebSocketException&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"WebSocketError"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"WebSocket error: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Lost connection to the server. Please check your internet and try again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NETWORK&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"UnhandledException"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Unhandled exception: ${e::class.simpleName} - ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;userMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Something unexpected happened. Please try again or contact support."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UNKNOWN&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exception map covers the full landscape of what Ktor can throw. Four categories:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serialization failures&lt;/strong&gt; (&lt;code&gt;SerializationException&lt;/code&gt;, &lt;code&gt;JsonConvertException&lt;/code&gt;, &lt;code&gt;MissingFieldException&lt;/code&gt;) — the response body can't be parsed. Maybe the API changed a field type, or a required field is missing. &lt;code&gt;SERIALIZATION&lt;/code&gt; signals to the UI that an app update might be needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Network failures&lt;/strong&gt; (&lt;code&gt;IOException&lt;/code&gt;, &lt;code&gt;UnresolvedAddressException&lt;/code&gt;, &lt;code&gt;WebSocketException&lt;/code&gt;) — the request never reached the server. The user is probably offline or on a flaky connection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Timeout failures&lt;/strong&gt; (&lt;code&gt;SocketTimeoutException&lt;/code&gt;, &lt;code&gt;ConnectTimeoutException&lt;/code&gt;, &lt;code&gt;HttpRequestTimeoutException&lt;/code&gt;) — the server is reachable but slow. Prompt the user to try again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP-level exceptions&lt;/strong&gt; (&lt;code&gt;ClientRequestException&lt;/code&gt;, &lt;code&gt;ServerResponseException&lt;/code&gt;) — Ktor throws these on 4xx/5xx when &lt;code&gt;expectSuccess&lt;/code&gt; is enabled. With our &lt;code&gt;handleResponse&lt;/code&gt; approach we handle status codes ourselves, but catching these as a fallback costs nothing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;CancellationException&lt;/code&gt; note:&lt;/strong&gt; We deliberately catch it here rather than re-throwing it. In strict coroutine convention, &lt;code&gt;CancellationException&lt;/code&gt; should be re-thrown. In our case the function returns a value rather than throwing — the call site sees an error result and moves on, while the coroutine itself is already being cancelled externally. The behaviour is correct. Be aware of this if you're adapting this code.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;logAndReturnError&lt;/code&gt; — Structured Logging at the Boundary
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;reified&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;logAndReturnError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;userMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;errorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ErrorType&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"""
        [$tag] $message
        │ Exception: ${exception?.let { it::class.simpleName } ?: "N/A"}
        │ Message:   ${exception?.message ?: "No details"}
        │ Stacktrace: ${exception?.stackTraceToString() ?: "No stacktrace"}
        """&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trimIndent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;serverMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serverMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;errorType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;errorType&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every error path funnels through this one function. It logs the technical details — tag, internal message, exception class, full stacktrace — and constructs a &lt;code&gt;ResponseResult.Error&lt;/code&gt; with a user-facing message. The separation is intentional: the log has everything a developer needs to diagnose the problem; the &lt;code&gt;ResponseResult&lt;/code&gt; has what the UI needs to communicate it.&lt;/p&gt;




&lt;h2&gt;
  
  
  What a Repository Looks Like With This in Place
&lt;/h2&gt;

&lt;p&gt;Here's a complete repository implementation. No try-catch, no error handling, no loading state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeRepositoryImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;networkClient&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HomeRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchBanners&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Banner&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BannerDto&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;networkClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;safeRequest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some/endpoint/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fetchCategoryItems&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CategoryWithItems&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;networkClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;safeRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CategoryItemDto&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some/endpoint/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;submitFilterSelection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;selectedItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CategoryItem&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CategoryWithItems&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;networkClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;safeRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CategoryItemDto&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"some/endpoint/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;setBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;selectedItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toPayload&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
            &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each function does three things: configure the request, make the call, map the result. The entire error surface — network down, server error, serialization failure, auth expiry — is handled by &lt;code&gt;safeRequest&lt;/code&gt; and &lt;code&gt;handleResponse&lt;/code&gt;, invisible to the repository. The repository's only job is to know the URL, the HTTP method, and how to map the DTO to a domain type.&lt;/p&gt;

&lt;p&gt;This also makes repositories easy to test. You mock the &lt;code&gt;HttpClient&lt;/code&gt; or the repository interface directly — no exception-throwing behaviour to account for.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Note on the &lt;code&gt;DefaultResponseDto&lt;/code&gt; Envelope
&lt;/h2&gt;

&lt;p&gt;Many APIs wrap their success responses in an envelope too, not just errors. If your backend returns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Success"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"isVerified"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then your response type would be &lt;code&gt;DefaultResponseDto&amp;lt;YourActualDataDto&amp;gt;&lt;/code&gt; and you'd map the inner data directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;networkClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;safeRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DefaultResponseDto&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;YourDataDto&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"your/endpoint/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;IllegalStateException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Missing data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or more cleanly with &lt;code&gt;flatMap&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flatMap&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;envelope&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;envelope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;ResponseResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The operators on &lt;code&gt;ResponseResult&lt;/code&gt; are designed exactly for this kind of layered unwrapping.&lt;/p&gt;




&lt;h2&gt;
  
  
  What We've Built
&lt;/h2&gt;

&lt;p&gt;After this part, the networking layer has a complete, consistent error-handling contract:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every repository function returns &lt;code&gt;ResponseResult&amp;lt;T&amp;gt;&lt;/code&gt; — success, error, or empty&lt;/li&gt;
&lt;li&gt;HTTP status codes are mapped to typed &lt;code&gt;ErrorType&lt;/code&gt; values the UI can act on&lt;/li&gt;
&lt;li&gt;Server error messages are extracted and forwarded when available&lt;/li&gt;
&lt;li&gt;Every exception Ktor can throw is caught and classified&lt;/li&gt;
&lt;li&gt;Technical errors go to logs; user-facing messages go to the UI&lt;/li&gt;
&lt;li&gt;Repository code is three lines per function, with zero error handling&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;The ViewModel still needs to react to these results — update loading state, show errors, navigate on success. In &lt;strong&gt;Part 3&lt;/strong&gt;, we'll build the &lt;code&gt;RequestState&lt;/code&gt; pattern and the &lt;code&gt;executeNetworkCall&lt;/code&gt; / &lt;code&gt;executeNetworkCallWithState&lt;/code&gt; functions that make that side of things just as clean. We'll also look at the &lt;code&gt;displayResult&lt;/code&gt; Composable extension that lets your UI react to state changes in a single, animated, readable block.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this useful? Drop a ❤️ or leave a comment — it helps more developers find the series.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>ktor</category>
      <category>kotlinmultiplatform</category>
    </item>
  </channel>
</rss>
