aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/BasicBottomSheet.kt
blob: dad8647903f01c967225d9170d933f5ed37ef528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package dev.msfjarvis.aps.ui.dialogs

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.FrameLayout
import androidx.annotation.StringRes
import androidx.core.view.isVisible
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dev.msfjarvis.aps.R
import dev.msfjarvis.aps.databinding.BasicBottomSheetBinding
import dev.msfjarvis.aps.ui.dialogs.BasicBottomSheet.Builder
import dev.msfjarvis.aps.util.extensions.resolveAttribute
import dev.msfjarvis.aps.util.extensions.viewBinding

/**
 * [BottomSheetDialogFragment] that exposes a simple [androidx.appcompat.app.AlertDialog] like API
 * through [Builder] to create a similar UI, just at the bottom of the screen.
 */
class BasicBottomSheet
private constructor(
  val title: String?,
  val message: String,
  val positiveButtonLabel: String?,
  val negativeButtonLabel: String?,
  val positiveButtonClickListener: View.OnClickListener?,
  val negativeButtonClickListener: View.OnClickListener?,
) : BottomSheetDialogFragment() {

  private val binding by viewBinding(BasicBottomSheetBinding::bind)

  private var behavior: BottomSheetBehavior<FrameLayout>? = null
  private val bottomSheetCallback =
    object : BottomSheetBehavior.BottomSheetCallback() {
      override fun onSlide(bottomSheet: View, slideOffset: Float) {}

      override fun onStateChanged(bottomSheet: View, newState: Int) {
        if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
          dismiss()
        }
      }
    }

  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    if (savedInstanceState != null) dismiss()
    return layoutInflater.inflate(R.layout.basic_bottom_sheet, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view.viewTreeObserver.addOnGlobalLayoutListener(
      object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
          view.viewTreeObserver.removeOnGlobalLayoutListener(this)
          val dialog = dialog as BottomSheetDialog? ?: return
          behavior = dialog.behavior
          behavior?.apply {
            state = BottomSheetBehavior.STATE_EXPANDED
            peekHeight = 0
            addBottomSheetCallback(bottomSheetCallback)
          }
          if (!title.isNullOrEmpty()) {
            binding.bottomSheetTitle.isVisible = true
            binding.bottomSheetTitle.text = title
          }
          binding.bottomSheetMessage.text = message
          if (positiveButtonClickListener != null) {
            positiveButtonLabel?.let { buttonLbl -> binding.bottomSheetOkButton.text = buttonLbl }
            binding.bottomSheetOkButton.isVisible = true
            binding.bottomSheetOkButton.setOnClickListener {
              positiveButtonClickListener.onClick(it)
              dismiss()
            }
          }
          if (negativeButtonClickListener != null) {
            binding.bottomSheetCancelButton.isVisible = true
            negativeButtonLabel?.let { buttonLbl ->
              binding.bottomSheetCancelButton.text = buttonLbl
            }
            binding.bottomSheetCancelButton.setOnClickListener {
              negativeButtonClickListener.onClick(it)
              dismiss()
            }
          }
        }
      }
    )
    val gradientDrawable =
      GradientDrawable().apply {
        setColor(requireContext().resolveAttribute(android.R.attr.windowBackground))
      }
    view.background = gradientDrawable
  }

  override fun dismiss() {
    super.dismiss()
    behavior?.removeBottomSheetCallback(bottomSheetCallback)
  }

  class Builder(val context: Context) {

    private var title: String? = null
    private var message: String? = null
    private var positiveButtonLabel: String? = null
    private var negativeButtonLabel: String? = null
    private var positiveButtonClickListener: View.OnClickListener? = null
    private var negativeButtonClickListener: View.OnClickListener? = null

    fun setTitleRes(@StringRes titleRes: Int): Builder {
      this.title = context.resources.getString(titleRes)
      return this
    }

    fun setTitle(title: String): Builder {
      this.title = title
      return this
    }

    fun setMessageRes(@StringRes messageRes: Int): Builder {
      this.message = context.resources.getString(messageRes)
      return this
    }

    fun setMessage(message: String): Builder {
      this.message = message
      return this
    }

    fun setPositiveButtonClickListener(
      buttonLabel: String? = null,
      listener: View.OnClickListener
    ): Builder {
      this.positiveButtonClickListener = listener
      this.positiveButtonLabel = buttonLabel
      return this
    }

    fun setNegativeButtonClickListener(
      buttonLabel: String? = null,
      listener: View.OnClickListener
    ): Builder {
      this.negativeButtonClickListener = listener
      this.negativeButtonLabel = buttonLabel
      return this
    }

    fun build(): BasicBottomSheet {
      require(message != null) { "Message needs to be set" }
      return BasicBottomSheet(
        title,
        message!!,
        positiveButtonLabel,
        negativeButtonLabel,
        positiveButtonClickListener,
        negativeButtonClickListener
      )
    }
  }
}