package com.tehmou.rxbookapp.view;

import com.tehmou.rxbookapp.R;
import com.tehmou.rxbookapp.pojo.GitHubRepository;
import com.tehmou.rxbookapp.utils.RxBinderUtil;
import com.tehmou.rxbookapp.utils.TextWatcherObservable;
import com.tehmou.rxbookapp.viewmodels.RepositoriesViewModel;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;

import java.util.List;

import rx.Observable;

/**
 * Created by ttuo on 06/01/15.
 */
public class RepositoriesView extends FrameLayout {
    private static final String TAG = RepositoriesView.class.getSimpleName();
    private final RxBinderUtil rxBinderUtil = new RxBinderUtil(this);

    private ArrayAdapter<GitHubRepository> listAdapter;
    private ListView listView;
    private EditText editText;
    private Observable<String> searchStringObservable;

    private RepositoriesViewModel viewModel;

    public RepositoriesView(Context context) {
        super(context, null);
    }

    public RepositoriesView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        listView = (ListView) findViewById(R.id.repositories_list_view);
        listAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (viewModel != null) {
                GitHubRepository repository = listAdapter.getItem(position);
                viewModel.selectRepository(repository);
            }
        });
        listView.setAdapter(listAdapter);

        this.editText = (EditText) findViewById(R.id.repositories_search);
        searchStringObservable = TextWatcherObservable.create(editText);
    }

    public void setViewModel(RepositoriesViewModel viewModel) {
        this.viewModel = viewModel;
        rxBinderUtil.clear();
        if (viewModel != null) {
            rxBinderUtil.bindProperty(viewModel.getRepositories(), this::setRepositories);
            viewModel.setSearchStringObservable(searchStringObservable);
        }
    }

    private void setRepositories(List<GitHubRepository> repositories) {
        listAdapter.clear();
        listAdapter.addAll(repositories);
    }
}

@@ -163,65 +163,8 @@
 il;%0A
-import com.tehmou.rxbookapp.utils.TextWatcherObservable;%0A
 impo
@@ -221,16 +221,16 @@
 wModel;%0A
+
 %0Aimport 
@@ -292,69 +292,8 @@
 et;%0A
-import android.view.View;%0Aimport android.widget.AdapterView;%0A
 impo
@@ -448,16 +448,16 @@
 .List;%0A%0A
-
 import r
@@ -469,16 +469,59 @@
 ervable;
+%0Aimport rx.android.widget.WidgetObservable;
 %0A%0A/**%0A *
@@ -1842,19 +1842,14 @@
 e = 
-TextWatcher
+Widget
 Obse
@@ -1859,14 +1859,12 @@
 ble.
-crea
 te
+xt
 (edi
@@ -1869,16 +1869,95 @@
 ditText)
+%0A                .map(onTextChangeEvent -%3E onTextChangeEvent.text().toString())
 ;%0A    %7D%0A

Use WidgetObservable instead of TextWatcherObservable