Skip to content

Instantly share code, notes, and snippets.

@aldajo92
Last active June 27, 2018 00:07
Show Gist options
  • Save aldajo92/4061649ace71c6294036b6af105d764e to your computer and use it in GitHub Desktop.
Save aldajo92/4061649ace71c6294036b6af105d764e to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="android.view.View" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/negative_spacing_medium"
android:layout_marginLeft="@dimen/spacing_large"
android:layout_marginRight="@dimen/spacing_large"
android:visibility="@{ view.hasProductImage ? View.VISIBLE : View.GONE }"
app:background="@color/transparent"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/radius_border_button">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium">
<ImageView
android:id="@+id/imageView_product"
android:layout_width="@dimen/size_image_sampling_dialog"
android:layout_height="@dimen/size_image_sampling_dialog"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/spacing_large"
android:layout_marginTop="@dimen/spacing_large"
android:background="@drawable/gradient_green"
android:scaleType="centerCrop"
android:src="@{ view.productImage }" />
</FrameLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/radius_border_button">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient_green"
android:orientation="vertical"
android:paddingBottom="@dimen/spacing_xlarge"
android:paddingLeft="@dimen/spacing_xxlarge"
android:paddingRight="@dimen/spacing_xxlarge"
android:paddingTop="@dimen/spacing_xxlarge">
<TextView
android:id="@+id/textView_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:gravity="center"
android:text="@{ view.preDescription }"
android:textColor="@color/white"
android:textSize="@dimen/font_large"
android:visibility="@{ view.hasPreDescription ? View.VISIBLE : View.GONE }"
tools:text="Te mandaremos un regalito." />
<TextView
android:id="@+id/textView_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:gravity="center"
android:text="@{ view.title }"
android:textColor="@color/white"
android:textSize="@dimen/font_xlarge"
android:textStyle="bold"
android:visibility="@{ view.hasTitle ? View.VISIBLE : View.GONE }"
tools:text="¿Lo recibiste?" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:gravity="center"
android:text="@{ view.description }"
android:textColor="@color/white"
android:textSize="@dimen/font_large"
android:visibility="@{ view.hasDescription ? View.VISIBLE : View.GONE }"
tools:text="Te mandaremos un regalito." />
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginBottom="@dimen/spacing_medium"
android:foreground="@drawable/border_image"
android:padding="3dp"
android:scaleType="centerCrop"
android:src="@{ view.addressImage }"
android:visibility="@{ view.hasAddressImage ? View.VISIBLE : View.GONE }"
tools:visibility="gone" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_rounded_box"
android:gravity="center"
android:padding="@dimen/spacing_medium"
android:text="@{ view.address }"
android:textColor="@color/white"
android:textSize="@dimen/font_large"
android:textStyle="bold"
android:visibility="@{ view.hasAddress ? View.VISIBLE : View.GONE }"
tools:text="Calle 123 #45-67" />
</LinearLayout>
<Button
android:id="@+id/button_remove"
style="@style/button_green_plain"
android:layout_width="match_parent"
android:layout_height="@dimen/button_small"
android:layout_margin="@dimen/spacing_xxlarge"
android:onClick="@{ () -> view.onPrimaryButtonClicked() }"
android:text="@{ view.primaryButtonText }"
android:textSize="@dimen/font_large"
android:visibility="@{ view.hasPrimaryButton ? View.VISIBLE : View.GONE }"
tools:text="@string/copy.delete" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:visibility="@{ view.hasYesNoButton ? View.VISIBLE : View.GONE }">
<Button
style="@style/button_green_plain"
android:layout_width="0dp"
android:layout_height="@dimen/button_small"
android:layout_margin="@dimen/spacing_xxlarge"
android:layout_weight="1"
android:onClick="@{ () -> view.onYesButtonClicked() }"
android:text="@{ view.yesButtonText }"
android:textSize="@dimen/font_large"
tools:text="@string/yes" />
<Button
style="@style/button_green_plain"
android:layout_width="0dp"
android:layout_height="@dimen/button_small"
android:layout_margin="@dimen/spacing_xxlarge"
android:layout_weight="1"
android:onClick="@{ () -> view.onNoButtonClicked() }"
android:text="@{ view.noButtonText }"
android:textSize="@dimen/font_large"
tools:text="@string/no" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</layout>
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.app.DialogFragment
import android.content.Context
import android.databinding.DataBindingUtil
import android.databinding.ObservableBoolean
import android.databinding.ObservableField
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
@SuppressLint("ValidFragment")
class SimpleNotificationDialog<T : Any> private constructor(context: Context) : DialogFragment() {
private val binding: DialogSimpleNotificationBinding by lazy {
DataBindingUtil.inflate<DialogSimpleNotificationBinding>(LayoutInflater.from(context),
R.layout.dialog_simple_notification, null, false)
}
val preDescription = ObservableField<String>()
val title = ObservableField<String>()
val description = ObservableField<String>()
val address = ObservableField<String>()
val primaryButtonText = ObservableField<String>()
val yesButtonText = ObservableField<String>()
val noButtonText = ObservableField<String>()
val productImage = ObservableField<Drawable>()
val addressImage = ObservableField<Drawable>()
val hasPreDescription = ObservableBoolean(false)
val hasTitle = ObservableBoolean(false)
val hasAddress = ObservableBoolean(false)
val hasDescription = ObservableBoolean(false)
val hasProductImage = ObservableBoolean(false)
val hasAddressImage = ObservableBoolean(false)
val hasPrimaryButton = ObservableBoolean(false)
val hasYesNoButton = ObservableBoolean(false)
private var callback: Callback<T>? = null
private var yesCallback: Callback<T>? = null
private var noCallback: Callback<T>? = null
private var data: T? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding.view = this
var dialog: Dialog = AlertDialog.Builder(activity).setView(binding.root).create()
dialog.window.setBackgroundDrawableResource(R.color.transparent)
return dialog
}
private constructor(context: Context, builder: SimpleNotificationDialog.Companion.Builder<T>) : this(context) {
if (builder.preDescription.isNotBlank()) {
preDescription.set(builder.preDescription)
hasPreDescription.set(true)
}
if (builder.title.isNotBlank()) {
title.set(builder.title)
hasTitle.set(true)
}
if (builder.description.isNotBlank()) {
description.set(builder.description)
hasDescription.set(true)
}
if (builder.address.isNotBlank()) {
address.set(builder.address)
hasAddress.set(true)
}
if (builder.primaryButton.isNotBlank()) {
primaryButtonText.set(builder.primaryButton)
hasPrimaryButton.set(true)
}
if (builder.yesButton.isNotBlank()) {
yesButtonText.set(builder.yesButton)
hasYesNoButton.set(true)
}
if (builder.noButton.isNotBlank()) {
noButtonText.set(builder.noButton)
hasYesNoButton.set(true)
}
builder.productImage?.let {
productImage.set(builder.productImage)
hasProductImage.set(true)
}
builder.addressImage?.let {
addressImage.set(builder.addressImage)
hasAddressImage.set(true)
}
builder.productImageUrl?.let {
if (it.isNotBlank()) {
Picasso.with(context)
.load(builder.productImageUrl)
.placeholder(ContextCompat.getDrawable(context, R.drawable.check))
.into(binding.imageViewProduct)
hasProductImage.set(true)
}
} ?: run {
this.dismiss()
}
builder.addressImageUrl?.let {
if (it.isNotBlank()) {
Picasso.with(context)
.load(builder.productImageUrl)
.placeholder(ContextCompat.getDrawable(context, R.drawable.check))
.into(binding.imageViewProduct)
hasProductImage.set(true)
}
} ?: run {
this.dismiss()
}
builder.callback?.let {
callback = builder.callback
}
builder.yesCallback?.let {
yesCallback = builder.yesCallback
}
builder.noCallback?.let {
noCallback = builder.noCallback
}
data = builder.data
}
fun onPrimaryButtonClicked() {
callback?.onResolve(null)
this.dismiss()
}
fun onYesButtonClicked() {
yesCallback?.onResolve(null)
this.dismiss()
}
fun onNoButtonClicked() {
noCallback?.onResolve(null)
this.dismiss()
}
companion object {
class Builder<T : Any> constructor(private val context: Context) {
var preDescription: String = ""
var title: String = ""
var description: String = ""
var address: String = ""
var productImage: Drawable? = null
var addressImage: Drawable? = null
var primaryButton: String = ""
var yesButton: String = ""
var noButton: String = ""
var productImageUrl: String? = ""
var addressImageUrl: String? = ""
var callback: Callback<T>? = null
var yesCallback: Callback<T>? = null
var noCallback: Callback<T>? = null
var data: T? = null
fun preDescription(preDescription: String) = apply { this.preDescription = preDescription }
fun preDescription(@StringRes resId: Int) = apply { this.preDescription = context.getString(resId) }
fun title(title: String) = apply { this.title = title }
fun title(@StringRes resId: Int) = apply { this.title = context.getString(resId) }
fun description(description: String) = apply { this.description = description }
fun description(@StringRes resId: Int) = apply { this.description = context.getString(resId) }
fun address(address: String) = apply { this.address = address }
fun address(@StringRes resId: Int) = apply { this.address = context.getString(resId) }
fun addressImage(image: Drawable) = apply { this.addressImage = image }
fun addressImage(@DrawableRes resId: Int) = apply { this.addressImage = ContextCompat.getDrawable(context, resId) }
fun productImage(image: Drawable) = apply { this.productImage = image }
fun productImageUrl(image: String?) = apply { this.productImageUrl = image }
fun addressImageUrl(image: String?) = apply { this.productImageUrl = image }
fun productImage(@DrawableRes resId: Int) = apply { this.productImage = ContextCompat.getDrawable(context, resId) }
fun primaryButton(primaryButton: String) = apply { this.primaryButton = primaryButton }
fun primaryButton(@StringRes resId: Int) = apply { this.primaryButton = context.getString(resId) }
fun yesButton(yesButton: String) = apply { this.yesButton = yesButton }
fun yesButton(@StringRes resId: Int) = apply { this.yesButton = context.getString(resId) }
fun noButton(noButton: String) = apply { this.noButton = noButton }
fun noButton(@StringRes resId: Int) = apply { this.noButton = context.getString(resId) }
fun callback(callback: Callback<T>) = apply { this.callback = callback }
fun yesCallback(callback: Callback<T>) = apply { this.yesCallback = callback }
fun noCallback(callback: Callback<T>) = apply { this.noCallback = callback }
fun data(data: T) = apply { this.data = data }
fun build() = SimpleNotificationDialog(context, this)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment