Skip to content

Commit

Permalink
[#8] 기본 다이얼로그 추가
Browse files Browse the repository at this point in the history
- 공통적으로 가져야할 속성들을 설정한 BaseDialogFragment 추가
- 함께 필요한 확장함수들 추가
  • Loading branch information
ethan-223 committed Mar 29, 2022
1 parent d746f55 commit 176dc6b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 176dc6b

Please sign in to comment.