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 all 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,81 @@
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
private lateinit var negativeButtonText: String

private var resultListener: OnDialogResultListener? = null

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
initDialogInfo()

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

DialogConfirmAction.CUSTOM -> {
resultListener?.onDialogResult(true)
}

DialogConfirmAction.NOTHING -> {}
}
dismiss()
}
}.create()
}

private fun initDialogInfo() {
title = arguments?.getString("title") ?: ""
message = arguments?.getString("message") ?: ""

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

negativeButtonText = if (action == DialogConfirmAction.CUSTOM) {
getString(R.string.cancel)
} else {
""
}
}

override fun onStart() {
super.onStart()

if (action == DialogConfirmAction.NOTHING || action == DialogConfirmAction.CUSTOM) {
dialog?.setCancelable(true)
} else {
dialog?.setCancelable(false)
}
}

fun setOnDialogResultListener(listener: OnDialogResultListener) {
resultListener = listener
}

interface OnDialogResultListener {
fun onDialogResult(result: Boolean)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package app.priceguard.ui

import android.app.Dialog
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,7 @@
package app.priceguard.ui.data

enum class DialogConfirmAction {
NOTHING,
FINISH,
CUSTOM
}
Loading