|
| 1 | +/* |
| 2 | + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: GPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package com.zeapo.pwdstore.ui.dialogs |
| 7 | + |
| 8 | +import android.content.Context |
| 9 | +import android.graphics.drawable.GradientDrawable |
| 10 | +import android.os.Bundle |
| 11 | +import android.view.LayoutInflater |
| 12 | +import android.view.View |
| 13 | +import android.view.ViewGroup |
| 14 | +import android.view.ViewTreeObserver |
| 15 | +import android.widget.FrameLayout |
| 16 | +import androidx.annotation.StringRes |
| 17 | +import androidx.core.view.isVisible |
| 18 | +import com.google.android.material.bottomsheet.BottomSheetBehavior |
| 19 | +import com.google.android.material.bottomsheet.BottomSheetDialog |
| 20 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment |
| 21 | +import com.zeapo.pwdstore.R |
| 22 | +import com.zeapo.pwdstore.databinding.BasicBottomSheetBinding |
| 23 | +import com.zeapo.pwdstore.utils.resolveAttribute |
| 24 | +import com.zeapo.pwdstore.utils.viewBinding |
| 25 | + |
| 26 | +/** |
| 27 | + * [BottomSheetDialogFragment] that exposes a simple [androidx.appcompat.app.AlertDialog] like |
| 28 | + * API through [Builder] to create a similar UI, just at the bottom of the screen. |
| 29 | + */ |
| 30 | +class BasicBottomSheet private constructor( |
| 31 | + val title: String, |
| 32 | + val message: String, |
| 33 | + val positiveButtonClickListener: View.OnClickListener?, |
| 34 | + val negativeButtonClickListener: View.OnClickListener?, |
| 35 | +) : BottomSheetDialogFragment() { |
| 36 | + |
| 37 | + private val binding by viewBinding(BasicBottomSheetBinding::bind) |
| 38 | + |
| 39 | + private var behavior: BottomSheetBehavior<FrameLayout>? = null |
| 40 | + private val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() { |
| 41 | + override fun onSlide(bottomSheet: View, slideOffset: Float) { |
| 42 | + } |
| 43 | + |
| 44 | + override fun onStateChanged(bottomSheet: View, newState: Int) { |
| 45 | + if (newState == BottomSheetBehavior.STATE_COLLAPSED) { |
| 46 | + dismiss() |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
| 52 | + if (savedInstanceState != null) dismiss() |
| 53 | + return layoutInflater.inflate(R.layout.basic_bottom_sheet, container, false) |
| 54 | + } |
| 55 | + |
| 56 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 57 | + super.onViewCreated(view, savedInstanceState) |
| 58 | + view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { |
| 59 | + override fun onGlobalLayout() { |
| 60 | + view.viewTreeObserver.removeOnGlobalLayoutListener(this) |
| 61 | + val dialog = dialog as BottomSheetDialog? ?: return |
| 62 | + behavior = dialog.behavior |
| 63 | + behavior?.apply { |
| 64 | + state = BottomSheetBehavior.STATE_EXPANDED |
| 65 | + peekHeight = 0 |
| 66 | + addBottomSheetCallback(bottomSheetCallback) |
| 67 | + } |
| 68 | + binding.bottomSheetTitle.text = title |
| 69 | + binding.bottomSheetMessage.text = message |
| 70 | + if (positiveButtonClickListener != null) { |
| 71 | + binding.bottomSheetOkButton.isVisible = true |
| 72 | + binding.bottomSheetOkButton.setOnClickListener { |
| 73 | + positiveButtonClickListener.onClick(it) |
| 74 | + dismiss() |
| 75 | + } |
| 76 | + } |
| 77 | + if (negativeButtonClickListener != null) { |
| 78 | + binding.bottomSheetCancelButton.isVisible = true |
| 79 | + binding.bottomSheetCancelButton.setOnClickListener { |
| 80 | + negativeButtonClickListener.onClick(it) |
| 81 | + dismiss() |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + val gradientDrawable = GradientDrawable().apply { |
| 87 | + setColor(requireContext().resolveAttribute(android.R.attr.windowBackground)) |
| 88 | + } |
| 89 | + view.background = gradientDrawable |
| 90 | + } |
| 91 | + |
| 92 | + override fun dismiss() { |
| 93 | + super.dismiss() |
| 94 | + behavior?.removeBottomSheetCallback(bottomSheetCallback) |
| 95 | + } |
| 96 | + |
| 97 | + class Builder(val context: Context) { |
| 98 | + |
| 99 | + private var title: String? = null |
| 100 | + private var message: String? = null |
| 101 | + private var positiveButtonClickListener: View.OnClickListener? = null |
| 102 | + private var negativeButtonClickListener: View.OnClickListener? = null |
| 103 | + |
| 104 | + fun setTitleRes(@StringRes titleRes: Int): Builder { |
| 105 | + this.title = context.resources.getString(titleRes) |
| 106 | + return this |
| 107 | + } |
| 108 | + |
| 109 | + fun setTitle(title: String): Builder { |
| 110 | + this.title = title |
| 111 | + return this |
| 112 | + } |
| 113 | + |
| 114 | + fun setMessageRes(@StringRes messageRes: Int): Builder { |
| 115 | + this.message = context.resources.getString(messageRes) |
| 116 | + return this |
| 117 | + } |
| 118 | + |
| 119 | + fun setMessage(message: String): Builder { |
| 120 | + this.message = message |
| 121 | + return this |
| 122 | + } |
| 123 | + |
| 124 | + fun setPositiveButtonClickListener(listener: View.OnClickListener): Builder { |
| 125 | + this.positiveButtonClickListener = listener |
| 126 | + return this |
| 127 | + } |
| 128 | + |
| 129 | + fun setNegativeButtonClickListener(listener: View.OnClickListener): Builder { |
| 130 | + this.negativeButtonClickListener = listener |
| 131 | + return this |
| 132 | + } |
| 133 | + |
| 134 | + fun build(): BasicBottomSheet { |
| 135 | + require(title != null) { "Title needs to be set" } |
| 136 | + require(message != null) { "Message needs to be set" } |
| 137 | + return BasicBottomSheet( |
| 138 | + title!!, |
| 139 | + message!!, |
| 140 | + positiveButtonClickListener, |
| 141 | + negativeButtonClickListener |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments