Skip to content

Commit

Permalink
add Font Support for Dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
belfu.ogretir committed Oct 1, 2024
1 parent 8017e7a commit 27f8329
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.trendyol.uicomponents.dialogs

import android.text.SpannableString
import android.webkit.DownloadListener
import android.webkit.WebView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.FontRes

open class Builder internal constructor() {

Expand All @@ -13,13 +12,23 @@ open class Builder internal constructor() {
var titleTextColor: Int? = null
var titleTextPosition: TextPosition? = null
var showCloseButton: Boolean = false
@ColorInt var closeButtonColor: Int? = null
@DrawableRes var closeButtonDrawable: Int? = null

@ColorInt
var closeButtonColor: Int? = null

@DrawableRes
var closeButtonDrawable: Int? = null
var closeButtonListener: ((DialogFragment) -> Unit)? = null
var animateCornerRadiusWhenExpand: Boolean = false
var onDialogDismissListener: ((DialogFragment) -> Unit)? = null
var cornerRadius: Float? = null
var isFullHeightWebView: Boolean = false

@FontRes
var titleFontFamily: Int? = null

@FontRes
var contentFontFamily: Int? = null
}

open class InfoDialogBuilder internal constructor() : Builder() {
Expand Down Expand Up @@ -54,7 +63,9 @@ open class InfoDialogBuilder internal constructor() : Builder() {
titleTextPosition = it.titleTextPosition,
contentTextPosition = it.contentTextPosition,
webViewContent = it.webViewContent,
isFullHeightWebView = it.isFullHeightWebView
isFullHeightWebView = it.isFullHeightWebView,
titleFontFamily = it.titleFontFamily,
contentFontFamily = it.contentFontFamily
).toBundle()
this.closeButtonListener = it.closeButtonListener ?: { }
this.onDismissListener = it.onDialogDismissListener ?: {}
Expand Down Expand Up @@ -86,7 +97,9 @@ open class AgreementDialogBuilder internal constructor() : InfoDialogBuilder() {
contentImage = it.contentImage,
rightButtonText = it.rightButtonText,
leftButtonText = it.leftButtonText,
webViewContent = it.webViewContent
webViewContent = it.webViewContent,
titleFontFamily = it.titleFontFamily,
contentFontFamily = it.contentFontFamily
).toBundle()
closeButtonListener = it.closeButtonListener
rightButtonClickListener = it.rightButtonClickListener
Expand Down Expand Up @@ -133,7 +146,9 @@ class SelectionDialogBuilder internal constructor() : InfoDialogBuilder() {
selectedItemDrawable = it.selectedItemDrawable,
selectedTextColor = it.selectedTextColor,
showRadioButton = it.showRadioButton,
webViewContent = it.webViewContent
webViewContent = it.webViewContent,
titleFontFamily = it.titleFontFamily,
contentFontFamily = it.contentFontFamily
).toBundle()
closeButtonListener = it.closeButtonListener
onItemSelectedListener = it.onItemSelectedListener
Expand Down Expand Up @@ -166,7 +181,9 @@ class InfoListDialogBuilder internal constructor() : InfoDialogBuilder() {
contentImage = it.contentImage,
webViewContent = it.webViewContent,
infoListItems = it.infoListItems,
itemDividers = it.itemDividers
itemDividers = it.itemDividers,
titleFontFamily = it.titleFontFamily,
contentFontFamily = it.contentFontFamily
).toBundle()
closeButtonListener = it.closeButtonListener
this.onDismissListener = it.onDialogDismissListener ?: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.view.LayoutInflater
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.webkit.DownloadListener
Expand Down Expand Up @@ -47,7 +46,8 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
dialogArguments.showItemsAsHtml,
dialogArguments.selectedItemDrawable,
dialogArguments.selectedTextColor,
dialogArguments.showRadioButton
dialogArguments.showRadioButton,
dialogArguments.contentFontFamily
)
}

Expand All @@ -57,7 +57,11 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
ViewModelProviders.of(this)[DialogListViewModel::class.java]
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentDialogBinding.inflate(inflater, container, false)
return binding.root
}
Expand All @@ -66,7 +70,8 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
if (dialogArguments.animateCornerRadiusWhenExpand) {
animateCornerRadiusWithStateChanged()
} else {
val cornerRadius = dialogArguments.cornerRadius ?: requireContext().pixel(R.dimen.ui_components_dialogs_corner_radius)
val cornerRadius = dialogArguments.cornerRadius
?: requireContext().pixel(R.dimen.ui_components_dialogs_corner_radius)
binding.cardView.outlineProvider = BottomSheetOutlineProvider(radius = cornerRadius)
}

