Skip to content

Commit

Permalink
Reformat code with K&R code style
Browse files Browse the repository at this point in the history
  • Loading branch information
petrnohejl committed Jun 25, 2018
1 parent 820ef3a commit c981c9d
Show file tree
Hide file tree
Showing 187 changed files with 1,918 additions and 4,762 deletions.
24 changes: 8 additions & 16 deletions alfonz-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ Choose an adapter which suits your needs:
Simple-type adapters can be used directly - just create a new instance. Multi-type adapters are abstract. Create a class, extend the generic adapter and call `super` in the constructor. Pass item layout, view (view layer in MVVM) and observable array collection in the `super`. Adapter will inflate the layout for an item and bind the view and the data into `BR.view` and `BR.data` variables.

```java
public class ProductListAdapter extends SimpleDataBoundRecyclerAdapter
{
public ProductListAdapter(ProductListView view, ProductListViewModel viewModel)
{
public class ProductListAdapter extends SimpleDataBoundRecyclerAdapter {
public ProductListAdapter(ProductListView view, ProductListViewModel viewModel) {
super(R.layout.fragment_product_list_item, view, viewModel.products);
}
}
Expand Down Expand Up @@ -63,36 +61,30 @@ Create an XML layout, define `view` and `data` variables, use `view` for invokin
Create a new instance of the adapter and set it in your RecyclerView. Implement event callbacks in the MVVM view layer - it is usually Fragment.

```java
public interface ProductListView extends AlfonzView, AdapterView
{
public interface ProductListView extends AlfonzView, AdapterView {
void onItemClick(ProductEntity product);
}
```

```java
public class ProductListFragment
extends BaseFragment<ProductListView, ProductListViewModel, FragmentProductListBinding>
implements ProductListView
{
implements ProductListView {
private ProductListAdapter mAdapter;

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupAdapter();
}

@Override
public void onItemClick(ProductEntity product)
{
public void onItemClick(ProductEntity product) {
startProductDetailActivity(product.getId());
}

private void setupAdapter()
{
if(mAdapter == null)
{
private void setupAdapter() {
if (mAdapter == null) {
mAdapter = new ProductListAdapter(this, getViewModel());
getBinding().fragmentProductListRecycler.setAdapter(mAdapter);
}
Expand Down
7 changes: 2 additions & 5 deletions alfonz-adapter/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
apply plugin: "com.android.library"
apply from: "../utils.gradle"


ext {
bintrayName = "alfonz-adapter"
libraryName = "Alfonz-Adapter"
artifact = "alfonz-adapter"
}


android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
Expand Down Expand Up @@ -37,16 +35,15 @@ android {
}
}


dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:support-compat:$androidSupportVersion"
implementation "com.android.support:support-core-ui:$androidSupportVersion"
implementation "com.android.support:recyclerview-v7:$androidSupportVersion"
implementation "com.android.support:support-annotations:$androidSupportVersion"
implementation "com.android.support:support-v4:$androidSupportVersion" // https://stackoverflow.com/questions/41568032/android-data-binding-dependency-conflict-with-the-support-library
implementation "com.android.support:support-v4:$androidSupportVersion"
// https://stackoverflow.com/questions/41568032/android-data-binding-dependency-conflict-with-the-support-library
}


apply from: "../install.gradle"
apply from: "../bintray.gradle"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

import android.arch.lifecycle.LifecycleOwner;


public interface AdapterView extends LifecycleOwner
{
public interface AdapterView extends LifecycleOwner {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@
import android.view.View;
import android.view.ViewGroup;


public abstract class BaseDataBoundPagerAdapter<T extends ViewDataBinding> extends PagerAdapter
{
public abstract class BaseDataBoundPagerAdapter<T extends ViewDataBinding> extends PagerAdapter {
private LayoutInflater mLayoutInflater;


protected abstract void bindItem(T binding, int position);


@LayoutRes
public abstract int getItemLayoutId(int position);


@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position)
{
if(mLayoutInflater == null)
{
public Object instantiateItem(@NonNull ViewGroup container, int position) {
if (mLayoutInflater == null) {
mLayoutInflater = LayoutInflater.from(container.getContext());
}

Expand All @@ -38,17 +31,13 @@ public Object instantiateItem(@NonNull ViewGroup container, int position)
return binding.getRoot();
}


@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object)
{
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}


@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
{
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.util.List;


/**
* A reference implementation for an adapter that wants to use data binding "the right way". It
* works with {@link BaseDataBoundRecyclerViewHolder}.
Expand All @@ -44,40 +43,33 @@
* @param <T> The type of the ViewDataBinding class. Can be omitted in multiple-binding-type use
* case.
*/
public abstract class BaseDataBoundRecyclerAdapter<T extends ViewDataBinding> extends RecyclerView.Adapter<BaseDataBoundRecyclerViewHolder<T>>
{
public abstract class BaseDataBoundRecyclerAdapter<T extends ViewDataBinding> extends RecyclerView.Adapter<BaseDataBoundRecyclerViewHolder<T>> {
private static final Object DB_PAYLOAD = new Object();

private LayoutInflater mLayoutInflater;
@Nullable
private RecyclerView mRecyclerView;
private LayoutInflater mLayoutInflater;


/**
* This is used to block items from updating themselves. RecyclerView wants to know when an
* item is invalidated and it prefers to refresh it via onRebind. It also helps with performance
* since data binding will not update views that are not changed.
*/
private final OnRebindCallback mOnRebindCallback = new OnRebindCallback()
{
private final OnRebindCallback mOnRebindCallback = new OnRebindCallback() {
@Override
public boolean onPreBind(@NonNull ViewDataBinding binding)
{
if(mRecyclerView == null || mRecyclerView.isComputingLayout())
{
public boolean onPreBind(@NonNull ViewDataBinding binding) {
if (mRecyclerView == null || mRecyclerView.isComputingLayout()) {
return true;
}
int childAdapterPosition = mRecyclerView.getChildAdapterPosition(binding.getRoot());
if(childAdapterPosition == RecyclerView.NO_POSITION)
{
if (childAdapterPosition == RecyclerView.NO_POSITION) {
return true;
}
notifyItemChanged(childAdapterPosition, DB_PAYLOAD);
return false;
}
};


/**
* Override this method to handle binding your items into views
*
Expand All @@ -87,18 +79,14 @@ public boolean onPreBind(@NonNull ViewDataBinding binding)
*/
protected abstract void bindItem(BaseDataBoundRecyclerViewHolder<T> holder, int position, List<Object> payloads);


@LayoutRes
public abstract int getItemLayoutId(int position);


@Override
@CallSuper
@NonNull
public BaseDataBoundRecyclerViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
if(mLayoutInflater == null)
{
public BaseDataBoundRecyclerViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (mLayoutInflater == null) {
mLayoutInflater = LayoutInflater.from(parent.getContext());
}

Expand All @@ -107,54 +95,39 @@ public BaseDataBoundRecyclerViewHolder<T> onCreateViewHolder(@NonNull ViewGroup
return vh;
}


@Override
public final void onBindViewHolder(@NonNull BaseDataBoundRecyclerViewHolder<T> holder, int position, @NonNull List<Object> payloads)
{
public final void onBindViewHolder(@NonNull BaseDataBoundRecyclerViewHolder<T> holder, int position, @NonNull List<Object> payloads) {
// when a VH is rebound to the same item, we don't have to call the setters
if(payloads.isEmpty() || hasNonDataBindingInvalidate(payloads))
{
if (payloads.isEmpty() || hasNonDataBindingInvalidate(payloads)) {
bindItem(holder, position, payloads);
}
holder.binding.executePendingBindings();
}


@Override
public final void onBindViewHolder(@NonNull BaseDataBoundRecyclerViewHolder<T> holder, int position)
{
public final void onBindViewHolder(@NonNull BaseDataBoundRecyclerViewHolder<T> holder, int position) {
throw new IllegalArgumentException("just overridden to make final.");
}


@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView)
{
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
mRecyclerView = recyclerView;
}


@Override
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView)
{
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
mRecyclerView = null;
mLayoutInflater = null;
}


@Override
public final int getItemViewType(int position)
{
public final int getItemViewType(int position) {
return getItemLayoutId(position);
}


private boolean hasNonDataBindingInvalidate(@NonNull List<Object> payloads)
{
for(Object payload : payloads)
{
if(payload != DB_PAYLOAD)
{
private boolean hasNonDataBindingInvalidate(@NonNull List<Object> payloads) {
for (Object payload : payloads) {
if (payload != DB_PAYLOAD) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,19 @@
import android.view.LayoutInflater;
import android.view.ViewGroup;


/**
* A generic ViewHolder that wraps a generated ViewDataBinding class.
*
* @param <T> The type of the ViewDataBinding class
*/
public class BaseDataBoundRecyclerViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder
{
public class BaseDataBoundRecyclerViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder {
@NonNull public final T binding;


public BaseDataBoundRecyclerViewHolder(@NonNull T binding)
{
public BaseDataBoundRecyclerViewHolder(@NonNull T binding) {
super(binding.getRoot());
this.binding = binding;
}


/**
* Creates a new ViewHolder for the given layout file.
* <p>
Expand All @@ -55,13 +50,11 @@ public BaseDataBoundRecyclerViewHolder(@NonNull T binding)
* @return A new ViewHolder that has a reference to the binding class
*/
@NonNull
public static <T extends ViewDataBinding> BaseDataBoundRecyclerViewHolder<T> create(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, @LayoutRes int layoutId)
{
public static <T extends ViewDataBinding> BaseDataBoundRecyclerViewHolder<T> create(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, @LayoutRes int layoutId) {
T binding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
return new BaseDataBoundRecyclerViewHolder<>(binding);
}


/**
* Creates a new ViewHolder for the given layout file.
* <p>
Expand All @@ -75,8 +68,7 @@ public static <T extends ViewDataBinding> BaseDataBoundRecyclerViewHolder<T> cre
* @return A new ViewHolder that has a reference to the binding class
*/
@NonNull
public static <T extends ViewDataBinding> BaseDataBoundRecyclerViewHolder<T> create(@NonNull LifecycleOwner lifecycleOwner, @NonNull LayoutInflater inflater, @NonNull ViewGroup parent, @LayoutRes int layoutId)
{
public static <T extends ViewDataBinding> BaseDataBoundRecyclerViewHolder<T> create(@NonNull LifecycleOwner lifecycleOwner, @NonNull LayoutInflater inflater, @NonNull ViewGroup parent, @LayoutRes int layoutId) {
T binding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
binding.setLifecycleOwner(lifecycleOwner);
return new BaseDataBoundRecyclerViewHolder<>(binding);
Expand Down
Loading

0 comments on commit c981c9d

Please sign in to comment.