OfflineQueue

class OfflineQueue(worker: KmpWorker, repository: TaskRepository, networkMonitor: NetworkMonitor)

Offline-first task queue that persists tasks when the device is offline and automatically replays them when connectivity is restored.

Flow:

enqueue(request)

isOnline?
↓ YES → execute immediately via KmpWorker
↓ NO → persist to TaskRepository (status = PENDING)

network restored

replay() → re-enqueue all PENDING tasks

Recovery on app restart: Pending tasks survive app termination because they are persisted to SQLDelight. On next app start, call replay manually or ensure start is called so the queue can re-enqueue them.

Usage:

val queue = OfflineQueue(kmpWorker, repository, networkMonitor)
queue.start()

queue.enqueue(myTaskRequest)

Constructors

Link copied to clipboard
constructor(worker: KmpWorker, repository: TaskRepository, networkMonitor: NetworkMonitor)

Functions

Link copied to clipboard
suspend fun enqueue(request: TaskRequest)

Enqueues a task request.

Link copied to clipboard
suspend fun pendingCount(): Int

Returns the number of tasks currently pending in the offline queue. A task is counted if its status is PENDING or RUNNING.

Link copied to clipboard
suspend fun replay()

Manually triggers replay of all pending tasks from the database.

Link copied to clipboard
fun start()

Starts the offline queue.

Link copied to clipboard
fun stop()

Stops the coroutine scope. Call in cleanup/onDestroy if needed.