Skip to content

Commit

Permalink
REFACTOR : contentLengthVisible 제거, priceToLong 확장함수 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
minseonglove committed Jan 26, 2023
1 parent a136cb2 commit 2b4af38
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.PermissionChecker
import androidx.core.content.PermissionChecker.checkCallingOrSelfPermission
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
Expand All @@ -31,6 +32,7 @@ import com.fakedevelopers.presentation.ui.productRegistration.PriceTextWatcher.C
import com.fakedevelopers.presentation.ui.productRegistration.PriceTextWatcher.Companion.MAX_TICK_LENGTH
import com.fakedevelopers.presentation.ui.util.ApiErrorHandler
import com.fakedevelopers.presentation.ui.util.KeyboardVisibilityUtils
import com.fakedevelopers.presentation.ui.util.priceToLong
import com.fakedevelopers.presentation.ui.util.repeatOnStarted
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -42,7 +44,13 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind
private lateinit var keyboardVisibilityUtils: KeyboardVisibilityUtils
private lateinit var permissionLauncher: ActivityResultLauncher<String>

private val args: ProductRegistrationFragmentArgs by navArgs()
private val viewModel: ProductRegistrationViewModel by viewModels()
private val expirationFilter by lazy {
InputFilter { source, _, _, _, dstart, _ ->
if (source == "0" && dstart == 0) "" else source.replace(IS_NOT_NUMBER.toRegex(), "")
}
}

private val backPressedCallback by lazy {
object : OnBackPressedCallback(true) {
Expand All @@ -55,7 +63,6 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.vm = viewModel
val args: ProductRegistrationFragmentArgs by navArgs()
args.productRegistrationDto?.let {
viewModel.initState(it)
if (it.selectedImageInfo.uris.isNotEmpty()) {
Expand All @@ -72,6 +79,8 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind
super.onStart()
requireActivity().onBackPressedDispatcher.addCallback(backPressedCallback)
viewModel.refreshImages()
binding.textviewProductRegistrationContentLength.isVisible =
binding.edittextProductRegistrationContent.isFocused
}

private fun checkStoragePermission() {
Expand Down Expand Up @@ -99,9 +108,6 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind
initEditTextFilter(binding.edittextProductRegistrationHopePrice, MAX_PRICE_LENGTH)
initEditTextFilter(binding.edittextProductRegistrationOpeningBid, MAX_PRICE_LENGTH)
initEditTextFilter(binding.edittextProductRegistrationTick, MAX_TICK_LENGTH)
val expirationFilter = InputFilter { source, _, _, _, dstart, _ ->
if (source == "0" && dstart == 0) "" else source.replace(IS_NOT_NUMBER.toRegex(), "")
}
// 만료 시간 필터 등록
binding.edittextProductRegistrationExpiration.apply {
addTextChangedListener(object : TextWatcher {
Expand Down Expand Up @@ -143,16 +149,16 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind
keyboardVisibilityUtils = KeyboardVisibilityUtils(
requireActivity().window,
onHideKeyboard = {
viewModel.setContentLengthVisibility(false)
binding.textviewProductRegistrationContentLength.isVisible = false
}
)
// 본문 에딧텍스트 터치, 포커싱
binding.edittextProductRegistrationContent.apply {
setOnClickListener {
viewModel.setContentLengthVisibility(true)
binding.textviewProductRegistrationContentLength.isVisible = true
}
setOnFocusChangeListener { _, hasFocus ->
viewModel.setContentLengthVisibility(hasFocus)
binding.textviewProductRegistrationContentLength.isVisible = hasFocus
}
}
// 툴바 뒤로가기 버튼
Expand Down Expand Up @@ -207,13 +213,6 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind
}
}
}
repeatOnStarted(viewLifecycleOwner) {
viewModel.contentLengthVisible.collect {
if (binding.edittextProductRegistrationContent.isFocused != viewModel.contentLengthVisible.value) {
viewModel.setContentLengthVisibility(true)
}
}
}
// 등록 버튼
repeatOnStarted(viewLifecycleOwner) {
viewModel.condition.collectLatest {
Expand Down Expand Up @@ -243,8 +242,8 @@ class ProductRegistrationFragment : BaseFragment<FragmentProductRegistrationBind

// 희망가 <= 최소 입찰가 인지 검사
private fun checkPriceCondition(): Boolean {
val openingBid = viewModel.openingBid.value.replace(IS_NOT_NUMBER.toRegex(), "").toLongOrNull() ?: return false
val hopePrice = viewModel.hopePrice.value.replace(IS_NOT_NUMBER.toRegex(), "").toLongOrNull()
val openingBid = viewModel.openingBid.value.priceToLong() ?: return false
val hopePrice = viewModel.hopePrice.value.priceToLong()
if (hopePrice != null && hopePrice <= openingBid) {
sendSnackBar(getString(R.string.product_registration_error_minimum_bid))
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ class ProductRegistrationViewModel @Inject constructor(
private val _productRegistrationResponse = MutableEventFlow<Response<String>>()
val productRegistrationResponse = _productRegistrationResponse.asEventFlow()

private val _contentLengthVisible = MutableStateFlow(false)
val contentLengthVisible: StateFlow<Boolean> get() = _contentLengthVisible

private val _condition = MutableStateFlow(false)
val condition: StateFlow<Boolean> get() = _condition

Expand Down Expand Up @@ -182,12 +179,6 @@ class ProductRegistrationViewModel @Inject constructor(
categoryID
)

fun setContentLengthVisibility(state: Boolean) {
viewModelScope.launch {
_contentLengthVisible.emit(state)
}
}

fun setProductCategory(list: List<ProductCategoryDto>) {
category = list
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="@{vm.contentLengthVisible ? View.VISIBLE : View.INVISIBLE}"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
Expand Down

0 comments on commit 2b4af38

Please sign in to comment.