Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Last active November 3, 2016 11:15
Show Gist options
  • Select an option

  • Save miquelbeltran/fb5b09b420f7cc97cdf4955f1e4d0999 to your computer and use it in GitHub Desktop.

Select an option

Save miquelbeltran/fb5b09b420f7cc97cdf4955f1e4d0999 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity implements BooksView {
EditText editText;
ImageButton imageButton;
BooksAdapter adapter;
ListView listView;
TextView textNoDataFound;
private BooksPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
imageButton = (ImageButton) findViewById(R.id.imageButton);
textNoDataFound = (TextView) findViewById(R.id.text_no_data_found);
adapter = new BooksAdapter(this, -1);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
presenter.performSearch(getUserInput());
}
});
// Note: Don't do this on production code, use Dependency Injection instead
// to provide the BooksInteractor and the BooksPresenter to the View
// Learn how to use Dagger 2 here:
// https://medium.com/@Miqubel/understanding-dagger-2-367ff1bd184f#.s2jza32df
BooksInteractor interactor = new BooksInteractorImpl();
presenter = new BooksPresenter(interactor);
presenter.bind(this);
}
@Override
protected void onDestroy() {
presenter.unbind();
super.onDestroy();
}
@Override
public void updateUi(List<Book> books) {
if (books.isEmpty()) {
// if no books found, show a message
textNoDataFound.setVisibility(View.VISIBLE);
} else {
textNoDataFound.setVisibility(View.GONE);
}
adapter.clear();
adapter.addAll(books);
}
private String getUserInput() {
return editText.getText().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment