Skip to content

Instantly share code, notes, and snippets.

@igorescodro
Last active October 17, 2023 17:29
Show Gist options
  • Save igorescodro/fcbbc4ef8682dd4350b92e79bcc3b8bf to your computer and use it in GitHub Desktop.
Save igorescodro/fcbbc4ef8682dd4350b92e79bcc3b8bf to your computer and use it in GitHub Desktop.
private lateinit var dataStore: DataStore<Preferences>
private val lock = SynchronizedObject()
fun getDataStore(producePath: () -> String): DataStore<Preferences> =
synchronized(lock) {
if (::dataStore.isInitialized) {
dataStore
} else {
PreferenceDataStoreFactory.createWithPath(
produceFile = { producePath().toPath() },
).also { dataStore = it }
}
}
internal const val dataStoreFileName = "settings.preferences_pb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment