Skip to content
Android Extensions
Kotlin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
app
art
gradle/wrapper
kotlin-extensions
.gitignore
.travis.yml
LICENSE
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

android-extensions

Build Status

This repository contains a set of android extensions which I use in my apps.

Animation Extensions

  1. Apply the same scale factor to X and Y.
view.animate().scale(1.f).start()
  1. 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()
  1. 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()
  1. Or you can do something like this:
 view.getHeightAnimator(endHeight).apply { 
    duration=300
    startDelay=10
    interpolator=AccelerateInterpolator()
 }.start()
  1. 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

  1. 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
  1. 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.
You can’t perform that action at this time.