Skip to content

Commit

Permalink
refactor: dialog -> dialogFragment로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
ootr47 committed Dec 9, 2023
1 parent 38b0cf0 commit d0aa5ae
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package app.priceguard.ui

import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import app.priceguard.R
import app.priceguard.ui.data.DialogConfirmAction
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class ConfirmDialogFragment : DialogFragment() {

private lateinit var title: String
private lateinit var message: String
private lateinit var action: DialogConfirmAction

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
title = arguments?.getString("title") ?: ""
message = arguments?.getString("message") ?: ""

val actionString = arguments?.getString("actionString")
actionString?.let { actionString ->
action = DialogConfirmAction.valueOf(actionString)
}?.run {
DialogConfirmAction.NOTHING
}

return MaterialAlertDialogBuilder(
requireActivity(),
R.style.ThemeOverlay_App_MaterialAlertDialog
).apply {
setTitle(title)
setMessage(message)
setPositiveButton(getString(R.string.confirm)) { _, _ ->
when (action) {
DialogConfirmAction.FINISH -> requireActivity().finish()
DialogConfirmAction.NOTHING -> {}
}
dismiss()
}
}.create()
}

override fun onStart() {
super.onStart()
dialog?.setCancelable(false)
}
}
45 changes: 45 additions & 0 deletions android/app/src/main/java/app/priceguard/ui/ErrorDialogFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package app.priceguard.ui

import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import app.priceguard.R
import app.priceguard.data.repository.token.TokenRepository
import app.priceguard.ui.login.LoginActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ErrorDialogFragment : DialogFragment() {

@Inject
lateinit var tokenRepository: TokenRepository

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(
requireActivity(),
R.style.ThemeOverlay_App_MaterialAlertDialog
).apply {
setTitle(getString(R.string.permission_denied_title))
setMessage(getString(R.string.permission_denied_message))
setPositiveButton(getString(R.string.confirm)) { _, _ -> goBackToLoginActivity(tokenRepository) }
}.create()
}

override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
goBackToLoginActivity(tokenRepository)
}

private fun goBackToLoginActivity(tokenRepository: TokenRepository) {
CoroutineScope(Dispatchers.IO).launch { tokenRepository.clearTokens() }
val intent = Intent(requireActivity(), LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
requireActivity().finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import app.priceguard.data.repository.token.TokenRepository
import app.priceguard.databinding.FragmentRegisterItemLinkBinding
import app.priceguard.ui.home.HomeActivity
import app.priceguard.ui.util.lifecycle.repeatOnStarted
import app.priceguard.ui.util.ui.showPermissionDeniedDialog
import app.priceguard.ui.util.ui.showDialogWithLogout
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

Expand Down Expand Up @@ -111,7 +111,7 @@ class RegisterItemLinkFragment : Fragment() {
is RegisterItemLinkViewModel.RegisterLinkEvent.FailureVerification -> {
when (event.errorType) {
ProductErrorState.PERMISSION_DENIED -> {
requireActivity().showPermissionDeniedDialog(tokenRepository)
showDialogWithLogout()
}

ProductErrorState.INVALID_REQUEST -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app.priceguard.ui.additem.setprice

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.view.LayoutInflater
Expand All @@ -16,10 +15,10 @@ import app.priceguard.data.repository.product.ProductErrorState
import app.priceguard.data.repository.token.TokenRepository
import app.priceguard.databinding.FragmentSetTargetPriceBinding
import app.priceguard.ui.additem.setprice.SetTargetPriceViewModel.SetTargetPriceEvent
import app.priceguard.ui.home.HomeActivity
import app.priceguard.ui.data.DialogConfirmAction
import app.priceguard.ui.util.lifecycle.repeatOnStarted
import app.priceguard.ui.util.ui.showPermissionDeniedDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import app.priceguard.ui.util.ui.showDialogWithAction
import app.priceguard.ui.util.ui.showDialogWithLogout
import com.google.android.material.slider.Slider
import com.google.android.material.slider.Slider.OnSliderTouchListener
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -164,34 +163,36 @@ class SetTargetPriceFragment : Fragment() {
setTargetPriceViewModel.event.collect { event ->
when (event) {
is SetTargetPriceEvent.SuccessProductAdd -> {
showActivityFinishDialog(
showDialogWithAction(
getString(R.string.success_add),
getString(R.string.success_add_message)
getString(R.string.success_add_message),
DialogConfirmAction.FINISH
)
}

is SetTargetPriceEvent.SuccessPriceUpdate -> {
showActivityFinishDialog(
showDialogWithAction(
getString(R.string.success_update),
getString(R.string.success_update_message)
getString(R.string.success_update_message),
DialogConfirmAction.FINISH
)
}

is SetTargetPriceEvent.FailurePriceAdd -> {
when (event.errorType) {
ProductErrorState.EXIST -> {
showActivityFinishDialog(
showDialogWithAction(
getString(R.string.error_add_product),
getString(R.string.exist_product)
)
}

ProductErrorState.PERMISSION_DENIED -> {
requireActivity().showPermissionDeniedDialog(tokenRepository)
showDialogWithLogout()
}

else -> {
showActivityFinishDialog(
showDialogWithAction(
getString(R.string.error),
getString(R.string.retry)
)
Expand All @@ -202,11 +203,11 @@ class SetTargetPriceFragment : Fragment() {
is SetTargetPriceEvent.FailurePriceUpdate -> {
when (event.errorType) {
ProductErrorState.PERMISSION_DENIED -> {
requireActivity().showPermissionDeniedDialog(tokenRepository)
showDialogWithLogout()
}

else -> {
showActivityFinishDialog(
showDialogWithAction(
getString(R.string.error_patch_price),
getString(R.string.retry)
)
Expand All @@ -218,26 +219,6 @@ class SetTargetPriceFragment : Fragment() {
}
}

private fun showActivityFinishDialog(title: String, message: String) {
MaterialAlertDialogBuilder(requireActivity(), R.style.ThemeOverlay_App_MaterialAlertDialog)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.confirm) { _, _ -> goToHomeActivity() }
.setOnDismissListener { goToHomeActivity() }
.create()
.show()
}

private fun goToHomeActivity() {
val activityIntent = requireActivity().intent
if (activityIntent?.action == Intent.ACTION_SEND) {
val intent = Intent(requireActivity(), HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
}
requireActivity().finish()
}

private fun FragmentSetTargetPriceBinding.updateSlideValueWithPrice(
targetPrice: Float,
percent: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app.priceguard.ui.data

enum class DialogConfirmAction {
NOTHING,
FINISH
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import app.priceguard.data.repository.token.TokenRepository
import app.priceguard.databinding.ActivityDetailBinding
import app.priceguard.materialchart.data.GraphMode
import app.priceguard.ui.additem.AddItemActivity
import app.priceguard.ui.data.DialogConfirmAction
import app.priceguard.ui.home.HomeActivity
import app.priceguard.ui.util.lifecycle.repeatOnStarted
import app.priceguard.ui.util.ui.showConfirmationDialog
import app.priceguard.ui.util.ui.showPermissionDeniedDialog
import app.priceguard.ui.util.ui.showConfirmDialog
import app.priceguard.ui.util.ui.showDialogWithLogout
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
Expand Down Expand Up @@ -129,7 +130,11 @@ class DetailActivity : AppCompatActivity() {
val productCodeFromDeepLink = deepLink?.getQueryParameter("code")

if (productCode == null && productCodeFromDeepLink == null) {
showDialogAndExit(getString(R.string.error), getString(R.string.invalid_access))
showConfirmDialog(
getString(R.string.error),
getString(R.string.invalid_access),
DialogConfirmAction.FINISH
)
return
}

Expand Down Expand Up @@ -165,20 +170,22 @@ class DetailActivity : AppCompatActivity() {
productDetailViewModel.event.collect { event ->
when (event) {
ProductDetailViewModel.ProductDetailEvent.Logout -> {
showDialogAndExit(getString(R.string.error), getString(R.string.logged_out))
showDialogWithLogout()
}

ProductDetailViewModel.ProductDetailEvent.NotFound -> {
showDialogAndExit(
showConfirmDialog(
getString(R.string.error),
getString(R.string.product_not_found)
getString(R.string.product_not_found),
DialogConfirmAction.FINISH
)
}

ProductDetailViewModel.ProductDetailEvent.UnknownError -> {
showDialogAndExit(
showConfirmDialog(
getString(R.string.error),
getString(R.string.undefined_error)
getString(R.string.undefined_error),
DialogConfirmAction.FINISH
)
}

Expand All @@ -199,27 +206,30 @@ class DetailActivity : AppCompatActivity() {
is ProductDetailViewModel.ProductDetailEvent.DeleteFailed -> {
when (event.errorType) {
ProductErrorState.NOT_FOUND -> {
showConfirmationDialog(
showConfirmDialog(
getString(R.string.delete_product_failed),
getString(R.string.product_not_found)
getString(R.string.product_not_found),
DialogConfirmAction.FINISH
)
}

ProductErrorState.INVALID_REQUEST -> {
showConfirmationDialog(
showConfirmDialog(
getString(R.string.delete_product_failed),
getString(R.string.invalid_request)
getString(R.string.invalid_request),
DialogConfirmAction.FINISH
)
}

ProductErrorState.PERMISSION_DENIED -> {
showPermissionDeniedDialog(tokenRepository)
showDialogWithLogout()
}

else -> {
showConfirmationDialog(
showConfirmDialog(
getString(R.string.delete_product_failed),
getString(R.string.undefined_error)
getString(R.string.undefined_error),
DialogConfirmAction.FINISH
)
}
}
Expand Down Expand Up @@ -254,7 +264,11 @@ class DetailActivity : AppCompatActivity() {
}

private fun goToHomeActivityIfDeepLinked() {
if (intent.data?.getQueryParameter("code") != null || intent.getBooleanExtra("directed", false)) {
if (intent.data?.getQueryParameter("code") != null || intent.getBooleanExtra(
"directed",
false
)
) {
val intent = Intent(this@DetailActivity, HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
Expand All @@ -276,16 +290,6 @@ class DetailActivity : AppCompatActivity() {
.show()
}

private fun showDialogAndExit(title: String, message: String) {
MaterialAlertDialogBuilder(this, R.style.ThemeOverlay_App_MaterialAlertDialog)
.setTitle(title)
.setMessage(message)
.setPositiveButton(getString(R.string.confirm)) { _, _ -> finish() }
.setCancelable(false)
.create()
.show()
}

private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import app.priceguard.ui.home.ProductSummaryAdapter
import app.priceguard.ui.home.ProductSummaryClickListener
import app.priceguard.ui.util.lifecycle.repeatOnStarted
import app.priceguard.ui.util.ui.disableAppBarRecyclerView
import app.priceguard.ui.util.ui.showConfirmationDialog
import app.priceguard.ui.util.ui.showPermissionDeniedDialog
import app.priceguard.ui.util.ui.showDialogWithAction
import app.priceguard.ui.util.ui.showDialogWithLogout
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

Expand Down Expand Up @@ -117,25 +117,25 @@ class ProductListFragment : Fragment() {
productListViewModel.events.collect { event ->
when (event) {
ProductErrorState.PERMISSION_DENIED -> {
requireActivity().showPermissionDeniedDialog(tokenRepository)
showDialogWithLogout()
}

ProductErrorState.INVALID_REQUEST -> {
requireActivity().showConfirmationDialog(
showDialogWithAction(
getString(R.string.product_list_failed),
getString(R.string.invalid_request)
)
}

ProductErrorState.NOT_FOUND -> {
requireActivity().showConfirmationDialog(
showDialogWithAction(
getString(R.string.product_list_failed),
getString(R.string.not_found)
)
}

else -> {
requireActivity().showConfirmationDialog(
showDialogWithAction(
getString(R.string.product_list_failed),
getString(R.string.undefined_error)
)
Expand Down
Loading

0 comments on commit d0aa5ae

Please sign in to comment.