Skip to content

Instantly share code, notes, and snippets.

@sockeqwe
sockeqwe / AdapterDelegateDslExample.kt
Created Jul 22, 2019
AdapterDelegates Kotlin DSL
View AdapterDelegateDslExample.kt
/**
* 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 Apr 28, 2019
switchMap operator for Kotlin's Flow type.
View SwitchMap.kt
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
/**
View News.kt
data class News(
val title : String
val favorite : Boolean
)
View MyFormPresenter.java
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)
}
}
View FirstPageLoading.java
class FirstPageLoading implements PartialStateChanges {
@Override
public HomeViewState computeNewState(HomeViewState previousState)
return previousState.builder()
.firstPageLoading(true)
.firstPageError(null)
.build();
}
}
View FooActivity.java
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);
View PersonModel.java
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 Jan 5, 2017 — forked from passsy/LastCall.java
Mockito verification of the last call on a method
View LastCall.java
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
RxJava wrapper for Firebase ValueEventListeners
View Query.java
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;
View MyFragmentPagerAdapter.java
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override public Fragment getItem(int position) {
return new OverviewFragmentBuilder(1).build();
}