Offline-First Architecture on Android with Jetpack Compose and Room
Designing a deterministic offline-first data layer in Kotlin using Android Room, Kotlin Flows, and Supabase delta sync for MyBBM.
Why Network-First Fails in Real Field Conditions
While building MyBBM, an Android fuel-efficiency calculator and trip-cost estimator used across East Kalimantan, one reality became clear immediately: network connectivity is an intermittent enhancement, not a guarantee.
If an app makes a direct HTTP call inside a ViewModel before rendering fuel-tier calculations, the user sees loading spinners in low-signal areas or loses their trip calculation altogether when switching cells along the Samarinda-Balikpapan corridor.
The Single Source of Truth (SSOT) Pattern
In an offline-first architecture, the Jetpack Compose UI layer never reads directly from a remote network response. Instead:
- 01.Android Room is the Single Source of Truth: All UI screens collect reactive
Flow<List<FuelPriceEntity>>streams emitted directly by Room DAOs. - 02.Non-Blocking Background Sync: Network synchronization with Supabase runs asynchronously and writes updated rows into Room.
- 03.Automatic UI Recomposition: As soon as Room commits a transaction, the active
Flowemits the fresh snapshot and Jetpack Compose recomposes seamlessly.
Defining the Local Room Entity and DAO
Here is how we model regional fuel tiers and trip history in Room with deterministic timestamps:
package id.mybbm.data.local
import androidx.room.Dao
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import kotlinx.coroutines.flow.Flow
@Entity(tableName = "fuel_prices")
data class FuelPriceEntity(
@PrimaryKey val fuelCode: String,
val displayName: String,
val octaneNumber: Int,
val priceIdrPerLiter: Int,
val regionCode: String,
val updatedAtEpochMs: Long
)
@Dao
interface FuelPriceDao {
@Query("SELECT * FROM fuel_prices WHERE regionCode = :region ORDER BY priceIdrPerLiter ASC")
fun observePricesByRegion(region: String): Flow<List<FuelPriceEntity>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsertAll(prices: List<FuelPriceEntity>)
}Coordinating Offline Reads and Supabase Sync in the Repository
The repository exposes the local Flow immediately while offering a resilient syncRegionalPrices suspend function that catches network failures without crashing the UI:
package id.mybbm.data.repository
import id.mybbm.data.local.FuelPriceDao
import id.mybbm.data.local.FuelPriceEntity
import id.mybbm.data.remote.SupabaseFuelApi
import kotlinx.coroutines.flow.Flow
class FuelRepository(
private val dao: FuelPriceDao,
private val remoteApi: SupabaseFuelApi
) {
fun observeRegionalPrices(regionCode: String): Flow<List<FuelPriceEntity>> {
return dao.observePricesByRegion(regionCode)
}
suspend fun syncRegionalPrices(regionCode: String): Result<Int> = runCatching {
val remoteDtos = remoteApi.fetchPricesForRegion(regionCode)
val entities = remoteDtos.map { dto ->
FuelPriceEntity(
fuelCode = dto.code,
displayName = dto.name,
octaneNumber = dto.octane,
priceIdrPerLiter = dto.priceIdr,
regionCode = dto.region,
updatedAtEpochMs = dto.updatedAtEpochMs
)
}
dao.upsertAll(entities)
entities.size
}
}Exposing Immutable State to Jetpack Compose
By combining stateIn inside the ViewModel, our Jetpack Compose screen renders in under a single frame from local disk storage while quietly displaying a HUD badge (SYNCED or OFFLINE CACHE) at the top of the calculator. Users get instant trip-cost estimates anywhere on the island.