Skip to content

Commit

Permalink
Feat: 커리어 수정하기 대외활동 버튼 활성화 조건 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kimyujin-com committed Feb 16, 2024
1 parent aaee90d commit d7232cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CareerEditActivityFragment : Fragment() {
}
})
//버튼 활성화
viewModel.isFilledAllOptions.observe(viewLifecycleOwner) { isEnabled ->
viewModel.isFilledAnyOptions.observe(viewLifecycleOwner) { isEnabled ->
binding?.btnCareerEdit?.isEnabled = isEnabled
binding?.btnCareerEdit?.backgroundTintList =
ContextCompat.getColorStateList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class CareerEditActivityViewModel : ViewModel() {
val endDate: MutableLiveData<String> = MutableLiveData()
val fileAddedEvent: MutableLiveData<Boolean> = MutableLiveData()
val addFiles: MutableList<MultipartBody.Part> = mutableListOf()
val addFilesLive: MutableLiveData<Boolean> = MutableLiveData<Boolean>().apply {
value = addFiles.isNotEmpty()
}

init {
studentId.value = 0
Expand All @@ -49,18 +52,19 @@ class CareerEditActivityViewModel : ViewModel() {
startDate.value = ""
endDate.value = ""
addFiles.clear()
addFilesLive.value = addFiles.isNotEmpty()
}

val isFilledAllOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
value = areBothFieldsFilled()
addSource(startDate) { value = areBothFieldsFilled() }
addSource(endDate) { value = areBothFieldsFilled() }
val isFilledAnyOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
value = isAnyFieldFilled()
addSource(title) { value = isAnyFieldFilled() }
addSource(startDate) { value = isAnyFieldFilled() }
addSource(endDate) { value = isAnyFieldFilled() }
addSource(addFilesLive) { value = isAnyFieldFilled() }
}

private fun areBothFieldsFilled(): Boolean {
return (startDate.value.isNullOrBlank() && endDate.value.isNullOrBlank()) || (isDateValid(
startDate.value
) && isDateValid(endDate.value))
private fun isAnyFieldFilled(): Boolean {
return !title.value.isNullOrBlank() || !startDate.value.isNullOrBlank() || !endDate.value.isNullOrBlank() || (addFilesLive.value ?: false)
}

private fun isDateValid(date: String?): Boolean {
Expand All @@ -71,6 +75,7 @@ class CareerEditActivityViewModel : ViewModel() {
val requestFile = RequestBody.create("image/*".toMediaTypeOrNull(), file)
val body = MultipartBody.Part.createFormData("addFiles", file.name, requestFile)
addFiles.add(body)
addFilesLive.value = addFiles.isNotEmpty()
fileAddedEvent.value = true
}

Expand Down Expand Up @@ -99,6 +104,7 @@ class CareerEditActivityViewModel : ViewModel() {
val requestFile: RequestBody = RequestBody.create(mimeType?.toMediaTypeOrNull(), file)
val filePart = MultipartBody.Part.createFormData("addFiles", fileName, requestFile)
addFiles.add(filePart)
addFilesLive.value = addFiles.isNotEmpty()
fileAddedEvent.value = true
}

Expand Down

0 comments on commit d7232cf

Please sign in to comment.