From 510946a0d05a2e44eb22b51adfd102c63b5e029a Mon Sep 17 00:00:00 2001 From: winter223 Date: Tue, 29 Mar 2022 13:57:07 +0900 Subject: [PATCH] =?UTF-8?q?[#8]=20=EA=B8=B0=EB=B3=B8=20=EB=8B=A4=EC=9D=B4?= =?UTF-8?q?=EC=96=BC=EB=A1=9C=EA=B7=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Content 로 텍스트만을 보여주는 TextConfirmDialogFragment 추가 - Fragment 를 띄우기 위한 확장함수 추가 --- .../dialog/TextConfirmDialogFragment.kt | 60 +++++++++++++++++++ .../common/extension/ActivityExtension.kt | 9 +++ .../common/extension/FragmentExtension.kt | 14 +++++ .../main/res/layout/dialog_content_text.xml | 16 +++++ 4 files changed, 99 insertions(+) create mode 100644 app/src/main/java/com/moyerun/moyeorun_android/common/dialog/TextConfirmDialogFragment.kt create mode 100644 app/src/main/res/layout/dialog_content_text.xml diff --git a/app/src/main/java/com/moyerun/moyeorun_android/common/dialog/TextConfirmDialogFragment.kt b/app/src/main/java/com/moyerun/moyeorun_android/common/dialog/TextConfirmDialogFragment.kt new file mode 100644 index 0000000..1bf5c35 --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/common/dialog/TextConfirmDialogFragment.kt @@ -0,0 +1,60 @@ +package com.moyerun.moyeorun_android.common.dialog + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.moyerun.moyeorun_android.common.extension.getStringFromArgument +import com.moyerun.moyeorun_android.common.extension.putStringToArgument +import com.moyerun.moyeorun_android.databinding.DialogContentTextBinding + +class TextConfirmDialogFragment : CustomContentDialogFragment() { + + private var onPositiveClick: () -> Unit = {} + private var onNegativeClick: () -> Unit = {} + + override fun onCreateContentView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + return DialogContentTextBinding.inflate(inflater, container, false).root + } + + override fun onContentViewCreated(view: View, savedInstanceState: Bundle?) { + val binding = DialogContentTextBinding.bind(view) + binding.textDialogContent.text = getStringFromArgument(ARG_CONTENT) + } + + override fun onPositiveClick() { + onPositiveClick.invoke() + } + + override fun onNegativeClick() { + onNegativeClick.invoke() + } + + companion object { + private const val ARG_CONTENT = "content" + + fun newInstance( + title: String, + content: String, + positiveLabel: String? = null, + negativeLabel: String? = null, + dismissOnPause: Boolean = false, + isCancelable: Boolean = false, + onPositiveClick: () -> Unit = {}, + onNegativeClick: () -> Unit = {} + ): TextConfirmDialogFragment = TextConfirmDialogFragment().apply { + putStringToArgument(ARG_CONTENT, content) + setTitle(title) + setPositiveLabel(positiveLabel) + setNegativeLabel(negativeLabel) + setDismissOnPause(dismissOnPause) + this.isCancelable = isCancelable + this.onPositiveClick = onPositiveClick + this.onNegativeClick = onNegativeClick + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ActivityExtension.kt b/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ActivityExtension.kt index d57b173..af4d5e4 100644 --- a/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ActivityExtension.kt +++ b/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ActivityExtension.kt @@ -2,7 +2,16 @@ package com.moyerun.moyeorun_android.common.extension import android.app.Activity import android.widget.Toast +import androidx.fragment.app.DialogFragment +import androidx.fragment.app.FragmentActivity fun Activity.toast(msg: String, isShort: Boolean = true) { Toast.makeText(this, msg, if (isShort) Toast.LENGTH_SHORT else Toast.LENGTH_LONG).show() +} + +inline fun FragmentActivity.showAllowingStateLoss( + tag: String?, + dialogFragmentFactory: () -> DialogFragment +) { + supportFragmentManager.showAllowingStateLoss(tag, dialogFragmentFactory) } \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/common/extension/FragmentExtension.kt b/app/src/main/java/com/moyerun/moyeorun_android/common/extension/FragmentExtension.kt index 7492e29..a999fc2 100644 --- a/app/src/main/java/com/moyerun/moyeorun_android/common/extension/FragmentExtension.kt +++ b/app/src/main/java/com/moyerun/moyeorun_android/common/extension/FragmentExtension.kt @@ -2,7 +2,9 @@ package com.moyerun.moyeorun_android.common.extension import android.widget.Toast import androidx.core.os.bundleOf +import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager fun Fragment.toast(msg: String, isShort: Boolean = false) { Toast.makeText(context, msg, if (isShort) Toast.LENGTH_SHORT else Toast.LENGTH_LONG).show() @@ -36,4 +38,16 @@ fun Fragment.putBooleanToArgument(key: String, value: Boolean) { fun Fragment.getBooleanFromArgument(key: String, value: Boolean): Boolean { return arguments?.getBoolean(key, value)?: value +} + +inline fun FragmentManager?.showAllowingStateLoss( + tag: String?, + dialogFragmentFactory: () -> DialogFragment +) { + if (this == null || isDestroyed) { + return + } + val transaction = beginTransaction() + transaction.add(dialogFragmentFactory(), tag) + transaction.commitAllowingStateLoss() } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_content_text.xml b/app/src/main/res/layout/dialog_content_text.xml new file mode 100644 index 0000000..76daa67 --- /dev/null +++ b/app/src/main/res/layout/dialog_content_text.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file