From a9a25581340cd85f8f43ed30a893b3db22c72ea5 Mon Sep 17 00:00:00 2001 From: Yu Jin Date: Wed, 15 May 2024 00:19:15 +0900 Subject: [PATCH] feat: use DialogUtil --- .../android/ui/mypage/MyPageActivity.kt | 90 ++++++------ .../review/delete/MyReviewDialogActivity.kt | 10 +- .../com/eatssu/android/util/DialogUtil.kt | 133 ++++++++++++++++++ 3 files changed, 183 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/com/eatssu/android/util/DialogUtil.kt diff --git a/app/src/main/java/com/eatssu/android/ui/mypage/MyPageActivity.kt b/app/src/main/java/com/eatssu/android/ui/mypage/MyPageActivity.kt index fb23892f..b37519f8 100644 --- a/app/src/main/java/com/eatssu/android/ui/mypage/MyPageActivity.kt +++ b/app/src/main/java/com/eatssu/android/ui/mypage/MyPageActivity.kt @@ -4,7 +4,6 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import androidx.activity.viewModels -import androidx.appcompat.app.AlertDialog import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import com.eatssu.android.BuildConfig @@ -19,8 +18,9 @@ import com.eatssu.android.ui.mypage.inquire.InquireActivity import com.eatssu.android.ui.mypage.myreview.MyReviewListActivity import com.eatssu.android.ui.mypage.terms.WebViewActivity import com.eatssu.android.ui.mypage.usernamechange.UserNameChangeActivity -import com.eatssu.android.util.extension.showToast -import com.eatssu.android.util.extension.startActivity +import com.eatssu.android.util.DialogUtil +import com.eatssu.android.util.showToast +import com.eatssu.android.util.startActivity import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch @@ -116,7 +116,6 @@ class MyPageActivity : BaseActivity(ActivityMyPageBinding } private fun initViewModel() { //Todo 리팩토링하기 - firebaseRemoteConfigRepository = FirebaseRemoteConfigRepository() versionViewModel = ViewModelProvider( @@ -129,61 +128,56 @@ class MyPageActivity : BaseActivity(ActivityMyPageBinding private fun showLogoutDialog() { - // 다이얼로그를 생성하기 위해 Builder 클래스 생성자를 이용해 줍니다. - val builder = AlertDialog.Builder(this) - builder.setTitle("로그아웃") - .setMessage("로그아웃 하시겠습니까?") - .setPositiveButton( - "로그아웃" - ) { _, _ -> - //로그아웃 - myPageViewModel.loginOut() - - lifecycleScope.launch { - myPageViewModel.uiState.collectLatest { - if (it.isLoginOuted) { - showToast(it.toastMessage) - startActivity() - finishAffinity() - } - + DialogUtil.createDialogWithCancelButton( + "로그아웃", + this@MyPageActivity, + "로그아웃 하시겠습니까?", + "취소", + "로그아웃" + ) { _, _ -> +// ActivityCompat.finishAffinity(this) +// exitProcess(0) + //로그아웃 + myPageViewModel.loginOut() + + lifecycleScope.launch { + myPageViewModel.uiState.collectLatest { + if (it.isLoginOuted) { + showToast(it.toastMessage) + startActivity() + finishAffinity() } + } } - .setNegativeButton("취소") { _, _ -> - } - // 다이얼로그를 띄워주기 - builder.show() + } } - private fun showSignoutDialog() { - // 다이얼로그를 생성하기 위해 Builder 클래스 생성자를 이용해 줍니다. - val builder = AlertDialog.Builder(this) - builder.setTitle("탈퇴하기") - .setMessage("탈퇴 하시겠습니까?") - .setPositiveButton( - "탈퇴하기" - ) { _, _ -> - //탈퇴처리 - myPageViewModel.signOut() - - lifecycleScope.launch { - myPageViewModel.uiState.collectLatest { - if (it.isSignOuted) { - showToast(it.toastMessage) - startActivity() - finishAffinity() - } + private fun showSignoutDialog() { + DialogUtil.createDialogWithCancelButton( + "탈퇴하기", + this@MyPageActivity, + "탈퇴 하시겠습니까?", + "취소", + "탈퇴하기" + ) { _, _ -> +// ActivityCompat.finishAffinity(this) +// exitProcess(0) + myPageViewModel.signOut() + + lifecycleScope.launch { + myPageViewModel.uiState.collectLatest { + if (it.isSignOuted) { + showToast(it.toastMessage) + startActivity() + finishAffinity() + } } } } - .setNegativeButton("취소") { _, _ -> } - // 다이얼로그를 띄워주기 - builder.show() - } private fun moveToPlayStore() { diff --git a/app/src/main/java/com/eatssu/android/ui/review/delete/MyReviewDialogActivity.kt b/app/src/main/java/com/eatssu/android/ui/review/delete/MyReviewDialogActivity.kt index 02045e13..74e2a7fb 100644 --- a/app/src/main/java/com/eatssu/android/ui/review/delete/MyReviewDialogActivity.kt +++ b/app/src/main/java/com/eatssu/android/ui/review/delete/MyReviewDialogActivity.kt @@ -43,9 +43,15 @@ class MyReviewDialogActivity : AppCompatActivity() { } private fun showReviewDeleteDialog() { - DialogUtil.createDialog( - "리뷰 삭제", "작성한 리뷰를 삭제하시겠습니까?", this@MyReviewDialogActivity, + DialogUtil.createDialogWithCancelButton( + "리뷰 삭제", + this@MyReviewDialogActivity, + "작성한 리뷰를 삭제하시겠습니까?", + "취소", + "삭제" ) { _, _ -> +// ActivityCompat.finishAffinity(this) +// exitProcess(0) viewModel.postData(reviewId) finish() } diff --git a/app/src/main/java/com/eatssu/android/util/DialogUtil.kt b/app/src/main/java/com/eatssu/android/util/DialogUtil.kt new file mode 100644 index 00000000..954dd109 --- /dev/null +++ b/app/src/main/java/com/eatssu/android/util/DialogUtil.kt @@ -0,0 +1,133 @@ +package com.eatssu.android.util + +import android.app.AlertDialog +import android.content.Context +import android.content.DialogInterface + +object DialogUtil { + +// fun createDialog(positiveMessage: String, +// message: String, +// context: Context, +// listener: DialogInterface.OnClickListener? = null +// ) { +// var builder: AlertDialog.Builder = AlertDialog.Builder(context) +// builder.setMessage(message) +// builder.setPositiveButton(positiveMessage, listener) { dialogInterface, i -> +// dialogInterface.dismiss() +// } +// builder.create().show() +// } + + + fun createDialogWithTitleAndMessage( + title: String, + message: String, + context: Context, + listener: DialogInterface.OnClickListener? = null, + ) { + var builder: AlertDialog.Builder = AlertDialog.Builder(context) + builder.setTitle(title) + builder.setMessage(message) + builder.setPositiveButton("확인", listener) + builder.create().show() + } + + + fun createDialogWithMessage(message: String, context: Context) { + var builder: AlertDialog.Builder = AlertDialog.Builder(context) + builder.setMessage(message) + builder.setPositiveButton("확인") { dialogInterface, i -> + dialogInterface.dismiss() + } + builder.create().show() + } + +// fun createDialogWithTitleAndMessage( +// title: String, +// message: String, +// context: Context, +// listener: DialogInterface.OnClickListener? = null, +// ) { +// var builder: AlertDialog.Builder = AlertDialog.Builder(context) +// builder.setTitle(title) +// builder.setMessage(message) +// builder.setPositiveButton("확인", listener) +// builder.create().show() +// } + + fun createDialogWithCancelButton( + title: String, + context: Context, + message: String? = null, + cancelText: String, + confirmText: String, + positiveButtonListener: DialogInterface.OnClickListener? = null, + negativeButtonListener: DialogInterface.OnClickListener? = null, + ) { + var builder: AlertDialog.Builder = AlertDialog.Builder(context) + builder.setTitle(title) + builder.setCancelable(true) + if (message != null) builder.setMessage(message) + builder.setNegativeButton(cancelText, negativeButtonListener) + builder.setPositiveButton(confirmText, positiveButtonListener) + builder.create().show() + } + + fun createDialogWithCancelButtonAndMessage( + title: String, + message: String, + context: Context, + cancelText: String, + confirmText: String, + listener: DialogInterface.OnClickListener? = null, + ) { + var builder: AlertDialog.Builder = AlertDialog.Builder(context) + builder.setTitle(title) + builder.setMessage(message) + builder.setCancelable(true) + builder.setNegativeButton( + cancelText, + DialogInterface.OnClickListener { dialogInterface, i -> + dialogInterface.cancel() + }, + ) + builder.setPositiveButton(confirmText, listener) + builder.create().show() + } + +// fun createDialogWithEditText( +// context: Context, +// title: String, +// callback: (String) -> Unit, +// editText: EditText? = null, +// ) { +// val builder = AlertDialog.Builder(context) +// builder.setTitle(title) +// editText?.let { +// val container = FrameLayout(context) +// val params = FrameLayout.LayoutParams( +// ViewGroup.LayoutParams.MATCH_PARENT, +// ViewGroup.LayoutParams.WRAP_CONTENT, +// ) +//// params.leftMargin = context.resources.getDimensionPixelSize(R.dimen.dialog_margin) +//// params.rightMargin = context.resources.getDimensionPixelSize(R.dimen.dialog_margin) +// it.layoutParams = params +// container.addView(it) +// builder.setView(container) +// } +// builder.setPositiveButton( +// "확인", +// ) { dialog, which -> +// dialog.dismiss() +// editText?.let { +// val strText = editText.text.toString() +// callback.invoke(strText) +// } +// } +// builder.setNegativeButton( +// "취소", +// ) { dialog, which -> dialog.cancel() } +// builder.show() +// } +}