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

Refactor/#486 금액 관련 처리 #493

Merged
merged 5 commits into from
Oct 17, 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
Expand Up @@ -7,6 +7,9 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
Expand Down Expand Up @@ -36,6 +39,8 @@ class PostEditorActivity : BaseActivity<ActivityPostEditorBinding, PostEditorVie

private var cameraUri: Uri? = null

private var price = ""

override val viewModel: PostEditorViewModel by viewModels()

private val adapter: PostEditorImagesAdapter by lazy {
Expand Down Expand Up @@ -81,6 +86,26 @@ class PostEditorActivity : BaseActivity<ActivityPostEditorBinding, PostEditorVie
intent.getParcelableExtraCompat(KEY_POST_EDITOR_POST) as? PostUiModel
}

private val textWatcher by lazy {
object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) =
Unit

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (!TextUtils.isEmpty(s.toString()) && s.toString() != price) {
viewModel.checkPriceValidate(
s.toString().replace(",", ""),
start,
start + count,
count,
)
}
}

override fun afterTextChanged(s: Editable?) = Unit
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
Expand All @@ -91,6 +116,7 @@ class PostEditorActivity : BaseActivity<ActivityPostEditorBinding, PostEditorVie
setObserver()
setPostIfUpdate()
setClickListener()
setPriceChangedListener()
}

override fun onTouchEvent(event: MotionEvent): Boolean {
Expand Down Expand Up @@ -141,7 +167,18 @@ class PostEditorActivity : BaseActivity<ActivityPostEditorBinding, PostEditorVie
viewModel.isPostPriceValid.observe(this) { isValid ->
if (!isValid) {
binding.root.makeSnackbar(this.getString(R.string.dialog_input_price_error_message))
binding.etPostPrice.setText("")
binding.etPostPrice.removeTextChangedListener(textWatcher)
binding.etPostPrice.setText(price)
binding.etPostPrice.setSelection(binding.etPostPrice.text.length)
binding.etPostPrice.addTextChangedListener(textWatcher)
} else {
price =
getString(
R.string.all_price,
binding.etPostPrice.text.toString().replace(",", "").toInt(),
)
binding.etPostPrice.setText(price)
binding.etPostPrice.setSelection(price.length)
}
}
viewModel.isUpdateAble.observe(this) { isAble ->
Expand Down Expand Up @@ -208,7 +245,7 @@ class PostEditorActivity : BaseActivity<ActivityPostEditorBinding, PostEditorVie
loadingDialog.show(supportFragmentManager, "LoadingDialog")
val postTitle = binding.etPostTitle.text.toString()
val postContent = binding.etPostContent.text.toString().ifBlank { "" }
val postPrice = binding.etPostPrice.text.toString().toInt()
val postPrice = binding.etPostPrice.text.toString().replace(",", "").toInt()
val postEditor = PostEditor(postTitle, postContent, postPrice)
when (originActivityKey) {
POST_CODE -> viewModel.savePost(this, postEditor)
Expand Down Expand Up @@ -285,6 +322,10 @@ class PostEditorActivity : BaseActivity<ActivityPostEditorBinding, PostEditorVie
finish()
}

private fun setPriceChangedListener() {
binding.etPostPrice.addTextChangedListener(textWatcher)
}

override fun onDestroy() {
super.onDestroy()
if (!isChangingConfigurations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ class PostEditorViewModel @Inject constructor(
fun checkPriceValidate(price: CharSequence, start: Int, end: Int, count: Int) {
val postPrice = price.toString()
runCatching {
if (postPrice != ConsumptionDialog.BLANK) postPrice.toInt()
if (postPrice != ConsumptionDialog.BLANK) {
postPrice.toInt()
}
}.onSuccess {
_isPostPriceValid.value = true
}.onFailure {
_isPostPriceValid.value = false
throw it
}
}

Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/activity_post_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
android:background="@android:color/transparent"
android:hint="@string/post_editor_price_hint"
android:inputType="number"
android:onTextChanged="@{viewModel.checkPriceValidate}"
android:text="@{viewModel.postEditor.price}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

android:textAlignment="textEnd"도 추가해보는건 어떨까요?
123,000 원 이 아니라
123,000원 으로 오른쪽 정렬되어 가격이 보입니다 !

android:textAlignment="textEnd"
android:textColor="@color/gray_434343"
android:textColorHint="@color/gray_615f5f"
android:textSize="14sp"
Expand Down