From c7c57b9730c5cf0f12ca14212a575929b00b3d2d Mon Sep 17 00:00:00 2001 From: goksuturker Date: Mon, 24 Jan 2022 11:29:11 +0300 Subject: [PATCH] Add custom dialog support --- buildSrc/src/main/kotlin/ComponentVersions.kt | 2 +- .../trendyol/uicomponents/dialogs/Builder.kt | 24 ++++++++ .../uicomponents/dialogs/BuilderExtensions.kt | 3 + .../uicomponents/dialogs/DialogFragment.kt | 16 +++-- .../dialogs/DialogFragmentArguments.kt | 4 +- .../uicomponents/dialogs/DialogViewState.kt | 5 +- .../src/main/res/layout/fragment_dialog.xml | 52 +++++++---------- .../main/res/layout/layout_dialog_header.xml | 29 ++++++++++ .../trendyol/uicomponents/DialogsActivity.kt | 15 +++++ .../uicomponents/ExampleCustomView.kt | 24 ++++++++ .../src/main/res/layout/activity_dialogs.xml | 10 ++++ .../main/res/layout/view_custom_example.xml | 58 +++++++++++++++++++ 12 files changed, 203 insertions(+), 39 deletions(-) create mode 100644 libraries/dialogs/src/main/res/layout/layout_dialog_header.xml create mode 100644 sample/src/main/java/com/trendyol/uicomponents/ExampleCustomView.kt create mode 100644 sample/src/main/res/layout/view_custom_example.xml diff --git a/buildSrc/src/main/kotlin/ComponentVersions.kt b/buildSrc/src/main/kotlin/ComponentVersions.kt index 71927a7c..6f2a829c 100644 --- a/buildSrc/src/main/kotlin/ComponentVersions.kt +++ b/buildSrc/src/main/kotlin/ComponentVersions.kt @@ -5,7 +5,7 @@ object ComponentVersions { const val ratingBarVersion = "1.0.2" const val imageSliderVersion = "1.0.8" const val phoneNumberVersion = "1.0.2" - const val dialogsVersion = "1.2.8" + const val dialogsVersion = "1.2.9" const val cardInputViewVersion = "1.1.3" const val quantityPickerViewVersion = "1.2.5" const val timelineViewVersion = "1.0.0" diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/Builder.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/Builder.kt index 350699ee..a6efe99c 100644 --- a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/Builder.kt +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/Builder.kt @@ -1,6 +1,7 @@ package com.trendyol.uicomponents.dialogs import android.text.SpannableString +import android.view.View import android.webkit.WebView import androidx.annotation.DrawableRes @@ -147,3 +148,26 @@ class InfoListDialogBuilder internal constructor() : InfoDialogBuilder() { } } + +class CustomDialogBuilder internal constructor() : InfoDialogBuilder() { + + var view: View? = null + + internal fun buildCustomDialog(block: CustomDialogBuilder.() -> Unit): DialogFragment { + return CustomDialogBuilder().apply(block).let { + DialogFragment().apply { + arguments = DialogFragmentArguments( + title = it.title, + showCloseButton = it.showCloseButton, + animateCornerRadiusWhenExpand = animateCornerRadiusWhenExpand, + cornerRadius = it.cornerRadius, + titleBackgroundColor = it.titleBackgroundColor, + titleTextColor = it.titleTextColor, + titleTextPosition = it.titleTextPosition, + view = { it.view } + ).toBundle() + this.closeButtonListener = it.closeButtonListener + } + } + } +} diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/BuilderExtensions.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/BuilderExtensions.kt index 4e95ea2d..6b09da82 100644 --- a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/BuilderExtensions.kt +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/BuilderExtensions.kt @@ -11,3 +11,6 @@ fun agreementDialog(block: AgreementDialogBuilder.() -> Unit): DialogFragment = fun selectionDialog(block: SelectionDialogBuilder.() -> Unit): DialogFragment = SelectionDialogBuilder().buildSelectionDialog(block) + +fun customDialog(block: CustomDialogBuilder.() -> Unit): DialogFragment = + CustomDialogBuilder().buildCustomDialog(block) diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragment.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragment.kt index a8d064ae..b328816b 100644 --- a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragment.kt +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragment.kt @@ -64,7 +64,7 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() { with(binding) { binding.cardView.clipToOutline = true - imageClose.setOnClickListener { + viewDialogHeader.imageClose.setOnClickListener { dismiss() closeButtonListener?.invoke(this@DialogFragment) } @@ -149,20 +149,21 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() { titleTextColor = dialogArguments.titleTextColor ?: R.color.ui_components_dialogs_primary_text_color, titleTextPosition = dialogArguments.titleTextPosition ?: TextPosition.START, contentTextPosition = dialogArguments.contentTextPosition ?: TextPosition.START, - webViewContent = dialogArguments.webViewContent + webViewContent = dialogArguments.webViewContent, + customView = dialogArguments.view?.invoke(), ) with(binding) { - with(viewTitleBackground) { + with(viewDialogHeader.viewTitleBackground) { setBackgroundColor(viewState.getTitleBackgroundColor(context)) visibility = viewState.getTitleVisibility() } - with(textTitle) { + with(viewDialogHeader.textTitle) { text = viewState.title textAlignment = viewState.getTitleTextPosition() setTextColor(viewState.getTitleTextColor(context)) } - imageClose.visibility = viewState.getCloseButtonVisibility() + viewDialogHeader.imageClose.visibility = viewState.getCloseButtonVisibility() with(imageContent) { setImageDrawable(viewState.getContentImageDrawable(context)) visibility = viewState.getContentImageVisibility() @@ -196,6 +197,11 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() { text = viewState.rightButtonText visibility = viewState.getRightButtonVisibility() } + with(frameLayoutCustom) { + visibility = viewState.getCustomViewVisibility() + if (viewState.getCustomViewVisibility() == View.VISIBLE) + addView(viewState.customView) + } } } diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragmentArguments.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragmentArguments.kt index 17c62db1..e3ae540d 100644 --- a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragmentArguments.kt +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogFragmentArguments.kt @@ -2,6 +2,7 @@ package com.trendyol.uicomponents.dialogs import android.os.Bundle import android.os.Parcelable +import android.view.View import android.webkit.WebView import androidx.annotation.ColorRes import androidx.annotation.DrawableRes @@ -34,7 +35,8 @@ class DialogFragmentArguments( val webViewContent: WebViewContent? = null, val webViewBuilder: (WebView.() -> Unit)? = null, val infoListItems: List>? = null, - val itemDividers: List = emptyList() + val itemDividers: List = emptyList(), + val view: (() -> View?)? = null ) : Parcelable { fun toBundle() = bundleOf("ARGUMENTS" to this) diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogViewState.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogViewState.kt index bf5c298f..2c267e7b 100644 --- a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogViewState.kt +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/DialogViewState.kt @@ -25,7 +25,8 @@ data class DialogViewState( val titleTextColor: Int, val titleTextPosition: TextPosition, val contentTextPosition: TextPosition, - val webViewContent: WebViewContent? + val webViewContent: WebViewContent?, + val customView: View?, ) { fun getTitleVisibility(): Int = if (title.isNullOrEmpty().not()) View.VISIBLE else View.GONE @@ -81,4 +82,6 @@ data class DialogViewState( fun getLeftButtonVisibility(): Int = if (leftButtonText != null) View.VISIBLE else View.GONE fun getRightButtonVisibility(): Int = if (rightButtonText != null) View.VISIBLE else View.GONE + + fun getCustomViewVisibility(): Int = if (customView != null) View.VISIBLE else View.GONE } diff --git a/libraries/dialogs/src/main/res/layout/fragment_dialog.xml b/libraries/dialogs/src/main/res/layout/fragment_dialog.xml index 53f70e87..4552523c 100644 --- a/libraries/dialogs/src/main/res/layout/fragment_dialog.xml +++ b/libraries/dialogs/src/main/res/layout/fragment_dialog.xml @@ -12,31 +12,9 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - - - - + + tools:hint="Your search query" + tools:visibility="gone" /> + tools:visibility="gone" /> + tools:text="Cancel" + tools:visibility="gone" /> + tools:text="Ok" + tools:visibility="gone" /> + + diff --git a/libraries/dialogs/src/main/res/layout/layout_dialog_header.xml b/libraries/dialogs/src/main/res/layout/layout_dialog_header.xml new file mode 100644 index 00000000..2c3cbcbe --- /dev/null +++ b/libraries/dialogs/src/main/res/layout/layout_dialog_header.xml @@ -0,0 +1,29 @@ + + + + + + + \ No newline at end of file diff --git a/sample/src/main/java/com/trendyol/uicomponents/DialogsActivity.kt b/sample/src/main/java/com/trendyol/uicomponents/DialogsActivity.kt index 6dde43b7..1efab937 100644 --- a/sample/src/main/java/com/trendyol/uicomponents/DialogsActivity.kt +++ b/sample/src/main/java/com/trendyol/uicomponents/DialogsActivity.kt @@ -21,6 +21,7 @@ class DialogsActivity : AppCompatActivity() { private val buttonSelectionWithSearchDialog by lazy { findViewById