From 176dc6ba5da75d56010c8ba0e7826472b01f5d36 Mon Sep 17 00:00:00 2001 From: winter223 Date: Tue, 29 Mar 2022 13:52:27 +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 - 공통적으로 가져야할 속성들을 설정한 BaseDialogFragment 추가 - 함께 필요한 확장함수들 추가 --- .../common/dialog/BaseDialogFragment.kt | 51 +++++++++++++++++++ .../common/extension/ContextExtension.kt | 20 ++++++++ .../common/extension/FragmentExtension.kt | 31 +++++++++++ 3 files changed, 102 insertions(+) create mode 100644 app/src/main/java/com/moyerun/moyeorun_android/common/dialog/BaseDialogFragment.kt create mode 100644 app/src/main/java/com/moyerun/moyeorun_android/common/extension/ContextExtension.kt diff --git a/app/src/main/java/com/moyerun/moyeorun_android/common/dialog/BaseDialogFragment.kt b/app/src/main/java/com/moyerun/moyeorun_android/common/dialog/BaseDialogFragment.kt new file mode 100644 index 0000000..7de8ee9 --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/common/dialog/BaseDialogFragment.kt @@ -0,0 +1,51 @@ +package com.moyerun.moyeorun_android.common.dialog + +import android.app.Dialog +import android.graphics.Color +import android.graphics.drawable.ColorDrawable +import android.os.Bundle +import android.view.LayoutInflater +import android.view.Window +import androidx.fragment.app.DialogFragment +import com.moyerun.moyeorun_android.common.extension.getBooleanFromArgument +import com.moyerun.moyeorun_android.common.extension.isActivityDestroyed +import com.moyerun.moyeorun_android.common.extension.putBooleanToArgument + +open class BaseDialogFragment : DialogFragment() { + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + return object : Dialog(requireContext()) { + override fun show() { + if (context.isActivityDestroyed()) return + super.show() + } + + override fun dismiss() { + if (context.isActivityDestroyed()) return + super.dismiss() + } + }.apply { + requestWindowFeature(Window.FEATURE_NO_TITLE) + window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) + val contentView = onCreateView(LayoutInflater.from(context), null, savedInstanceState) + if (contentView != null) { + setContentView(contentView) + } + } + } + + override fun onPause() { + super.onPause() + if (isCancelable && getBooleanFromArgument(ARG_DISMISS_ON_PAUSE, false)) { + dismiss() + } + } + + protected fun setDismissOnPause(dismissOnPause: Boolean) { + putBooleanToArgument(ARG_DISMISS_ON_PAUSE, dismissOnPause) + } + + companion object { + private const val ARG_DISMISS_ON_PAUSE = "dismissOnPause" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ContextExtension.kt b/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ContextExtension.kt new file mode 100644 index 0000000..832e52e --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/common/extension/ContextExtension.kt @@ -0,0 +1,20 @@ +package com.moyerun.moyeorun_android.common.extension + +import android.app.Activity +import android.app.Application +import android.content.Context +import android.content.ContextWrapper + +fun Context.isActivityDestroyed(): Boolean { + if (this is Application) return false + val activity = getActivity()?: return true + return activity.isFinishing || activity.isDestroyed +} + +fun Context.getActivity(): Activity? { + return when (this) { + is Activity -> this + is ContextWrapper -> baseContext.getActivity() + else -> null + } +} \ 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 2ad8fe7..7492e29 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 @@ -1,8 +1,39 @@ package com.moyerun.moyeorun_android.common.extension import android.widget.Toast +import androidx.core.os.bundleOf import androidx.fragment.app.Fragment fun Fragment.toast(msg: String, isShort: Boolean = false) { Toast.makeText(context, msg, if (isShort) Toast.LENGTH_SHORT else Toast.LENGTH_LONG).show() +} + +fun Fragment.putStringToArgument(key: String, value: String?) { + val args = arguments + if (args == null) { + arguments = bundleOf(key to value) + } else { + args.apply { + putString(key, value) + } + } +} + +fun Fragment.getStringFromArgument(key: String, defaultValue: String = ""): String { + return arguments?.getString(key, defaultValue)?: defaultValue +} + +fun Fragment.putBooleanToArgument(key: String, value: Boolean) { + val args = arguments + if (args == null) { + arguments = bundleOf(key to value) + } else { + args.apply { + putBoolean(key, value) + } + } +} + +fun Fragment.getBooleanFromArgument(key: String, value: Boolean): Boolean { + return arguments?.getBoolean(key, value)?: value } \ No newline at end of file