Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Created October 19, 2016 16:27
Show Gist options
  • Save miquelbeltran/b46c303f74d7a94a209b654650d22058 to your computer and use it in GitHub Desktop.
Save miquelbeltran/b46c303f74d7a94a209b654650d22058 to your computer and use it in GitHub Desktop.
public class BooksPresenter {
BooksView view;
private BooksInteractor interactor;
public BooksPresenter(BooksInteractor interactor) {
this.interactor = interactor;
}
public void bind(BooksView view) {
this.view = view;
}
public void unbind() {
view = null;
}
public void performSearch(String userInput) {
String formatUserInput = userInput.trim().replaceAll("\\s+", "+");
// Just call the method on the GoogleBooksService
interactor.search("search+" + formatUserInput)
// enqueue runs the request on a separate thread
.enqueue(new Callback<BookSearchResult>() {
// We receive a Response with the content we expect already parsed
@Override
public void onResponse(Call<BookSearchResult> call,
Response<BookSearchResult> books) {
if (view != null)
view.updateUi(books.body().getBooks());
}
// In case of error, this method gets called
@Override
public void onFailure(Call<BookSearchResult> call, Throwable t) {
t.printStackTrace();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment