B4X Store
jdashboardUI / Tables & Data / jdProperties
Tables & Data

jdProperties

Persistent settings with KeyValueStore, in-memory caching, default values and display text

Search all jdashboardUI docs
Source
jdashboardUI.b4xlib · Public API only
Store language: en

Tables & Data · jdashboardUI 24 · B4J / KeyValueStore

jdProperties is a lightweight class for storing persistent application settings and properties. It uses KeyValueStore internally and adds three useful layers: an in-memory cache, default values, and an optional display text associated with each property.

It is a good fit for user preferences, application state, UI options, recent paths, configuration values and other small pieces of data that should survive between runs.

Highlights

  • Persistence powered by KeyValueStore.
  • Automatically creates its storage folder under File.DirApp.
  • In-memory cache to avoid repeated reads.
  • Default values through SetDefaultValue.
  • Separate stored value and display text through PutDisplayValueText.
  • Existence checks with Exists.
  • Individual and full deletion.
  • Optional access to the underlying KeyValueStore.
  • Public isInitialized field.
  • No Designer, Views or events.

Dependency

jdashboardUI 24 declares the dependency:

KeyValueStore

You do not need to create a KeyValueStore manually when using jdProperties.


Initialize

Declaration:

Private Properties As jdProperties

Typical initialization:

Properties.Initialize("Settings", "settings.db")

Public signature:

Initialize(FolderName As String, name As String)

On B4J, the class:

  1. creates File.DirApp/FolderName when needed;
  2. opens the file passed in name;
  3. initializes the cache and default-value map;
  4. sets isInitialized = True when initialization succeeds.

Example:

Private Properties As jdProperties

Private Sub B4XPage_Created (Root1 As B4XView)
    Properties.Initialize("Settings", "settings.db")

    If Properties.isInitialized Then
        Log("Settings ready")
    End If
End Sub

One-time initialization

If the instance is already initialized, another call to Initialize returns immediately.

Use another jdProperties instance when you need a different folder or database file.


Store and read values

Put

Stores the value in KeyValueStore and updates the cache:

Properties.Put("theme", "dark")
Properties.Put("window_width", 1280)
Properties.Put("remember_session", True)

Signature:

Put(k As String, v As Object)

Get

Checks the in-memory cache first. When the key is not cached yet, it is loaded from KeyValueStore and then cached.

Dim Theme As String = Properties.Get("theme")
Dim Width As Int = Properties.Get("window_width")
Dim Remember As Boolean = Properties.Get("remember_session")

Signature:

Get(k As String) As Object

The return type is Object; B4J can assign it to a compatible target type.


Default values

Register defaults before reading values:

Properties.SetDefaultValue("theme", "dark")
Properties.SetDefaultValue("font_size", 14)
Properties.SetDefaultValue("show_toolbar", True)

Dim Theme As String = Properties.Get("theme")
Dim FontSize As Int = Properties.Get("font_size")
Dim ShowToolbar As Boolean = Properties.Get("show_toolbar")

When the key is not stored, Get uses the value registered by SetDefaultValue.

SetDefaultValue

SetDefaultValue(k As String, defaultValue As Object)

GetDefaultValue

Dim DefaultTheme As Object = Properties.GetDefaultValue("theme")

Signature:

GetDefaultValue(k As String) As Object

It returns Null when no default has been registered.

Defaults live only in the instance's internal Map. They are not persisted to settings.db.


Stored value + display text

jdProperties can keep an internal value and a separate user-facing label.

Example:

Properties.Put("language", "es")
Properties.PutDisplayValueText("language", "Spanish")

Dim LanguageCode As String = Properties.Get("language")
Dim LanguageText As String = Properties.GetDisplayValueText("language")

Log(LanguageCode) ' es
Log(LanguageText) ' Spanish

This is useful when the application stores an ID or code but displays a friendly label.

PutDisplayValueText

PutDisplayValueText(k As String, displayValueText As String)

Internally, the display value is stored under:

k & "_DisplayValueText"

GetDisplayValueText

GetDisplayValueText(k As String) As String

It returns an empty string when no display value exists.

Avoid manually creating keys ending in _DisplayValueText; that suffix is reserved by this class for the associated display value.


Exists

Check whether a property exists:

If Properties.Exists("theme") Then
    Log(Properties.Get("theme"))
Else
    Log("theme has not been saved")
End If

Signature:

Exists(k As String) As Boolean

The implementation checks the cache first and then falls back to KeyValueStore.ContainsKey.

When the distinction between “missing” and “present” matters, call Exists before Get:

If Properties.Exists("server_url") Then
    Dim Url As String = Properties.Get("server_url")
End If

This avoids relying on a key that may already have entered the cache during an earlier read.


Remove

Removes both the main value and its display text:

Properties.Remove("language")

Signature:

Remove(k As String)

Both cache entries are removed as well.


DeleteAllProperties

Deletes all persisted values and clears the cache:

Properties.DeleteAllProperties

Signature:

DeleteAllProperties

Important: the implementation does not clear the default-value map. After DeleteAllProperties, Get can still return defaults registered through SetDefaultValue.

Example:

Properties.SetDefaultValue("theme", "dark")
Properties.Put("theme", "light")

Properties.DeleteAllProperties

Log(Properties.Get("theme")) ' dark

GetKeyValueStore

Returns the underlying store:

Dim Store As KeyValueStore = Properties.GetKeyValueStore

Signature:

GetKeyValueStore As KeyValueStore

This is useful when you need a KeyValueStore feature that jdProperties does not expose directly.

Cache warning

If you modify the returned store directly:

Dim Store As KeyValueStore = Properties.GetKeyValueStore
Store.Put("theme", "blue")

the internal jdProperties cache is not automatically synchronized.

If theme was already cached, a later:

Log(Properties.Get("theme"))

can continue returning the previous cached value.

Use Put, Remove and DeleteAllProperties whenever possible to keep persistence and cache aligned.


Complete example: application preferences

Sub Class_Globals
    Private Root As B4XView
    Private Properties As jdProperties
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1

    Properties.Initialize("Settings", "settings.db")

    Properties.SetDefaultValue("theme", "dark")
    Properties.SetDefaultValue("language", "en")
    Properties.SetDefaultValue("autosave", True)

    LoadSettings
End Sub

Private Sub LoadSettings
    Dim Theme As String = Properties.Get("theme")
    Dim Language As String = Properties.Get("language")
    Dim AutoSave As Boolean = Properties.Get("autosave")

    Log("Theme: " & Theme)
    Log("Language: " & Language)
    Log("AutoSave: " & AutoSave)
End Sub

Private Sub SaveSettings
    Properties.Put("theme", "light")
    Properties.Put("language", "es")
    Properties.PutDisplayValueText("language", "Spanish")
    Properties.Put("autosave", False)
End Sub

Private Sub ResetSettings
    Properties.DeleteAllProperties
    LoadSettings
End Sub

Example: store an ID and a user-facing description

Dim ProvinceId As Int = 13
Dim ProvinceName As String = "Manabí"

Properties.Put("province_id", ProvinceId)
Properties.PutDisplayValueText("province_id", ProvinceName)

Log(Properties.Get("province_id"))
Log(Properties.GetDisplayValueText("province_id"))

The persisted value can be used internally while the associated text is ready for the UI.


Public field

Field Type Description
isInitialized Boolean True when Initialize completed successfully.

Complete public API

Member Return Description
Initialize(FolderName As String, name As String) — Initializes folder, storage file, cache and defaults.
Get(k As String) Object Gets a value through the cache and optional default.
GetDisplayValueText(k As String) String Gets the associated display text.
Put(k As String, v As Object) — Persists a value and updates the cache.
PutDisplayValueText(k As String, displayValueText As String) — Stores the associated display text.
Remove(k As String) — Removes value, display text and cache entries.
DeleteAllProperties — Deletes all persisted values and clears the cache.
SetDefaultValue(k As String, defaultValue As Object) — Registers an in-memory default.
GetDefaultValue(k As String) Object Returns a registered default or Null.
GetKeyValueStore KeyValueStore Returns the underlying store.
Exists(k As String) Boolean Checks cache or persistent storage.

Designer Properties and events

jdProperties is a data class.

It does not declare:

  • DesignerCreateView;
  • AddToParent;
  • Designer Properties;
  • public events.

Source-backed behavior notes

  • The analyzed library declares B4J support.
  • On B4J, FolderName is created below File.DirApp.
  • Initialize catches storage initialization errors and leaves isInitialized = False when initialization fails.
  • The cache lives for the lifetime of the instance.
  • Defaults live only in memory.
  • DeleteAllProperties does not remove defaults.
  • The _DisplayValueText suffix is used internally.
  • Direct writes through GetKeyValueStore can leave the cache stale.

  • JDataset: SQLite data binding and CRUD.
  • TableViewUtils: visual helpers for table cells.
  • JTableview: advanced visual table.