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

Material theme and remove flashbar #91

Merged
merged 3 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ dependencies {
//implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"

// A highly customizable, powerful and easy-to-use alerting library for Android.
// https://github.com/aritraroy/Flashbar
implementation "com.andrognito.flashbar:flashbar:$rootProject.flashBarVersion"

// ----------------------------------------------------------------
// Android Unit and Instrumentation test
// ----------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2019 Hossain Khan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hossainkhan.android.demo.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.button.MaterialButton
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.hossainkhan.android.demo.R


/**
* Bottom sheet dialog to show layout information.
*/
class LayoutInfoDialog : BottomSheetDialogFragment() {
companion object {
private const val BUNDLE_ARG_KEY_TITLE = "BUNDLE_TITLE"
private const val BUNDLE_ARG_KEY_DESC = "BUNDLE_DESCRIPTION"

fun newInstance(title: String, description: String): LayoutInfoDialog {
val args = Bundle()
args.putString(BUNDLE_ARG_KEY_TITLE, title)
args.putString(BUNDLE_ARG_KEY_DESC, description)

val dialog = LayoutInfoDialog()

dialog.arguments = args

return dialog
}
}


lateinit var infoTitle: TextView
lateinit var infoDescription: TextView
lateinit var okButton: MaterialButton
lateinit var previewXml: MaterialButton
var previewXmlListener: (() -> Unit)? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(com.hossainkhan.android.demo.R.layout.dialog_layout_info_sheet, container, false)
infoTitle = view.findViewById(R.id.layout_info_title)
infoDescription = view.findViewById(R.id.layout_info_description)
okButton = view.findViewById(R.id.layout_info_ok)
previewXml = view.findViewById(R.id.layout_info_preview_xml)
return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// Make the bottom sheet dialog expand to full height
// Source: https://medium.com/@OguzhanAlpayli/bottom-sheet-dialog-fragment-expanded-full-height-65b725c8309
view.viewTreeObserver.addOnGlobalLayoutListener {
val dialog = dialog as BottomSheetDialog
val bottomSheet = dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
val behavior = BottomSheetBehavior.from<View>(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.peekHeight = 0
}

bindView(
arguments!!.getString(BUNDLE_ARG_KEY_TITLE, ""),
arguments!!.getString(BUNDLE_ARG_KEY_DESC, "")
)
}

override fun onPause() {
super.onPause()
dismiss()
}


private fun bindView(title: String, description: String) {
infoTitle.text = title
infoDescription.text = description

okButton.setOnClickListener { dismiss() }
previewXml.setOnClickListener {
dismiss()
previewXmlListener?.invoke()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MovieDetailsPreviewActivity : LayoutPreviewBaseActivity() {
// Some custom logic to make the UI alive!
when (view.id) {
R.id.rating_thumbs_up, R.id.rating_thumbs_down -> {
applyColorTint((view as ImageButton), R.color.white)
applyColorTint((view as ImageButton), android.R.color.white)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import androidx.core.app.NavUtils
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import com.andrognito.flashbar.Flashbar
import com.hossainkhan.android.demo.R
import com.hossainkhan.android.demo.data.LayoutInformation
import com.hossainkhan.android.demo.ui.dialog.LayoutInfoDialog
import com.hossainkhan.android.demo.viewmodel.LayoutPreviewViewModelFactory
import dagger.android.AndroidInjection
import timber.log.Timber
Expand Down Expand Up @@ -81,7 +81,7 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {

private lateinit var viewModel: LayoutInfoViewModel

internal var flashbar: Flashbar? = null
internal var infoDialog: LayoutInfoDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
Expand All @@ -104,12 +104,6 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
})
}

override fun onStop() {
super.onStop()
flashbar?.dismiss()
flashbar = null
}

private fun updateActionBar(layoutInformation: LayoutInformation) {
supportActionBar?.title = layoutInformation.title
}
Expand All @@ -118,39 +112,21 @@ open class LayoutPreviewBaseActivity : AppCompatActivity() {
* Loads layout information and previews in a snackbar.
*/
private fun showLayoutInfo(layoutInformation: LayoutInformation, fromUser: Boolean = false) {
if (flashbar == null) {
flashbar = Flashbar.Builder(this)
.gravity(Flashbar.Gravity.BOTTOM)
.title(layoutInformation.title.toString())
.message(layoutInformation.description.toString())
.backgroundColorRes(R.color.colorPrimaryDark)
.positiveActionText(R.string.btn_cta_preview_layout_xml)
.negativeActionText(R.string.btn_cta_okay)
.positiveActionTextColorRes(R.color.colorAccent)
.negativeActionTextColorRes(R.color.colorAccent)
.positiveActionTapListener(object : Flashbar.OnActionTapListener {
override fun onActionTapped(bar: Flashbar) {
Timber.d("Loading the XML for ")
bar.dismiss()
loadLayoutUrl()
}
})
.negativeActionTapListener(object : Flashbar.OnActionTapListener {
override fun onActionTapped(bar: Flashbar) {
Timber.d("Closing dialog.")
bar.dismiss()
}
})
.build()
}

Timber.d("Flash bar showing: %s", flashbar?.isShown())
if (flashbar?.isShown() == false) {
infoDialog = LayoutInfoDialog.newInstance(
layoutInformation.title.toString(),
layoutInformation.description.toString()
)
infoDialog?.previewXmlListener = { loadLayoutUrl() }

Timber.d("Layout info showing: %s", infoDialog?.isVisible)
if (infoDialog?.isVisible == false) {
if (fromUser || viewModel.isFirstTime) {
flashbar?.show()
infoDialog?.let {
it.show(supportFragmentManager, "dialog")
}
}
} else {
flashbar?.dismiss()
infoDialog?.dismiss()
}
}

Expand Down
21 changes: 11 additions & 10 deletions app/src/main/res/layout/demo_movie_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/movie_trailer"
style="@style/Widget.AppCompat.Button.Borderless"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:drawableStart="@drawable/ic_play_circle_outline_black_24dp"
android:drawablePadding="5dp"
android:drawableTint="@color/md_pink_300"
android:text="Trailer"
android:textColor="@color/md_grey_400"
app:icon="@drawable/ic_play_circle_outline_black_24dp"
app:iconPadding="5dp"
app:iconTint="@color/md_pink_300"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down Expand Up @@ -128,8 +128,8 @@
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:drawableStart="@drawable/ic_people_black_18dp"
android:drawablePadding="5dp"
android:drawableEnd="@drawable/ic_baseline_4k_18dp"
android:drawablePadding="5dp"
android:drawableTint="@android:color/white"
android:text="93%"
app:layout_constraintLeft_toRightOf="@+id/user_rating"
Expand Down Expand Up @@ -187,21 +187,22 @@
app:layout_constraintTop_toBottomOf="@+id/rating_thumbs_down" />

<!-- =========================== Begin Chained Button =========================== -->
<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/button_rent"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Rent from $4.99"
app:layout_constraintEnd_toStartOf="@+id/button_buy"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="@+id/guideline_vertical_start"
app:layout_constraintTop_toBottomOf="@+id/movie_storyline" />

<Button
<com.google.android.material.button.MaterialButton
android:id="@+id/button_buy"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Buy from $19.99"
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/res/layout/dialog_layout_info_sheet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright (c) 2019 Hossain Khan
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="16dp">

<TextView
android:id="@+id/layout_info_title"
style="@style/TextAppearance.AppCompat.Title.Inverse"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Layout info title" />

<TextView
android:id="@+id/layout_info_description"
style="@style/TextAppearance.AppCompat.Medium.Inverse"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_info_title"
tools:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n\n
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen book." />

<com.google.android.material.button.MaterialButton
android:id="@+id/layout_info_preview_xml"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@string/btn_cta_preview_layout_xml"
app:layout_constraintEnd_toStartOf="@+id/layout_info_ok"
app:layout_constraintTop_toBottomOf="@+id/layout_info_description" />

<com.google.android.material.button.MaterialButton
android:id="@+id/layout_info_ok"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@string/btn_cta_okay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_info_description" />


</androidx.constraintlayout.widget.ConstraintLayout>
10 changes: 8 additions & 2 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--
Base application theme.
Use material theme for clean look.
See
- https://material.io/develop/android/docs/getting-started/
- https://material.io/develop/android/components/
-->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ ext {
daggerVersion = '2.15' // https://github.com/google/dagger
timberLibraryVersion = '4.7.1' // https://github.com/JakeWharton/timber
leakcanaryLibraryVersion = '1.6.3' // https://github.com/square/leakcanary/releases
flashBarVersion = '1.0.3' // https://github.com/aritraroy/Flashbar/releases
}

//
Expand Down