Add filters to search (AND-106)

This commit is contained in:
Grishka 2024-11-08 20:13:31 +03:00
parent 91d65b4e27
commit 20ed47032e
4 changed files with 60 additions and 8 deletions

View File

@ -13,7 +13,7 @@ android {
applicationId "org.joinmastodon.android" applicationId "org.joinmastodon.android"
minSdk 23 minSdk 23
targetSdk 34 targetSdk 34
versionCode 125 versionCode 126
versionName "2.8.0" versionName "2.8.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -42,7 +42,7 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
private static final int QUERY_RESULT=937; private static final int QUERY_RESULT=937;
private static final int SCAN_RESULT=456; private static final int SCAN_RESULT=456;
private TabLayout tabLayout; private TabLayout tabLayout, searchTabLayout;
private ViewPager2 pager; private ViewPager2 pager;
private FrameLayout[] tabViews; private FrameLayout[] tabViews;
private TabLayoutMediator tabLayoutMediator; private TabLayoutMediator tabLayoutMediator;
@ -62,6 +62,7 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
private String currentQuery; private String currentQuery;
private Intent scannerIntent; private Intent scannerIntent;
private Runnable searchExitCallback=this::exitSearch; private Runnable searchExitCallback=this::exitSearch;
private SearchResult.Type searchFilter;
@Override @Override
public void onCreate(Bundle savedInstanceState){ public void onCreate(Bundle savedInstanceState){
@ -79,6 +80,7 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
LinearLayout view=(LinearLayout) inflater.inflate(R.layout.fragment_discover, container, false); LinearLayout view=(LinearLayout) inflater.inflate(R.layout.fragment_discover, container, false);
tabLayout=view.findViewById(R.id.tabbar); tabLayout=view.findViewById(R.id.tabbar);
searchTabLayout=view.findViewById(R.id.search_tabbar);
pager=view.findViewById(R.id.pager); pager=view.findViewById(R.id.pager);
tabViews=new FrameLayout[4]; tabViews=new FrameLayout[4];
@ -205,6 +207,30 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
}); });
tabsDivider=view.findViewById(R.id.tabs_divider); tabsDivider=view.findViewById(R.id.tabs_divider);
searchTabLayout.setTabTextColors(UiUtils.getThemeColor(getActivity(), R.attr.colorM3OnSurfaceVariant), UiUtils.getThemeColor(getActivity(), R.attr.colorM3Primary));
searchTabLayout.setTabTextSize(V.dp(14));
searchTabLayout.addTab(searchTabLayout.newTab().setText(R.string.posts));
searchTabLayout.addTab(searchTabLayout.newTab().setText(R.string.hashtags));
searchTabLayout.addTab(searchTabLayout.newTab().setText(R.string.search_people));
searchTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab){
searchFilter=switch(tab.getPosition()){
case 0 -> SearchResult.Type.STATUS;
case 1 -> SearchResult.Type.HASHTAG;
case 2 -> SearchResult.Type.ACCOUNT;
default -> throw new IllegalStateException("Unexpected value: " + tab.getPosition());
};
searchFragment.setQuery(currentQuery, searchFilter);
}
@Override
public void onTabUnselected(TabLayout.Tab tab){}
@Override
public void onTabReselected(TabLayout.Tab tab){}
});
return view; return view;
} }
@ -227,11 +253,11 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
searchActive=true; searchActive=true;
pager.setVisibility(View.GONE); pager.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE); tabLayout.setVisibility(View.GONE);
searchTabLayout.setVisibility(View.VISIBLE);
searchView.setVisibility(View.VISIBLE); searchView.setVisibility(View.VISIBLE);
searchBack.setImageResource(me.grishka.appkit.R.drawable.ic_arrow_back); searchBack.setImageResource(me.grishka.appkit.R.drawable.ic_arrow_back);
searchBack.setEnabled(true); searchBack.setEnabled(true);
searchBack.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); searchBack.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
tabsDivider.setVisibility(View.GONE);
addBackCallback(searchExitCallback); addBackCallback(searchExitCallback);
} }
} }
@ -242,12 +268,12 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
searchActive=false; searchActive=false;
pager.setVisibility(View.VISIBLE); pager.setVisibility(View.VISIBLE);
tabLayout.setVisibility(View.VISIBLE); tabLayout.setVisibility(View.VISIBLE);
searchTabLayout.setVisibility(View.GONE);
searchView.setVisibility(View.GONE); searchView.setVisibility(View.GONE);
searchText.setText(R.string.search_mastodon); searchText.setText(R.string.search_mastodon);
searchBack.setImageResource(R.drawable.ic_search_24px); searchBack.setImageResource(R.drawable.ic_search_24px);
searchBack.setEnabled(false); searchBack.setEnabled(false);
searchBack.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); searchBack.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
tabsDivider.setVisibility(View.VISIBLE);
currentQuery=null; currentQuery=null;
removeBackCallback(searchExitCallback); removeBackCallback(searchExitCallback);
} }
@ -267,14 +293,14 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
if(reqCode==QUERY_RESULT && success){ if(reqCode==QUERY_RESULT && success){
enterSearch(); enterSearch();
currentQuery=result.getString("query"); currentQuery=result.getString("query");
SearchResult.Type type;
if(result.containsKey("filter")){ if(result.containsKey("filter")){
type=SearchResult.Type.values()[result.getInt("filter")]; searchFilter=SearchResult.Type.values()[result.getInt("filter")];
}else{ }else{
type=null; searchFilter=SearchResult.Type.STATUS;
} }
searchFragment.setQuery(currentQuery, type); searchFragment.setQuery(currentQuery, searchFilter);
searchText.setText(currentQuery); searchText.setText(currentQuery);
updateSearchTabBar();
} }
} }
@ -300,6 +326,17 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{
} }
} }
private void updateSearchTabBar(){
int tab=switch(searchFilter){
case STATUS -> 0;
case HASHTAG -> 1;
case ACCOUNT -> 2;
};
if(searchTabLayout.getSelectedTabPosition()==tab)
return;
searchTabLayout.selectTab(searchTabLayout.getTabAt(tab));
}
private class DiscoverPagerAdapter extends RecyclerView.Adapter<SimpleViewHolder>{ private class DiscoverPagerAdapter extends RecyclerView.Adapter<SimpleViewHolder>{
@NonNull @NonNull
@Override @Override

View File

@ -71,6 +71,20 @@
app:tabMode="auto" app:tabMode="auto"
android:background="?colorM3Surface"/> android:background="?colorM3Surface"/>
<org.joinmastodon.android.ui.tabs.TabLayout
android:id="@+id/search_tabbar"
android:layout_width="match_parent"
android:layout_height="48dp"
app:tabGravity="fill"
app:tabIndicator="@drawable/tab_indicator_m3"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicatorColor="?colorM3Primary"
app:tabIndicatorFullWidth="false"
app:tabMinWidth="0dp"
app:tabMode="fixed"
android:visibility="gone"
android:background="?colorM3Surface"/>
<View <View
android:id="@+id/tabs_divider" android:id="@+id/tabs_divider"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -831,6 +831,7 @@
<string name="familiar_followers_two">Followed by %s and %s</string> <string name="familiar_followers_two">Followed by %s and %s</string>
<string name="profile_saved_posts">Saved</string> <string name="profile_saved_posts">Saved</string>
<string name="profile_saved_posts_explanation">Your saved posts are only visible to you.</string> <string name="profile_saved_posts_explanation">Your saved posts are only visible to you.</string>
<string name="search_people">People</string>
<plurals name="familiar_followers_many"> <plurals name="familiar_followers_many">
<item quantity="one">Followed by %1$s, %2$s, and %3$,d other</item> <item quantity="one">Followed by %1$s, %2$s, and %3$,d other</item>
<item quantity="other">Followed by %1$s, %2$s, and %3$,d others</item> <item quantity="other">Followed by %1$s, %2$s, and %3$,d others</item>