package com.yoelglus.presentation.patterns.rmvp;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.memoizrlabs.Shank;
import com.yoelglus.presentation.patterns.R;
import com.yoelglus.presentation.patterns.model.ItemModel;

import java.util.List;

import rx.Observable;
import rx.subjects.PublishSubject;

import static java.util.Collections.emptyList;

/**
 * An activity representing a list of Items. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link RmvpItemDetailActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 */
public class RmvpItemListActivity extends AppCompatActivity implements RmvpItemsListPresenter.View {

    private SimpleItemRecyclerViewAdapter mAdapter;
    private RmvpItemsListPresenter mPresenter;
    private PublishSubject<Void> mAddItemClicks = PublishSubject.create();
    private PublishSubject<String> mItemClicks = PublishSubject.create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = Shank.provideNew(RmvpItemsListPresenter.class, this);

        setContentView(R.layout.activity_item_list);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(getTitle());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(view -> mAddItemClicks.onNext(null));

        View recyclerView = findViewById(R.id.item_list);
        assert recyclerView != null;
        setupRecyclerView((RecyclerView) recyclerView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.takeView(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.dropView(this);
    }

    @Override
    public void showItems(List<ItemModel> itemModels) {
        mAdapter.setValues(itemModels);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public Observable<Void> addItemClicks() {
        return mAddItemClicks;
    }

    @Override
    public Observable<String> itemClicks() {
        return mItemClicks;
    }

    private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
        mAdapter = new SimpleItemRecyclerViewAdapter();
        recyclerView.setAdapter(mAdapter);
    }

    public class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {

        private List<ItemModel> mValues = emptyList();

        void setValues(List<ItemModel> values) {
            mValues = values;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_content, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.mItem = mValues.get(position);
            holder.mIdView.setText(mValues.get(position).getId());
            holder.mContentView.setText(mValues.get(position).getContent());

            holder.mView.setOnClickListener(v -> mItemClicks.onNext(holder.mItem.getId()));
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            private final View mView;
            private final TextView mIdView;
            private final TextView mContentView;
            private ItemModel mItem;

            ViewHolder(View view) {
                super(view);
                mView = view;
                mIdView = (TextView) view.findViewById(R.id.id);
                mContentView = (TextView) view.findViewById(R.id.content);
            }

            @Override
            public String toString() {
                return super.toString() + " '" + mContentView.getText() + "'";
            }
        }
    }

}

@@ -116,67 +116,8 @@
 ll;%0A
-import android.support.design.widget.FloatingActionButton;%0A
 impo
@@ -161,16 +161,16 @@
 tivity;%0A
+
 import a
@@ -376,16 +376,62 @@
 tView;%0A%0A
+import com.jakewharton.rxbinding.view.RxView;%0A
 import c
@@ -1287,83 +1287,8 @@
 er;%0A
-    private PublishSubject%3CVoid%3E mAddItemClicks = PublishSubject.create();%0A
     
@@ -1748,159 +1748,8 @@
 );%0A%0A
-        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);%0A        fab.setOnClickListener(view -%3E mAddItemClicks.onNext(null));%0A
 %0A   
@@ -2362,22 +2362,45 @@
 urn 
-mAddItemClicks
+RxView.clicks(findViewById(R.id.fab))
 ;%0A  
@@ -2489,16 +2489,31 @@
 emClicks
+.asObservable()
 ;%0A    %7D%0A
@@ -3532,85 +3532,92 @@
     
-holder.mView.setOnClickListener(v -%3E mItemClicks.onNext(holder.mItem.getId())
+RxView.clicks(holder.mView).map(aVoid -%3E holder.mItem.getId()).subscribe(mItemClicks
 );%0A 

Remove redundant subject