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