Skip to content

Instantly share code, notes, and snippets.

sockeqwe / AdapterDelegateDslExample.kt
Created July 22, 2019 08:19
AdapterDelegates Kotlin DSL
* Let's say we want to display a list of Animals in a RecyclerView.
* The list can contain items of type Dog, Cat and Snake.
// AdapterDelegate for Dog.
fun dogAdapterDelegate(picasso : Picasso) = adapterDelegate<Dog, Animal>(R.layout.item_dog){ // Generics Types means this AdapterDelegate is used if item is instanceof Dog (whole list is List<Anmial>)
// this block is run once in onCreateViewHolder. Think of it as an intializer for a ViewHolder.
val name = findViewById(
val image = findViewById(
sockeqwe / SwitchMap.kt
Created April 28, 2019 11:33
switchMap operator for Kotlin's Flow type.
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
data class News(
val title : String
val favorite : Boolean
class MyFormPresenter extends MviBasePresenter<MyFormView, FormViewState> {
public void bindIntents(){
Observable<FormViewState> state = intent( MyFormView::submitIntent)
.map(formInputData -> new FormViewState( isValidName(, isValidEmail( ) );
subscribeViewState(state, MyFormView::render)
class FirstPageLoading implements PartialStateChanges {
public HomeViewState computeNewState(HomeViewState previousState)
return previousState.builder()
class FooActivity extends MviActivity<...> {
@Inject FooPresenter
public void onCreate(Bundle bundle){
FooComponent component = DaggerFooComponent.builder()
.fooModule(new FooModule(bundle))
class PersonsModel {
// In a real application fields would be private
// and we would have getters to access them
final boolean loading;
final List<Person> persons;
final Throwable error;
public(boolean loading, List<Person> persons, Throwable error){
this.loading = loading;
this.persons = persons;
sockeqwe /
Created January 5, 2017 10:27 — forked from passsy/
Mockito verification of the last call on a method
package com.pascalwelsch.mockito;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.verification.ArgumentsAreDifferent;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.junit.JUnitTool;
import org.mockito.internal.reporting.SmartPrinter;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool;
sockeqwe /
Created May 22, 2016 20:45
RxJava wrapper for Firebase ValueEventListeners
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fernandocejas.arrow.optional.Optional;
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
@Override public Fragment getItem(int position) {
return new OverviewFragmentBuilder(1).build();