Skip to content

Commit

Permalink
Merge pull request esoxjem#68 from rijogeorge7/searchView
Browse files Browse the repository at this point in the history
Search view
  • Loading branch information
esoxjem authored Jul 23, 2018
2 parents b01f10d + 376d3cf commit f93198b
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.1.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

//RX View Binding
implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.1.1'

// Network
implementation "com.squareup.okhttp3:okhttp:${okhttpVersion}"
implementation "com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
Expand Down Expand Up @@ -110,4 +113,4 @@ dependencies {
}
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@
import android.support.annotation.VisibleForTesting;
import android.support.test.espresso.IdlingResource;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


import com.esoxjem.movieguide.R;
import com.esoxjem.movieguide.Constants;
import com.esoxjem.movieguide.details.MovieDetailsActivity;
import com.esoxjem.movieguide.details.MovieDetailsFragment;
import com.esoxjem.movieguide.Movie;
import com.esoxjem.movieguide.util.RxUtils;
import com.esoxjem.movieguide.util.EspressoIdlingResource;
import com.jakewharton.rxbinding2.support.v7.widget.RxSearchView;
import java.util.concurrent.TimeUnit;
import io.reactivex.disposables.Disposable;


public class MoviesListingActivity extends AppCompatActivity implements MoviesListingFragment.Callback {
public static final String DETAILS_FRAGMENT = "DetailsFragment";
private boolean twoPaneMode;
private Disposable searchViewTextSubscription;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -52,6 +61,31 @@ private void setToolbar() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
final MoviesListingFragment mlFragment = (MoviesListingFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_listing);
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
MoviesListingFragment mlFragment = (MoviesListingFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_listing);
mlFragment.searchViewBackButtonClicked();
return true;
}
});

searchViewTextSubscription = RxSearchView.queryTextChanges(searchView)
.debounce(500, TimeUnit.MILLISECONDS)
.subscribe(charSequence -> {
if (charSequence.length() > 0) {
mlFragment.searchViewClicked(charSequence.toString());
}
});

return true;
}

Expand Down Expand Up @@ -93,4 +127,10 @@ private void loadMovieFragment(Movie movie) {
public IdlingResource getCountingIdlingResource() {
return EspressoIdlingResource.getIdlingResource();
}

@Override
protected void onDestroy() {
RxUtils.unsubscribe(searchViewTextSubscription);
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(Constants.MOVIE, (ArrayList<? extends Parcelable>) movies);
}

public void searchViewClicked(String searchText){
moviesPresenter.searchMovie(searchText);
}

public void searchViewBackButtonClicked() {
moviesPresenter.searchMovieBackPressed();
}

public interface Callback {
void onMoviesLoaded(Movie movie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface MoviesListingInteractor
{
boolean isPaginationSupported();
Observable<List<Movie>> fetchMovies(int page);
Observable<List<Movie>> searchMovie(String searchQuery);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.esoxjem.movieguide.listing;

import android.support.annotation.NonNull;
import com.esoxjem.movieguide.Movie;
import com.esoxjem.movieguide.MoviesWraper;
import com.esoxjem.movieguide.favorites.FavoritesInteractor;
Expand Down Expand Up @@ -54,4 +55,9 @@ public Observable<List<Movie>> fetchMovies(int page) {
}
}

@Override
public Observable<List<Movie>> searchMovie(@NonNull String searchQuery) {
return tmdbWebService.searchMovies(searchQuery).map(MoviesWraper::getMovieList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ public interface MoviesListingPresenter

void setView(MoviesListingView view);

void searchMovie(String searchText);

void searchMovieBackPressed();

void destroy();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.esoxjem.movieguide.listing;

import android.support.annotation.NonNull;

import com.esoxjem.movieguide.Movie;
import com.esoxjem.movieguide.util.EspressoIdlingResource;
import com.esoxjem.movieguide.util.RxUtils;
Expand All @@ -18,8 +20,10 @@ class MoviesListingPresenterImpl implements MoviesListingPresenter {
private MoviesListingView view;
private MoviesListingInteractor moviesInteractor;
private Disposable fetchSubscription;
private Disposable movieSearchSubscription;
private int currentPage = 1;
private List<Movie> loadedMovies = new ArrayList<>();
private boolean showingSearchResult = false;

MoviesListingPresenterImpl(MoviesListingInteractor interactor) {
moviesInteractor = interactor;
Expand All @@ -28,12 +32,16 @@ class MoviesListingPresenterImpl implements MoviesListingPresenter {
@Override
public void setView(MoviesListingView view) {
this.view = view;
if(!showingSearchResult){
displayMovies();
}

}

@Override
public void destroy() {
view = null;
RxUtils.unsubscribe(fetchSubscription);
RxUtils.unsubscribe(fetchSubscription, movieSearchSubscription);
}

private void displayMovies() {
Expand All @@ -50,6 +58,15 @@ private void displayMovies() {
.subscribe(this::onMovieFetchSuccess, this::onMovieFetchFailed);
}

private void displayMovieSearchResult(@NonNull final String searchText) {
showingSearchResult = true;
showLoading();
movieSearchSubscription = moviesInteractor.searchMovie(searchText)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onMovieSearchSuccess, this::onMovieSearchFailed);
}

@Override
public void firstPage() {
currentPage = 1;
Expand All @@ -59,12 +76,32 @@ public void firstPage() {

@Override
public void nextPage() {
if(showingSearchResult)
return;
if (moviesInteractor.isPaginationSupported()) {
currentPage++;
displayMovies();
}
}

@Override
public void searchMovie(final String searchText) {
if(searchText == null || searchText.length() < 1) {
displayMovies();
} else {
displayMovieSearchResult(searchText);
}

}

@Override
public void searchMovieBackPressed() {
if(showingSearchResult) {
showingSearchResult = false;
loadedMovies.clear();
displayMovies();
}
}

private void showLoading() {
if (isViewAttached()) {
Expand All @@ -87,6 +124,18 @@ private void onMovieFetchFailed(Throwable e) {
view.loadingFailed(e.getMessage());
}

private void onMovieSearchSuccess(List<Movie> movies) {
loadedMovies.clear();
loadedMovies = new ArrayList<>(movies);
if (isViewAttached()) {
view.showMovies(loadedMovies);
}
}

private void onMovieSearchFailed(Throwable e) {
view.loadingFailed(e.getMessage());
}

private boolean isViewAttached() {
return view != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public interface TmdbWebService {
@GET("3/movie/{movieId}/reviews")
Observable<ReviewsWrapper> reviews(@Path("movieId") String movieId);

@GET("3/search/movie?language=en-US&page=1")
Observable<MoviesWraper> searchMovies(@Query("query") String searchQuery);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.esoxjem.movieguide.util;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class SoftKeyboardUtils {

/**
* Hides soft keyboard
*
* @param v - view instance
*/
public static void hideSoftInput(View v) {
if (v != null) {
((InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}

//restrict instanciation
private SoftKeyboardUtils() {
}
}
8 changes: 8 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
android:orderInCategory="100"
android:title="@string/action_filter"
app:showAsAction="always"/>

<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<string name="newest">Newest</string>
<string name="highest_rated">Highest Rated</string>
<string name="most_popular">Most Popular</string>
<string name="action_search">Search</string>
</resources>

0 comments on commit f93198b

Please sign in to comment.