|
| 1 | +/* |
| 2 | + * Copyright 2022 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.material.catalog.search; |
| 18 | + |
| 19 | +import io.material.catalog.R; |
| 20 | + |
| 21 | +import android.annotation.SuppressLint; |
| 22 | +import android.app.Activity; |
| 23 | +import android.os.Bundle; |
| 24 | +import android.view.LayoutInflater; |
| 25 | +import android.view.MenuItem; |
| 26 | +import android.view.View; |
| 27 | +import android.view.ViewGroup; |
| 28 | +import android.widget.ImageView; |
| 29 | +import android.widget.TextView; |
| 30 | +import androidx.annotation.DrawableRes; |
| 31 | +import androidx.annotation.Nullable; |
| 32 | +import androidx.annotation.StringRes; |
| 33 | +import com.google.android.material.search.SearchBar; |
| 34 | +import com.google.android.material.search.SearchView; |
| 35 | +import com.google.android.material.snackbar.Snackbar; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +final class SearchDemoUtils { |
| 40 | + |
| 41 | + private SearchDemoUtils() {} |
| 42 | + |
| 43 | + public static void setUpSearchBar(Activity activity, SearchBar searchBar) { |
| 44 | + searchBar.inflateMenu(R.menu.cat_searchbar_menu); |
| 45 | + searchBar.setOnMenuItemClickListener( |
| 46 | + menuItem -> { |
| 47 | + showSnackbar(activity, menuItem); |
| 48 | + return true; |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressLint("NewApi") |
| 53 | + public static void setUpSearchView( |
| 54 | + Activity activity, SearchBar searchBar, SearchView searchView) { |
| 55 | + searchView.inflateMenu(R.menu.cat_searchview_menu); |
| 56 | + searchView.setOnMenuItemClickListener( |
| 57 | + menuItem -> { |
| 58 | + showSnackbar(activity, menuItem); |
| 59 | + return true; |
| 60 | + }); |
| 61 | + searchView |
| 62 | + .getEditText() |
| 63 | + .setOnEditorActionListener( |
| 64 | + (v, actionId, event) -> { |
| 65 | + submitSearchQuery(searchBar, searchView, searchView.getText().toString()); |
| 66 | + return false; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + static void showSnackbar(Activity activity, MenuItem menuItem) { |
| 71 | + Snackbar.make( |
| 72 | + activity.findViewById(android.R.id.content), menuItem.getTitle(), Snackbar.LENGTH_SHORT) |
| 73 | + .show(); |
| 74 | + } |
| 75 | + |
| 76 | + static void startOnLoadAnimation(SearchBar searchBar, @Nullable Bundle bundle) { |
| 77 | + // Don't start animation on rotation. Only needed in demo because minIntervalSeconds is 0. |
| 78 | + if (bundle == null) { |
| 79 | + searchBar.startOnLoadAnimation(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + static void setUpSuggestions( |
| 84 | + ViewGroup suggestionContainer, SearchBar searchBar, SearchView searchView) { |
| 85 | + addSuggestionTitleView( |
| 86 | + suggestionContainer, R.string.cat_searchview_suggestion_section_title_yesterday); |
| 87 | + addSuggestionItemViews(suggestionContainer, getYesterdaySuggestions(), searchBar, searchView); |
| 88 | + |
| 89 | + addSuggestionTitleView( |
| 90 | + suggestionContainer, R.string.cat_searchview_suggestion_section_title_this_week); |
| 91 | + addSuggestionItemViews(suggestionContainer, getThisWeekSuggestions(), searchBar, searchView); |
| 92 | + } |
| 93 | + |
| 94 | + private static void addSuggestionTitleView(ViewGroup parent, @StringRes int titleResId) { |
| 95 | + TextView titleView = |
| 96 | + (TextView) |
| 97 | + LayoutInflater.from(parent.getContext()) |
| 98 | + .inflate(R.layout.cat_search_suggestion_title, parent, false); |
| 99 | + |
| 100 | + titleView.setText(titleResId); |
| 101 | + |
| 102 | + parent.addView(titleView); |
| 103 | + } |
| 104 | + |
| 105 | + private static void addSuggestionItemViews( |
| 106 | + ViewGroup parent, |
| 107 | + List<SuggestionItem> suggestionItems, |
| 108 | + SearchBar searchBar, |
| 109 | + SearchView searchView) { |
| 110 | + for (SuggestionItem suggestionItem : suggestionItems) { |
| 111 | + addSuggestionItemView(parent, suggestionItem, searchBar, searchView); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static void addSuggestionItemView( |
| 116 | + ViewGroup parent, SuggestionItem suggestionItem, SearchBar searchBar, SearchView searchView) { |
| 117 | + View view = |
| 118 | + LayoutInflater.from(parent.getContext()) |
| 119 | + .inflate(R.layout.cat_search_suggestion_item, parent, false); |
| 120 | + |
| 121 | + ImageView iconView = view.findViewById(R.id.cat_searchbar_suggestion_icon); |
| 122 | + TextView titleView = view.findViewById(R.id.cat_searchbar_suggestion_title); |
| 123 | + TextView subtitleView = view.findViewById(R.id.cat_searchbar_suggestion_subtitle); |
| 124 | + |
| 125 | + iconView.setImageResource(suggestionItem.iconResId); |
| 126 | + titleView.setText(suggestionItem.title); |
| 127 | + subtitleView.setText(suggestionItem.subtitle); |
| 128 | + |
| 129 | + view.setOnClickListener(v -> submitSearchQuery(searchBar, searchView, suggestionItem.title)); |
| 130 | + |
| 131 | + parent.addView(view); |
| 132 | + } |
| 133 | + |
| 134 | + private static List<SuggestionItem> getYesterdaySuggestions() { |
| 135 | + List<SuggestionItem> suggestionItems = new ArrayList<>(); |
| 136 | + suggestionItems.add( |
| 137 | + new SuggestionItem( |
| 138 | + R.drawable.ic_schedule_vd_theme_24, "481 Van Brunt Street", "Brooklyn, NY")); |
| 139 | + suggestionItems.add( |
| 140 | + new SuggestionItem( |
| 141 | + R.drawable.ic_home_vd_theme_24, "Home", "199 Pacific Street, Brooklyn, NY")); |
| 142 | + return suggestionItems; |
| 143 | + } |
| 144 | + |
| 145 | + private static List<SuggestionItem> getThisWeekSuggestions() { |
| 146 | + List<SuggestionItem> suggestionItems = new ArrayList<>(); |
| 147 | + suggestionItems.add( |
| 148 | + new SuggestionItem( |
| 149 | + R.drawable.ic_schedule_vd_theme_24, |
| 150 | + "BEP GA", |
| 151 | + "Forsyth Street, New York, NY")); |
| 152 | + suggestionItems.add( |
| 153 | + new SuggestionItem( |
| 154 | + R.drawable.ic_schedule_vd_theme_24, |
| 155 | + "Sushi Nakazawa", |
| 156 | + "Commerce Street, New York, NY")); |
| 157 | + suggestionItems.add( |
| 158 | + new SuggestionItem( |
| 159 | + R.drawable.ic_schedule_vd_theme_24, |
| 160 | + "IFC Center", |
| 161 | + "6th Avenue, New York, NY")); |
| 162 | + return suggestionItems; |
| 163 | + } |
| 164 | + |
| 165 | + private static void submitSearchQuery(SearchBar searchBar, SearchView searchView, String query) { |
| 166 | + searchBar.setText(query); |
| 167 | + searchView.hide(); |
| 168 | + } |
| 169 | + |
| 170 | + private static class SuggestionItem { |
| 171 | + @DrawableRes private final int iconResId; |
| 172 | + private final String title; |
| 173 | + private final String subtitle; |
| 174 | + |
| 175 | + private SuggestionItem(int iconResId, String title, String subtitle) { |
| 176 | + this.iconResId = iconResId; |
| 177 | + this.title = title; |
| 178 | + this.subtitle = subtitle; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments