Skip to content

Instantly share code, notes, and snippets.

@sockeqwe
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(R.id.name)
val image = findViewById(R.id.image)
@sockeqwe
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> {
@Override
public void bindIntents(){
Observable<FormViewState> state = intent( MyFormView::submitIntent)
.map(formInputData -> new FormViewState( isValidName(formInputData.name), isValidEmail(formInputData.email) ) );
subscribeViewState(state, MyFormView::render)
}
}
class FirstPageLoading implements PartialStateChanges {
@Override
public HomeViewState computeNewState(HomeViewState previousState)
return previousState.builder()
.firstPageLoading(true)
.firstPageError(null)
.build();
}
}
class FooActivity extends MviActivity<...> {
@Inject FooPresenter
public void onCreate(Bundle bundle){
super.onCreate(bundle);
FooComponent component = DaggerFooComponent.builder()
.fooModule(new FooModule(bundle))
.build();
component.inject(this);
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
sockeqwe / LastCall.java
Created January 5, 2017 10:27 — forked from passsy/LastCall.java
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
sockeqwe / Query.java
Created May 22, 2016 20:45
RxJava wrapper for Firebase ValueEventListeners
package com.usehomeroom.vasuki.data;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fernandocejas.arrow.optional.Optional;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override public Fragment getItem(int position) {
return new OverviewFragmentBuilder(1).build();
}