vpaliy / Android Extensions
Programming Languages
Labels
Projects that are alternatives of or similar to Android Extensions
android-extensions
This repository contains a set of android extensions which I use in my apps.
Animation Extensions
- Apply the same scale factor to X and Y.
view.animate().scale(1.f).start()
- An easier way to build the animator object.
view.animator {
scale(factor=1.5f)
translationX(distanceX())
translationY(distanceY())
duration=300L
startDelay=100L
interpolator=AccelerateInterpolator()
}.start()
- Now it's super easy to build a width/height animator. You just need to pass the end height/width.
view.getHeightAnimator(view.height/2).start()
- Or you can do something like this:
view.getHeightAnimator(endHeight).apply {
duration=300
startDelay=10
interpolator=AccelerateInterpolator()
}.start()
- Adding listeners is easier:
view.animator().onStart{}.onEnd{}.onCancel{}.onRepeat{}.animator().apply{
duration=300
startDelay=50
scale(0.5f)
}.start()
Note! You don't want to use the listeners in the apply{}
block. It will create a new listener each time, without preserving the previous listeners.
Don't do that:
view.animator().apply{
onEnd{}
onStart{}
}.start()
Or this:
view.animator().apply{
onStart{
onEnd{}
}
}.start()
Common Extensions
- Eliminate the tedious "if" problem in Kotlin.
val condition=...
val result=if(condition) 1 else 0
Here's a better way to do this:
val condition=...
val result=condition then 1?:0
- If you need to check a parameter for null and then pass it into a function:
fun requestItem(id:String?):Int{
if(id!=null){
return fetch(id)
}
return error()
}
A bit better:
fun request(id:String?)=id then(this::fetch)?:error()
Add an exception:
fun requestItem(id:String?):Int{
if(id!=null){
return fetch(id)
}
throw IllegalArgumentException()
}
fun request(id:String?)=id then(this::fetch)?:throw IllegalArgumentException()
Logs
-
Equivalent of
Log.i(javaClass.simpleName,message)
val list:List<User> =fetchData() list.forEach{ info(it) }
-
Equivalent of
Log.e(javaClass.simpleName,message)
val list:List<User> =fetchData() list.forEach{ error(it) }
-
Equivalent of
Log.d(javaClass.simpleName,message)
val list:List<User> =fetchData() list.forEach{ debug(it) }
-
Equivalent of
Log.wtf(javaClass.simpleName,message)
val list:List<User> =fetchData() list.forEach{ wtf(it) }
-
Equivalent of
Log.v(javaClass.simpleName,message)
val list:List<User> =fetchData() list.forEach{ verbose(it) }
-
Equivalent of
Log.w(javaClass.simpleName,message)
val list:List<User> =fetchData() list.forEach{ warning(it) }
Assign you own tags:
error(tag="Tag",message)
Or you use tags from the context:
repository.requestItem(object:Callback{
override fun onSuccess(item: Item) {
info(this@MainActivity,item)
}
override fun onError(ex: Throwable) {
error(this@MainActivity,ex)
}
})
The output would be: MainActivity: message
Also, you can use a function that will provide a message.Consider this example:
repository.requestItem(object:Callback{
override fun onSuccess(item: Person) {
warning(this@MainActivity){
"He is ${item.age} years old"
}
}
override fun onError(ex: Throwable) {
error(this@MainActivity){
ex.cause
}
}
})
Also, you can chain log calls:
list.forEach{
info(it).warning(it).error(it)
}
Installation
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.vpaliyX:android-extensions:-SNAPSHOT'
}
License
MIT License
Copyright (c) 2017 Vasyl Paliy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.