Expand All @@ -90,13 +95,21 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
initializeSelectionDialog(items)
}
dialogArguments.infoListItems?.let { items ->
initializeInfoListDialog(items, dialogArguments.itemDividers)
initializeInfoListDialog(
items,
dialogArguments.itemDividers,
dialogArguments.contentFontFamily
)
}
if (dialogArguments.horizontalPadding != null || dialogArguments.verticalPadding != null) {
val topPadding = dialogArguments.verticalPadding?.toInt() ?: nestedScrollView.paddingTop
val bottomPadding = dialogArguments.verticalPadding?.toInt() ?: nestedScrollView.paddingBottom
val startPadding = dialogArguments.horizontalPadding?.toInt() ?: nestedScrollView.paddingStart
val endPadding = dialogArguments.horizontalPadding?.toInt() ?: nestedScrollView.paddingEnd
val topPadding =
dialogArguments.verticalPadding?.toInt() ?: nestedScrollView.paddingTop
val bottomPadding =
dialogArguments.verticalPadding?.toInt() ?: nestedScrollView.paddingBottom
val startPadding =
dialogArguments.horizontalPadding?.toInt() ?: nestedScrollView.paddingStart
val endPadding =
dialogArguments.horizontalPadding?.toInt() ?: nestedScrollView.paddingEnd
nestedScrollView.setPadding(startPadding, topPadding, endPadding, bottomPadding)
}
}
Expand Down Expand Up @@ -135,10 +148,12 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {

private fun initializeInfoListDialog(
items: List<Pair<CharSequence, CharSequence>>,
itemDividers: List<ItemDivider>
itemDividers: List<ItemDivider>,
fontFamily: Int?
) {
with(binding.recyclerViewItems) {
infoListAdapter.setItems(items)
infoListAdapter.contentFontFamily = fontFamily
itemDividers.forEach {
addItemDecoration(ItemDecorator(it))
}
Expand All @@ -162,11 +177,15 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
isSearchEnabled = dialogArguments.enableSearch,
isClearSearchButtonVisible = dialogArguments.showClearSearchButton,
searchHint = dialogArguments.searchHint,
titleBackgroundColor = dialogArguments.titleBackgroundColor ?: R.color.ui_components_dialogs_gray,
titleTextColor = dialogArguments.titleTextColor ?: R.color.ui_components_dialogs_primary_text_color,
titleBackgroundColor = dialogArguments.titleBackgroundColor
?: R.color.ui_components_dialogs_gray,
titleTextColor = dialogArguments.titleTextColor
?: R.color.ui_components_dialogs_primary_text_color,
titleTextPosition = dialogArguments.titleTextPosition ?: TextPosition.START,
contentTextPosition = dialogArguments.contentTextPosition ?: TextPosition.START,
webViewContent = dialogArguments.webViewContent,
titleFontFamily = dialogArguments.titleFontFamily,
contentFontFamily = dialogArguments.contentFontFamily
)

with(binding) {
Expand All @@ -178,6 +197,7 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
text = viewState.title
textAlignment = viewState.getTitleTextPosition()
setTextColor(viewState.getTitleTextColor(context))
typeface = viewState.getTitleFontFamily(context)
}
with(viewDialogHeader.imageClose) {
visibility = viewState.getCloseButtonVisibility()
Expand All @@ -191,6 +211,7 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
text = viewState.getContent()
textAlignment = viewState.getContentTextPosition()
visibility = viewState.getContentTextVisibility()
typeface = viewState.getContentFontFamily(context)
}
with(webViewContent) {
visibility = viewState.getWebViewContentVisibility()
Expand All @@ -209,6 +230,7 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
with(editTextSearch) {
hint = viewState.searchHint
visibility = viewState.isSearchVisible()
typeface = viewState.getContentFontFamily(context)
}
imageClearSearchQuery.visibility = viewState.getClearButtonVisibility()
recyclerViewItems.visibility = viewState.getListVisibility()
Expand Down Expand Up @@ -310,4 +332,4 @@ class DialogFragment internal constructor() : BaseBottomSheetDialog() {
return fragmentManager.findFragmentByTag(TAG) as? DialogFragment
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.trendyol.uicomponents.dialogs

import android.os.Bundle
import android.os.Parcelable
import android.webkit.WebView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.FontRes
import androidx.core.os.bundleOf
import kotlinx.parcelize.Parcelize

Expand Down Expand Up @@ -39,7 +39,9 @@ class DialogFragmentArguments(
val webViewContent: WebViewContent? = null,
val infoListItems: List<Pair<CharSequence, CharSequence>>? = null,
val itemDividers: List<ItemDivider> = emptyList(),
val isFullHeightWebView : Boolean = false
val isFullHeightWebView: Boolean = false,
@FontRes val titleFontFamily: Int? = null,
@FontRes val contentFontFamily: Int? = null
) : Parcelable {

fun toBundle() = bundleOf("ARGUMENTS" to this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.FontRes
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.HtmlCompat
import com.trendyol.dialog.R

Expand All @@ -29,6 +31,8 @@ data class DialogViewState(
val titleTextPosition: TextPosition,
val contentTextPosition: TextPosition,
val webViewContent: WebViewContent?,
@FontRes val titleFontFamily: Int?,
@FontRes val contentFontFamily: Int?,
) {

fun getTitleVisibility(): Int = if (title.isNullOrEmpty().not()) View.VISIBLE else View.GONE
Expand All @@ -40,10 +44,17 @@ data class DialogViewState(
}

@ColorInt
fun getTitleBackgroundColor(context: Context): Int = ContextCompat.getColor(context, titleBackgroundColor)
fun getTitleBackgroundColor(context: Context): Int =
ContextCompat.getColor(context, titleBackgroundColor)

fun getTitleTextColor(context: Context) = ContextCompat.getColor(context, titleTextColor)

fun getTitleFontFamily(context: Context) =
titleFontFamily?.let { ResourcesCompat.getFont(context, it) }

fun getContentFontFamily(context: Context) =
contentFontFamily?.let { ResourcesCompat.getFont(context, it) }

fun getTitleTextPosition(): Int {
return when (titleTextPosition) {
TextPosition.CENTER -> 4
Expand Down Expand Up @@ -71,7 +82,8 @@ data class DialogViewState(
}
}

fun getContentImageVisibility(): Int = if (contentImage?.takeIf { it != 0 } != null) View.VISIBLE else View.GONE
fun getContentImageVisibility(): Int =
if (contentImage?.takeIf { it != 0 } != null) View.VISIBLE else View.GONE

fun getContentTextVisibility(): Int = if (content.isNotEmpty()) View.VISIBLE else View.GONE

Expand All @@ -86,7 +98,8 @@ data class DialogViewState(

fun isSearchVisible(): Int = if (isSearchEnabled) View.VISIBLE else View.GONE

fun getClearButtonVisibility(): Int = if (isClearSearchButtonVisible) View.VISIBLE else View.GONE
fun getClearButtonVisibility(): Int =
if (isClearSearchButtonVisible) View.VISIBLE else View.GONE

fun getListVisibility(): Int = if (isListVisible) View.VISIBLE else View.GONE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.trendyol.uicomponents.dialogs.list

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.FontRes
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.trendyol.dialog.databinding.ItemUiComponentsSelectionDialogBinding
Expand All @@ -10,7 +11,8 @@ internal class DialogListAdapter(
private val showItemsAsHtml: Boolean,
private val selectedItemDrawable: Int?,
private val selectedTextColor: Int?,
private val showRadioButton: Boolean
private val showRadioButton: Boolean,
@FontRes private val contentFontFamily: Int?
) : ListAdapter<Pair<Boolean, CharSequence>, DialogListAdapter.ItemViewHolder>(ListItemDiffCallback()) {

var onItemSelectedListener: ((Int) -> Unit)? = null
Expand All @@ -25,7 +27,13 @@ internal class DialogListAdapter(
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder =
ItemViewHolder(ItemUiComponentsSelectionDialogBinding.inflate(LayoutInflater.from(parent.context), parent, false))
ItemViewHolder(
ItemUiComponentsSelectionDialogBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)

inner class ItemViewHolder(
private val binding: ItemUiComponentsSelectionDialogBinding
Expand All @@ -48,7 +56,8 @@ internal class DialogListAdapter(
selectedItemDrawable = selectedItemDrawable,
selectedTextColor = selectedTextColor,
isChecked = item.first,
showRadioButton = showRadioButton
showRadioButton = showRadioButton,
contentFontFamily = contentFontFamily
)

with(binding) {
Expand All @@ -59,6 +68,7 @@ internal class DialogListAdapter(
with(radioButtonItem) {
text = viewState.getText()
setTextColor(viewState.getSelectedTextColor(context))
typeface = viewState.getFontFamily(context)
}
with(imageViewCheckedDrawable) {
setImageDrawable(viewState.getSelectedItemDrawable(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.trendyol.uicomponents.dialogs.list
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.FontRes
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.HtmlCompat
import com.trendyol.dialog.R

Expand All @@ -13,7 +15,8 @@ data class DialogListItemViewState(
val selectedItemDrawable: Int?,
val selectedTextColor: Int?,
val isChecked: Boolean,
val showRadioButton: Boolean
val showRadioButton: Boolean,
@FontRes val contentFontFamily: Int?,
) {

fun getText(): CharSequence =
Expand All @@ -39,5 +42,7 @@ data class DialogListItemViewState(
fun getRadioButtonVisibility(): Int = if (showRadioButton) View.VISIBLE else View.GONE

fun getRadioButtonChecked(): Boolean = isChecked

fun getFontFamily(context: Context) = contentFontFamily?.let { ResourcesCompat.getFont(context, it) }
}

Loading

0 comments on commit 27f8329

Please sign in to comment.