-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 공통적으로 가져야할 속성들을 설정한 BaseDialogFragment 추가 - 함께 필요한 확장함수들 추가
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/moyerun/moyeorun_android/common/dialog/BaseDialogFragment.kt
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
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" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/moyerun/moyeorun_android/common/extension/ContextExtension.kt
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
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 | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/moyerun/moyeorun_android/common/extension/FragmentExtension.kt
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
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 | ||
} |