diff --git a/MaterialDialogLibrary/build.gradle b/MaterialDialogLibrary/build.gradle index 3742b51..728e078 100644 --- a/MaterialDialogLibrary/build.gradle +++ b/MaterialDialogLibrary/build.gradle @@ -1,15 +1,17 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' apply plugin: 'com.github.dcendents.android-maven' apply plugin: "com.jfrog.bintray" version = "2.1" android { - compileSdkVersion 29 + compileSdkVersion 30 defaultConfig { minSdkVersion 19 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName version @@ -28,19 +30,21 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation "androidx.core:core-ktx:1.3.1" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.annotation:annotation:1.1.0' // Material Design Library - implementation 'com.google.android.material:material:1.0.0' + implementation 'com.google.android.material:material:1.2.1' // Lottie Animation Library implementation 'com.airbnb.android:lottie:3.3.0' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + testImplementation 'junit:junit:4.13' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' } ext { bintrayRepo = 'maven' @@ -159,4 +163,7 @@ bintray { } apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' -apply plugin: 'com.jfrog.bintray' \ No newline at end of file +apply plugin: 'com.jfrog.bintray' +repositories { + mavenCentral() +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/AbstractDialog.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/AbstractDialog.java deleted file mode 100644 index 601e3ba..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/AbstractDialog.java +++ /dev/null @@ -1,330 +0,0 @@ -package com.shreyaspatil.MaterialDialog; - -import android.app.Activity; -import android.app.Dialog; -import android.content.res.ColorStateList; -import android.content.res.Configuration; -import android.content.res.TypedArray; -import android.os.Build; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RawRes; -import androidx.core.content.ContextCompat; - -import com.airbnb.lottie.LottieAnimationView; -import com.google.android.material.button.MaterialButton; -import com.shreyaspatil.MaterialDialog.interfaces.DialogInterface; -import com.shreyaspatil.MaterialDialog.interfaces.OnCancelListener; -import com.shreyaspatil.MaterialDialog.interfaces.OnDismissListener; -import com.shreyaspatil.MaterialDialog.interfaces.OnShowListener; -import com.shreyaspatil.MaterialDialog.model.DialogButton; - -public class AbstractDialog implements DialogInterface { - - //Constants - public static final int BUTTON_POSITIVE = 1; - public static final int BUTTON_NEGATIVE = -1; - public static final int NO_ICON = -111; - public static final int NO_ANIMATION = -111; - - protected Dialog mDialog; - protected Activity mActivity; - protected String title; - protected String message; - protected boolean mCancelable; - protected DialogButton mPositiveButton; - protected DialogButton mNegativeButton; - protected int mAnimationResId; - protected String mAnimationFile; - protected LottieAnimationView mAnimationView; - - protected OnDismissListener mOnDismissListener; - protected OnCancelListener mOnCancelListener; - protected OnShowListener mOnShowListener; - - - protected AbstractDialog(@NonNull Activity mActivity, - @NonNull String title, - @NonNull String message, - boolean mCancelable, - @NonNull DialogButton mPositiveButton, - @NonNull DialogButton mNegativeButton, - @RawRes int mAnimationResId, - @NonNull String mAnimationFile) { - this.mActivity = mActivity; - this.title = title; - this.message = message; - this.mCancelable = mCancelable; - this.mPositiveButton = mPositiveButton; - this.mNegativeButton = mNegativeButton; - this.mAnimationResId = mAnimationResId; - this.mAnimationFile = mAnimationFile; - } - - protected View createView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) { - // Inflate and set the layout for the dialog - // Pass null as the parent view because its going in the dialog layout - final View dialogView = inflater.inflate(R.layout.layout_alert_dialog, container, false); - - // Initialize Views - TextView mTitleView = dialogView.findViewById(R.id.textView_title); - TextView mMessageView = dialogView.findViewById(R.id.textView_message); - MaterialButton mPositiveButtonView = dialogView.findViewById(R.id.button_positive); - MaterialButton mNegativeButtonView = dialogView.findViewById(R.id.button_negative); - mAnimationView = dialogView.findViewById(R.id.animation_view); - - // Set Title - if (title != null) { - mTitleView.setVisibility(View.VISIBLE); - mTitleView.setText(title); - } else { - mTitleView.setVisibility(View.GONE); - } - - // Set Message - if (message != null) { - mMessageView.setVisibility(View.VISIBLE); - mMessageView.setText(message); - } else { - mMessageView.setVisibility(View.GONE); - } - - // Set Positive Button - if (mPositiveButton != null) { - mPositiveButtonView.setVisibility(View.VISIBLE); - mPositiveButtonView.setText(mPositiveButton.getTitle()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mPositiveButton.getIcon() != NO_ICON) { - mPositiveButtonView.setIcon(mActivity.getDrawable(mPositiveButton.getIcon())); - } - - mPositiveButtonView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - mPositiveButton.getOnClickListener().onClick(AbstractDialog.this, BUTTON_POSITIVE); - } - }); - } else { - mPositiveButtonView.setVisibility(View.INVISIBLE); - } - - // Set Negative Button - if (mNegativeButton != null) { - mNegativeButtonView.setVisibility(View.VISIBLE); - mNegativeButtonView.setText(mNegativeButton.getTitle()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mNegativeButton.getIcon() != NO_ICON) { - mNegativeButtonView.setIcon(mActivity.getDrawable(mNegativeButton.getIcon())); - } - - mNegativeButtonView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - mNegativeButton.getOnClickListener().onClick(AbstractDialog.this, BUTTON_NEGATIVE); - } - }); - } else { - mNegativeButtonView.setVisibility(View.INVISIBLE); - } - - // If Orientation is Horizontal, Hide AnimationView - int orientation = mActivity.getResources().getConfiguration().orientation; - if (orientation == Configuration.ORIENTATION_LANDSCAPE) { - mAnimationView.setVisibility(View.GONE); - } else { - // Set Animation from Resource - if (mAnimationResId != NO_ANIMATION) { - mAnimationView.setVisibility(View.VISIBLE); - mAnimationView.setAnimation(mAnimationResId); - mAnimationView.playAnimation(); - - // Set Animation from Assets File - } else if (mAnimationFile != null) { - mAnimationView.setVisibility(View.VISIBLE); - mAnimationView.setAnimation(mAnimationFile); - mAnimationView.playAnimation(); - - } else { - mAnimationView.setVisibility(View.GONE); - } - } - - // Apply Styles - TypedArray a = mActivity.getTheme().obtainStyledAttributes(R.styleable.MaterialDialog); - - try { - // Set Dialog Background - dialogView.setBackgroundColor( - a.getColor(R.styleable.MaterialDialog_material_dialog_background, - mActivity.getResources().getColor(R.color.material_dialog_background))); - - // Set Title Text Color - mTitleView.setTextColor( - a.getColor(R.styleable.MaterialDialog_material_dialog_title_text_color, - mActivity.getResources().getColor(R.color.material_dialog_title_text_color))); - - // Set Message Text Color - mMessageView.setTextColor( - a.getColor(R.styleable.MaterialDialog_material_dialog_message_text_color, - mActivity.getResources().getColor((R.color.material_dialog_message_text_color)))); - - // Set Positive Button Icon Tint - ColorStateList mPositiveButtonTint = a.getColorStateList( - R.styleable.MaterialDialog_material_dialog_positive_button_text_color); - - if (mPositiveButtonTint == null) { - mPositiveButtonTint = ContextCompat.getColorStateList( - mActivity.getApplicationContext(), - R.color.material_dialog_positive_button_text_color); - } - mPositiveButtonView.setTextColor(mPositiveButtonTint); - mPositiveButtonView.setIconTint(mPositiveButtonTint); - - // Set Negative Button Icon & Text Tint - ColorStateList mNegativeButtonTint = a.getColorStateList( - R.styleable.MaterialDialog_material_dialog_negative_button_text_color); - - if (mNegativeButtonTint == null) { - mNegativeButtonTint = ContextCompat.getColorStateList( - mActivity.getApplicationContext(), - R.color.material_dialog_negative_button_text_color); - } - mNegativeButtonView.setIconTint(mNegativeButtonTint); - mNegativeButtonView.setTextColor(mNegativeButtonTint); - - // Set Positive Button Background Tint - ColorStateList mBackgroundTint = a.getColorStateList( - R.styleable.MaterialDialog_material_dialog_positive_button_color); - - if (mBackgroundTint == null) { - mBackgroundTint = ContextCompat.getColorStateList( - mActivity.getApplicationContext(), - R.color.material_dialog_positive_button_color); - } - mPositiveButtonView.setBackgroundTintList(mBackgroundTint); - if (mBackgroundTint != null) { - mNegativeButtonView.setRippleColor(mBackgroundTint.withAlpha(75)); - } - - } catch (Exception e) { - e.printStackTrace(); - } finally { - a.recycle(); - } - - return dialogView; - } - - /** - * Displays the Dialog - */ - public void show() { - if (mDialog != null) { - mDialog.show(); - } else { - throwNullDialog(); - } - } - - /** - * Cancels the Dialog - */ - @Override - public void cancel() { - if (mDialog != null) { - mDialog.cancel(); - } else { - throwNullDialog(); - } - } - - /** - * Dismisses the Dialog - */ - @Override - public void dismiss() { - if (mDialog != null) { - mDialog.dismiss(); - } else { - throwNullDialog(); - } - } - - /** - * @param onShowListener interface for callback events when dialog is showed. - */ - public void setOnShowListener(@NonNull final OnShowListener onShowListener) { - this.mOnShowListener = onShowListener; - - mDialog.setOnShowListener(new android.content.DialogInterface.OnShowListener() { - @Override - public void onShow(android.content.DialogInterface dialogInterface) { - showCallback(); - } - }); - } - - /** - * @param onCancelListener interface for callback events when dialog is cancelled. - */ - public void setOnCancelListener(@NonNull final OnCancelListener onCancelListener) { - this.mOnCancelListener = onCancelListener; - - mDialog.setOnCancelListener(new android.content.DialogInterface.OnCancelListener() { - @Override - public void onCancel(android.content.DialogInterface dialogInterface) { - cancelCallback(); - } - }); - } - - /** - * @param onDismissListener interface for callback events when dialog is dismissed; - */ - public void setOnDismissListener(@NonNull final OnDismissListener onDismissListener) { - this.mOnDismissListener = onDismissListener; - - mDialog.setOnDismissListener(new android.content.DialogInterface.OnDismissListener() { - @Override - public void onDismiss(android.content.DialogInterface dialogInterface) { - dismissCallback(); - } - }); - } - - /** - * @return {@link LottieAnimationView} from the Dialog. - */ - public LottieAnimationView getAnimationView() { - return mAnimationView; - } - - private void showCallback() { - if (mOnShowListener != null) { - mOnShowListener.onShow(this); - } - } - - private void dismissCallback() { - if (mOnDismissListener != null) { - mOnDismissListener.onDismiss(this); - } - } - - private void cancelCallback() { - if (mOnCancelListener != null) { - mOnCancelListener.onCancel(this); - } - } - - private void throwNullDialog() { - throw new NullPointerException("Called method on null Dialog. Create dialog using `Builder` before calling on Dialog"); - } - - public interface OnClickListener { - void onClick(DialogInterface dialogInterface, int which); - } -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/AbstractDialog.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/AbstractDialog.kt new file mode 100644 index 0000000..9c83398 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/AbstractDialog.kt @@ -0,0 +1,254 @@ +package com.shreyaspatil.MaterialDialog + +import android.app.Activity +import android.app.Dialog +import android.content.res.Configuration +import android.os.Build +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.content.ContextCompat +import com.airbnb.lottie.LottieAnimationView +import com.shreyaspatil.MaterialDialog.interfaces.DialogInterface +import com.shreyaspatil.MaterialDialog.interfaces.OnCancelListener +import com.shreyaspatil.MaterialDialog.interfaces.OnDismissListener +import com.shreyaspatil.MaterialDialog.interfaces.OnShowListener +import com.shreyaspatil.MaterialDialog.model.DialogButton +import kotlinx.android.synthetic.main.layout_alert_dialog.view.* + +abstract class AbstractDialog( + private val mActivity: Activity, + private val title: String?, + private val message: String?, + private val mPositiveButton: DialogButton?, + private val mNegativeButton: DialogButton?, + private val mAnimationResId: Int?, + private val mAnimationFile: String? +) : DialogInterface { + + companion object { + //Constants + const val BUTTON_POSITIVE = 1 + const val BUTTON_NEGATIVE = -1 + const val NO_ICON = -111 + const val NO_ANIMATION = -111 + } + + private lateinit var mAnimationView: LottieAnimationView + protected var mDialog: Dialog? = null + + private lateinit var mOnDismissListener: OnDismissListener + private lateinit var mOnCancelListener: OnCancelListener + private lateinit var mOnShowListener: OnShowListener + + protected fun createView(inflater: LayoutInflater, container: ViewGroup?): View { + // Inflate and set the layout for the dialog + // Pass null as the parent view because its going in the dialog layout + val dialogView = inflater.inflate(R.layout.layout_alert_dialog, container, false) + + // Initialize Views + val mTitleView = dialogView.textView_title + val mMessageView = dialogView.textView_message + val mPositiveButtonView = dialogView.button_positive + val mNegativeButtonView = dialogView.button_negative + mAnimationView = dialogView.animation_view + + // Set Title + title?.let { + mTitleView.apply { + visibility = View.VISIBLE + text = it + } + } + + // set message + message?.let { + mMessageView.apply { + visibility = View.VISIBLE + text = it + } + } + + // Set positive button + mPositiveButton?.let { + mPositiveButtonView.apply { + visibility = View.VISIBLE + text = it.title + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && it.icon != NO_ICON) { + icon = ContextCompat.getDrawable(mActivity, it.icon) + } + setOnClickListener { mPositiveButton.onClick(this@AbstractDialog, BUTTON_POSITIVE) } + } + } + + // Set negative button + mNegativeButton?.let { + mNegativeButtonView.apply { + visibility = View.VISIBLE + text = it.title + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && it.icon != NO_ICON) { + icon = ContextCompat.getDrawable(mActivity, it.icon) + } + setOnClickListener { mNegativeButton.onClick(this@AbstractDialog, BUTTON_NEGATIVE) } + } + } + + // If Orientation is Horizontal, Hide AnimationView + val orientation = mActivity.resources.configuration.orientation + mAnimationView.apply { + if (orientation == Configuration.ORIENTATION_LANDSCAPE){ + visibility = View.GONE + } else { + // Set Animation from Resource + when { + mAnimationResId != NO_ANIMATION -> { + visibility = View.VISIBLE + setAnimation(mAnimationResId!!) + playAnimation() + + } + mAnimationFile != null -> { + // Set Animation from Assets File + visibility = View.VISIBLE + setAnimation(mAnimationFile) + playAnimation() + } + else -> visibility = View.GONE + } + } + } + + // Apply Styles + val a = mActivity.theme.obtainStyledAttributes(R.styleable.MaterialDialog) + + try { + // Set Dialog Background + dialogView.setBackgroundColor( + a.getColor(R.styleable.MaterialDialog_material_dialog_background, + ContextCompat.getColor(mActivity, R.color.material_dialog_background))) + + // Set Title Text Color + mTitleView.setTextColor( + a.getColor(R.styleable.MaterialDialog_material_dialog_title_text_color, + ContextCompat.getColor(mActivity, R.color.material_dialog_title_text_color))) + + // Set Message Text Color + mMessageView.setTextColor( + a.getColor(R.styleable.MaterialDialog_material_dialog_message_text_color, + ContextCompat.getColor(mActivity, R.color.material_dialog_message_text_color))) + + // Set Positive Button Icon Tint + var mPositiveButtonTint = a.getColorStateList( + R.styleable.MaterialDialog_material_dialog_positive_button_text_color) + if (mPositiveButtonTint == null) { + mPositiveButtonTint = ContextCompat.getColorStateList( + mActivity.applicationContext, + R.color.material_dialog_positive_button_text_color) + } + mPositiveButtonView.setTextColor(mPositiveButtonTint) + mPositiveButtonView.iconTint = mPositiveButtonTint + + // Set Negative Button Icon & Text Tint + var mNegativeButtonTint = a.getColorStateList( + R.styleable.MaterialDialog_material_dialog_negative_button_text_color) + if (mNegativeButtonTint == null) { + mNegativeButtonTint = ContextCompat.getColorStateList( + mActivity.applicationContext, + R.color.material_dialog_negative_button_text_color) + } + mNegativeButtonView.iconTint = mNegativeButtonTint + mNegativeButtonView.setTextColor(mNegativeButtonTint) + + // Set Positive Button Background Tint + var mBackgroundTint = a.getColorStateList( + R.styleable.MaterialDialog_material_dialog_positive_button_color) + if (mBackgroundTint == null) { + mBackgroundTint = ContextCompat.getColorStateList( + mActivity.applicationContext, + R.color.material_dialog_positive_button_color) + } + mPositiveButtonView.backgroundTintList = mBackgroundTint + if (mBackgroundTint != null) { + mNegativeButtonView.rippleColor = mBackgroundTint.withAlpha(75) + } + } catch (e: Exception) { + e.printStackTrace() + } finally { + a.recycle() + } + + return dialogView + } + + /** + * Displays the Dialog + */ + fun show() { + mDialog?.show() ?: throwNullDialog() + } + + /** + * Cancels the Dialog + */ + override fun cancel() { + mDialog?.cancel() ?: throwNullDialog() + } + + /** + * Dismisses the Dialog + */ + override fun dismiss() { + mDialog?.dismiss() ?: throwNullDialog() + } + + /** + * @param onShowListener interface for callback events when dialog is showed. + */ + fun setOnShowListener(onShowListener: OnShowListener) { + mOnShowListener = onShowListener + mDialog?.setOnShowListener { showCallback() } + } + + /** + * @param onCancelListener interface for callback events when dialog is cancelled. + */ + fun setOnCancelListener(onCancelListener: OnCancelListener) { + mOnCancelListener = onCancelListener + mDialog?.setOnCancelListener { cancelCallback() } + } + + /** + * @param onDismissListener interface for callback events when dialog is dismissed; + */ + fun setOnDismissListener(onDismissListener: OnDismissListener) { + mOnDismissListener = onDismissListener + mDialog?.setOnDismissListener { dismissCallback() } + } + + /** + * @return [LottieAnimationView] from the Dialog. + */ + fun getAnimationView(): LottieAnimationView? { + return mAnimationView + } + + private fun showCallback() { + mOnShowListener.onShow(this) + } + + private fun dismissCallback() { + mOnDismissListener.onDismiss(this) + } + + private fun cancelCallback() { + mOnCancelListener.onCancel(this) + } + + private fun throwNullDialog() { + throw NullPointerException("Called method on null Dialog. Create dialog using `Builder` before calling on Dialog") + } + + interface OnClickListener { + fun onClick(dialogInterface: DialogInterface?, which: Int) + } +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/BottomSheetMaterialDialog.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/BottomSheetMaterialDialog.java deleted file mode 100644 index 0416db1..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/BottomSheetMaterialDialog.java +++ /dev/null @@ -1,224 +0,0 @@ -package com.shreyaspatil.MaterialDialog; - -import android.app.Activity; -import android.content.Context; -import android.content.DialogInterface; -import android.graphics.Outline; -import android.os.Build; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewOutlineProvider; -import android.widget.FrameLayout; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RawRes; - -import com.google.android.material.bottomsheet.BottomSheetBehavior; -import com.shreyaspatil.MaterialDialog.model.DialogButton; - -/** - * Creates BottomSheet Material Dialog with 2 buttons. - *

