Skip to content

Commit

Permalink
fix duplicate items issue in listing
Browse files Browse the repository at this point in the history
  • Loading branch information
esoxjem committed Dec 4, 2019
1 parent 0537689 commit b271d0a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
Expand Up @@ -13,6 +13,7 @@
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.esoxjem.movieguide.BaseApplication;
Expand All @@ -22,6 +23,8 @@
import com.esoxjem.movieguide.listing.sorting.SortingDialogFragment;
import com.google.android.material.snackbar.Snackbar;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -66,16 +69,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
unbinder = ButterKnife.bind(this, rootView);
initLayoutReferences();
moviesListing.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);

if (!recyclerView.canScrollVertically(1)) {
moviesPresenter.nextPage();
}
}
});
return rootView;
}

Expand Down Expand Up @@ -117,11 +110,26 @@ private void initLayoutReferences() {
} else {
columns = getResources().getInteger(R.integer.no_of_columns);
}
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), columns);
LinearLayoutManager layoutManager = new GridLayoutManager(getActivity(), columns);

moviesListing.setLayoutManager(layoutManager);
adapter = new MoviesListingAdapter(movies, this);
moviesListing.setAdapter(adapter);

moviesListing.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

if ((visibleItemCount + firstVisibleItemPosition) >=
totalItemCount && firstVisibleItemPosition >= 0) {
moviesPresenter.nextPage();
}
}
});
}

@Override
Expand Down
Expand Up @@ -23,7 +23,7 @@ class MoviesListingPresenterImpl implements MoviesListingPresenter {
private Disposable movieSearchSubscription;
private int currentPage = 1;
private List<Movie> loadedMovies = new ArrayList<>();
private boolean showingSearchResult = false;
private boolean isLoading = false;

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

displayMovies();
}

@Override
Expand All @@ -45,8 +42,13 @@ public void destroy() {
}

private void displayMovies() {
EspressoIdlingResource.increment();
if (isLoading) return;

isLoading = true;
showLoading();

EspressoIdlingResource.increment();

fetchSubscription = moviesInteractor.fetchMovies(currentPage)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -58,15 +60,6 @@ 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 @@ -76,7 +69,7 @@ public void firstPage() {

@Override
public void nextPage() {
if (showingSearchResult)
if (isLoading)
return;
if (moviesInteractor.isPaginationSupported()) {
currentPage++;
Expand All @@ -91,14 +84,22 @@ public void searchMovie(final String searchText) {
} else {
displayMovieSearchResult(searchText);
}
}

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

@Override
public void searchMovieBackPressed() {
if (showingSearchResult) {
showingSearchResult = false;
if (isLoading) {
loadedMovies.clear();
isLoading = false;
displayMovies();
}
}
Expand All @@ -115,13 +116,17 @@ private void onMovieFetchSuccess(List<Movie> movies) {
} else {
loadedMovies = new ArrayList<>(movies);
}

if (isViewAttached()) {
view.showMovies(loadedMovies);
}

isLoading = false;
}

private void onMovieFetchFailed(Throwable e) {
view.loadingFailed(e.getMessage());
isLoading = false;
}

private void onMovieSearchSuccess(List<Movie> movies) {
Expand All @@ -130,10 +135,12 @@ private void onMovieSearchSuccess(List<Movie> movies) {
if (isViewAttached()) {
view.showMovies(loadedMovies);
}
isLoading = false;
}

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

private boolean isViewAttached() {
Expand Down

0 comments on commit b271d0a

Please sign in to comment.