Skip to content

Instantly share code, notes, and snippets.

View albodelu's full-sized avatar

Al B. albodelu

View GitHub Profile
@Atternatt
Atternatt / BackgroundColorAnimation.kt
Last active September 21, 2017 04:58
Property delegation Examples
fun backgroundColorAnimator(): ReadWriteProperty<View, Int> =
object : ReadWriteProperty<View, Int> {
override fun getValue(thisRef: View, property: KProperty<*>): Int {
return (thisRef.background as? ColorDrawable)?.color ?: 0
}
override fun setValue(thisRef: View, property: KProperty<*>, value: Int) {
ValueAnimator.ofObject(ArgbEvaluator(), getValue(thisRef, property), value).apply {
@Atternatt
Atternatt / DelegatesAndExtensions.kt
Last active September 21, 2017 04:57
DelegatesAndExtensions
fun alphaWithBounceAnimator(): ReadWriteProperty<ImageView, Int> =
object : ReadWriteProperty<ImageView, Int> {
override fun getValue(thisRef: ImageView, property: KProperty<*>): Int {
return thisRef.imageAlpha
}
override fun setValue(thisRef: ImageView, property: KProperty<*>, value: Int) {
val animX = ObjectAnimator.ofFloat(thisRef, "scaleX", 1f, 2.5f, 1f)
@Atternatt
Atternatt / BindingAdapter.kt
Last active September 21, 2017 04:57
Binding Adapters
//forma clásica extrapolada directamente de java
//funcion estatica donde el primer parámetro es la vista y los n siguientes los especificados por le BindingAdapter
@BindingAdapter("bind:url")
fun loadUrl(imageView: ImageView, url: String) {
Picasso.with(imageView.ctx).load(url).transform(CircleTransform()).into(imageView)
}
//opción alternativa
@BindingAdapter("bind:url")
fun ImageView.loadUrl(url: String) {
@Atternatt
Atternatt / Synchronization.kt
Last active September 21, 2017 04:57
Databinding
//primera opción extener una funcin sobre [BaseObservable] que emita una [ObservableProperty] que emite el cambio y lo notifica
// a los databinders para repintar
inline fun <Param> BaseObservable.bindingParam(initialValue: Param,
crossinline afterChange: (Param) -> Unit): ReadWriteProperty<BaseObservable, Param> =
object : ObservableProperty<Param>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: Param, newValue: Param) {
if (oldValue != newValue) {
[email protected]()
@Atternatt
Atternatt / DefaultPropertyDelegation.kt
Last active September 21, 2017 04:57
Property Deleagtion
class Database(private context: Context) {
val helper: SQLiteOpenHelper by lazy { SQLiteOpenHelper(context,...-) }
val observableProperty: Int by Delegates.observable { thisRef , newValue , oldValue -> doSomething()}
val vetoableProperty: Int by Delegates.vetoable { _ , newValue , oldValue -> return if (oldValue < newValue) true else false }
}
@Atternatt
Atternatt / ByDouble.kt
Last active September 21, 2017 04:56
Kotlin Delegation Example
class ByDouble(incrementer: Incrementer) : Incrementer by incrementer {
override fun increment(number: Int): Int {
return super.increment(number) * 2
}
}
@PillowUnicorn
PillowUnicorn / ios-bitrise.yml
Created April 28, 2017 22:29
Pillow's iOS bitrise.yml
---
format_version: 1.1.0
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
app:
envs:
- BITRISE_PROJECT_PATH: ios/pro_mobile.xcodeproj
opts:
is_expand: false
- BITRISE_SCHEME: pro_mobile
opts:
@PillowUnicorn
PillowUnicorn / android_bitrise.yml
Created April 28, 2017 22:21
Pillow's Android bitrise.yml
---
format_version: 1.1.0
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
trigger_map:
- push_branch: qa
workflow: qa
workflows:
_init_install:
steps:
- activate-ssh-key:
import android.databinding.Observable as DataBindingObservable
private inline fun <reified T : DataBindingObservable, R : Any?> T.observe(
crossinline block: (T) -> R
): Observable<R> = create { subscriber ->
object : android.databinding.Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(observable: DataBindingObservable, id: Int) = try {
subscriber.onNext(block(observable as T))
} catch (e: Exception) {
subscriber.onError(e)
@Atternatt
Atternatt / Examples.kt
Created April 24, 2017 20:31
inline, reified, infix
inline fun <reified NewActivity : Activity> Activity.start(bundle: Bundle? = null) {
val intent: Intent = Intent(this, NewActivity::class.java)
bundle?.let { intent.putExtras(it) }
this.startActivity(intent)
}
startActivity<MainActivity>()
infix fun <Value>Boolean.then(block: () -> Value) {