Skip to content

Commit

Permalink
Refactor/#486 금액 관련 처리 (#493)
Browse files Browse the repository at this point in the history
* refactor: 금액 입력 시 단위가 보일 수 있도록 수정 및 금액 초과 입력 시에 금액 전체가 사라지지 않고 입력값이 반영이 안되도록 수정

* refactor: 필요없는 코드 제거

* refactor: 입력 금액 오른쪽 정렬

* fix: 글 등록시 앱이 터지는 오류 수정
  • Loading branch information
Choisehyeon authored Oct 17, 2023
1 parent 0ebec08 commit d772c42
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
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}"
android:textAlignment="textEnd"
android:textColor="@color/gray_434343"
android:textColorHint="@color/gray_615f5f"
android:textSize="14sp"
Expand Down

0 comments on commit d772c42

Please sign in to comment.