Skip to content

Conversation

harrytmthy
Copy link
Owner

@harrytmthy harrytmthy commented May 30, 2025

Overview

This PR introduces support for instance-scoped SafeBoxStateListener, enabling consumers to observe the internal state of a specific SafeBox instance without relying on global observers.

State updates are lifecycle-bound to the instance, and automatically cleaned up during safeBox.close(), preventing leaks and ensuring proper teardown.


Motivation

Previously, there was no way to observe what a SafeBox was doing at runtime unless you had external coordination. This addition makes it possible to:

  • Display "in progress" indicators during writing.
  • React to closure events in a scoped, low-overhead manner.
  • Or simply track SafeBox behavior for close observations.

This is especially useful for ViewModel-scoped SafeBox instances that may be short-lived and tied to screen transitions.

API

SafeBox.create(
    context = ctx,
    fileName = "my_prefs",
    listener = SafeBoxStateListener { state ->
        when (state) {
            WRITING -> // do something
            IDLE ->    // do something
            CLOSED -> cleanUp()
        }
    }
)

Behavior Notes

  • Listener receives the initial IDLE state upon creation.
  • State changes:
    • WRITING during write() and delete().
    • IDLE once operations complete.
    • CLOSED when close() is called.
  • No manual removal is necessary. Listener is instance-bound and disposed automatically during teardown.

Partially addresses #12.
The remaining work (global observer support) will be completed in a follow-up PR.

@harrytmthy harrytmthy self-assigned this May 30, 2025
@harrytmthy harrytmthy added the enhancement New feature or request label May 30, 2025
@harrytmthy harrytmthy merged commit 9cf93cb into main May 30, 2025
3 checks passed
@harrytmthy harrytmthy deleted the feat/add-safebox-state-listener branch May 30, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant