datastore
Skill15 starsChanged 9 months agoMITView on GitHub
DataStore (Preferences and Proto) for Android AI agents. Use this skill whenever storing user preferences, settings, small key-value data, replacing SharedPreferences, using DataStore Preferences, DataStore Proto, reading/writing Flow from DataStore, Hilt DataStore injection, migration from SharedPreferences, or any persistent lightweight data storage. Always apply when the task involves user settings, theme preferences, onboarding state, auth tokens (non-secure), feature flags, or any small persistent data that is not a database.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- ong6/skillstheir own version, first seen Oct 2, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- ong6/skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- accessibilitySkill
- adaptive-uiSkill
- android-architectureSkill
- android-setupSkill
- android-testingSkill
- biometricsSkill
- ci-cd-androidSkill
- compose-animationSkill
- compose-navigationSkill
- compose-uiSkill
- design-systemSkill
- design-tokensSkill
- firebaseSkill
- gradleSkill
- hilt-diSkill
- kotlin-patternsSkill
- material3Skill
- notificationsSkill
- offline-firstSkill
- performanceSkill
- permissionsSkill
- play-store-releaseSkill
- retrofitSkill
- room-databaseSkill
- securitySkill
- supabase-androidSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 8
Always apply when the task involves user settings, theme preferences, onboarding state, auth tokens (non-secure), feature flags, or any …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
DataStore
DataStore is the modern replacement for SharedPreferences. Coroutine-based, type-safe, safe for Main thread.
Preferences DataStore — for simple key-value pairs
// Setup
implementation(libs.androidx.datastore.preferences)// ✅ Define all preference keys in one place
object PreferenceKeys {
val IS_DARK_THEME = booleanPreferencesKey("is_dark_theme")
val ONBOARDING_COMPLETE = booleanPreferencesKey("onboarding_complete")
val USER_ID = stringPreferencesKey("user_id")
val NOTIFICATION_ENABLED = booleanPreferencesKey("notifications_enabled")
val LANGUAGE = stringPreferencesKey("language")
val FONT_SIZE = intPreferencesKey("font_size")
}
// ✅ Hilt module — create DataStore singleton
@Module
@InstallIn(SingletonComponent::class)
object DataStoreModule {
@Provides
@Singleton
fun provideDataStore(@ApplicationContext context: Context): DataStore<Preferences> =
PreferenceDataStoreFactory.create(
corruptionHandler = ReplaceFileCorruptionHandler { emptyPreferences() },
migrations = listOf(SharedPreferencesMigration(context, "legacy_prefs")),
scope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
produceFile = { context.preferencesDataStoreFile("app_preferences") }
)
}
// ✅ Repository pattern for DataStore
class UserPreferencesRepository @Inject constructor(
private val dataStore: DataStore<Preferences>
) {
// Read — returns Flow, auto-updates on change
val isDarkTheme: Flow<Boolean> = dataStore.data
.catch { exception ->
if (exception is IOException) emit(emptyPreferences())
else throw exception
}
.map { preferences -> preferences[PreferenceKeys.IS_DARK_THEME] ?: false }
val onboardingComplete: Flow<Boolean> = dataStore.data
.catch { if (it is IOException) emit(emptyPreferences()) else throw it }
.map { it[PreferenceKeys.ONBOARDING_COMPLETE] ?: false }
// Read all preferences at once
val userPreferences: Flow<UserPreferences> = dataStore.data
.catch { if (it is IOException) emit(emptyPreferences()) else throw it }
.map { preferences ->
UserPreferences(
isDarkTheme = preferences[PreferenceKeys.IS_DARK_THEME] ?: false,
notificationsEnabled = preferences[PreferenceKeys.NOTIFICATION_ENABLED] ?: true,
language = preferences[PreferenceKeys.LANGUAGE] ?: "en",
fontSize = preferences[PreferenceKeys.FONT_SIZE] ?: 16
)
}
// Write — always suspend, safe on any coroutine
suspend fun setDarkTheme(enabled: Boolean) {
dataStore.edit { preferences ->
preferences[PreferenceKeys.IS_DARK_THEME] = enabled
}
}
suspend fun setOnboardingComplete() {
dataStore.edit { preferences ->
preferences[PreferenceKeys.ONBOARDING_COMPLETE] = true
}
}
suspend fun updateLanguage(language: String) {
dataStore.edit { preferences ->
preferences[PreferenceKeys.LANGUAGE] = language
}
}
suspend fun clearAll() {
dataStore.edit { it.clear() }
}
}
data class UserPreferences(
val isDarkTheme: Boolean = false,
val notificationsEnabled: Boolean = true,
val language: String = "en",
val fontSize: Int = 16
)Proto DataStore — for complex structured data
// Setup
implementation(libs.androidx.datastore)
implementation(libs.protobuf.kotlin.lite)// src/main/proto/user_settings.proto
syntax = "proto3";
option java_package = "com.company.app";
option java_multiple_files = true;
message UserSettings {
bool dark_mode = 1;
string language = 2;
NotificationSettings notifications = 3;
repeated string recent_searches = 4;
}
message NotificationSettings {
bool enabled = 1;
bool marketing = 2;
bool updates = 3;
}// ✅ Serializer for Proto DataStore
object UserSettingsSerializer : Serializer<UserSettings> {
override val defaultValue: UserSettings = UserSettings.getDefaultInstance()
override suspend fun readFrom(input: InputStream): UserSettings = try {
UserSettings.parseFrom(input)
} catch (e: InvalidProtocolBufferException) {
throw CorruptionException("Cannot read proto", e)
}
override suspend fun writeTo(t: UserSettings, output: OutputStream) {
t.writeTo(output)
}
}
// ✅ Proto DataStore module
@Module
@InstallIn(SingletonComponent::class)
object ProtoDataStoreModule {
@Provides
@Singleton
fun provideUserSettingsDataStore(@ApplicationContext context: Context): DataStore<UserSettings> =
DataStoreFactory.create(
serializer = UserSettingsSerializer,
produceFile = { context.dataStoreFile("user_settings.pb") }
)
}ViewModel — read DataStore in ViewModel
// ✅ Map DataStore Flow to UiState in ViewModel
@HiltViewModel
class SettingsViewModel @Inject constructor(
private val prefsRepository: UserPreferencesRepository
) : ViewModel() {
val uiState: StateFlow<SettingsUiState> = prefsRepository.userPreferences
.map { prefs ->
SettingsUiState.Success(
isDarkTheme = prefs.isDarkTheme,
notificationsEnabled = prefs.notificationsEnabled,
language = prefs.language
)
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = SettingsUiState.Loading
)
fun onDarkThemeToggled(enabled: Boolean) {
viewModelScope.launch {
prefsRepository.setDarkTheme(enabled)
}
}
}Migration from SharedPreferences
// ✅ Migrate automatically on first DataStore read
PreferenceDataStoreFactory.create(
migrations = listOf(
SharedPreferencesMigration(
context = context,
sharedPreferencesName = "legacy_prefs",
keysToMigrate = setOf("is_dark_theme", "user_id") // migrate specific keys only
)
),
produceFile = { context.preferencesDataStoreFile("app_preferences") }
)Common Mistakes
❌ Using SharedPreferences — always use DataStore in new code
❌ Reading DataStore with .first() on Main thread — observe as Flow in ViewModel
❌ Creating multiple DataStore instances for same file — always @Singleton
❌ No corruption handler — add ReplaceFileCorruptionHandler { emptyPreferences() }
❌ Storing large data in DataStore — use Room for lists/complex objects
❌ Calling dataStore.edit {} from Main thread — always from a coroutine
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

