Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialog -> dialogFragment로 변경 #202

Merged
merged 14 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
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
EunhoKang marked this conversation as resolved.
Show resolved Hide resolved
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 onStart() {
super.onStart()
dialog?.setCancelable(false)
}

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
}
EunhoKang marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -18,8 +18,6 @@ import app.priceguard.ui.detail.DetailActivity
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.showConfirmationDialog
import app.priceguard.ui.util.ui.showPermissionDeniedDialog
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

Expand Down Expand Up @@ -105,25 +103,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