Skip to content

Commit

Permalink
Merge pull request #32 from MoyeoRun/feature/common-dialog
Browse files Browse the repository at this point in the history
[#8] 기본 다이얼로그 추가
  • Loading branch information
ethan-223 authored Apr 26, 2022
2 parents eb3fd73 + c4f6425 commit 09a01eb
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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.isActivityDestroyed

open class RoundDialogFragment : DialogFragment() {

private var dismissOnPause = false

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(SAVED_DISMISS_ON_PAUSE, dismissOnPause)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
if (savedInstanceState != null) {
dismissOnPause = savedInstanceState.getBoolean(SAVED_DISMISS_ON_PAUSE, false)
}

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 && dismissOnPause) {
dismiss()
}
}

protected fun setDismissOnPause(dismissOnPause: Boolean) {
this.dismissOnPause = dismissOnPause
}

companion object {
private const val SAVED_DISMISS_ON_PAUSE = "dismissOnPause"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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,22 @@
package com.moyerun.moyeorun_android.common.extension

import android.widget.Toast
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()
}

inline fun FragmentManager?.showAllowingStateLoss(
tag: String?,
dialogFragmentFactory: () -> DialogFragment
) {
if (this == null || isDestroyed) {
return
}
val transaction = beginTransaction()
transaction.add(dialogFragmentFactory(), tag)
transaction.commitAllowingStateLoss()
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/round_background_12.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="12dp" />
<solid android:color="@color/main_white" />
</shape>
</item>
</layer-list>
5 changes: 5 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<color name="text_gray">#828282</color>
<color name="text_hint_gray">#A9A9A9</color>


<!-- Line colors -->
<color name="line_dialog_divider">#EBECEF</color>


<!-- button colors -->
<color name="button_pressed_true">#0047D0</color>
<color name="button_enable_false">#C4C4C4</color>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">MoyeoRun-android</string>

<!-- Common -->
<string name="cancel">취소</string>
<string name="ok">확인</string>

</resources>
40 changes: 40 additions & 0 deletions app/src/main/res/values/styles_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" />

<style name="Dialog.Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">@color/line_dialog_divider</item>
</style>

<style name="Dialog.Background">
<item name="android:layout_width">297dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/round_background_12</item>
</style>

<style name="Dialog.Title" parent="Widget.AppCompat.TextView">
<item name="android:textColor">@color/text_default_black</item>
<item name="android:textSize">22dp</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
</style>

<style name="Dialog.Button" parent="Widget.AppCompat.Button">
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
<item name="android:paddingVertical">21dp</item>
<item name="android:textSize">16dp</item>
</style>

<style name="Dialog.Button.Negative">
<item name="android:textColor">@color/text_default_black</item>
</style>

<style name="Dialog.Button.Positive">
<item name="android:textColor">@color/main_blue</item>
</style>

</resources>

0 comments on commit 09a01eb

Please sign in to comment.