Created
October 1, 2021 04:27
-
-
Save darylsze/45ce27389213ffb181958c0469620108 to your computer and use it in GitHub Desktop.
DialogBuilder in Kotlin DSL (WIP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DialogBuilder(val activityContext: FragmentActivity) { | |
class TextBuilder(val context: Context) { | |
sealed class TextConfig { | |
data class TitleConfig( | |
val text: String, | |
val bold: Boolean = true, | |
@DimenRes val textSizeSdp: Int = R.dimen._16sdp | |
): TextConfig() | |
data class ContentConfig( | |
val text: String, | |
val bold: Boolean = false, | |
@DimenRes val textSizeSdp: Int = R.dimen._11sdp | |
): TextConfig() | |
} | |
fun init(config: TextConfig): TextView { | |
return TextView(context).apply { | |
text = config.text | |
textSize = resources.getDimensionPixelSize(config.textSizeSdp).toDp(context) | |
if (config.bold) { | |
setTypeface(Typeface.DEFAULT_BOLD) | |
} | |
} | |
} | |
} | |
class ImageBuilder(val context: Context) { | |
// as our app uses SDP to align size among android screen sizes, | |
// so providing static height and width is safe for various device types | |
data class ImageConfig( | |
@DrawableRes val src: Int, | |
@DimenRes val height: Int? = null, | |
@DimenRes val width: Int? = null | |
) | |
fun init(config: ImageConfig): ImageView { | |
if (config.height == null && config.width == null) { | |
throw Exception("you must provide at least height or width") | |
} | |
return ImageView(context).apply { | |
setImageResource(config.src) | |
updateLayoutParams { | |
if (config.height != null) { | |
height = resources.getDimensionPixelSize(config.height) | |
} | |
if (config.width != null) { | |
width = resources.getDimensionPixelOffset(config.width) | |
} | |
} | |
} | |
} | |
} | |
private val layoutSequence = mutableListOf<View>() | |
fun addText(config: TextBuilder.TextConfig) { | |
layoutSequence.add(TextBuilder(activityContext).init(config)) | |
} | |
fun addImage(config: ImageBuilder.ImageConfig) { | |
layoutSequence.add(ImageBuilder(activityContext).init(config)) | |
} | |
// todo addCustomLayout() | |
fun addDivider() { | |
layoutSequence.add(Divider(context = activityContext)) | |
} | |
fun build(): DialogPlus { | |
val d = DialogPlus.newDialog(activityContext) | |
.setOverlayBackgroundResource(R.color.dialog_overlay_color) | |
.setExpanded(false) | |
.setGravity(Gravity.CENTER) | |
.setContentWidth((System.getScreenWidth() * 0.85).toInt()) | |
.setContentBackgroundResource(Color.TRANSPARENT) | |
.setContentHolder(ViewHolder(R.layout.title_image_message_yes_dalog)) | |
.setCancelable(false) | |
.create() | |
val titleTv = d.findViewById(R.id.titleTv) as TextView | |
val image = d.findViewById(R.id.image) as ImageView | |
val imgClose = d.findViewById(R.id.closeImg) as ImageView | |
val msgTv = d.findViewById(R.id.msgTv) as TextView | |
val confirmTv = d.findViewById(R.id.confirmTv) as TextView | |
Glide.with(image.context) | |
.load(R.drawable.sv_image_invite_new_banner_tutorial_sim) | |
.into(image) | |
titleTv.text = System.getLocalString("dialog_invite_coupon_title") | |
msgTv.text = System.getLocalString("onboarding_alert_msg_visitor_mode_with_invitecode") | |
confirmTv.text = System.getLocalString("general_button_confirm") | |
imgClose.setOnClickListener { v -> d.dismiss() } | |
// prevent double click. | |
confirmTv.clicks() | |
.throttleFirst(500, TimeUnit.MICROSECONDS) | |
.doOnNext { Unit -> | |
d.dismiss() | |
callback.onVoid() | |
} | |
.subscribe() | |
d.show() | |
} | |
} | |
fun FragmentActivity.dialogBuilder(setup: DialogBuilder.() -> Unit): DialogPlus { | |
val dialog = DialogBuilder(this) | |
dialog.setup() | |
return dialog.build() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment