diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SearchFragment.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SearchFragment.java index 73ad2c25..ccf9f7f1 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SearchFragment.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/SearchFragment.java @@ -2,9 +2,14 @@ package com.cappielloantonio.tempo.ui.fragment; import android.content.ComponentName; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.inputmethod.EditorInfo; +import android.widget.ImageView; +import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -28,17 +33,15 @@ import com.cappielloantonio.tempo.ui.adapter.AlbumAdapter; import com.cappielloantonio.tempo.ui.adapter.ArtistAdapter; import com.cappielloantonio.tempo.ui.adapter.SongHorizontalAdapter; import com.cappielloantonio.tempo.util.Constants; -import com.cappielloantonio.tempo.util.MusicUtil; import com.cappielloantonio.tempo.viewmodel.SearchViewModel; import com.google.common.util.concurrent.ListenableFuture; -import com.paulrybitskyi.persistentsearchview.adapters.model.SuggestionItem; -import com.paulrybitskyi.persistentsearchview.listeners.OnSuggestionChangeListener; -import com.paulrybitskyi.persistentsearchview.utils.SuggestionCreationUtil; import java.util.Collections; @UnstableApi public class SearchFragment extends Fragment implements ClickCallback { + private static final String TAG = "SearchFragment"; + private FragmentSearchBinding bind; private MainActivity activity; private SearchViewModel searchViewModel; @@ -60,6 +63,7 @@ public class SearchFragment extends Fragment implements ClickCallback { initSearchResultView(); initSearchView(); + inputFocus(); return view; } @@ -70,12 +74,6 @@ public class SearchFragment extends Fragment implements ClickCallback { initializeMediaBrowser(); } - @Override - public void onResume() { - super.onResume(); - inputFocus(); - } - @Override public void onStop() { releaseMediaBrowser(); @@ -118,64 +116,101 @@ public class SearchFragment extends Fragment implements ClickCallback { } private void initSearchView() { - if (isQueryValid(searchViewModel.getQuery())) { - search(searchViewModel.getQuery()); - } + setRecentSuggestions(); - bind.persistentSearchView.setInputQuery(searchViewModel.getQuery()); - setSuggestions(); + bind.searchView + .getEditText() + .setOnEditorActionListener((textView, actionId, keyEvent) -> { - bind.persistentSearchView.setOnSearchQueryChangeListener((searchView, oldQuery, newQuery) -> { - if (!newQuery.trim().equals("") && newQuery.trim().length() > 1) { - searchViewModel.getSearchSuggestion(newQuery).observe(getViewLifecycleOwner(), suggestions -> searchView.setSuggestions(SuggestionCreationUtil.asRegularSearchSuggestions(MusicUtil.getReadableStrings(suggestions)), false)); - } else { - setSuggestions(); - } - }); + String query = bind.searchView.getText().toString(); - bind.persistentSearchView.setOnSuggestionChangeListener(new OnSuggestionChangeListener() { - @Override - public void onSuggestionPicked(SuggestionItem suggestion) { - search(suggestion.getItemModel().getText()); - } + if (actionId == EditorInfo.IME_ACTION_DONE) { + if (isQueryValid(query)) { + search(bind.searchView.getText().toString()); + return true; + } else { + Toast.makeText(requireContext(), getString(R.string.search_info_minimum_characters), Toast.LENGTH_SHORT).show(); + return false; + } + } - @Override - public void onSuggestionRemoved(SuggestionItem suggestion) { - } - }); + return false; + }); - bind.persistentSearchView.setOnSearchConfirmedListener((searchView, query) -> { - if (isQueryValid(query)) { - searchView.collapse(); - search(query); - } else { - Toast.makeText(requireContext(), getString(R.string.search_info_minimum_characters), Toast.LENGTH_SHORT).show(); - } - }); + bind.searchView + .getEditText() + .addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { - bind.persistentSearchView.setOnSuggestionChangeListener(new OnSuggestionChangeListener() { - @Override - public void onSuggestionPicked(SuggestionItem suggestion) { - search(suggestion.getItemModel().getText()); - } + } - @Override - public void onSuggestionRemoved(SuggestionItem suggestion) { - searchViewModel.deleteRecentSearch(suggestion.getItemModel().getText()); - } - }); + @Override + public void onTextChanged(CharSequence charSequence, int start, int before, int count) { + if (count > 1) { + setSearchSuggestions(charSequence.toString()); + } else { + setRecentSuggestions(); + } + } - bind.persistentSearchView.setOnClearInputBtnClickListener(v -> searchViewModel.setQuery("")); + @Override + public void afterTextChanged(Editable editable) { + + } + }); } - private void setSuggestions() { - bind.persistentSearchView.setSuggestions(SuggestionCreationUtil.asRecentSearchSuggestions(searchViewModel.getRecentSearchSuggestion()), false); + public void setRecentSuggestions() { + bind.searchViewSuggestionContainer.removeAllViews(); + + for (String suggestion : searchViewModel.getRecentSearchSuggestion()) { + View view = LayoutInflater.from(bind.searchViewSuggestionContainer.getContext()).inflate(R.layout.item_search_suggestion, bind.searchViewSuggestionContainer, false); + + ImageView leadingImageView = view.findViewById(R.id.search_suggestion_icon); + TextView titleView = view.findViewById(R.id.search_suggestion_title); + ImageView tailingImageView = view.findViewById(R.id.search_suggestion_delete_icon); + + leadingImageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_history, null)); + titleView.setText(suggestion); + + view.setOnClickListener(v -> search(suggestion)); + + tailingImageView.setOnClickListener(v -> { + searchViewModel.deleteRecentSearch(suggestion); + setRecentSuggestions(); + }); + + bind.searchViewSuggestionContainer.addView(view); + } + } + + public void setSearchSuggestions(String query) { + searchViewModel.getSearchSuggestion(query).observe(getViewLifecycleOwner(), suggestions -> { + bind.searchViewSuggestionContainer.removeAllViews(); + + for (String suggestion : suggestions) { + View view = LayoutInflater.from(bind.searchViewSuggestionContainer.getContext()).inflate(R.layout.item_search_suggestion, bind.searchViewSuggestionContainer, false); + + ImageView leadingImageView = view.findViewById(R.id.search_suggestion_icon); + TextView titleView = view.findViewById(R.id.search_suggestion_title); + ImageView tailingImageView = view.findViewById(R.id.search_suggestion_delete_icon); + + leadingImageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_search, null)); + titleView.setText(suggestion); + tailingImageView.setVisibility(View.GONE); + + view.setOnClickListener(v -> search(suggestion)); + + bind.searchViewSuggestionContainer.addView(view); + } + }); } public void search(String query) { searchViewModel.setQuery(query); - - bind.persistentSearchView.setInputQuery(query); + bind.searchBar.setText(query); + bind.searchView.hide(); performSearch(query); } @@ -216,7 +251,7 @@ public class SearchFragment extends Fragment implements ClickCallback { } private void inputFocus() { - bind.persistentSearchView.expand(); + bind.searchView.show(); } private void initializeMediaBrowser() { diff --git a/app/src/main/res/drawable/ic_close.xml b/app/src/main/res/drawable/ic_close.xml index d5d2297a..c1aa0f10 100644 --- a/app/src/main/res/drawable/ic_close.xml +++ b/app/src/main/res/drawable/ic_close.xml @@ -1,10 +1,9 @@ + android:viewportWidth="960" + android:viewportHeight="960"> - + android:fillColor="@color/titleTextColor" + android:pathData="M256,760L200,704L424,480L200,256L256,200L480,424L704,200L760,256L536,480L760,704L704,760L480,536L256,760Z"/> + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_history.xml b/app/src/main/res/drawable/ic_history.xml new file mode 100644 index 00000000..e6b3b458 --- /dev/null +++ b/app/src/main/res/drawable/ic_history.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_search.xml b/app/src/main/res/layout/fragment_search.xml index 3d41bd58..eec6e6fb 100644 --- a/app/src/main/res/layout/fragment_search.xml +++ b/app/src/main/res/layout/fragment_search.xml @@ -1,54 +1,14 @@ - - - - - + android:layout_height="match_parent" + app:layout_behavior="@string/searchbar_scrolling_view_behavior"> @@ -150,4 +111,39 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_search_suggestion.xml b/app/src/main/res/layout/item_search_suggestion.xml new file mode 100644 index 00000000..9a644ea7 --- /dev/null +++ b/app/src/main/res/layout/item_search_suggestion.xml @@ -0,0 +1,48 @@ + + + + + + + + +