mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-20 04:39:04 +00:00
Add guild search bar
This commit is contained in:
parent
93f264f0b4
commit
3896d73e43
4 changed files with 97 additions and 3 deletions
35
Habitica/res/layout/fragment_guild_recyclerview.xml
Normal file
35
Habitica/res/layout/fragment_guild_recyclerview.xml
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_width="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<SearchView android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/guild_search_view"
|
||||
android:queryHint="Search for guilds"/>
|
||||
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/coordinatorLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
|
||||
<com.habitrpg.android.habitica.ui.helpers.RecyclerViewEmptySupport
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scrollbarSize="3dp"
|
||||
android:scrollbarThumbVertical="@color/md_grey_500"
|
||||
android:scrollbars="vertical" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/empty_view"
|
||||
style="@style/EmptyView"/>
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
</LinearLayout>
|
||||
|
|
@ -243,6 +243,7 @@ To start, which parts of your life do you want to improve?</string>
|
|||
<string name="privacy">Privacy</string>
|
||||
<string name="write_message">Write Message</string>
|
||||
<string name="post">Post</string>
|
||||
<string name="guild_search_hint">Search for guilds</string>
|
||||
|
||||
<string name="todo_due" formatted="false">Due: %s</string>
|
||||
<string name="daily_streak" formatted="false">current streak: %d</string>
|
||||
|
|
|
|||
|
|
@ -13,21 +13,26 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Filter;
|
||||
import android.widget.Filterable;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class PublicGuildsRecyclerViewAdapter extends RecyclerView.Adapter<PublicGuildsRecyclerViewAdapter.GuildViewHolder> {
|
||||
public class PublicGuildsRecyclerViewAdapter extends RecyclerView.Adapter<PublicGuildsRecyclerViewAdapter.GuildViewHolder> implements Filterable{
|
||||
|
||||
public APIHelper apiHelper;
|
||||
private List<Group> publicGuildList;
|
||||
private List<String> memberGuildIDs;
|
||||
private List<Group> publicGuildListCopy;
|
||||
|
||||
public void setPublicGuildList(List<Group> publicGuildList) {
|
||||
this.publicGuildList = publicGuildList;
|
||||
this.publicGuildListCopy = new ArrayList<>(publicGuildList);
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
|
|
@ -93,6 +98,42 @@ public class PublicGuildsRecyclerViewAdapter extends RecyclerView.Adapter<Public
|
|||
private boolean isInGroup(Group guild) {
|
||||
return this.memberGuildIDs != null && this.memberGuildIDs.contains(guild.id);
|
||||
}
|
||||
@Override
|
||||
public Filter getFilter() {
|
||||
return new Filter() {
|
||||
@Override
|
||||
protected FilterResults performFiltering(CharSequence constraint) {
|
||||
List<Group> filteredGuilds = null;
|
||||
if(constraint.length() == 0) {
|
||||
filteredGuilds = publicGuildListCopy;
|
||||
} else {
|
||||
filteredGuilds = getFilteredResults(constraint.toString().toLowerCase());
|
||||
}
|
||||
|
||||
FilterResults results = new FilterResults();
|
||||
results.values = filteredGuilds;
|
||||
return results;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void publishResults(CharSequence constraint, FilterResults results) {
|
||||
publicGuildList = (List<Group>) results.values;
|
||||
PublicGuildsRecyclerViewAdapter.this.notifyDataSetChanged();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected List<Group> getFilteredResults(String constraint) {
|
||||
List<Group> filteredGuilds = new ArrayList<>();
|
||||
|
||||
for(Group guild : publicGuildListCopy) {
|
||||
if(guild.name.toLowerCase().contains(constraint)) {
|
||||
filteredGuilds.add(guild);
|
||||
}
|
||||
}
|
||||
|
||||
return filteredGuilds;
|
||||
}
|
||||
|
||||
static class GuildViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
|
|
|
|||
|
|
@ -14,13 +14,14 @@ import android.support.v7.widget.RecyclerView;
|
|||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.SearchView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class PublicGuildsFragment extends BaseMainFragment {
|
||||
public class PublicGuildsFragment extends BaseMainFragment implements SearchView.OnQueryTextListener {
|
||||
|
||||
List<String> memberGuildIDs;
|
||||
List<Group> guilds;
|
||||
|
|
@ -30,13 +31,17 @@ public class PublicGuildsFragment extends BaseMainFragment {
|
|||
|
||||
private View view;
|
||||
private PublicGuildsRecyclerViewAdapter viewAdapter;
|
||||
private SearchView guildSearchView;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
super.onCreateView(inflater, container, savedInstanceState);
|
||||
if (view == null) {
|
||||
view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
|
||||
view = inflater.inflate(R.layout.fragment_guild_recyclerview, container, false);
|
||||
|
||||
guildSearchView = (SearchView)view.findViewById(R.id.guild_search_view);
|
||||
guildSearchView.setOnQueryTextListener(this);
|
||||
|
||||
unbinder = ButterKnife.bind(this, view);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this.activity));
|
||||
|
|
@ -76,4 +81,16 @@ public class PublicGuildsFragment extends BaseMainFragment {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextSubmit(String s) {
|
||||
viewAdapter.getFilter().filter(s);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextChange(String s) {
|
||||
viewAdapter.getFilter().filter(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue