Skip to content

Commit

Permalink
refactor: BindingAdapter 분리 및 리팩토링 #469 (#472)
Browse files Browse the repository at this point in the history
* refactor: BindingAdapter 파일 분리

* refactor: ButtonBindingAdapter 리팩토링

* refactor: ImageBindingAdapter 리팩토링

* refactor: TextBindingAdapter 리팩토링

* refactor: ExtBindingAdapter 리팩토링

* refactor: 오전, 오후 텍스트 strings.xml로 분리

* refactor: 매직 넘버 상수로 분리

* style: EtxBindingAdapters -> BindingAdapters.kt

* chore: 불필요한 메서드 제거

* refactor: 상수화 및 String formatting

* fix: 추억 수정 화면 dateRangePicker 중복 버그 해결

* refactor: 추억 기간 선택 시 visibility 바인딩 어댑터 적용

* refactor: okhttp3 import 제거 및 scrollToBottom rename
  • Loading branch information
s6m1n authored Oct 14, 2024
1 parent 57b28da commit 3ef1e5d
Show file tree
Hide file tree
Showing 24 changed files with 557 additions and 688 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.on.staccato.presentation.bindingadapter

import android.net.Uri
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.ScrollView
import androidx.databinding.BindingAdapter
import com.on.staccato.presentation.timeline.model.TimelineUiModel

@BindingAdapter("visibleOrGone")
fun View.setVisibility(isVisible: Boolean?) {
visibility =
if (isVisible == true) {
View.VISIBLE
} else {
View.GONE
}
}

@BindingAdapter("scrollToBottom")
fun ScrollView.setScrollToBottom(isScrollable: Boolean) {
if (isScrollable) {
post { fullScroll(ScrollView.FOCUS_DOWN) }
}
}

@BindingAdapter(value = ["visibilityByThumbnailUri", "visibilityByThumbnailUrl"])
fun View.setThumbnail(
thumbnailUri: Uri?,
thumbnailUrl: String?,
) {
visibility =
if (thumbnailUri == null && thumbnailUrl == null) {
View.VISIBLE
} else {
View.GONE
}
}

@BindingAdapter(value = ["visibilityByThumbnailUri", "visibilityByThumbnailUrl"])
fun ProgressBar.setThumbnailLoadingProgressBar(
thumbnailUri: Uri?,
thumbnailUrl: String?,
) {
visibility =
if (thumbnailUri != null && thumbnailUrl == null) {
View.VISIBLE
} else {
View.GONE
}
}

@BindingAdapter(
value = ["visibilityByTimeline", "visibilityByLoading"],
)
fun View.setTimelineEmptyViewVisible(
timeLine: List<TimelineUiModel>? = null,
isTimelineLoading: Boolean,
) {
visibility = if (isTimeLineEmpty(timeLine, isTimelineLoading)) View.VISIBLE else View.GONE
}

@BindingAdapter(
value = ["visibilityByTimeline", "visibilityByLoading"],
)
fun ViewGroup.setMemoryAddButtonVisible(
timeLine: List<TimelineUiModel>? = null,
isTimelineLoading: Boolean,
) {
visibility = if (isTimeLineEmpty(timeLine, isTimelineLoading)) View.GONE else View.VISIBLE
}

private fun isTimeLineEmpty(
timeLine: List<TimelineUiModel>?,
isTimelineLoading: Boolean,
) = timeLine.isNullOrEmpty() && isTimelineLoading.not()
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.on.staccato.presentation.bindingadapter

import android.widget.Button
import android.widget.ImageButton
import androidx.databinding.BindingAdapter
import com.google.android.material.button.MaterialButton
import com.on.staccato.R
import com.on.staccato.domain.model.MemoryCandidate
import com.on.staccato.presentation.momentcreation.model.AttachedPhotosUiModel
import java.time.LocalDate
import java.time.LocalDateTime

@BindingAdapter("loginButtonEnabled")
fun Button.setLoginButtonEnabled(nickName: String?) {
isEnabled =
if (nickName.isNullOrBlank()) {
setTextColor(resources.getColor(R.color.gray4, null))
false
} else {
setTextColor(resources.getColor(R.color.white, null))
true
}
}

@BindingAdapter(
value = ["staccatoTitle", "staccatoAddress", "staccatoVisitedAt", "staccatoPhotos", "staccatoMemory"],
)
fun Button.setStaccatoSaveButtonEnabled(
staccatoTitle: String?,
staccatoAddress: String?,
staccatoVisitedAt: LocalDateTime?,
staccatoPhotos: AttachedPhotosUiModel?,
staccatoMemory: MemoryCandidate?,
) {
isEnabled =
if (staccatoTitle.isNullOrBlank() ||
staccatoAddress == null ||
staccatoMemory == null ||
staccatoVisitedAt == null ||
staccatoPhotos?.isLoading() == true
) {
setTextColor(resources.getColor(R.color.gray4, null))
false
} else {
setTextColor(resources.getColor(R.color.white, null))
true
}
}

@BindingAdapter(
value = ["memoryTitle", "memoryStartDate", "memoryEndDate", "isPeriodActive", "isPhotoUploading"],
)
fun Button.setMemorySaveButtonEnabled(
memoryTitle: String?,
memoryStartDate: LocalDate?,
memoryEndDate: LocalDate?,
isPeriodActive: Boolean,
isPhotoUploading: Boolean?,
) {
val isPeriodNotExistent = (memoryStartDate == null) || (memoryEndDate == null)
val isPeriodRequirementsInvalid = isPeriodActive && isPeriodNotExistent
isEnabled =
if (memoryTitle.isNullOrBlank() || isPhotoUploading == true || isPeriodRequirementsInvalid) {
setTextColor(resources.getColor(R.color.gray4, null))
false
} else {
setTextColor(resources.getColor(R.color.white, null))
true
}
}

@BindingAdapter("visitedAtSelectButtonEnabled")
fun Button.setVisitedAtSelectButtonEnabled(years: List<Int>?) {
isEnabled =
if (years.isNullOrEmpty()) {
setTextColor(resources.getColor(R.color.gray4, null))
false
} else {
setTextColor(resources.getColor(R.color.white, null))
true
}
}

@BindingAdapter("recoveryButtonEnabled")
fun Button.setRecoveryButtonEnabled(recoveryCode: String?) {
isEnabled =
if (recoveryCode.isNullOrBlank() || recoveryCode.length < MAX_RECOVERY_CODE) {
setTextColor(resources.getColor(R.color.gray4, null))
false
} else {
setTextColor(resources.getColor(R.color.white, null))
true
}
}

@BindingAdapter("currentLocationButtonLoading")
fun MaterialButton.setCurrentLocationButtonLoading(isLoading: Boolean?) {
isClickable = isLoading == false
if (isLoading == true) {
setText(R.string.all_empty)
setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
} else {
setText(R.string.visit_creation_load_current_location)
}
}

@BindingAdapter("sendButtonEnabled")
fun ImageButton.setSendButtonEnabled(value: String?) {
isEnabled = !value.isNullOrBlank()
}

private const val MAX_RECOVERY_CODE = 36
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.on.staccato.presentation.bindingadapter

import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.load
import coil.transform.RoundedCornersTransformation

@BindingAdapter("selected")
fun ImageView.setSelected(selected: Boolean) {
isSelected = selected
}

@BindingAdapter(
value = ["coilImageUrl", "coilPlaceHolder"],
)
fun ImageView.loadCoilImage(
url: String?,
placeHolder: Drawable? = null,
) {
load(url) {
placeholder(placeHolder)
error(placeHolder)
}
}

@BindingAdapter(
value = ["coilCircleImageUrl", "coilPlaceHolder"],
)
fun ImageView.loadCoilCircleImage(
url: String?,
placeHolder: Drawable? = null,
) {
load(url) {
placeholder(placeHolder)
transformations(RoundedCornersTransformation(1000f))
error(placeHolder)
}
}

@BindingAdapter(
value = ["coilRoundedCornerImageUrl", "coilRoundedCornerPlaceHolder", "coilRoundingRadius"],
)
fun ImageView.setCoilRoundedCornerImage(
url: String?,
placeHolder: Drawable? = null,
roundingRadius: Float,
) {
load(url) {
placeholder(placeHolder)
transformations(RoundedCornersTransformation(roundingRadius))
error(placeHolder)
}
}

@BindingAdapter(
value = ["coilRoundedCornerImageUrl", "coilRoundedCornerImageUri", "coilRoundedCornerPlaceHolder", "coilRoundingRadius"],
)
fun ImageView.setCoilRoundedCornerImageWithUri(
url: String?,
uri: Uri?,
placeHolder: Drawable? = null,
roundingRadius: Float,
) {
val image = uri ?: url
load(image) {
placeholder(placeHolder)
transformations(RoundedCornersTransformation(roundingRadius))
error(placeHolder)
}
}

@BindingAdapter(
value = [
"colorImageResource",
"grayImageResource",
],
)
fun ImageView.setImageResourceWithId(
colorResId: Int,
grayResId: Int,
) {
setImageResource(
if (isSelected) {
colorResId
} else {
grayResId
},
)
}
Loading

0 comments on commit 3ef1e5d

Please sign in to comment.