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

Commit 0ecdaf9

Browse files
committed
[REFACTOR] Replaced **flashbar** library with Material bottom sheet due to issue reported below.
aritraroy/Flashbar#8 This is possible because of simple use case for this demo app. For other complex app, this solution may not be feasible as flashbar has far more features than just bottom docked bar.
1 parent 2e8f4cb commit 0ecdaf9

File tree

6 files changed

+172
-43
lines changed

6 files changed

+172
-43
lines changed

app/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ dependencies {
8181
//implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
8282
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
8383

84-
// A highly customizable, powerful and easy-to-use alerting library for Android.
85-
// https://github.com/aritraroy/Flashbar
86-
implementation "com.andrognito.flashbar:flashbar:$rootProject.flashBarVersion"
87-
8884
// ----------------------------------------------------------------
8985
// Android Unit and Instrumentation test
9086
// ----------------------------------------------------------------
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
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.hossainkhan.android.demo.ui.dialog
18+
19+
import android.os.Bundle
20+
import android.view.LayoutInflater
21+
import android.view.View
22+
import android.view.ViewGroup
23+
import android.widget.FrameLayout
24+
import android.widget.TextView
25+
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
26+
import com.google.android.material.button.MaterialButton
27+
import com.google.android.material.bottomsheet.BottomSheetBehavior
28+
import com.google.android.material.bottomsheet.BottomSheetDialog
29+
import com.hossainkhan.android.demo.R
30+
31+
32+
/**
33+
* Bottom sheet dialog to show layout information.
34+
*/
35+
class LayoutInfoDialog(
36+
private val title: String = "",
37+
private val desciption: String = "",
38+
private val previewXmlListener: (() -> Unit)? = null
39+
) : BottomSheetDialogFragment() {
40+
lateinit var infoTitle: TextView
41+
lateinit var infoDescription: TextView
42+
lateinit var okButton: MaterialButton
43+
lateinit var previewXml: MaterialButton
44+
45+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
46+
val view = inflater.inflate(R.layout.dialog_layout_info_sheet, container, false)
47+
infoTitle = view.findViewById(R.id.layout_info_title)
48+
infoDescription = view.findViewById(R.id.layout_info_description)
49+
okButton = view.findViewById(R.id.layout_info_ok)
50+
previewXml = view.findViewById(R.id.layout_info_preview_xml)
51+
return view
52+
}
53+
54+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
55+
super.onViewCreated(view, savedInstanceState)
56+
57+
// Make the bottom sheet dialog expand to full height
58+
// Source: https://medium.com/@OguzhanAlpayli/bottom-sheet-dialog-fragment-expanded-full-height-65b725c8309
59+
view.viewTreeObserver.addOnGlobalLayoutListener {
60+
val dialog = dialog as BottomSheetDialog
61+
val bottomSheet = dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
62+
val behavior = BottomSheetBehavior.from<View>(bottomSheet)
63+
behavior.state = BottomSheetBehavior.STATE_EXPANDED
64+
behavior.peekHeight = 0
65+
}
66+
67+
bindView()
68+
}
69+
70+
override fun onPause() {
71+
super.onPause()
72+
dismiss()
73+
}
74+
75+
76+
private fun bindView() {
77+
infoTitle.text = title
78+
infoDescription.text = desciption
79+
80+
okButton.setOnClickListener { dismiss() }
81+
previewXml.setOnClickListener {
82+
dismiss()
83+
previewXmlListener?.invoke()
84+
}
85+
}
86+
}

app/src/main/java/com/hossainkhan/android/demo/ui/functionaldemo/MovieDetailsPreviewActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MovieDetailsPreviewActivity : LayoutPreviewBaseActivity() {
4545
// Some custom logic to make the UI alive!
4646
when (view.id) {
4747
R.id.rating_thumbs_up, R.id.rating_thumbs_down -> {
48-
applyColorTint((view as ImageButton), R.color.white)
48+
applyColorTint((view as ImageButton), android.R.color.white)
4949
}
5050
}
5151
}

app/src/main/java/com/hossainkhan/android/demo/ui/layoutpreview/LayoutPreviewBaseActivity.kt

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import androidx.core.app.NavUtils
2828
import androidx.appcompat.app.AppCompatActivity
2929
import android.view.Menu
3030
import android.view.MenuItem
31-
import com.andrognito.flashbar.Flashbar
3231
import com.hossainkhan.android.demo.R
3332
import com.hossainkhan.android.demo.data.LayoutInformation
33+
import com.hossainkhan.android.demo.ui.dialog.LayoutInfoDialog
3434
import com.hossainkhan.android.demo.viewmodel.LayoutPreviewViewModelFactory
3535
import dagger.android.AndroidInjection
3636
import timber.log.Timber
@@ -81,7 +81,7 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
8181

8282
private lateinit var viewModel: LayoutInfoViewModel
8383

84-
internal var flashbar: Flashbar? = null
84+
internal var infoDialog: LayoutInfoDialog? = null
8585

8686
override fun onCreate(savedInstanceState: Bundle?) {
8787
AndroidInjection.inject(this)
@@ -104,12 +104,6 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
104104
})
105105
}
106106

107-
override fun onStop() {
108-
super.onStop()
109-
flashbar?.dismiss()
110-
flashbar = null
111-
}
112-
113107
private fun updateActionBar(layoutInformation: LayoutInformation) {
114108
supportActionBar?.title = layoutInformation.title
115109
}
@@ -118,39 +112,20 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
118112
* Loads layout information and previews in a snackbar.
119113
*/
120114
private fun showLayoutInfo(layoutInformation: LayoutInformation, fromUser: Boolean = false) {
121-
if (flashbar == null) {
122-
flashbar = Flashbar.Builder(this)
123-
.gravity(Flashbar.Gravity.BOTTOM)
124-
.title(layoutInformation.title.toString())
125-
.message(layoutInformation.description.toString())
126-
.backgroundColorRes(R.color.colorPrimaryDark)
127-
.positiveActionText(R.string.btn_cta_preview_layout_xml)
128-
.negativeActionText(R.string.btn_cta_okay)
129-
.positiveActionTextColorRes(R.color.colorAccent)
130-
.negativeActionTextColorRes(R.color.colorAccent)
131-
.positiveActionTapListener(object : Flashbar.OnActionTapListener {
132-
override fun onActionTapped(bar: Flashbar) {
133-
Timber.d("Loading the XML for ")
134-
bar.dismiss()
135-
loadLayoutUrl()
136-
}
137-
})
138-
.negativeActionTapListener(object : Flashbar.OnActionTapListener {
139-
override fun onActionTapped(bar: Flashbar) {
140-
Timber.d("Closing dialog.")
141-
bar.dismiss()
142-
}
143-
})
144-
.build()
145-
}
115+
infoDialog = LayoutInfoDialog(
116+
layoutInformation.title.toString(),
117+
layoutInformation.description.toString()
118+
) { loadLayoutUrl() }
146119

147-
Timber.d("Flash bar showing: %s", flashbar?.isShown())
148-
if (flashbar?.isShown() == false) {
120+
Timber.d("Layout info showing: %s", infoDialog?.isVisible)
121+
if (infoDialog?.isVisible == false) {
149122
if (fromUser || viewModel.isFirstTime) {
150-
flashbar?.show()
123+
infoDialog?.let {
124+
it.show(supportFragmentManager, "dialog")
125+
}
151126
}
152127
} else {
153-
flashbar?.dismiss()
128+
infoDialog?.dismiss()
154129
}
155130
}
156131

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
~ Copyright (c) 2019 Hossain Khan
5+
~
6+
~ Licensed under the Apache License, Version 2.0 (the "License");
7+
~ you may not use this file except in compliance with the License.
8+
~ You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
19+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
xmlns:tools="http://schemas.android.com/tools"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:background="@color/colorPrimary"
25+
android:padding="24dp">
26+
27+
<TextView
28+
android:id="@+id/layout_info_title"
29+
style="@style/TextAppearance.AppCompat.Title.Inverse"
30+
android:layout_width="0dp"
31+
android:layout_height="wrap_content"
32+
app:layout_constraintEnd_toEndOf="parent"
33+
app:layout_constraintStart_toStartOf="parent"
34+
app:layout_constraintTop_toTopOf="parent"
35+
tools:text="Layout info title" />
36+
37+
<TextView
38+
android:id="@+id/layout_info_description"
39+
style="@style/TextAppearance.AppCompat.Medium.Inverse"
40+
android:layout_width="0dp"
41+
android:layout_height="wrap_content"
42+
android:layout_marginTop="8dp"
43+
app:layout_constraintEnd_toEndOf="parent"
44+
app:layout_constraintStart_toStartOf="parent"
45+
app:layout_constraintTop_toBottomOf="@+id/layout_info_title"
46+
tools:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n\n
47+
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an
48+
unknown printer took a galley of type and scrambled it to make a type specimen book." />
49+
50+
<com.google.android.material.button.MaterialButton
51+
android:id="@+id/layout_info_preview_xml"
52+
style="@style/Widget.MaterialComponents.Button"
53+
android:layout_width="wrap_content"
54+
android:layout_height="wrap_content"
55+
android:layout_marginTop="16dp"
56+
android:layout_marginEnd="8dp"
57+
android:text="@string/btn_cta_preview_layout_xml"
58+
app:layout_constraintEnd_toStartOf="@+id/layout_info_ok"
59+
app:layout_constraintTop_toBottomOf="@+id/layout_info_description" />
60+
61+
<com.google.android.material.button.MaterialButton
62+
android:id="@+id/layout_info_ok"
63+
style="@style/Widget.MaterialComponents.Button"
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_marginTop="16dp"
67+
android:layout_marginEnd="8dp"
68+
android:text="@string/btn_cta_okay"
69+
app:layout_constraintEnd_toEndOf="parent"
70+
app:layout_constraintTop_toBottomOf="@+id/layout_info_description" />
71+
72+
73+
</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ ext {
107107
daggerVersion = '2.15' // https://github.com/google/dagger
108108
timberLibraryVersion = '4.7.1' // https://github.com/JakeWharton/timber
109109
leakcanaryLibraryVersion = '1.6.3' // https://github.com/square/leakcanary/releases
110-
flashBarVersion = '1.0.3' // https://github.com/aritraroy/Flashbar/releases
111110
}
112111

113112
//

0 commit comments

Comments
 (0)