Add JitPack
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
Add KSP plugin
plugins {
// ...
id 'com.google.devtools.ksp' version '1.8.10-1.0.9'
}
Add generated files to classpath
android {
// ...
sourceSets.configureEach {
kotlin.srcDir("$buildDir/generated/ksp/$name/kotlin/")
}
}
Add Gradle dependencies
dependencies {
// ...
implementation 'com.github.CurtesMalteser.ksp-preferences:annotation:v0.0.1-rc'
ksp 'com.github.CurtesMalteser.ksp-preferences:processor:v0.0.1-rc'
}
If you are using Dagger/Hilt
kapt {
correctErrorTypes = true
}
- Declare your interface and annotate
@WithPreferences
- Declare a suspend function which takes as single parameter, one of the supported Preferences DataStore types
- Add a property with same name as the parameter declared in the previous point with suffix Flow
- Flow generic type must match the type of the parameter declared previously
- Compile project (code is generated)
- Keys are inferred from the parameter and property type, so this must match
@WithPreferences
interface AppData {
val myBooleanFlow: Flow<Boolean>
val myIntFlow: Flow<Int>
// ...
suspend fun testBoolean(myBoolean: Boolean)
suspend fun testInt(myInt: Int)
// ...
}
- Define your proto file and respective Serializer as usual
- Declare your interface and annotate
@WithPreferences
- Declare a single function which takes the proto object or the builder as param
- Supported:
- Named param: (prefs: (UserPreferences) -> UserPreferences)
- With
this
: (prefs: (UserPreferences.Builder) -> UserPreferences)
- Supported:
- Declare property with Flow to collect proto object
- Compile project (code is generated)
@WithProto
interface UserPreferencesData {
val userPreferencesFlow: Flow<UserPreferences>
suspend fun updateUserPrefsBuilderThis(prefs: UserPreferences.Builder.() -> UserPreferences)
}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.