From 20ed47032ece944e5a32bf2afbf2e9f1030d9a37 Mon Sep 17 00:00:00 2001 From: Grishka Date: Fri, 8 Nov 2024 20:13:31 +0300 Subject: [PATCH] Add filters to search (AND-106) --- mastodon/build.gradle | 2 +- .../fragments/discover/DiscoverFragment.java | 51 ++++++++++++++++--- .../src/main/res/layout/fragment_discover.xml | 14 +++++ mastodon/src/main/res/values/strings.xml | 1 + 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/mastodon/build.gradle b/mastodon/build.gradle index a6bffa89..aca406f2 100644 --- a/mastodon/build.gradle +++ b/mastodon/build.gradle @@ -13,7 +13,7 @@ android { applicationId "org.joinmastodon.android" minSdk 23 targetSdk 34 - versionCode 125 + versionCode 126 versionName "2.8.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/DiscoverFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/DiscoverFragment.java index ca93bdd4..cab6f94b 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/DiscoverFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/DiscoverFragment.java @@ -42,7 +42,7 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{ private static final int QUERY_RESULT=937; private static final int SCAN_RESULT=456; - private TabLayout tabLayout; + private TabLayout tabLayout, searchTabLayout; private ViewPager2 pager; private FrameLayout[] tabViews; private TabLayoutMediator tabLayoutMediator; @@ -62,6 +62,7 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{ private String currentQuery; private Intent scannerIntent; private Runnable searchExitCallback=this::exitSearch; + private SearchResult.Type searchFilter; @Override 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); tabLayout=view.findViewById(R.id.tabbar); + searchTabLayout=view.findViewById(R.id.search_tabbar); pager=view.findViewById(R.id.pager); tabViews=new FrameLayout[4]; @@ -205,6 +207,30 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{ }); 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; } @@ -227,11 +253,11 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{ searchActive=true; pager.setVisibility(View.GONE); tabLayout.setVisibility(View.GONE); + searchTabLayout.setVisibility(View.VISIBLE); searchView.setVisibility(View.VISIBLE); searchBack.setImageResource(me.grishka.appkit.R.drawable.ic_arrow_back); searchBack.setEnabled(true); searchBack.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); - tabsDivider.setVisibility(View.GONE); addBackCallback(searchExitCallback); } } @@ -242,12 +268,12 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{ searchActive=false; pager.setVisibility(View.VISIBLE); tabLayout.setVisibility(View.VISIBLE); + searchTabLayout.setVisibility(View.GONE); searchView.setVisibility(View.GONE); searchText.setText(R.string.search_mastodon); searchBack.setImageResource(R.drawable.ic_search_24px); searchBack.setEnabled(false); searchBack.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); - tabsDivider.setVisibility(View.VISIBLE); currentQuery=null; removeBackCallback(searchExitCallback); } @@ -267,14 +293,14 @@ public class DiscoverFragment extends AppKitFragment implements ScrollableToTop{ if(reqCode==QUERY_RESULT && success){ enterSearch(); currentQuery=result.getString("query"); - SearchResult.Type type; if(result.containsKey("filter")){ - type=SearchResult.Type.values()[result.getInt("filter")]; + searchFilter=SearchResult.Type.values()[result.getInt("filter")]; }else{ - type=null; + searchFilter=SearchResult.Type.STATUS; } - searchFragment.setQuery(currentQuery, type); + searchFragment.setQuery(currentQuery, searchFilter); 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{ @NonNull @Override diff --git a/mastodon/src/main/res/layout/fragment_discover.xml b/mastodon/src/main/res/layout/fragment_discover.xml index a09ef1d5..adca4006 100644 --- a/mastodon/src/main/res/layout/fragment_discover.xml +++ b/mastodon/src/main/res/layout/fragment_discover.xml @@ -71,6 +71,20 @@ app:tabMode="auto" android:background="?colorM3Surface"/> + + Followed by %s and %s Saved Your saved posts are only visible to you. + People Followed by %1$s, %2$s, and %3$,d other Followed by %1$s, %2$s, and %3$,d others