|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 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 com.duckduckgo.app.browser.defaultbrowsing.prompts.ui.experiment |
| 18 | + |
| 19 | +import android.annotation.SuppressLint |
| 20 | +import android.content.Context |
| 21 | +import android.content.DialogInterface |
| 22 | +import android.view.LayoutInflater |
| 23 | +import android.widget.FrameLayout |
| 24 | +import com.duckduckgo.app.browser.databinding.BottomSheetExperimentHomeScreenWidgetBinding |
| 25 | +import com.google.android.material.R |
| 26 | +import com.google.android.material.bottomsheet.BottomSheetBehavior |
| 27 | +import com.google.android.material.bottomsheet.BottomSheetDialog |
| 28 | +import com.google.android.material.shape.CornerFamily |
| 29 | +import com.google.android.material.shape.MaterialShapeDrawable |
| 30 | + |
| 31 | +@SuppressLint("NoBottomSheetDialog") |
| 32 | +class ExperimentalHomeScreenWidgetBottomSheetDialog( |
| 33 | + private val context: Context, |
| 34 | + isLightModeEnabled: Boolean, |
| 35 | +) : BottomSheetDialog(context) { |
| 36 | + |
| 37 | + private val binding: BottomSheetExperimentHomeScreenWidgetBinding = |
| 38 | + BottomSheetExperimentHomeScreenWidgetBinding.inflate(LayoutInflater.from(context)) |
| 39 | + |
| 40 | + var eventListener: EventListener? = null |
| 41 | + |
| 42 | + init { |
| 43 | + setContentView(binding.root) |
| 44 | + // We need the dialog to always be expanded and not draggable because the content takes up a lot of vertical space and requires a scroll view, |
| 45 | + // especially in landscape aspect-ratios. If the dialog started as collapsed, the drag would interfere with internal scroll. |
| 46 | + this.behavior.state = BottomSheetBehavior.STATE_EXPANDED |
| 47 | + this.behavior.isDraggable = false |
| 48 | + |
| 49 | + setOnShowListener { dialogInterface -> |
| 50 | + setRoundCorners(dialogInterface) |
| 51 | + eventListener?.onShown() |
| 52 | + } |
| 53 | + setOnCancelListener { |
| 54 | + eventListener?.onCanceled() |
| 55 | + dismiss() |
| 56 | + } |
| 57 | + binding.experimentHomeScreenWidgetBottomSheetDialogImage.setImageResource( |
| 58 | + if (isLightModeEnabled) { |
| 59 | + com.duckduckgo.app.browser.R.drawable.experiment_widget_promo_light |
| 60 | + } else { |
| 61 | + com.duckduckgo.app.browser.R.drawable.experiment_widget_promo_dark |
| 62 | + }, |
| 63 | + ) |
| 64 | + binding.experimentHomeScreenWidgetBottomSheetDialogPrimaryButton.setOnClickListener { |
| 65 | + eventListener?.onAddWidgetButtonClicked() |
| 66 | + dismiss() |
| 67 | + } |
| 68 | + binding.experimentHomeScreenWidgetBottomSheetDialogGhostButton.setOnClickListener { |
| 69 | + eventListener?.onNotNowButtonClicked() |
| 70 | + dismiss() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * By default, when bottom sheet dialog is expanded, the corners become squared. |
| 76 | + * This function ensures that the bottom sheet dialog will have rounded corners even when in an expanded state. |
| 77 | + */ |
| 78 | + private fun setRoundCorners(dialogInterface: DialogInterface) { |
| 79 | + val bottomSheetDialog = dialogInterface as BottomSheetDialog |
| 80 | + val bottomSheet = bottomSheetDialog.findViewById<FrameLayout>(R.id.design_bottom_sheet) |
| 81 | + |
| 82 | + val shapeDrawable = MaterialShapeDrawable.createWithElevationOverlay(context) |
| 83 | + shapeDrawable.shapeAppearanceModel = shapeDrawable.shapeAppearanceModel |
| 84 | + .toBuilder() |
| 85 | + .setTopLeftCorner(CornerFamily.ROUNDED, context.resources.getDimension(com.duckduckgo.mobile.android.R.dimen.dialogBorderRadius)) |
| 86 | + .setTopRightCorner(CornerFamily.ROUNDED, context.resources.getDimension(com.duckduckgo.mobile.android.R.dimen.dialogBorderRadius)) |
| 87 | + .build() |
| 88 | + bottomSheet?.background = shapeDrawable |
| 89 | + } |
| 90 | + |
| 91 | + interface EventListener { |
| 92 | + fun onShown() |
| 93 | + fun onCanceled() |
| 94 | + fun onAddWidgetButtonClicked() |
| 95 | + fun onNotNowButtonClicked() |
| 96 | + } |
| 97 | +} |
0 commit comments