Skip to content

Commit

Permalink
Fix some minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Jun 14, 2020
1 parent 0d799b3 commit ec9d441
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ Limitations:
- Ideally add database for local storage
- There is no error handling for now; would need to propagate errors from data source all the way to UI
- Main fragment doesn't save state, so when you come back from details it resets
- Need to click twice of search icon to get to the search field
- Search field clearing doesn't work correctly
15 changes: 12 additions & 3 deletions app/src/main/java/com/tananaev/giphy/ui/main/MainFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,40 @@ public View onCreateView(
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
RecyclerView gridView = view.findViewById(R.id.grid);
gridView.setLayoutManager(new GridLayoutManager(getContext(), COLUMNS));
gridView.setLayoutManager(new NpaGridLayoutManager(getContext(), COLUMNS));
gridView.setAdapter(adapter);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
viewModel = new ViewModelProvider(this, viewModelFactory).get(MainViewModel.class);
viewModel.fetchData(null).observe(getActivity(), adapter::submitList);
fetchData(null);
}

private void fetchData(String query) {
viewModel.fetchData(query).observe(getActivity(), adapter::submitList);
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);

SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
viewModel.fetchData(query).observe(getActivity(), adapter::submitList);
fetchData(query);
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
if (newText.isEmpty()) {
fetchData(null);
return true;
}
return false;
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.tananaev.giphy.ui.main;

import android.content.Context;
import android.util.AttributeSet;

import androidx.recyclerview.widget.GridLayoutManager;

/**
* A workaround for crash: https://stackoverflow.com/q/30220771/2548565
*/
public class NpaGridLayoutManager extends GridLayoutManager {

/**
* Disable predictive animations. There is a bug in RecyclerView which causes views that
* are being reloaded to pull invalid ViewHolders from the internal recycler stack if the
* adapter size has decreased since the ViewHolder was recycled.
*/
@Override
public boolean supportsPredictiveItemAnimations() {
return false;
}

public NpaGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public NpaGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}

public NpaGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
}

0 comments on commit ec9d441

Please sign in to comment.