Skip to content

Commit

Permalink
added most popular methods to ViewFinder, added Context to Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
vivchar committed Jan 18, 2018
1 parent 9755c92 commit 3276ed5
Show file tree
Hide file tree
Showing 29 changed files with 780 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter;
import com.github.vivchar.rendererrecyclerviewadapter.ViewModel;
import com.github.vivchar.rendererrecyclerviewadapter.ViewRenderer;
import com.github.vivchar.rendererrecyclerviewadapter.binder.LoadMoreViewBinder;
import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewBinder;
import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewProvider;

Expand Down Expand Up @@ -75,9 +76,9 @@ public View onCreateView(final LayoutInflater inflater,
mSwipeToRefresh = (SwipeRefreshLayout) inflate.findViewById(R.id.refresh);
mSwipeToRefresh.setOnRefreshListener(() -> mGithubPresenter.onRefresh());

mRecyclerViewAdapter = new RendererRecyclerViewAdapter();
mRecyclerViewAdapter = new RendererRecyclerViewAdapter(getContext());
mRecyclerViewAdapter.setDiffCallback(new ItemsDiffCallback());
mRecyclerViewAdapter.registerRenderer(new LoadMoreViewRenderer(R.layout.item_load_more, getContext()));
mRecyclerViewAdapter.registerRenderer(new LoadMoreViewBinder(R.layout.item_load_more));
mRecyclerViewAdapter.registerRenderer(createStargazerRenderer(R.layout.item_user_full_width));
mRecyclerViewAdapter.registerRenderer(createListRenderer()
.registerRenderer(createForkRenderer())
Expand All @@ -87,7 +88,6 @@ public View onCreateView(final LayoutInflater inflater,
mRecyclerViewAdapter.registerRenderer(new ViewBinder<>(
R.layout.item_category,
CategoryModel.class,
getContext(),
(model, finder, payloads) -> finder
.find(R.id.title, (ViewProvider<TextView>) view -> view.setText(model.getName()))
.setOnClickListener(R.id.viewAll, (v -> mGithubPresenter.onCategoryClicked(model)))
Expand Down Expand Up @@ -133,28 +133,28 @@ public void onStop() {

@NonNull
private ViewRenderer createForkRenderer() {
return new ForkViewRenderer(getContext(), new Listener());
return new ForkViewRenderer(new Listener());
}

@NonNull
private ViewRenderer createStargazerRenderer(final int layout) {
return new StargazerViewRenderer(layout, getContext(), new Listener());
return new StargazerViewRenderer(layout, new Listener());
}

@NonNull
private ViewRenderer createCategoryRenderer() {
return new CategoryViewRenderer(getContext(), new Listener());
return new CategoryViewRenderer(new Listener());
}

@NonNull
private CompositeViewRenderer createListRenderer() {
return new RecyclerViewRenderer(getContext());
return new RecyclerViewRenderer();
}

@NonNull
private ViewRenderer createUserRenderer() {
/* vivchar: ideally we should use other model */
return new UserViewRenderer(getContext());
return new UserViewRenderer();
}

@NonNull
Expand Down Expand Up @@ -191,7 +191,7 @@ public void showMessageView(@NonNull final String message) {

@Override
public void showSelectedUsers(@NonNull final ArrayList<ViewModel> list) {
final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter();
final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext());
adapter.registerRenderer(createUserRenderer());

final LayoutInflater inflater = LayoutInflater.from(getContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class CategoryViewRenderer extends ViewRenderer<CategoryModel, CategoryVi
@NonNull
private final Listener mListener;

public CategoryViewRenderer(final Context context, @NonNull final Listener listener) {
super(CategoryModel.class, context);
public CategoryViewRenderer(@NonNull final Listener listener) {
super(CategoryModel.class);
mListener = listener;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class ForkViewRenderer extends ViewRenderer<ForkModel, ForkViewHolder> {
@NonNull
private final Listener mListener;

public ForkViewRenderer(final Context context, @NonNull final Listener listener) {
super(ForkModel.class, context);
public ForkViewRenderer(@NonNull final Listener listener) {
super(ForkModel.class);
mListener = listener;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class RecyclerViewRenderer extends CompositeViewRenderer<RecyclerViewMode

private static final String TAG = RecyclerViewRenderer.class.getSimpleName();

public RecyclerViewRenderer(@NonNull final Context context) {
super(RecyclerViewModel.class, context);
public RecyclerViewRenderer() {
super(RecyclerViewModel.class);
}

@Override
Expand Down Expand Up @@ -65,7 +65,7 @@ public int createViewStateID(@NonNull final RecyclerViewModel model) {
@NonNull
@Override
protected RendererRecyclerViewAdapter createAdapter() {
final NestedAdapter nestedAdapter = new NestedAdapter();
final NestedAdapter nestedAdapter = new NestedAdapter(getContext());
nestedAdapter.setDiffCallback(new DefaultDiffCallback());
return nestedAdapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/
public class UserViewRenderer extends ViewRenderer<StargazerModel, UserViewHolder> {

public UserViewRenderer(@NonNull final Context context) {
super(StargazerModel.class, context);
public UserViewRenderer() {
super(StargazerModel.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class StargazerViewRenderer extends ViewRenderer<StargazerModel, Stargaze
private final Listener mListener;
private final int mLayoutID;

public StargazerViewRenderer(final int layoutID, final Context context, @NonNull final Listener listener) {
super(StargazerModel.class, context);
public StargazerViewRenderer(final int layoutID, @NonNull final Listener listener) {
super(StargazerModel.class);
mLayoutID = layoutID;
mListener = listener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter();
final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext());

adapter.registerRenderer(
new CompositeViewBinder<>(
R.layout.item_simple_composite,
R.id.recycler_view,
DefaultCompositeViewModel.class,
getContext(),
Collections.singletonList(new BetweenSpacesItemDecoration(10, 10))
).registerRenderer(getAnyViewRenderer())
);
Expand All @@ -62,8 +61,7 @@ private ViewRenderer getAnyViewRenderer() {
return new ViewBinder<>(
R.layout.item_simple_square,
DiffUtilFragment.DiffViewModel.class,
getContext(),
(model, finder, payloads) -> finder.find(R.id.text, (ViewProvider<TextView>) textView -> textView.setText(model.getText()))
(model, finder, payloads) -> finder.setText(R.id.text, model.getText())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

mAdapter = new RendererRecyclerViewAdapter();
mAdapter = new RendererRecyclerViewAdapter(getContext());

mAdapter.setDiffCallback(new DiffCallback());
// adapter.enableDiffUtil(); /* Or just call it to enable DiffUtil with DefaultDiffCallback */

mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, DiffViewModel.class, getContext(),
mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, DiffViewModel.class,
(model, finder, payloads) -> finder
.find(R.id.text, (ViewProvider<TextView>) textView -> textView.setText(model.getText()))
.setText(R.id.text, model.getText())
.setOnClickListener(R.id.text, v -> {
reloadItems(model);
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.vivchar.example.pages.simple;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand All @@ -14,10 +15,12 @@
import com.github.vivchar.example.R;
import com.github.vivchar.example.widgets.EndlessScrollListener;
import com.github.vivchar.example.widgets.ItemOffsetDecoration;
import com.github.vivchar.rendererrecyclerviewadapter.binder.BinderAdapter;
import com.github.vivchar.rendererrecyclerviewadapter.binder.LoadMoreViewBinder;
import com.github.vivchar.rendererrecyclerviewadapter.RendererRecyclerViewAdapter;
import com.github.vivchar.rendererrecyclerviewadapter.ViewModel;
import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewBinder;
import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewFinder;
import com.github.vivchar.rendererrecyclerviewadapter.binder.ViewProvider;

import java.lang.reflect.Type;
Expand All @@ -42,14 +45,14 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

mAdapter = new RendererRecyclerViewAdapter();
mAdapter = new RendererRecyclerViewAdapter(getContext());
mAdapter.enableDiffUtil();


// mAdapter.setLoadMoreModel(new YourLoadMoreModel()); /* you can change the LoadMoreModel if needed */
mAdapter.registerRenderer(new LoadMoreViewBinder(R.layout.item_load_more, getContext()));
mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, SimpleViewModel.class, getContext(),
(model, finder, payloads) -> finder.find(R.id.text, (ViewProvider<TextView>) textView -> textView.setText(model.getText()))
mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, SimpleViewModel.class,
(model, finder, payloads) -> finder.setText(R.id.text, model.getText())
));
// adapter.registerRenderer(...);
// adapter.registerRenderer(...);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

mAdapter = new RendererRecyclerViewAdapter();
mAdapter = new RendererRecyclerViewAdapter(getContext());

mAdapter.setDiffCallback(new PayloadDiffCallback());

mAdapter.registerRenderer(new ViewBinder<>(
R.layout.item_payload_square, PayloadViewModel.class, getContext(),
R.layout.item_payload_square, PayloadViewModel.class,
(model, finder, payloads) -> {
finder.setOnClickListener(R.id.text, v -> changeItem(model));
final TextView textView = finder.find(R.id.text);

if (payloads.isEmpty()) {
/* full bind */
textView.setText(model.getText());
finder.find(R.id.desciption, (ViewProvider<TextView>) desciption -> desciption.setText(model.getDescription()));
finder.setText(R.id.desciption, model.getDescription());
} else {
/* partially bind */
final Object payload = payloads.get(0);
Expand All @@ -62,7 +62,7 @@ R.layout.item_payload_square, PayloadViewModel.class, getContext(),
textView.animate().rotation(360).start();
textView.setText(model.getText());
} else if (payload.equals(DESCRIPTION_CHANGED)) {
finder.find(R.id.desciption, (ViewProvider<TextView>) desciption -> desciption.setText(model.getDescription()));
finder.setText(R.id.desciption, model.getDescription());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter();
final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext());

adapter.registerRenderer(new ViewBinder<>(R.layout.item_simple, RectViewModel.class, getContext(),
adapter.registerRenderer(new ViewBinder<>(R.layout.item_simple, RectViewModel.class,
(model, finder, payloads) -> finder
.find(R.id.text, (ViewProvider<TextView>) textView -> textView.setText(model.getText()))
.setText(R.id.text, model.getText())
.setOnClickListener(R.id.text, v -> {
Toast.makeText(getContext(), "Text Clicked " + model.getText(), Toast.LENGTH_SHORT).show();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter();
final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext());

adapter.registerRenderer(new RectViewRenderer(RectViewModel.class, getContext()));
adapter.registerRenderer(new RectViewRenderer(RectViewModel.class));
// adapter.registerRenderer(...);
// adapter.registerRenderer(...);

Expand All @@ -51,8 +51,8 @@ public View onCreateView(final LayoutInflater inflater,

public static class RectViewRenderer extends ViewRenderer<RectViewModel, RectViewHolder> {

public RectViewRenderer(@NonNull final Class<RectViewModel> type, @NonNull final Context context) {
super(type, context);
public RectViewRenderer(@NonNull final Class<RectViewModel> type) {
super(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ public View onCreateView(final LayoutInflater inflater,

final View view = inflater.inflate(R.layout.fragment_list, container, false);

final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter();
final RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter(getContext());

adapter.registerRenderer(
new CompositeViewBinder<>(
R.layout.item_simple_composite,
R.id.recycler_view,
StateViewModel.class,
getContext(),
Collections.singletonList(new BetweenSpacesItemDecoration(10, 10)),
new CompositeViewStateProvider<StateViewModel, CompositeViewHolder>() {
@Override
Expand Down Expand Up @@ -78,8 +77,8 @@ public int createViewStateID(@NonNull final StateViewModel model) {
}

private ViewRenderer getAnyViewRenderer() {
return new ViewBinder<>(R.layout.item_simple_square, DiffUtilFragment.DiffViewModel.class, getContext(),
(model, finder, payloads) -> finder.find(R.id.text, (ViewProvider<TextView>) textView -> textView.setText(model.getText()))
return new ViewBinder<>(R.layout.item_simple_square, DiffUtilFragment.DiffViewModel.class,
(model, finder, payloads) -> finder.setText(R.id.text, model.getText())
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.vivchar.example.widgets;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.ViewGroup;
Expand All @@ -17,6 +19,10 @@ public class NestedAdapter extends RendererRecyclerViewAdapter {

private static final String TAG = NestedAdapter.class.getSimpleName();

public NestedAdapter(@NonNull final Context context) {
super(context);
}

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int typeIndex) {
final ViewHolder viewHolder = super.onCreateViewHolder(parent, typeIndex);
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=2.3.1
VERSION_CODE=24
VERSION_NAME=2.4.0
VERSION_CODE=25
GROUP=com.github.vivchar

POM_DESCRIPTION=A single adapter with multiple view types for the whole project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,34 @@ public abstract class CompositeViewRenderer <M extends CompositeViewModel, VH ex
@NonNull
private final ArrayList<ViewRenderer> mRenderers = new ArrayList<>();

/**
* Please use a constructor without Context
* and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)}
*/
@Deprecated
public CompositeViewRenderer(@NonNull final Class<M> type, @NonNull final Context context) {
super(type, context);
}

/**
* Please use a constructor without Context
* and set Context via {@link RendererRecyclerViewAdapter#RendererRecyclerViewAdapter(Context)}
*/
@Deprecated
public CompositeViewRenderer(@NonNull final Class<M> type, @NonNull final Context context, @NonNull final ViewRenderer... renderers) {
super(type, context);
Collections.addAll(mRenderers, renderers);
}

public CompositeViewRenderer(@NonNull final Class<M> type) {
super(type);
}

public CompositeViewRenderer(@NonNull final Class<M> type, @NonNull final ViewRenderer... renderers) {
super(type);
Collections.addAll(mRenderers, renderers);
}

@Override
public void bindView(@NonNull final M model, @NonNull final VH holder) {
holder.getAdapter().setItems(model.getItems());
Expand Down Expand Up @@ -85,7 +104,7 @@ protected RecyclerView.LayoutManager createLayoutManager() {

@NonNull
protected RendererRecyclerViewAdapter createAdapter() {
return new RendererRecyclerViewAdapter();
return new RendererRecyclerViewAdapter(getContext());
}

@NonNull
Expand Down
Loading

0 comments on commit 3276ed5

Please sign in to comment.