android-kompat
android-kompat copied to clipboard
Android Compat Kotlin extension functions and properties
Android Kompat
Android Compat Kotlin extension functions and properties
Why This?
With the help of Kotlin language, we can get rid of the ugly syntax like:
fun ugly() {
val view : View
val x = ViewCompat.getX(view)
ViewCompat.setX(view, 1.0f)
}
and replace it with:
fun withExtensions() {
val view : View
val x = view.xCompat
view.xCompat = 1.0f
}
The xCompat property is defined as an extension property which directly calls to ViewCompat methods:
var View.xCompat: Float
get() = ViewCompat.getX(this)
set(value) = ViewCompat.setX(this, value)
All the functions and properties are posfixed with *Compat so you can see it pops up in IDE and know there's a compat method you should be using.
Download
I just applied for the maven central account. Use Jitpack for now:
dependencies {
compile "com.github.nohitme:android-kompat:0.1" // or use commit hash directly
}
Notes
- The packages are the same as the ones defined in Android Support Compat, so it's easier to track the changes
- All the
public staticmethods are transalted into corresponding functinos or properties.- Methods start with
set,get, oris (with Boolean return type)are defined as extension properties unless they take in extra arguments and are not suitable for extension properties. - If either getter or setter is not defined in compat class and it can be found in the normal Android class, the normal Android method would be used in conjunction with the compat method to make the extension property look more 'natural'
- Methods that are not suitable for properties will be converted into simple extionsion methods.
- Methods start with
- The project will be updated alongside with the latest stable support libarary.
Feedbacks
If you find anything missing or wrong, feel free to submit pull requests or file an issue. Thanks.