Skip to content

Commit

Permalink
Use a SearchView to filter uniforms/textures
Browse files Browse the repository at this point in the history
Which is probably a bit more Material Design than the EditText
below the lists.
  • Loading branch information
markusfisch committed Jul 10, 2023
1 parent 9a7b73c commit 3aa71d0
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;

import de.markusfisch.android.shadereditor.R;
import de.markusfisch.android.shadereditor.activity.AbstractSubsequentActivity;
import de.markusfisch.android.shadereditor.activity.AddUniformActivity;
import de.markusfisch.android.shadereditor.adapter.PresetUniformAdapter;
import de.markusfisch.android.shadereditor.widget.SearchMenu;

public class UniformPresetPageFragment extends Fragment {
private PresetUniformAdapter uniformsAdapter;
private ListView listView;
private EditText searchBar;

@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
}

@Override
public View onCreateView(
Expand All @@ -37,12 +43,19 @@ public View onCreateView(
listView = view.findViewById(R.id.uniforms);
initListView(activity);

searchBar = activity.findViewById(R.id.search_bar);
initSearchBar();

return view;
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
SearchMenu.addSearchMenu(menu, inflater, this::filterUniforms);
}

private void filterUniforms(String query) {
uniformsAdapter.getFilter().filter(query);
uniformsAdapter.notifyDataSetChanged();
}

private void initListView(Context context) {
uniformsAdapter = new PresetUniformAdapter(context);

Expand All @@ -54,26 +67,6 @@ private void initListView(Context context) {
});
}

private void initSearchBar() {
searchBar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
uniformsAdapter.getFilter().filter(s);
uniformsAdapter.notifyDataSetChanged();
}

@Override
public void afterTextChanged(Editable s) {
}
});
}

private void addUniform(PresetUniformAdapter.Uniform uniform) {
if (uniform.isSampler()) {
AbstractSubsequentActivity.addFragment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
Expand All @@ -21,6 +22,7 @@
import de.markusfisch.android.shadereditor.activity.TextureViewActivity;
import de.markusfisch.android.shadereditor.adapter.TextureAdapter;
import de.markusfisch.android.shadereditor.app.ShaderEditorApp;
import de.markusfisch.android.shadereditor.widget.SearchMenu;

public class UniformSampler2dPageFragment extends Fragment {
protected String searchQuery;
Expand All @@ -32,6 +34,12 @@ public class UniformSampler2dPageFragment extends Fragment {
private View noTexturesMessage;
private String samplerType = AbstractSamplerPropertiesFragment.SAMPLER_2D;

@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
}

@Override
public View onCreateView(
LayoutInflater inflater,
Expand All @@ -48,8 +56,8 @@ public View onCreateView(
listView = view.findViewById(R.id.textures);
initListView(view);

searchBar = getActivity().findViewById(R.id.search_bar);
initSearchBar();
//searchBar = getActivity().findViewById(R.id.search_bar);
//initSearchBar();

progressBar = view.findViewById(R.id.progress_bar);
noTexturesMessage = view.findViewById(R.id.no_textures_message);
Expand Down Expand Up @@ -77,6 +85,11 @@ public void onDestroyView() {
noTexturesMessage = null;
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, MenuInflater inflater) {
SearchMenu.addSearchMenu(menu, inflater, this::filterTextures);
}

public void setSamplerType(String type) {
samplerType = type;
}
Expand Down Expand Up @@ -169,26 +182,11 @@ private void initListView(View view) {
(parent, view1, position, id) -> showTexture(id));
}

private void initSearchBar() {
searchBar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
private void filterTextures(String query) {
searchQuery = query == null
? null
: query.toLowerCase();

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
searchQuery = s.length() == 0
? null
: s.toString().toLowerCase();

loadTexturesAsync(getActivity());
}

@Override
public void afterTextChanged(Editable s) {
}
});
loadTexturesAsync(getActivity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.markusfisch.android.shadereditor.widget;

import android.support.annotation.NonNull;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;

import de.markusfisch.android.shadereditor.R;

public class SearchMenu {
public interface OnSearchListener {
void filter(String value);
}

public static void addSearchMenu(@NonNull Menu menu,
MenuInflater inflater,
OnSearchListener onSearchListener) {
inflater.inflate(R.menu.search, menu);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(menu.findItem(R.id.search));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
onSearchListener.filter(newText);
return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
onSearchListener.filter(query);
return true;
}
});
searchView.setOnCloseListener(() -> {
onSearchListener.filter(null);
return false;
});
}
}
25 changes: 6 additions & 19 deletions app/src/main/res/layout/fragment_uniform_pages.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
<LinearLayout
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_weight="1.0"
android:layout_width="match_parent"
android:layout_height="0dp">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_translucent"/>
</android.support.v4.view.ViewPager>
<EditText
android:id="@+id/search_bar"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_action_search"
android:hint="@string/search_uniforms_hint"
android:inputType="textFilter" />
</LinearLayout>
android:background="@color/primary_translucent"/>
</android.support.v4.view.ViewPager>
10 changes: 10 additions & 0 deletions app/src/main/res/menu/search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:material="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_action_search"
android:title="@string/search"
material:showAsAction="ifRoom|collapseActionView"
material:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<string name="sampler_2d">sampler2D</string>
<string name="sampler_cube">samplerCube</string>
<string name="preset">preset</string>
<string name="search_uniforms_hint">Search uniforms…</string>
<string name="search">Search</string>

<!-- UniformPresetPageFragment -->
<string name="uniform_format">%1$s (%2$s)</string>
Expand Down

0 comments on commit 3aa71d0

Please sign in to comment.