JetpackEntity

data class JetpackEntity(val id: String = UUID.randomUUID().toString(), val name: String, val price: Double, val userId: String = String(), val lastUpdated: Long = 0, val lastSynced: Long = 0, val needsSync: Boolean = false, val deleted: Boolean = false, val syncAction: SyncAction = SyncAction.NONE)

Room database entity representing a Jetpack item with offline-first sync capabilities.

This entity implements an offline-first pattern with automatic sync tracking. All local modifications are tracked via sync metadata fields, enabling bidirectional synchronization with a remote data source (e.g., Firestore, REST API).

Sync Metadata Pattern

The entity uses several fields to track synchronization state:

  • lastUpdated: Timestamp of the last local modification (set on every update)

  • lastSynced: Timestamp of the last successful sync with remote

  • needsSync: Flag indicating this entity has pending changes to sync

  • deleted: Soft delete flag (entity is hidden from UI but kept for sync)

  • syncAction: The type of sync operation to perform (SyncAction)

Lifecycle States

  1. Created Locally:

    • needsSync = true, syncAction = UPSERT, lastUpdated > lastSynced

  2. Updated Locally:

    • needsSync = true, syncAction = UPSERT, lastUpdated > lastSynced

  3. Deleted Locally (Soft Delete):

    • deleted = true, needsSync = true, syncAction = DELETE

  4. Synced:

    • needsSync = false, syncAction = NONE, lastSynced = current timestamp

  5. Updated from Remote:

    • Upserted with remote data, lastUpdated and lastSynced updated

Usage Example

// Create new entity (needs sync)
val newJetpack = JetpackEntity(
name = "Jetpack MK-1",
price = 499.99,
userId = currentUserId,
lastUpdated = System.currentTimeMillis(),
needsSync = true,
syncAction = SyncAction.UPSERT
)

// Update existing entity
val updated = existingJetpack.copy(
price = 399.99,
lastUpdated = System.currentTimeMillis(),
needsSync = true,
syncAction = SyncAction.UPSERT
)

// Soft delete entity
val softDeleted = existingJetpack.copy(
deleted = true,
needsSync = true,
syncAction = SyncAction.DELETE,
lastUpdated = System.currentTimeMillis()
)

// Mark as synced after successful remote operation
val synced = existingJetpack.copy(
needsSync = false,
syncAction = SyncAction.NONE,
lastSynced = System.currentTimeMillis()
)

See also

JetpackDao

for database operations

LocalDataSource

for data access abstraction

for sync operation types

Constructors

Link copied to clipboard
constructor(id: String = UUID.randomUUID().toString(), name: String, price: Double, userId: String = String(), lastUpdated: Long = 0, lastSynced: Long = 0, needsSync: Boolean = false, deleted: Boolean = false, syncAction: SyncAction = SyncAction.NONE)

Properties

Link copied to clipboard

Soft delete flag. When true, the entity is hidden from normal queries but kept in the database for synchronization. Allows deletion to be synced with remote before permanent removal.

Link copied to clipboard
val id: String

Unique identifier (UUID). Auto-generated if not provided.

Link copied to clipboard

Timestamp (milliseconds since epoch) when this entity was last successfully synced with the remote data source. Updated after successful sync operations.

Link copied to clipboard

Timestamp (milliseconds since epoch) when this entity was last modified locally. Updated on every create/update operation. Used to determine if entity needs sync.

Link copied to clipboard

Display name of the jetpack. Required field for domain logic.

Link copied to clipboard

Flag indicating this entity has pending local changes that need to be synced to remote. Set to true on create/update/delete, reset to false after successful sync.

Link copied to clipboard

Price of the jetpack in the app's currency. Must be non-negative.

Link copied to clipboard

The type of synchronization action to perform during the next sync operation. See SyncAction for possible values.

Link copied to clipboard

Identifier of the user who owns this jetpack. Used for multi-user filtering.