Created
November 27, 2015 01:13
-
-
Save ShinjiKobayashi/ff6c3611404555d2e360 to your computer and use it in GitHub Desktop.
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 CustomDialogFragment : DialogFragment() { | |
interface OnDialogActionListerner { | |
fun onPositiveButtonClicked() | |
fun onNegativeButtonClicked() | |
} | |
class Builder { | |
// ActivityかFragmentどちらかは必ずInputさせる | |
internal val mActivity: AppCompatActivity? | |
internal val mFragment: Fragment? | |
private var mTitle: String? = null | |
private var mMessage: String? = null | |
private var mPositiveButtonLabel: String? = null | |
private var mNeagtiveButtonLabel: String? = null | |
constructor(activity: AppCompatActivity) { | |
mActivity = activity | |
mFragment = null | |
} | |
constructor(fragment: Fragment) { | |
mActivity = null | |
mFragment = fragment | |
} | |
fun setTitle(title: String): Builder { | |
mTitle = title | |
return this | |
} | |
// 他のケース作ってないけど、同じ形式でResからの入力サポートできる | |
fun setTitle(resId: Int): Builder { | |
return setTitle(context.resources.getString(resId)) | |
} | |
fun setMessage(message: String): Builder { | |
mMessage = message | |
return this | |
} | |
fun setPositiveButtonLabel(positiveButtonLabel: String): Builder { | |
mPositiveButtonLabel = positiveButtonLabel | |
return this | |
} | |
fun setNeagtiveButtonLabel(neagtiveButtonLabel: String): Builder { | |
mNeagtiveButtonLabel = neagtiveButtonLabel | |
return this | |
} | |
fun build(): CustomDialogFragment? { | |
val bundle = Bundle() | |
bundle.putString(BUNDLE_KEY_TITLE, mTitle) | |
bundle.putString(BUNDLE_KEY_MESSAGE, mMessage) | |
bundle.putString(BUNDLE_KEY_POSITIVE_LABEL, mPositiveButtonLabel) | |
bundle.putString(BUNDLE_KEY_NEGATIVE_LABEL, mNeagtiveButtonLabel) | |
val fragment = CustomDialogFragment() | |
fragment.arguments = bundle | |
return fragment | |
} | |
private val context: Context | |
get() = (mActivity ?: mFragment!!.activity).applicationContext | |
companion object { | |
val BUNDLE_KEY_TITLE = "title" | |
val BUNDLE_KEY_MESSAGE = "message" | |
val BUNDLE_KEY_POSITIVE_LABEL = "positive_label" | |
val BUNDLE_KEY_NEGATIVE_LABEL = "negative_label" | |
} | |
} | |
var mListener: OnDialogActionListerner? = null | |
private var shown: Boolean = false | |
override fun onAttach(context: Context?) { | |
super.onAttach(context) | |
} | |
override fun onDetach() { | |
mListener = null | |
super.onDetach() | |
} | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
val bundle = arguments | |
// DialogのLayoutを変更したいのであれば、ここでinflateする | |
var view = activity.layoutInflater.inflate(R.layout.dialog, null, false) | |
val builder = AlertDialog.Builder(activity) | |
if (bundle.getString(Builder.BUNDLE_KEY_TITLE) != null) { | |
builder.setTitle(bundle.getString(Builder.BUNDLE_KEY_TITLE)) | |
} | |
if (bundle.getString(Builder.BUNDLE_KEY_MESSAGE) != null) { | |
builder.setMessage(bundle.getString(Builder.BUNDLE_KEY_MESSAGE)) | |
} | |
if (bundle.getString(Builder.BUNDLE_KEY_POSITIVE_LABEL) != null) { | |
builder.setPositiveButton(bundle.getString(Builder.BUNDLE_KEY_POSITIVE_LABEL), | |
object : DialogInterface.OnClickListener { | |
override fun onClick(dialog: DialogInterface?, which: Int) { | |
mListener?.onPositiveButtonClicked() | |
} | |
}) | |
} | |
if (bundle.getString(Builder.BUNDLE_KEY_NEGATIVE_LABEL) != null) { | |
builder.setNegativeButton(bundle.getString(Builder.BUNDLE_KEY_NEGATIVE_LABEL), | |
object : DialogInterface.OnClickListener { | |
override fun onClick(dialog: DialogInterface?, which: Int) { | |
mListener?.onNegativeButtonClicked() | |
} | |
}) | |
} | |
builder.setView(view) | |
return builder.create() | |
} | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
} | |
override fun onResume() { | |
super.onResume() | |
// Dialogの大きさを変更するときはここで行う | |
var lp = dialog.window.attributes | |
lp.width = (resources.displayMetrics.widthPixels * 0.8).toInt() | |
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT | |
dialog.window.attributes = lp | |
} | |
override fun show(manager: FragmentManager?, tag: String?) { | |
// show, onDissmissをoverrideしてDialogが重なった表示とならないようにしている | |
if (shown) return | |
super.show(manager, tag) | |
shown = true | |
} | |
override fun onDismiss(dialog: DialogInterface?) { | |
shown = false | |
super.onDismiss(dialog) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コンバートしたソースがベースだから、まだ粗いっぽい。