FeatureHandler
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:
Feature.LocalFeature uses
booleanPreferencesKeyto store the enabled stateFeature.RemoteFeature uses
intPreferencesKeyto store the state ordinal
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
The initial list of features to register and manage.
See also
Functions
Registers and persists a list of features.
Checks whether a feature is currently enabled as a Compose State.
Checks whether a feature is currently enabled.