Skip to content

Commit

Permalink
[Catalog] Added Search Demo .
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 490241875
  • Loading branch information
raajkumars authored and drchen committed Nov 22, 2022
1 parent b08257b commit 030fe32
Show file tree
Hide file tree
Showing 34 changed files with 1,427 additions and 0 deletions.
1 change: 1 addition & 0 deletions catalog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def srcDirs = [
'preferences',
'progressindicator',
'radiobutton',
'search',
'shapetheming',
'slider',
'tableofcontents',
Expand Down
8 changes: 8 additions & 0 deletions catalog/java/io/material/catalog/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@
<activity
android:name="io.material.catalog.color.ColorHarmonizationDemoActivity"
android:exported="true" />
<activity
android:name="io.material.catalog.search.SearchMainDemoActivity"
android:windowSoftInputMode="adjustNothing"
android:exported="true" />
<activity
android:name="io.material.catalog.search.SearchRecyclerDemoActivity"
android:windowSoftInputMode="adjustNothing"
android:exported="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
Copyright 2022 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:viewportWidth="80.0"
android:viewportHeight="80.0"
android:width="24dp"
android:height="24dp"
tools:ignore="NewApi">
<group
android:translateX="12"
android:translateY="22">
<path
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"
android:fillColor="#ffffff"
android:strokeColor="#cccccc"
android:strokeWidth="0.5"/>
<group
android:translateX="6"
android:translateY="6">
<path
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"
android:fillColor="#80868b" />
</group>
</group>
</vector>
181 changes: 181 additions & 0 deletions catalog/java/io/material/catalog/search/SearchDemoUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.material.catalog.search;

import io.material.catalog.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.google.android.material.search.SearchBar;
import com.google.android.material.search.SearchView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;

final class SearchDemoUtils {

private SearchDemoUtils() {}

public static void setUpSearchBar(Activity activity, SearchBar searchBar) {
searchBar.inflateMenu(R.menu.cat_searchbar_menu);
searchBar.setOnMenuItemClickListener(
menuItem -> {
showSnackbar(activity, menuItem);
return true;
});
}

@SuppressLint("NewApi")
public static void setUpSearchView(
Activity activity, SearchBar searchBar, SearchView searchView) {
searchView.inflateMenu(R.menu.cat_searchview_menu);
searchView.setOnMenuItemClickListener(
menuItem -> {
showSnackbar(activity, menuItem);
return true;
});
searchView
.getEditText()
.setOnEditorActionListener(
(v, actionId, event) -> {
submitSearchQuery(searchBar, searchView, searchView.getText().toString());
return false;
});
}

static void showSnackbar(Activity activity, MenuItem menuItem) {
Snackbar.make(
activity.findViewById(android.R.id.content), menuItem.getTitle(), Snackbar.LENGTH_SHORT)
.show();
}

static void startOnLoadAnimation(SearchBar searchBar, @Nullable Bundle bundle) {
// Don't start animation on rotation. Only needed in demo because minIntervalSeconds is 0.
if (bundle == null) {
searchBar.startOnLoadAnimation();
}
}

static void setUpSuggestions(
ViewGroup suggestionContainer, SearchBar searchBar, SearchView searchView) {
addSuggestionTitleView(
suggestionContainer, R.string.cat_searchview_suggestion_section_title_yesterday);
addSuggestionItemViews(suggestionContainer, getYesterdaySuggestions(), searchBar, searchView);

addSuggestionTitleView(
suggestionContainer, R.string.cat_searchview_suggestion_section_title_this_week);
addSuggestionItemViews(suggestionContainer, getThisWeekSuggestions(), searchBar, searchView);
}

private static void addSuggestionTitleView(ViewGroup parent, @StringRes int titleResId) {
TextView titleView =
(TextView)
LayoutInflater.from(parent.getContext())
.inflate(R.layout.cat_search_suggestion_title, parent, false);

titleView.setText(titleResId);

parent.addView(titleView);
}

private static void addSuggestionItemViews(
ViewGroup parent,
List<SuggestionItem> suggestionItems,
SearchBar searchBar,
SearchView searchView) {
for (SuggestionItem suggestionItem : suggestionItems) {
addSuggestionItemView(parent, suggestionItem, searchBar, searchView);
}
}

private static void addSuggestionItemView(
ViewGroup parent, SuggestionItem suggestionItem, SearchBar searchBar, SearchView searchView) {
View view =
LayoutInflater.from(parent.getContext())
.inflate(R.layout.cat_search_suggestion_item, parent, false);

ImageView iconView = view.findViewById(R.id.cat_searchbar_suggestion_icon);
TextView titleView = view.findViewById(R.id.cat_searchbar_suggestion_title);
TextView subtitleView = view.findViewById(R.id.cat_searchbar_suggestion_subtitle);

iconView.setImageResource(suggestionItem.iconResId);
titleView.setText(suggestionItem.title);
subtitleView.setText(suggestionItem.subtitle);

view.setOnClickListener(v -> submitSearchQuery(searchBar, searchView, suggestionItem.title));

parent.addView(view);
}

private static List<SuggestionItem> getYesterdaySuggestions() {
List<SuggestionItem> suggestionItems = new ArrayList<>();
suggestionItems.add(
new SuggestionItem(
R.drawable.ic_schedule_vd_theme_24, "481 Van Brunt Street", "Brooklyn, NY"));
suggestionItems.add(
new SuggestionItem(
R.drawable.ic_home_vd_theme_24, "Home", "199 Pacific Street, Brooklyn, NY"));
return suggestionItems;
}

private static List<SuggestionItem> getThisWeekSuggestions() {
List<SuggestionItem> suggestionItems = new ArrayList<>();
suggestionItems.add(
new SuggestionItem(
R.drawable.ic_schedule_vd_theme_24,
"BEP GA",
"Forsyth Street, New York, NY"));
suggestionItems.add(
new SuggestionItem(
R.drawable.ic_schedule_vd_theme_24,
"Sushi Nakazawa",
"Commerce Street, New York, NY"));
suggestionItems.add(
new SuggestionItem(
R.drawable.ic_schedule_vd_theme_24,
"IFC Center",
"6th Avenue, New York, NY"));
return suggestionItems;
}

private static void submitSearchQuery(SearchBar searchBar, SearchView searchView, String query) {
searchBar.setText(query);
searchView.hide();
}

private static class SuggestionItem {
@DrawableRes private final int iconResId;
private final String title;
private final String subtitle;

private SuggestionItem(int iconResId, String title, String subtitle) {
this.iconResId = iconResId;
this.title = title;
this.subtitle = subtitle;
}
}
}
95 changes: 95 additions & 0 deletions catalog/java/io/material/catalog/search/SearchFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.material.catalog.search;

import io.material.catalog.R;

import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import dagger.Provides;
import dagger.android.ContributesAndroidInjector;
import dagger.multibindings.IntoSet;
import io.material.catalog.application.scope.ActivityScope;
import io.material.catalog.application.scope.FragmentScope;
import io.material.catalog.feature.Demo;
import io.material.catalog.feature.DemoLandingFragment;
import io.material.catalog.feature.FeatureDemo;
import java.util.ArrayList;
import java.util.List;

/** A landing fragment that links to Open Search Bar demos for the Catalog app. */
public class SearchFragment extends DemoLandingFragment {

@Override
public int getTitleResId() {
return R.string.cat_searchbar_title;
}

@Override
public int getDescriptionResId() {
return R.string.cat_searchbar_description;
}

@Override
@NonNull
public Demo getMainDemo() {
return new Demo() {
@Nullable
@Override
public Intent createActivityIntent() {
return new Intent(getContext(), SearchMainDemoActivity.class);
}
};
}

@Override
@NonNull
public List<Demo> getAdditionalDemos() {
List<Demo> additionalDemos = new ArrayList<>();
additionalDemos.add(
new Demo(R.string.cat_searchbar_recycler_title) {
@Override
public Intent createActivityIntent() {
return new Intent(getContext(), SearchRecyclerDemoActivity.class);
}
});
return additionalDemos;
}

/** The Dagger module for {@link SearchFragment} dependencies. */
@dagger.Module
public abstract static class Module {

@FragmentScope
@ContributesAndroidInjector
abstract SearchFragment contributeInjector();

@IntoSet
@Provides
@ActivityScope
static FeatureDemo provideFeatureDemo() {
return new FeatureDemo(R.string.cat_searchbar_title, R.drawable.ic_search_bar) {
@Override
public Fragment createFragment() {
return new SearchFragment();
}
};
}
}
}
Loading

0 comments on commit 030fe32

Please sign in to comment.