Skip to content

Commit

Permalink
[#8] 기본 다이얼로그 추가
Browse files Browse the repository at this point in the history
- Content 로 텍스트만을 보여주는 TextConfirmDialogFragment 추가
- Fragment 를 띄우기 위한 확장함수 추가
  • Loading branch information
ethan-223 committed Mar 29, 2022
1 parent 700812d commit 510946a
Show file tree
Hide file tree
Showing 4 changed files with 99 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.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.moyerun.moyeorun_android.common.extension.getStringFromArgument
import com.moyerun.moyeorun_android.common.extension.putStringToArgument
import com.moyerun.moyeorun_android.databinding.DialogContentTextBinding

class TextConfirmDialogFragment : CustomContentDialogFragment() {

private var onPositiveClick: () -> Unit = {}
private var onNegativeClick: () -> Unit = {}

override fun onCreateContentView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return DialogContentTextBinding.inflate(inflater, container, false).root
}

override fun onContentViewCreated(view: View, savedInstanceState: Bundle?) {
val binding = DialogContentTextBinding.bind(view)
binding.textDialogContent.text = getStringFromArgument(ARG_CONTENT)
}

override fun onPositiveClick() {
onPositiveClick.invoke()
}

override fun onNegativeClick() {
onNegativeClick.invoke()
}

companion object {
private const val ARG_CONTENT = "content"

fun newInstance(
title: String,
content: String,
positiveLabel: String? = null,
negativeLabel: String? = null,
dismissOnPause: Boolean = false,
isCancelable: Boolean = false,
onPositiveClick: () -> Unit = {},
onNegativeClick: () -> Unit = {}
): TextConfirmDialogFragment = TextConfirmDialogFragment().apply {
putStringToArgument(ARG_CONTENT, content)
setTitle(title)
setPositiveLabel(positiveLabel)
setNegativeLabel(negativeLabel)
setDismissOnPause(dismissOnPause)
this.isCancelable = isCancelable
this.onPositiveClick = onPositiveClick
this.onNegativeClick = onNegativeClick
}
}
}
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
Expand Up @@ -2,7 +2,9 @@ package com.moyerun.moyeorun_android.common.extension

import android.widget.Toast
import androidx.core.os.bundleOf
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()
Expand Down Expand Up @@ -36,4 +38,16 @@ fun Fragment.putBooleanToArgument(key: String, value: Boolean) {

fun Fragment.getBooleanFromArgument(key: String, value: Boolean): Boolean {
return arguments?.getBoolean(key, value)?: value
}

inline fun FragmentManager?.showAllowingStateLoss(
tag: String?,
dialogFragmentFactory: () -> DialogFragment
) {
if (this == null || isDestroyed) {
return
}
val transaction = beginTransaction()
transaction.add(dialogFragmentFactory(), tag)
transaction.commitAllowingStateLoss()
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/dialog_content_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="@dimen/padding_dialog_common"
android:paddingVertical="23dp">

<TextView
android:id="@+id/text_dialog_content"
style="@style/Dialog.Content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="텍스트" />

</FrameLayout>

0 comments on commit 510946a

Please sign in to comment.