Skip to content

Instantly share code, notes, and snippets.

View nsk-mironov's full-sized avatar

Vladimir Mironov nsk-mironov

  • Lisbon, Portugal
View GitHub Profile
@nsk-mironov
nsk-mironov / LithoDsl.kt
Last active August 9, 2018 13:32
Litho DSL
import com.facebook.litho.Column
import com.facebook.litho.Component
import com.facebook.litho.ComponentContext
import com.facebook.litho.Row
import com.facebook.litho.fresco.FrescoImage
import com.facebook.litho.sections.widget.RecyclerCollectionComponent
import com.facebook.litho.widget.Card
import com.facebook.litho.widget.CardClip
import com.facebook.litho.widget.EditText
import com.facebook.litho.widget.EmptyComponent
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.Observer
import io.reactivex.internal.fuseable.HasUpstreamObservableSource
import io.reactivex.internal.fuseable.QueueFuseable
import io.reactivex.internal.observers.BasicFuseableObserver
abstract class BaseObservableMap<T : Any, U : Any> protected constructor(private val source: ObservableSource<T>) : Observable<U>(), HasUpstreamObservableSource<T> {
override fun source(): ObservableSource<T> {
return source
@nsk-mironov
nsk-mironov / AccountViewModel.kt
Created April 11, 2016 13:38
Databinding with DelegatedProperties
class AccountViewModel : ObservableModel by ObservableModelDelegate() {
@get:Bindable var name by property<CharSequence>("", BR.name)
@get:Bindable var authenticated by property(false, BR.authenticated)
@get:Bindable var avatar by property("", BR.avatar)
}
final class com/github/vmironov/MainKt$main$first$1 extends kotlin/jvm/internal/FunctionReference implements kotlin/jvm/functions/Function2 {
// access flags 0x1041
public synthetic bridge invoke(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 0
ALOAD 1
CHECKCAST com/github/vmironov/A
ALOAD 2
CHECKCAST java/lang/Number
INVOKEVIRTUAL java/lang/Number.intValue ()I
INVOKEVIRTUAL com/github/vmironov/MainKt$main$first$1.invoke (Lcom/github/vmironov/A;I)I
@nsk-mironov
nsk-mironov / Reflection.kt
Created March 13, 2016 16:50
Kotlin Metadata
package com.github.vmironov
import kotlin.reflect.jvm.internal.impl.serialization.ClassData
import kotlin.reflect.jvm.internal.impl.serialization.Flags
import kotlin.reflect.jvm.internal.impl.serialization.ProtoBuf
import kotlin.reflect.jvm.internal.impl.serialization.jvm.JvmProtoBufUtil
data class Foo(val foo: String)
data class Bar(val bar: String)
@nsk-mironov
nsk-mironov / Delegates.md
Last active December 25, 2017 23:27
Delegated properties

Delegated properties is a really nice feature of Kotlin and we are using them a lot in our code base. The entire project contains more than 500 delegated properties. Unfortunatelly, each property compiles to a class with 6 extra methods. While this can be fine if you're running on JVM, this is absolutely unacceptable in case you're targeting Android.

For example, the following class produces 2 more classes, Delegate$bar$1 and Delegate$foo$1:

public class Delegate(private val values: Map<String, Any>) {
  public val bar: String by values
  public val foo: String by values
}
<T:Ljava/lang/Object;A:[TT;>(TA;I)TA;
public class StackOverflow {
public static void main(String[] args) {
final Observable<Integer> firstObs = Observable.just(1, 2, 3, 4, 5);
final Observable<Integer> secondObs = Observable.just(2, 4, 6);
final Observable<Integer> result = secondObs.toList().flatMap(new Func1<List<Integer>, Observable<Integer>>() {
@Override
public Observable<Integer> call(final List<Integer> ignore) {
return firstObs.filter(new Func1<Integer, Boolean>() {
@Override
@nsk-mironov
nsk-mironov / OperatorThrottleList.java
Last active August 29, 2015 14:26
OperatorThrottleList
public final class OperatorThrottleList<T> implements Operator<T, List<T>> {
private final Scheduler scheduler;
private final long interval;
public OperatorThrottleList(final long interval, final TimeUnit unit, final Scheduler scheduler) {
this.interval = unit.toMillis(interval);
this.scheduler = scheduler;
}
@Override
public class Main {
public static void main(final String[] args) throws Exception {
Observable.from(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12))
.groupBy(item -> item % 4)
.flatMap(Observable::toList)
.filter(integers -> integers.size() > 2)
.toList()
.subscribe(result -> {
System.out.printf("%d results %s%n", result.size(), result);
});