- * Use {@link BottomSheetMaterialDialog.Builder} to create a new instance. - */ -public class BottomSheetMaterialDialog extends AbstractDialog { - - protected BottomSheetMaterialDialog(@NonNull final Activity mActivity, - @NonNull String title, - @NonNull String message, - boolean mCancelable, - @NonNull DialogButton mPositiveButton, - @NonNull DialogButton mNegativeButton, - @RawRes int mAnimationResId, - @NonNull String mAnimationFile) { - super(mActivity, title, message, mCancelable, mPositiveButton, mNegativeButton, mAnimationResId, mAnimationFile); - - // Init Dialog, Create Bottom Sheet Dialog - mDialog = new BottomSheetDialog(mActivity); - - LayoutInflater inflater = mActivity.getLayoutInflater(); - - View dialogView = createView(inflater, null); - mDialog.setContentView(dialogView); - - // Set Cancelable property - mDialog.setCancelable(mCancelable); - - // Clip AnimationView to round Corners - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - dialogView.setOutlineProvider(new ViewOutlineProvider() { - @Override - public void getOutline(View view, Outline outline) { - float radius = mActivity.getResources().getDimension(R.dimen.radiusTop); - outline.setRoundRect(0, 0, view.getWidth(), view.getHeight() + (int) radius, radius); - } - }); - dialogView.setClipToOutline(true); - } else { - dialogView.findViewById(R.id.relative_layout_dialog).setPadding(0, (int) mActivity.getResources().getDimension(R.dimen.paddingTop), 0, 0); - } - - // Expand Bottom Sheet after showing. - mDialog.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialog) { - BottomSheetDialog d = (BottomSheetDialog) dialog; - - FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet); - - if (bottomSheet != null) { - BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED); - } - } - }); - } - - @Override - protected View createView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) { - return super.createView(inflater, container); - } - - /** - * Builder for {@link BottomSheetMaterialDialog}. - */ - public static class Builder { - private Activity activity; - private String title; - private String message; - private boolean isCancelable; - private DialogButton positiveButton; - private DialogButton negativeButton; - private int animationResId = NO_ANIMATION; - private String animationFile; - - /** - * @param activity where BottomSheet Material Dialog is to be built. - */ - public Builder(@NonNull Activity activity) { - this.activity = activity; - } - - /** - * @param title Sets the Title of BottomSheet Material Dialog. - * @return this, for chaining. - */ - @NonNull - public Builder setTitle(@NonNull String title) { - this.title = title; - return this; - } - - /** - * @param message Sets the Message of BottomSheet Material Dialog. - * @return this, for chaining. - */ - @NonNull - public Builder setMessage(@NonNull String message) { - this.message = message; - return this; - } - - /** - * @param isCancelable Sets cancelable property of BottomSheet Material Dialog. - * @return this, for chaining. - */ - @NonNull - public Builder setCancelable(boolean isCancelable) { - this.isCancelable = isCancelable; - return this; - } - - /** - * Sets the Positive Button to BottomSheet Material Dialog without icon - * - * @param name sets the name/label of button. - * @param onClickListener interface for callback event on click of button. - * @return this, for chaining. - */ - @NonNull - public Builder setPositiveButton(@NonNull String name, @NonNull OnClickListener onClickListener) { - return setPositiveButton(name, NO_ICON, onClickListener); - } - - /** - * Sets the Positive Button to BottomSheet Material Dialog with icon - * - * @param name sets the name/label of button. - * @param icon sets the resource icon for button. - * @param onClickListener interface for callback event on click of button. - * @return this, for chaining. - */ - @NonNull - public Builder setPositiveButton(@NonNull String name, int icon, @NonNull OnClickListener onClickListener) { - positiveButton = new DialogButton(name, icon, onClickListener); - return this; - } - - /** - * Sets the Negative Button to BottomSheet Material Dialog without icon. - * - * @param name sets the name/label of button. - * @param onClickListener interface for callback event on click of button. - * @see this, for chaining. - */ - @NonNull - public Builder setNegativeButton(@NonNull String name, @NonNull OnClickListener onClickListener) { - return setNegativeButton(name, NO_ICON, onClickListener); - } - - /** - * Sets the Negative Button to BottomSheet Material Dialog with icon - * - * @param name sets the name/label of button. - * @param icon sets the resource icon for button. - * @param onClickListener interface for callback event on click of button. - * @return this, for chaining. - */ - @NonNull - public Builder setNegativeButton(@NonNull String name, int icon, @NonNull OnClickListener onClickListener) { - negativeButton = new DialogButton(name, icon, onClickListener); - return this; - } - - /** - * It sets the resource json to the {@link com.airbnb.lottie.LottieAnimationView}. - * - * @param animationResId sets the resource to {@link com.airbnb.lottie.LottieAnimationView}. - * @return this, for chaining. - */ - @NonNull - public Builder setAnimation(@RawRes int animationResId) { - this.animationResId = animationResId; - return this; - } - - /** - * It sets the json file to the {@link com.airbnb.lottie.LottieAnimationView} from assets. - * - * @param fileName sets the file from assets to {@link com.airbnb.lottie.LottieAnimationView}. - * @return this, for chaining. - */ - @NonNull - public Builder setAnimation(@NonNull String fileName) { - this.animationFile = fileName; - return this; - } - - /** - * Build the {@link BottomSheetMaterialDialog}. - */ - @NonNull - public BottomSheetMaterialDialog build() { - return new BottomSheetMaterialDialog(activity, title, message, isCancelable, positiveButton, negativeButton, animationResId, animationFile); - } - } - - class BottomSheetDialog extends com.google.android.material.bottomsheet.BottomSheetDialog { - - BottomSheetDialog(@NonNull Context context) { - super(context, R.style.BottomSheetDialogTheme); - } - } -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/BottomSheetMaterialDialog.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/BottomSheetMaterialDialog.kt new file mode 100644 index 0000000..56ccb09 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/BottomSheetMaterialDialog.kt @@ -0,0 +1,83 @@ +package com.shreyaspatil.MaterialDialog + +import android.app.Activity +import android.graphics.Outline +import android.os.Build +import android.view.View +import android.view.ViewOutlineProvider +import android.widget.FrameLayout +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetDialog +import com.shreyaspatil.MaterialDialog.model.DialogBuilder +import com.shreyaspatil.MaterialDialog.model.DialogButton +import kotlinx.android.synthetic.main.layout_alert_dialog.view.* + +/** + * Creates BottomSheet Material Dialog with 2 buttons. + *

+ * Use {@link BottomSheetMaterialDialog.Builder} to create a new instance. + */ +class BottomSheetMaterialDialog( + mActivity: Activity, + title: String?, + message: String?, + mCancelable: Boolean?, + mPositiveButton: DialogButton?, + mNegativeButton: DialogButton?, + mAnimationResId: Int?, + mAnimationFile: String? +) : AbstractDialog(mActivity, title, message, mPositiveButton, mNegativeButton, mAnimationResId, mAnimationFile) { + + init { + + val dialogView = createView(mActivity.layoutInflater, null) + .apply { + // Clip AnimationView to round Corners + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + outlineProvider = object : ViewOutlineProvider() { + override fun getOutline(view: View?, outline: Outline?) { + val radius = mActivity.resources.getDimension(R.dimen.radiusTop) + outline!!.setRoundRect(0, 0, view!!.width, view.height + radius.toInt(), radius) + } + } + clipToOutline = true + } else { + relative_layout_dialog.setPadding( + 0, + mActivity.resources.getDimension(R.dimen.paddingTop).toInt(), + 0, + 0 + ) + } + } + + // Init Dialog, Create Bottom Sheet Dialog + mDialog = BottomSheetDialog(mActivity, R.style.BottomSheetDialogTheme) + .apply { + setContentView(dialogView) + setCancelable(mCancelable!!) + + setOnShowListener { + val d = it as BottomSheetDialog + val bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet) + + if (bottomSheet != null) { + BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED + } + } + } + } + + /** + * Builder for {@link BottomSheetMaterialDialog}. + * @param activity where BottomSheet Material Dialog is to be built. + */ + class Builder(private val activity: Activity) : DialogBuilder() { + + /** + * Build the {@link BottomSheetMaterialDialog}. + */ + fun build(): BottomSheetMaterialDialog = + BottomSheetMaterialDialog(activity, title, message, isCancelable, mPositiveButton, mNegativeButton, animationResId, mAnimationFile) + } +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/DslBuilder.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/DslBuilder.kt new file mode 100644 index 0000000..0c04735 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/DslBuilder.kt @@ -0,0 +1,31 @@ +package com.shreyaspatil.MaterialDialog + +import android.app.Activity + +/** + * Creates a Material Dialog with 2 buttons. + *

+ * Use {@link Builder} to create a new instance. + *

+ * @param activity where Material Dialog is to be built. + * @param block a lambda function for configuration + */ +fun materialDialog( + activity: Activity, + builder: MaterialDialog.Builder.() -> Unit +) : MaterialDialog = MaterialDialog.Builder(activity) + .apply(builder).build() + +/** + * Creates BottomSheet Material Dialog with 2 buttons. + *

+ * Use {@link BottomSheetMaterialDialog.Builder} to create a new instance. + *

+ * @param activity where Material Dialog is to be built. + * @param block a lambda function for configuration + */ +fun bottomSheetMaterialDialog( + activity: Activity, + builder: BottomSheetMaterialDialog.Builder.() -> Unit +) : BottomSheetMaterialDialog = BottomSheetMaterialDialog.Builder(activity) + .apply(builder).build() diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/MaterialDialog.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/MaterialDialog.java deleted file mode 100644 index 453a872..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/MaterialDialog.java +++ /dev/null @@ -1,169 +0,0 @@ -package com.shreyaspatil.MaterialDialog; - -import android.app.Activity; -import android.view.LayoutInflater; -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.annotation.RawRes; -import androidx.appcompat.app.AlertDialog; - -import com.shreyaspatil.MaterialDialog.model.DialogButton; - -/** - * Creates a Material Dialog with 2 buttons. - *

- * Use {@link Builder} to create a new instance. - */ -public class MaterialDialog extends AbstractDialog { - - - protected MaterialDialog(@NonNull final Activity mActivity, - @NonNull String title, - @NonNull String message, - boolean mCancelable, - @NonNull DialogButton mPositiveButton, - @NonNull DialogButton mNegativeButton, - @RawRes int mAnimationResId, - @NonNull String mAnimationFile) { - super(mActivity, title, message, mCancelable, mPositiveButton, mNegativeButton, mAnimationResId, mAnimationFile); - - // Init Dialog - final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); - - LayoutInflater inflater = mActivity.getLayoutInflater(); - - View dialogView = createView(inflater, null); - - builder.setView(dialogView); - - // Set Cancelable property - builder.setCancelable(mCancelable); - - // Create and show dialog - mDialog = builder.create(); - } - - /** - * Builder for {@link MaterialDialog}. - */ - public static class Builder { - private Activity activity; - private String title; - private String message; - private boolean isCancelable; - private DialogButton positiveButton; - private DialogButton negativeButton; - private int animationResId = NO_ANIMATION; - private String animationFile; - - /** - * @param activity where Material Dialog is to be built. - */ - public Builder(@NonNull Activity activity) { - this.activity = activity; - } - - /** - * @param title Sets the Title of Material Dialog. - * @return this, for chaining. - */ - @NonNull - public Builder setTitle(@NonNull String title) { - this.title = title; - return this; - } - - /** - * @param message Sets the Message of Material Dialog. - * @return this, for chaining. - */ - @NonNull - public Builder setMessage(@NonNull String message) { - this.message = message; - return this; - } - - /** - * @param isCancelable Sets cancelable property of Material Dialog. - * @return this, for chaining. - */ - @NonNull - public Builder setCancelable(boolean isCancelable) { - this.isCancelable = isCancelable; - return this; - } - - /** Sets the Positive Button to Material Dialog without icon - * @param name sets the name/label of button. - * @param onClickListener interface for callback event on click of button. - * @see this, for chaining. - */ - @NonNull - public Builder setPositiveButton(@NonNull String name, @NonNull OnClickListener onClickListener) { - return setPositiveButton(name, NO_ICON, onClickListener); - } - - /** Sets the Positive Button to Material Dialog with icon - * @param name sets the name/label of button. - * @param icon sets the resource icon for button. - * @param onClickListener interface for callback event on click of button. - * @return this, for chaining. - */ - @NonNull - public Builder setPositiveButton(@NonNull String name, int icon, @NonNull OnClickListener onClickListener) { - positiveButton = new DialogButton(name, icon, onClickListener); - return this; - } - - /** Sets the Negative Button to Material Dialog without icon. - * @param name sets the name/label of button. - * @param onClickListener interface for callback event on click of button. - * @see this, for chaining. - */ - @NonNull - public Builder setNegativeButton(@NonNull String name, @NonNull OnClickListener onClickListener) { - return setNegativeButton(name, NO_ICON, onClickListener); - } - - /** Sets the Negative Button to Material Dialog with icon - * @param name sets the name/label of button. - * @param icon sets the resource icon for button. - * @param onClickListener interface for callback event on click of button. - * @return this, for chaining. - */ - @NonNull - public Builder setNegativeButton(@NonNull String name, int icon, @NonNull OnClickListener onClickListener) { - negativeButton = new DialogButton(name, icon, onClickListener); - return this; - } - - /** It sets the resource json to the {@link com.airbnb.lottie.LottieAnimationView}. - * @param animationResId sets the resource to {@link com.airbnb.lottie.LottieAnimationView}. - * @return this, for chaining. - */ - @NonNull - public Builder setAnimation(@RawRes int animationResId) { - this.animationResId = animationResId; - return this; - } - - /** It sets the json file to the {@link com.airbnb.lottie.LottieAnimationView} from assets. - * @param fileName sets the file from assets to {@link com.airbnb.lottie.LottieAnimationView}. - * @return this, for chaining. - */ - @NonNull - public Builder setAnimation(@NonNull String fileName) { - this.animationFile = fileName; - return this; - } - - /** - * Build the {@link MaterialDialog}. - */ - @NonNull - public MaterialDialog build() { - return new MaterialDialog(activity, title, message, isCancelable, positiveButton, negativeButton, animationResId, animationFile); - } - } -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/MaterialDialog.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/MaterialDialog.kt new file mode 100644 index 0000000..fb83ad1 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/MaterialDialog.kt @@ -0,0 +1,49 @@ +package com.shreyaspatil.MaterialDialog + +import android.app.Activity +import androidx.appcompat.app.AlertDialog +import com.shreyaspatil.MaterialDialog.model.DialogBuilder +import com.shreyaspatil.MaterialDialog.model.DialogButton + +/** + * Creates a Material Dialog with 2 buttons. + *

+ * Use {@link Builder} to create a new instance. + */ +class MaterialDialog( + mActivity: Activity, + title: String?, + message: String?, + mCancelable: Boolean?, + mPositiveButton: DialogButton?, + mNegativeButton: DialogButton?, + mAnimationResId: Int?, + mAnimationFile: String? +) : AbstractDialog(mActivity, title, message, mPositiveButton, mNegativeButton, mAnimationResId, mAnimationFile) +{ + + init { + + val dialogView = createView(mActivity.layoutInflater, null) + + //init dialog + mDialog = AlertDialog.Builder(mActivity).run { + setView(dialogView) + setCancelable(mCancelable!!) + create() + } + } + + /** + * Builder for {@link MaterialDialog}. + * @param activity where Material Dialog is to be built. + */ + class Builder(private val activity: Activity) : DialogBuilder(){ + + /** + * Build the {@link MaterialDialog}. + */ + fun build() : MaterialDialog = + MaterialDialog(activity, title, message, isCancelable, mPositiveButton, mNegativeButton, animationResId, mAnimationFile) + } +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/DialogInterface.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/DialogInterface.java deleted file mode 100644 index 7d9d20d..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/DialogInterface.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.shreyaspatil.MaterialDialog.interfaces; - -public interface DialogInterface { - void cancel(); - void dismiss(); -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/DialogInterface.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/DialogInterface.kt new file mode 100644 index 0000000..8f57c02 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/DialogInterface.kt @@ -0,0 +1,6 @@ +package com.shreyaspatil.MaterialDialog.interfaces + +interface DialogInterface { + fun cancel() + fun dismiss() +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnCancelListener.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnCancelListener.java deleted file mode 100644 index ffdc6c6..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnCancelListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.shreyaspatil.MaterialDialog.interfaces; - -public interface OnCancelListener { - void onCancel(DialogInterface dialogInterface); -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnCancelListener.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnCancelListener.kt new file mode 100644 index 0000000..7a69ce0 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnCancelListener.kt @@ -0,0 +1,5 @@ +package com.shreyaspatil.MaterialDialog.interfaces + +interface OnCancelListener { + fun onCancel(dialogInterface: DialogInterface) +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnDismissListener.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnDismissListener.java deleted file mode 100644 index 6c71309..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnDismissListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.shreyaspatil.MaterialDialog.interfaces; - -public interface OnDismissListener { - void onDismiss(DialogInterface dialogInterface); -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnDismissListener.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnDismissListener.kt new file mode 100644 index 0000000..04dcabb --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnDismissListener.kt @@ -0,0 +1,5 @@ +package com.shreyaspatil.MaterialDialog.interfaces + +interface OnDismissListener { + fun onDismiss(dialogInterface: DialogInterface) +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnShowListener.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnShowListener.java deleted file mode 100644 index 6ffeba6..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnShowListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.shreyaspatil.MaterialDialog.interfaces; - -public interface OnShowListener { - void onShow(DialogInterface dialogInterface); -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnShowListener.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnShowListener.kt new file mode 100644 index 0000000..8ce2fa3 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/interfaces/OnShowListener.kt @@ -0,0 +1,5 @@ +package com.shreyaspatil.MaterialDialog.interfaces + +interface OnShowListener { + fun onShow(dialogInterface: DialogInterface) +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogBuilder.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogBuilder.kt new file mode 100644 index 0000000..fd853a3 --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogBuilder.kt @@ -0,0 +1,65 @@ +package com.shreyaspatil.MaterialDialog.model + +import com.shreyaspatil.MaterialDialog.AbstractDialog +import com.shreyaspatil.MaterialDialog.interfaces.DialogInterface + +/** + * Builder for Dialog. + */ +open class DialogBuilder() { + + var title: String? = null + var message: String? = null + var isCancelable: Boolean? = true + protected var mPositiveButton: DialogButton? = null + protected var mNegativeButton: DialogButton? = null + protected var animationResId: Int? = AbstractDialog.NO_ANIMATION + protected var mAnimationFile: String? = null + + /** Sets the Positive Button to Dialog without icon + * @param name sets the name/label of button. + * @param onClick higher order function for callback event on click of button. + */ + fun setPositiveButton(name: String, icon: Int, onClick: (DialogInterface, Int) -> Unit) { + mPositiveButton = DialogButton(name, icon, onClick) + } + + /** Sets the Positive Button to Dialog without icon + * @param name sets the name/label of button. + * @param onClick higher order function for callback event on click of button. + */ + fun setPositiveButton(name: String, onClick: (DialogInterface, Int) -> Unit) { + mPositiveButton = DialogButton(name, AbstractDialog.NO_ICON, onClick) + } + + /** Sets the Positive Button to Dialog with icon + * @param name sets the name/label of button. + * @param icon sets the resource icon for button. + * @param onClick higher order function for callback event on click of button. + */ + fun setNegativeButton(name: String, icon: Int, onClick: (DialogInterface, Int) -> Unit) { + mNegativeButton = DialogButton(name, icon, onClick) + } + + /** Sets the Positive Button to Dialog without icon + * @param name sets the name/label of button. + * @param onClick higher order function for callback event on click of button. + */ + fun setNegativeButton(name: String, onClick: (DialogInterface, Int) -> Unit) { + mNegativeButton = DialogButton(name, AbstractDialog.NO_ICON, onClick) + } + + /** It sets the resource json to the {@link com.airbnb.lottie.LottieAnimationView}. + * @param animationResId sets the resource to {@link com.airbnb.lottie.LottieAnimationView}. + */ + fun setAnimation(animationResId: Int) { + this.animationResId = animationResId + } + + /** It sets the json file to the {@link com.airbnb.lottie.LottieAnimationView} from assets. + * @param fileName sets the file from assets to {@link com.airbnb.lottie.LottieAnimationView}. + */ + fun setAnimation(fileName: String) { + this.mAnimationFile = fileName + } +} \ No newline at end of file diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogButton.java b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogButton.java deleted file mode 100644 index 6837deb..0000000 --- a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogButton.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.shreyaspatil.MaterialDialog.model; - -import com.shreyaspatil.MaterialDialog.AbstractDialog; - -public class DialogButton { - private String title; - private int icon; - private AbstractDialog.OnClickListener onClickListener; - - public DialogButton(String title, int icon, AbstractDialog.OnClickListener onClickListener) { - this.title = title; - this.icon = icon; - this.onClickListener = onClickListener; - } - - public String getTitle() { - return title; - } - - public int getIcon() { - return icon; - } - - public AbstractDialog.OnClickListener getOnClickListener() { - return onClickListener; - } -} diff --git a/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogButton.kt b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogButton.kt new file mode 100644 index 0000000..ceb28fa --- /dev/null +++ b/MaterialDialogLibrary/src/main/java/com/shreyaspatil/MaterialDialog/model/DialogButton.kt @@ -0,0 +1,10 @@ +package com.shreyaspatil.MaterialDialog.model + +import com.shreyaspatil.MaterialDialog.interfaces.DialogInterface + +data class DialogButton( + val title: String, + val icon: Int, + val onClick: (DialogInterface, Int) -> Unit +) + diff --git a/MaterialDialogLibrary/src/main/res/layout/layout_alert_dialog.xml b/MaterialDialogLibrary/src/main/res/layout/layout_alert_dialog.xml index cf5b30a..1b88e61 100644 --- a/MaterialDialogLibrary/src/main/res/layout/layout_alert_dialog.xml +++ b/MaterialDialogLibrary/src/main/res/layout/layout_alert_dialog.xml @@ -32,15 +32,19 @@ android:layout_below="@id/animation_view" android:layout_width="match_parent" android:layout_height="wrap_content" + android:visibility="gone" style="@style/MaterialDialog.Title" - tools:text="Title" /> + tools:text="Title" + tools:visibility="visible"/> + tools:text="Positive" + tools:visibility="visible"/> diff --git a/app/build.gradle b/app/build.gradle index f0827cc..7adadd7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,13 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 29 + compileSdkVersion 30 defaultConfig { applicationId "com.shreyaspatil.MaterialDialogExample" minSdkVersion 19 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -20,20 +22,25 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation "androidx.core:core-ktx:1.3.1" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.1' // Material Dialog Library - implementation 'com.shreyaspatil:MaterialDialog:2.1' + //implementation 'com.shreyaspatil:MaterialDialog:2.1' // Material Design Library - implementation 'com.google.android.material:material:1.0.0' + implementation 'com.google.android.material:material:1.2.1' // Lottie Animation Library implementation 'com.airbnb.android:lottie:3.3.0' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - //implementation project(path: ':MaterialDialogLibrary') + testImplementation 'junit:junit:4.13' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + implementation project(path: ':MaterialDialogLibrary') +} +repositories { + mavenCentral() } diff --git a/app/src/main/java/com/shreyaspatil/MaterialDialogExample/MainActivity.java b/app/src/main/java/com/shreyaspatil/MaterialDialogExample/MainActivity.java deleted file mode 100644 index 9184b15..0000000 --- a/app/src/main/java/com/shreyaspatil/MaterialDialogExample/MainActivity.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.shreyaspatil.MaterialDialogExample; - -import android.os.Bundle; -import android.view.View; -import android.widget.Button; -import android.widget.Toast; - -import androidx.appcompat.app.AppCompatActivity; - -import com.shreyaspatil.MaterialDialog.BottomSheetMaterialDialog; -import com.shreyaspatil.MaterialDialog.MaterialDialog; -import com.shreyaspatil.MaterialDialog.interfaces.DialogInterface; -import com.shreyaspatil.MaterialDialog.interfaces.OnCancelListener; -import com.shreyaspatil.MaterialDialog.interfaces.OnDismissListener; -import com.shreyaspatil.MaterialDialog.interfaces.OnShowListener; - -public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnShowListener, OnCancelListener, OnDismissListener { - - private MaterialDialog mSimpleDialog; - private MaterialDialog mAnimatedDialog; - private BottomSheetMaterialDialog mSimpleBottomSheetDialog; - private BottomSheetMaterialDialog mAnimatedBottomSheetDialog; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - Button mButtonSimpleDialog = findViewById(R.id.button_simple_dialog); - Button mButtonAnimatedDialog = findViewById(R.id.button_animated_dialog); - Button mButtonBottomSheetDialog = findViewById(R.id.button_simple_bottomsheet_dialog); - Button mButtonAnimatedBottomSheetDialog = findViewById(R.id.button_animated_bottomsheet_dialog); - - // Simple Material Dialog - mSimpleDialog = new MaterialDialog.Builder(this) - .setTitle("Delete?") - .setMessage("Are you sure want to delete this file?") - .setCancelable(false) - .setPositiveButton("Delete", R.drawable.ic_delete, new MaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .setNegativeButton("Cancel", R.drawable.ic_close, new MaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int which) { - Toast.makeText(getApplicationContext(), "Cancelled!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .build(); - - // Simple BottomSheet Material Dialog - mSimpleBottomSheetDialog = new BottomSheetMaterialDialog.Builder(this) - .setTitle("Delete?") - .setMessage("Are you sure want to delete this file?") - .setCancelable(false) - .setPositiveButton("Delete", R.drawable.ic_delete, new BottomSheetMaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .setNegativeButton("Cancel", R.drawable.ic_close, new BottomSheetMaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int which) { - Toast.makeText(getApplicationContext(), "Cancelled!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .build(); - - // Animated Simple Material Dialog - mAnimatedDialog = new MaterialDialog.Builder(this) - .setTitle("Delete?") - .setMessage("Are you sure want to delete this file?") - .setCancelable(false) - .setPositiveButton("Delete", R.drawable.ic_delete, new MaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .setNegativeButton("Cancel", R.drawable.ic_close, new MaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int which) { - Toast.makeText(getApplicationContext(), "Cancelled!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .setAnimation("delete_anim.json") - .build(); - - // Animated BottomSheet Material Dialog - mAnimatedBottomSheetDialog = new BottomSheetMaterialDialog.Builder(this) - .setTitle("Delete?") - .setMessage("Are you sure want to delete this file?") - .setCancelable(false) - .setPositiveButton("Delete", R.drawable.ic_delete, new BottomSheetMaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - Toast.makeText(getApplicationContext(), "Deleted!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .setNegativeButton("Cancel", R.drawable.ic_close, new BottomSheetMaterialDialog.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int which) { - Toast.makeText(getApplicationContext(), "Cancelled!", Toast.LENGTH_SHORT).show(); - dialogInterface.dismiss(); - } - }) - .setAnimation("delete_anim.json") - .build(); - - mButtonSimpleDialog.setOnClickListener(this); - mButtonBottomSheetDialog.setOnClickListener(this); - mButtonAnimatedDialog.setOnClickListener(this); - mButtonAnimatedBottomSheetDialog.setOnClickListener(this); - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.button_simple_dialog : - mSimpleDialog.show(); - break; - - case R.id.button_simple_bottomsheet_dialog : - mSimpleBottomSheetDialog.show(); - break; - - case R.id.button_animated_dialog : - mAnimatedDialog.show(); - break; - - case R.id.button_animated_bottomsheet_dialog : - mAnimatedBottomSheetDialog.show(); - break; - } - } - - @Override - public void onShow(DialogInterface dialogInterface) { - - } - - @Override - public void onCancel(DialogInterface dialogInterface) { - - } - - @Override - public void onDismiss(DialogInterface dialogInterface) { - - } -} diff --git a/app/src/main/java/com/shreyaspatil/MaterialDialogExample/MainActivity.kt b/app/src/main/java/com/shreyaspatil/MaterialDialogExample/MainActivity.kt new file mode 100644 index 0000000..00998bf --- /dev/null +++ b/app/src/main/java/com/shreyaspatil/MaterialDialogExample/MainActivity.kt @@ -0,0 +1,113 @@ +package com.shreyaspatil.MaterialDialogExample + +import android.os.Bundle +import android.view.View +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.shreyaspatil.MaterialDialog.BottomSheetMaterialDialog +import com.shreyaspatil.MaterialDialog.MaterialDialog +import com.shreyaspatil.MaterialDialog.bottomSheetMaterialDialog +import com.shreyaspatil.MaterialDialog.interfaces.DialogInterface +import com.shreyaspatil.MaterialDialog.interfaces.OnCancelListener +import com.shreyaspatil.MaterialDialog.interfaces.OnDismissListener +import com.shreyaspatil.MaterialDialog.interfaces.OnShowListener +import com.shreyaspatil.MaterialDialog.materialDialog +import kotlinx.android.synthetic.main.activity_main.* + +class MainActivity : AppCompatActivity(), View.OnClickListener, OnShowListener, OnCancelListener, OnDismissListener { + + private lateinit var mSimpleDialog: MaterialDialog + private lateinit var mAnimatedDialog: MaterialDialog + private lateinit var mSimpleBottomSheetDialog: BottomSheetMaterialDialog + private lateinit var mAnimatedBottomSheetDialog: BottomSheetMaterialDialog + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + mSimpleDialog = materialDialog(this) { + title = "Delete" + message = "Are you sure you want to delete this file?" + isCancelable = false + setPositiveButton("Delete", R.drawable.ic_delete) { dialog, which -> + Toast.makeText(applicationContext, "Deleted!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + setNegativeButton("Cancel", R.drawable.ic_close) { dialog, which -> + Toast.makeText(applicationContext, "Cancelled!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + } + + mSimpleBottomSheetDialog = bottomSheetMaterialDialog(this) { + title = "Delete" + message = "Are you sure you want to delete this file?" + isCancelable = false + setPositiveButton("Delete", R.drawable.ic_delete) { dialog, which -> + Toast.makeText(applicationContext, "Deleted!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + setNegativeButton("Cancel", R.drawable.ic_close) { dialog, which -> + Toast.makeText(applicationContext, "Cancelled!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + } + + mAnimatedDialog = materialDialog(this) { + title = "Delete" + message = "Are you sure you want to delete this file?" + isCancelable = false + setPositiveButton("Delete", R.drawable.ic_delete) { dialog, which -> + Toast.makeText(applicationContext, "Deleted!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + setNegativeButton("Cancel", R.drawable.ic_close) { dialog, which -> + Toast.makeText(applicationContext, "Cancelled!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + setAnimation("delete_anim.json") + } + + mAnimatedBottomSheetDialog = bottomSheetMaterialDialog(this) { + title = "Delete" + message = "Are you sure you want to delete this file?" + isCancelable = false + setPositiveButton("Delete", R.drawable.ic_delete) { dialog, which -> + Toast.makeText(applicationContext, "Deleted!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + setNegativeButton("Cancel", R.drawable.ic_close) { dialog, which -> + Toast.makeText(applicationContext, "Cancelled!", Toast.LENGTH_SHORT).show() + dialog.dismiss() + } + setAnimation("delete_anim.json") + } + + + button_simple_dialog.setOnClickListener(this) + button_simple_bottomsheet_dialog.setOnClickListener(this) + button_animated_dialog.setOnClickListener(this) + button_animated_bottomsheet_dialog.setOnClickListener(this) + } + + override fun onClick(v: View?) { + when (v!!.id) { + R.id.button_simple_dialog -> mSimpleDialog.show() + R.id.button_simple_bottomsheet_dialog -> mSimpleBottomSheetDialog.show() + R.id.button_animated_dialog -> mAnimatedDialog.show() + R.id.button_animated_bottomsheet_dialog -> mAnimatedBottomSheetDialog.show() + } + } + + override fun onShow(dialogInterface: DialogInterface) { + + } + + override fun onCancel(dialogInterface: DialogInterface) { + + } + + override fun onDismiss(dialogInterface: DialogInterface) { + + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 87c06c0..8808e21 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,16 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.4.10' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' - + classpath 'com.android.tools.build:gradle:4.0.1' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2124f08..228c789 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip