Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.pantos27.gist
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.FrameLayout
import android.widget.ImageView
import com.pantos27.gist.R
import com.pantos27.gist.extentions.show
class DialogBuilder : DialogFragment() {
companion object {
const val TAG = "DialogBuilder"
}
interface ClickOnButtonListener
{
fun onclickButton()
}
interface ClickOnLinkListener{
fun onclickLinkListenr()
fun onDismiss()
}
private var imgCloseResource: Int? = null
private var headerText: CharSequence? = null
private var bodyText: CharSequence? = null
private var imageUrl: String? = null
private var imageResourceId: Int? = null
private var layoutResourcePopup = R.layout.popup_general
private var linkUrl: String? = null
private var btnText: CharSequence? = null
private var secondaryBtnText: CharSequence? = null
var linkListener:ClickOnLinkListener?=null
private var buttonClickListenr:ClickOnButtonListener?=null
private var secondaryButtonClickListenr:ClickOnButtonListener?=null
private var frameLayout:View?=null
private var cancelableDialog :Boolean= true
override fun isCancelable(): Boolean {
return cancelableDialog
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = inflater.inflate(layoutResourcePopup, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
//set dismiss button
frameLayout?.let {
val frameLayoutView:FrameLayout=(view.findViewById<FrameLayout>(R.id.dialog_frame_layout))
frameLayoutView.show()
frameLayoutView.addView(it)
}
val imageView = view.findViewById<ImageView>(R.id.dialog_image_close_button)
imgCloseResource?.let {
imageView.show()
imageView.setImageResource(it)
imageView.contentDescription = getString(R.string.accessibility_close)
}
imageView.setOnClickListener { dismiss()
linkListener?.onDismiss()}
headerText?.let {
view.findViewById<TextView>(R.id.dialog_image_text_title).run{
show()
text = it
}
}
bodyText?.let {
view.findViewById<TextView>(R.id.dialog_image_text_body).run{
show()
text = it
}
}
linkUrl?.let {
view.findViewById<TextView>(R.id.dialog_image_text_link).run {
show()
text = it
setOnClickListener{
linkListener?.onclickLinkListenr()
dismiss()
}
}
}
imageUrl?.let {
//use Glide here
}
imageResourceId?.let {
view.findViewById<ImageView>(R.id.dialog_image_image).run {
show()
setImageDrawable(resources.getDrawable(it,context.theme))
}
}
if(!btnText.isNullOrEmpty()) {
btnText?.let {
view.findViewById<Button>(R.id.btn_push_popup).run {
show()
text = it
setOnClickListener {
dismiss()
linkListener?.onDismiss()
buttonClickListenr?.onclickButton()
}
}
}
}
if(!secondaryBtnText.isNullOrEmpty()) {
val secondaryBtn: TextView = view.findViewById(R.id.secondary_btn)
secondaryBtn.show()
secondaryBtn.text = secondaryBtnText
secondaryBtn.setOnClickListener {
dismiss()
linkListener?.onDismiss()
secondaryButtonClickListenr?.onclickButton()
}
}
}
fun setFrameLayout(frameLayoutView: View):DialogBuilder
{
this.frameLayout=frameLayoutView
return this
}
fun setCloseImg(closeImgResource: Int): DialogBuilder {
imgCloseResource = closeImgResource
return this
}
fun setHeaderText(text: CharSequence?): DialogBuilder {
headerText = text
return this
}
fun setButtonClickListener(listener: ClickOnButtonListener?):DialogBuilder
{
buttonClickListenr=listener
return this
}
fun setLinkClickListener(listener: ClickOnLinkListener?): DialogBuilder {
linkListener = listener
return this
}
fun setBodyText(charSequence: CharSequence?): DialogBuilder {
bodyText = charSequence
return this
}
fun setLayoutResourcePopup(id: Int): DialogBuilder {
layoutResourcePopup = id
return this
}
fun setImageUrl(text: String): DialogBuilder {
imageUrl = text
return this
}
fun setImageResourceId(id: Int): DialogBuilder {
imageResourceId = id
return this
}
fun setLink(link: String): DialogBuilder {
linkUrl = link
return this
}
fun setButtonText(buttonText: CharSequence?): DialogBuilder {
btnText = buttonText
return this
}
fun setSecondaryBtnText(secondaryButtonText: CharSequence?): DialogBuilder {
secondaryBtnText = secondaryButtonText
return this
}
fun setIsCancelable(isCancelable:Boolean):DialogBuilder{
this.cancelableDialog=isCancelable
setCancelable(this.cancelableDialog)
return this
}
fun setSecondaryButtonClickListenr(listener: ClickOnButtonListener):DialogBuilder
{
secondaryButtonClickListenr = listener
return this
}
fun show(manager: FragmentManager?) {
manager?.let {
show(manager, TAG)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment