FeatureHandler

class FeatureHandler(dataStore: DataStore<Preferences>, initialFeatures: List<Feature>)

Handler for managing feature flag state persistence and retrieval.

This class provides the core functionality for the FeatureFlip module, handling:

  • Registration of feature flags

  • Persistent storage using DataStore

  • State queries (both Flow and Compose State)

  • State updates with validation

Architecture

The handler maintains an internal registry mapping each Feature to its corresponding DataStore preference key:

Usage

Creating a Handler

@Composable
fun MyApp() {
val features = remember {
listOf(
Feature.LocalFeature(
name = "dark_mode",
description = "Enable dark theme",
isEnabled = false
),
Feature.RemoteFeature(
name = "new_feature",
description = "Experimental feature",
defaultRemoteValue = true,
state = FeatureState.REMOTE
)
)
}

val featureHandler = rememberFeatureHandler(features)

CompositionLocalProvider(LocalFeatureHandler provides featureHandler) {
Content()
}
}

Checking Feature State in Composables

@Composable
fun MyScreen() {
val featureHandler = LocalFeatureHandler.current
val isDarkMode by featureHandler.isFeatureEnabled("dark_mode")

if (isDarkMode) {
DarkThemeContent()
} else {
LightThemeContent()
}
}

Checking Feature State with Flow

class MyViewModel(private val featureHandler: FeatureHandler) : ViewModel() {
val isDarkModeEnabled: Flow<Boolean> =
featureHandler.isFeatureEnabledFlow("dark_mode")
}

Updating Feature State

viewModelScope.launch {
featureHandler.setFeatureState("new_feature", FeatureState.LOCAL_ON)
}

Thread Safety

All operations are thread-safe. DataStore handles concurrent access internally, and the handler's internal state is protected.

Parameters

initialFeatures

The initial list of features to register and manage.

See also

Constructors

Link copied to clipboard
constructor(dataStore: DataStore<Preferences>, initialFeatures: List<Feature>)

Functions

Link copied to clipboard
suspend fun addFeatures(featuresToAdd: List<Feature>)

Registers and persists a list of features.

Link copied to clipboard

Checks whether a feature is currently enabled as a Compose State.

Link copied to clipboard
fun isFeatureEnabledFlow(featureName: String): Flow<Boolean>

Checks whether a feature is currently enabled.