Skip to content

Commit

Permalink
Make the search text clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
gzsombor committed Aug 14, 2024
1 parent d8e6aec commit 497d167
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import free.rm.skytube.R;
import free.rm.skytube.businessobjects.VideoCategory;
import free.rm.skytube.databinding.FragmentSearchBinding;
import free.rm.skytube.databinding.SearchActionbarBinding;
import free.rm.skytube.gui.businessobjects.adapters.VideoGridAdapter;

/**
Expand All @@ -46,6 +47,8 @@ public class SearchVideoGridFragment extends VideosGridFragment {
private String searchQuery = "";
/** Edit searched query through long press on search query**/
private SearchView editSearchView;
private SearchActionbarBinding searchActionbar;
private MenuItem searchMenuItem;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -58,6 +61,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the layout for this fragment
searchActionbar = SearchActionbarBinding.inflate(inflater);

FragmentSearchBinding binding = FragmentSearchBinding.inflate(inflater, container, false);
initSearch(container.getContext(), videoGridAdapter, binding);

Expand All @@ -68,17 +73,21 @@ protected void initSearch(@NonNull Context context, VideoGridAdapter videoGridAd
initVideos(context, videoGridAdapterParam, binding.videosGridview);
// setup the toolbar/actionbar
setSupportActionBar(binding.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// set the action bar's title
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setTitle(searchQuery);
actionBar.setCustomView(searchActionbar.getRoot());
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

searchActionbar.actionbarTitle.setText(searchQuery);
// make searched query editable on touching the textView
searchActionbar.actionbarTitle.setOnClickListener(click -> {
showSearchArea(true);
});

//makes searched query editable on long press
binding.toolbar.setOnLongClickListener(view1 -> {
editSearchView.setIconified(false);
editSearchView.setQuery(searchQuery,false);
showSearchArea(true);
return false;
});
// the app will call onCreateOptionsMenu() for when the user wants to search
Expand All @@ -88,28 +97,43 @@ protected void initSearch(@NonNull Context context, VideoGridAdapter videoGridAd
videoGridAdapter.initializeList();
}

private void showSearchArea(boolean visible) {
getSupportActionBar().setDisplayShowCustomEnabled(!visible);
if (visible) {
searchMenuItem.expandActionView();
}
}

private void updateSearchView(boolean visible) {
if (visible) {
editSearchView.onActionViewExpanded();
editSearchView.setQuery(searchQuery, false);
} else {
editSearchView.setIconified(true);
}
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchMenuItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
editSearchView = searchView;
// will be called when the user clicks on the actionbar's search icon
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// if the user has previously search, then copy the query into the search view
if (searchQuery != null && !searchQuery.isEmpty()) {
searchView.onActionViewExpanded();
searchView.setQuery(searchQuery, false);
}

if (searchQuery != null && !searchQuery.isEmpty()) {
updateSearchView(true);
}
// now expand the search view
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
showSearchArea(false);
return true;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private synchronized void startRefreshTask(boolean isShowFetchingVideosDialog, b

@Override
public void onSubscriptionRefreshStarted() {
if (gridviewBinding.swipeRefreshLayout != null) {
if (gridviewBinding != null && gridviewBinding.swipeRefreshLayout != null) {
gridviewBinding.swipeRefreshLayout.setRefreshing(true);
}
}
Expand All @@ -177,7 +177,7 @@ public void onChannelsFound(boolean hasChannels) {
@Override
public void onSubscriptionRefreshFinished() {
// Remove the progress bar(s)
if (gridviewBinding.swipeRefreshLayout != null) {
if (gridviewBinding != null && gridviewBinding.swipeRefreshLayout != null) {
gridviewBinding.swipeRefreshLayout.setRefreshing(false);
}
hideFetchingVideosDialog();
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/search_actionbar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:id="@+id/actionbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
/>

</LinearLayout>

0 comments on commit 497d167

Please sign in to comment.