|
| 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.sidesheet; |
| 18 | + |
| 19 | +import io.material.catalog.R; |
| 20 | + |
| 21 | +import android.os.Bundle; |
| 22 | +import androidx.appcompat.app.AppCompatActivity; |
| 23 | +import androidx.appcompat.app.AppCompatDialog; |
| 24 | +import androidx.appcompat.widget.Toolbar; |
| 25 | +import android.view.LayoutInflater; |
| 26 | +import android.view.View; |
| 27 | +import android.view.ViewGroup; |
| 28 | +import android.widget.Button; |
| 29 | +import android.widget.TextView; |
| 30 | +import androidx.annotation.IdRes; |
| 31 | +import androidx.annotation.LayoutRes; |
| 32 | +import androidx.annotation.NonNull; |
| 33 | +import androidx.annotation.Nullable; |
| 34 | +import androidx.annotation.StringRes; |
| 35 | +import androidx.core.view.ViewCompat; |
| 36 | +import com.google.android.material.sidesheet.SideSheetBehavior; |
| 37 | +import com.google.android.material.sidesheet.SideSheetCallback; |
| 38 | +import com.google.android.material.sidesheet.SideSheetDialog; |
| 39 | +import io.material.catalog.feature.DemoFragment; |
| 40 | +import io.material.catalog.preferences.CatalogPreferencesHelper; |
| 41 | + |
| 42 | +/** A fragment that displays the main Side Sheet demo for the Catalog app. */ |
| 43 | +public final class SideSheetMainDemoFragment extends DemoFragment { |
| 44 | + |
| 45 | + @Nullable private CatalogPreferencesHelper catalogPreferencesHelper; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onCreate(@Nullable Bundle bundle) { |
| 49 | + super.onCreate(bundle); |
| 50 | + // The preferences helper is used in an adhoc way with the toolbar since the demo draws its own |
| 51 | + // action bar, in order to allow the side sheet to be 100% of the screen's height. |
| 52 | + catalogPreferencesHelper = new CatalogPreferencesHelper(getParentFragmentManager()); |
| 53 | + } |
| 54 | + |
| 55 | + @NonNull |
| 56 | + @Override |
| 57 | + public View onCreateDemoView( |
| 58 | + @NonNull LayoutInflater layoutInflater, |
| 59 | + @Nullable ViewGroup viewGroup, |
| 60 | + @Nullable Bundle bundle) { |
| 61 | + View view = layoutInflater.inflate(getDemoContent(), viewGroup, false /* attachToRoot */); |
| 62 | + setUpToolbar(view); |
| 63 | + |
| 64 | + // Set up standard side sheet. |
| 65 | + View standardRightSideSheet = |
| 66 | + setUpSideSheet( |
| 67 | + view, |
| 68 | + R.id.standard_side_sheet_container, |
| 69 | + R.id.show_standard_side_sheet_button, |
| 70 | + R.id.close_icon_button); |
| 71 | + |
| 72 | + setSideSheetCallback( |
| 73 | + standardRightSideSheet, R.id.side_sheet_state_text, R.id.side_sheet_slide_offset_text); |
| 74 | + |
| 75 | + // Set up vertically scrolling side sheet. |
| 76 | + View verticallyScrollingSideSheet = |
| 77 | + setUpSideSheet( |
| 78 | + view, |
| 79 | + R.id.vertically_scrolling_side_sheet_container, |
| 80 | + R.id.show_vertically_scrolling_side_sheet_button, |
| 81 | + R.id.vertically_scrolling_side_sheet_close_icon_button); |
| 82 | + |
| 83 | + setSideSheetCallback( |
| 84 | + verticallyScrollingSideSheet, |
| 85 | + R.id.vertically_scrolling_side_sheet_state_text, |
| 86 | + R.id.vertically_scrolling_side_sheet_slide_offset_text); |
| 87 | + |
| 88 | + // Set up modal side sheet. |
| 89 | + SideSheetDialog sideSheetDialog = new SideSheetDialog(requireContext()); |
| 90 | + setUpModalSheet( |
| 91 | + sideSheetDialog, |
| 92 | + R.layout.cat_sidesheet_content, |
| 93 | + R.id.m3_side_sheet, |
| 94 | + R.id.side_sheet_title_text, |
| 95 | + R.string.cat_sidesheet_modal_title); |
| 96 | + |
| 97 | + sideSheetDialog.setDismissWithSheetAnimationEnabled(true); |
| 98 | + View showModalSideSheetButton = view.findViewById(R.id.show_modal_side_sheet_button); |
| 99 | + showModalSideSheetButton.setOnClickListener(v -> sideSheetDialog.show()); |
| 100 | + |
| 101 | + sideSheetDialog |
| 102 | + .getBehavior() |
| 103 | + .addCallback( |
| 104 | + createSideSheetCallback( |
| 105 | + sideSheetDialog.findViewById(R.id.side_sheet_state_text), |
| 106 | + sideSheetDialog.findViewById(R.id.side_sheet_slide_offset_text))); |
| 107 | + |
| 108 | + View modalSideSheetCloseIconButton = sideSheetDialog.findViewById(R.id.close_icon_button); |
| 109 | + if (modalSideSheetCloseIconButton != null) { |
| 110 | + modalSideSheetCloseIconButton.setOnClickListener(v -> sideSheetDialog.hide()); |
| 111 | + } |
| 112 | + |
| 113 | + return view; |
| 114 | + } |
| 115 | + |
| 116 | + private View setUpSideSheet( |
| 117 | + @NonNull View view, |
| 118 | + @IdRes int sideSheetContainerId, |
| 119 | + @IdRes int showSideSheetButtonId, |
| 120 | + @IdRes int closeIconButtonId) { |
| 121 | + View sideSheet = view.findViewById(sideSheetContainerId); |
| 122 | + SideSheetBehavior<View> sideSheetBehavior = SideSheetBehavior.from(sideSheet); |
| 123 | + |
| 124 | + Button showSideSheetButton = view.findViewById(showSideSheetButtonId); |
| 125 | + showSideSheetButton.setOnClickListener(unusedView -> showSideSheet(sideSheetBehavior)); |
| 126 | + |
| 127 | + View standardSideSheetCloseIconButton = sideSheet.findViewById(closeIconButtonId); |
| 128 | + standardSideSheetCloseIconButton.setOnClickListener(v -> hideSideSheet(sideSheetBehavior)); |
| 129 | + |
| 130 | + return sideSheet; |
| 131 | + } |
| 132 | + |
| 133 | + private void setSideSheetCallback( |
| 134 | + View sideSheet, @IdRes int stateTextViewId, @IdRes int slideOffsetTextId) { |
| 135 | + SideSheetBehavior<View> sideSheetBehavior = SideSheetBehavior.from(sideSheet); |
| 136 | + sideSheetBehavior.addCallback( |
| 137 | + createSideSheetCallback( |
| 138 | + sideSheet.findViewById(stateTextViewId), sideSheet.findViewById(slideOffsetTextId))); |
| 139 | + } |
| 140 | + |
| 141 | + private void setUpModalSheet( |
| 142 | + @NonNull AppCompatDialog sheetDialog, |
| 143 | + @LayoutRes int sheetContentLayoutRes, |
| 144 | + @IdRes int sheetContentRootIdRes, |
| 145 | + @IdRes int sheetTitleIdRes, |
| 146 | + @StringRes int sheetTitleStringRes) { |
| 147 | + sheetDialog.setContentView(sheetContentLayoutRes); |
| 148 | + View modalSheetContent = sheetDialog.findViewById(sheetContentRootIdRes); |
| 149 | + if (modalSheetContent != null) { |
| 150 | + TextView modalSideSheetTitle = modalSheetContent.findViewById(sheetTitleIdRes); |
| 151 | + modalSideSheetTitle.setText(sheetTitleStringRes); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void setUpToolbar(@NonNull View view) { |
| 156 | + @NonNull Toolbar toolbar = ViewCompat.requireViewById(view, R.id.toolbar); |
| 157 | + @Nullable AppCompatActivity activity = (AppCompatActivity) getActivity(); |
| 158 | + if (activity != null) { |
| 159 | + toolbar.setNavigationOnClickListener(v -> activity.onBackPressed()); |
| 160 | + if (catalogPreferencesHelper != null) { |
| 161 | + catalogPreferencesHelper.onCreateOptionsMenu(toolbar.getMenu(), activity.getMenuInflater()); |
| 162 | + toolbar.setOnMenuItemClickListener(catalogPreferencesHelper::onOptionsItemSelected); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void showSideSheet(SideSheetBehavior<View> sheetBehavior) { |
| 168 | + sheetBehavior.expand(); |
| 169 | + } |
| 170 | + |
| 171 | + private void hideSideSheet(SideSheetBehavior<View> sheetBehavior) { |
| 172 | + sheetBehavior.hide(); |
| 173 | + } |
| 174 | + |
| 175 | + @LayoutRes |
| 176 | + int getDemoContent() { |
| 177 | + return R.layout.cat_sidesheet_fragment; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean shouldShowDefaultDemoActionBar() { |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + private SideSheetCallback createSideSheetCallback( |
| 186 | + TextView stateTextView, TextView slideOffsetTextView) { |
| 187 | + return new SideSheetCallback() { |
| 188 | + @Override |
| 189 | + public void onStateChanged(@NonNull View sheet, int newState) { |
| 190 | + switch (newState) { |
| 191 | + case SideSheetBehavior.STATE_DRAGGING: |
| 192 | + stateTextView.setText(R.string.cat_sidesheet_state_dragging); |
| 193 | + break; |
| 194 | + case SideSheetBehavior.STATE_EXPANDED: |
| 195 | + stateTextView.setText(R.string.cat_sidesheet_state_expanded); |
| 196 | + break; |
| 197 | + case SideSheetBehavior.STATE_SETTLING: |
| 198 | + stateTextView.setText(R.string.cat_sidesheet_state_settling); |
| 199 | + break; |
| 200 | + case SideSheetBehavior.STATE_HIDDEN: |
| 201 | + default: |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void onSlide(@NonNull View sheet, float slideOffset) { |
| 208 | + slideOffsetTextView.setText( |
| 209 | + getResources().getString(R.string.cat_sidesheet_slide_offset_text, slideOffset)); |
| 210 | + } |
| 211 | + }; |
| 212 | + } |
| 213 | +} |
0 commit comments