Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 5d5a068

Browse files
committed
BasicBottomSheet: init
Signed-off-by: Harsh Shandilya <[email protected]>
1 parent afbe29e commit 5d5a068

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
3+
~ SPDX-License-Identifier: GPL-3.0-only
4+
-->
5+
6+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
7+
xmlns:app="http://schemas.android.com/apk/res-auto"
8+
xmlns:tools="http://schemas.android.com/tools"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
android:paddingBottom="24dp">
12+
13+
<com.google.android.material.textview.MaterialTextView
14+
android:id="@+id/bottom_sheet_title"
15+
style="@style/TextAppearance.MaterialComponents.Headline6"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:layout_marginStart="8dp"
19+
android:layout_marginTop="8dp"
20+
app:layout_constraintStart_toStartOf="parent"
21+
app:layout_constraintTop_toTopOf="parent"
22+
tools:text="Bottom sheet title" />
23+
24+
<com.google.android.material.textview.MaterialTextView
25+
android:id="@+id/bottom_sheet_message"
26+
style="@style/TextAppearance.MaterialComponents.Body1"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:layout_marginStart="8dp"
30+
android:layout_marginTop="16dp"
31+
android:layout_marginEnd="8dp"
32+
app:layout_constraintTop_toBottomOf="@id/bottom_sheet_title"
33+
tools:text="A long body of text that serves as the bottom sheet message" />
34+
35+
<com.google.android.material.button.MaterialButton
36+
android:id="@+id/bottom_sheet_cancel_button"
37+
style="@style/AppTheme.OutlinedButton"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:layout_marginTop="24dp"
41+
android:layout_marginEnd="8dp"
42+
android:text="@string/dialog_cancel"
43+
android:visibility="gone"
44+
app:layout_constraintEnd_toEndOf="parent"
45+
app:layout_constraintTop_toBottomOf="@id/bottom_sheet_message"
46+
tools:visibility="visible" />
47+
48+
<com.google.android.material.button.MaterialButton
49+
android:id="@+id/bottom_sheet_ok_button"
50+
style="@style/AppTheme.OutlinedButton"
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:layout_marginTop="24dp"
54+
android:layout_marginEnd="8dp"
55+
android:text="@string/dialog_ok"
56+
android:visibility="gone"
57+
app:layout_constraintEnd_toStartOf="@id/bottom_sheet_cancel_button"
58+
app:layout_constraintTop_toBottomOf="@id/bottom_sheet_message"
59+
tools:visibility="visible" />
60+
61+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)