Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delegates for DataStore creation #24

Merged
merged 4 commits into from
Apr 17, 2024
Merged

Add delegates for DataStore creation #24

merged 4 commits into from
Apr 17, 2024

Conversation

osipxd
Copy link
Owner

@osipxd osipxd commented Apr 16, 2024

Delegates encryptedDataStore and encryptedPreferencesDataStore were added to simplify DataStore creation.
If you have the following code:

val dataStore = DataStoreFactory.createEncrypted(serializer) {
    EncryptedFile.Builder(
        context.dataStoreFile("filename"),
        context,
        MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
        EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
    ).build()
}

You can simplify it using delegate for DataStore creation.

// 1. Move the field to top level of you Kotlin file and turn it to an extension on Context
// 2. Replace `DataStoreFactory.createEncrypted` with `encryptedDataStore`
val Context.dataStore by encryptedDataStore(
    fileName = "filename", // Keep file the same
    serializer = serializer,
)

Note

This only will be interchangeable if you used context.dataStoreFile(...) to create datastore file.
In case you have custom logic for master key creation, pass the created master key as a parameter masterKey to the delegate.

Closes #21

@osipxd osipxd force-pushed the feature/delegates branch 3 times, most recently from a4f371a to 640da2e Compare April 16, 2024 09:37
@osipxd osipxd added this to the 1.0.0 milestone Apr 16, 2024
@osipxd osipxd self-assigned this Apr 16, 2024
@osipxd osipxd mentioned this pull request Apr 17, 2024
@osipxd osipxd merged commit fa84435 into main Apr 17, 2024
@osipxd osipxd deleted the feature/delegates branch April 17, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Delegated Property
1 participant