Skip to content

Instantly share code, notes, and snippets.

interface Lifecycle : ObservableSource<Lifecycle.Event> {
enum class Event {
BEGIN,
END
}
// Remainder omitted
}
val output: PublishSubject<String> = PublishSubject.create()
val input: Consumer<String> = Consumer { System.out.println(it) }
val lifecycle = ManualLifecycle()
val binder = Binder(lifecycle)
binder.bind(output to input)
output.onNext("1")
lifecycle.begin()
fun createBinderForActivity(activity: AppCompatActivity) = Binder(
CreateDestroyBinderLifecycle(activity.lifecycle)
)
fun Observable<T>.toBinderLifecycle() = Lifecycle.wrap(this
.first()
.map { END }
.startWith(BEGIN)
)
fun Completable.toBinderLifecycle() = Lifecycle.wrap(
Observable.concat(
Observable.just(BEGIN),
this.andThen(Observable.just(END))
)
)
with(binder) {
bind(feature to view using stateToViewModelTransformer)
bind(view to feature using uiEventToWishTransformer)
bind(view to analyticsTracker)
}
sealed class Lce<out T> {
open val content: T? = null
open val error: Throwable? = null
open val isLoading: Boolean = false
object Loading : Lce<Nothing>() { override val isLoading: Boolean = true }
data class Content<out T>(override val content: T) : Lce<T>()
data class Error(override val error: Throwable) : Lce<Nothing>()
}
@zsoltk
zsoltk / UuidHelper.java
Created April 8, 2019 11:00 — forked from jeffjohnson9046/UuidHelper.java
Convert UUID to byte array and vice versa. Useful for when UUIDs are stored in MySQL tables as VARBINARY(16)
import java.nio.ByteBuffer;
import java.util.UUID;
public class UuidAdapter {
public static byte[] getBytesFromUUID(UUID uuid) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
interface FooView : Consumer<ViewModel>, ObservableSource<Event> {
data class ViewModel(
val title: String,
val bgColor: Int
)
sealed class Event {
object ButtonClicked : Event()
data class TextFocusChanged(val hasFocus: Boolean) : Event()
class FooViewImpl @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0,
private val events: PublishRelay<Event> = PublishRelay.create<Event>()
) : LinearLayout(context, attrs, defStyle),
FooView,
// delegate implementing ObservableSource to our Relay
ObservableSource<Event> by events {