Skip to content

Commit 030fe32

Browse files
raajkumarsdrchen
authored andcommitted
[Catalog] Added Search Demo .
PiperOrigin-RevId: 490241875
1 parent b08257b commit 030fe32

34 files changed

+1427
-0
lines changed

catalog/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def srcDirs = [
7878
'preferences',
7979
'progressindicator',
8080
'radiobutton',
81+
'search',
8182
'shapetheming',
8283
'slider',
8384
'tableofcontents',

catalog/java/io/material/catalog/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@
9696
<activity
9797
android:name="io.material.catalog.color.ColorHarmonizationDemoActivity"
9898
android:exported="true" />
99+
<activity
100+
android:name="io.material.catalog.search.SearchMainDemoActivity"
101+
android:windowSoftInputMode="adjustNothing"
102+
android:exported="true" />
103+
<activity
104+
android:name="io.material.catalog.search.SearchRecyclerDemoActivity"
105+
android:windowSoftInputMode="adjustNothing"
106+
android:exported="true" />
99107
</application>
100108

101109
</manifest>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
https://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+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
17+
xmlns:tools="http://schemas.android.com/tools"
18+
android:viewportWidth="80.0"
19+
android:viewportHeight="80.0"
20+
android:width="24dp"
21+
android:height="24dp"
22+
tools:ignore="NewApi">
23+
<group
24+
android:translateX="12"
25+
android:translateY="22">
26+
<path
27+
android:pathData="M4 0l202 0c2.216 0 4 1.784 4 4l0 28c0 2.216 -1.784 4 -4 4L4 36C1.784 36 0 34.216 0 32L0 4C0 1.784 1.784 0 4 0Z"
28+
android:fillColor="#ffffff"
29+
android:strokeColor="#cccccc"
30+
android:strokeWidth="0.5"/>
31+
<group
32+
android:translateX="6"
33+
android:translateY="6">
34+
<path
35+
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"
36+
android:fillColor="#80868b" />
37+
</group>
38+
</group>
39+
</vector>
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.content.Intent;
22+
import androidx.fragment.app.Fragment;
23+
import androidx.annotation.NonNull;
24+
import androidx.annotation.Nullable;
25+
import dagger.Provides;
26+
import dagger.android.ContributesAndroidInjector;
27+
import dagger.multibindings.IntoSet;
28+
import io.material.catalog.application.scope.ActivityScope;
29+
import io.material.catalog.application.scope.FragmentScope;
30+
import io.material.catalog.feature.Demo;
31+
import io.material.catalog.feature.DemoLandingFragment;
32+
import io.material.catalog.feature.FeatureDemo;
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
36+
/** A landing fragment that links to Open Search Bar demos for the Catalog app. */
37+
public class SearchFragment extends DemoLandingFragment {
38+
39+
@Override
40+
public int getTitleResId() {
41+
return R.string.cat_searchbar_title;
42+
}
43+
44+
@Override
45+
public int getDescriptionResId() {
46+
return R.string.cat_searchbar_description;
47+
}
48+
49+
@Override
50+
@NonNull
51+
public Demo getMainDemo() {
52+
return new Demo() {
53+
@Nullable
54+
@Override
55+
public Intent createActivityIntent() {
56+
return new Intent(getContext(), SearchMainDemoActivity.class);
57+
}
58+
};
59+
}
60+
61+
@Override
62+
@NonNull
63+
public List<Demo> getAdditionalDemos() {
64+
List<Demo> additionalDemos = new ArrayList<>();
65+
additionalDemos.add(
66+
new Demo(R.string.cat_searchbar_recycler_title) {
67+
@Override
68+
public Intent createActivityIntent() {
69+
return new Intent(getContext(), SearchRecyclerDemoActivity.class);
70+
}
71+
});
72+
return additionalDemos;
73+
}
74+
75+
/** The Dagger module for {@link SearchFragment} dependencies. */
76+
@dagger.Module
77+
public abstract static class Module {
78+
79+
@FragmentScope
80+
@ContributesAndroidInjector
81+
abstract SearchFragment contributeInjector();
82+
83+
@IntoSet
84+
@Provides
85+
@ActivityScope
86+
static FeatureDemo provideFeatureDemo() {
87+
return new FeatureDemo(R.string.cat_searchbar_title, R.drawable.ic_search_bar) {
88+
@Override
89+
public Fragment createFragment() {
90+
return new SearchFragment();
91+
}
92+
};
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)