Skip to content

Commit

Permalink
Feat: 커리어 대외활동 더하기/수정하기 버튼 조건에 파일 사이즈 제한 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kimyujin-com committed Feb 17, 2024
1 parent 7cf2aaa commit 4f6a65f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,52 @@ class CareerAddActivityViewModel : ViewModel() {
val endDate: MutableLiveData<String> = MutableLiveData()
val fileAddedEvent: MutableLiveData<Boolean> = MutableLiveData()
val imageList: MutableList<MultipartBody.Part> = mutableListOf()
val totalFileSize = MutableLiveData<Long>(0)

init {
title.value = ""
startDate.value = ""
endDate.value = ""
imageList.clear()
totalFileSize.value = 0
}

fun init() {
title.value = ""
startDate.value = ""
endDate.value = ""
imageList.clear()
totalFileSize.value = 0
}

/* 버튼 활성화 기능 */
val isFilledAllOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
addSource(title) { value = areBothFieldsFilled() }
addSource(startDate) { value = areBothFieldsFilled() }
addSource(endDate) { value = areBothFieldsFilled() }
addSource(totalFileSize) { value = areBothFieldsFilled() }
}

private fun areBothFieldsFilled(): Boolean {
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && isDateValid(
startDate.value
) && isDateValid(endDate.value)
) && isDateValid(endDate.value) && isFileSizeValid()
}

private fun isDateValid(date: String?): Boolean {
return !date.isNullOrBlank() && date.length == 8
}

//파일 사이즈가 30MB를 넘지 않는지 체크
private fun isFileSizeValid(): Boolean {
return totalFileSize.value ?: 0 <= 30 * 1024 * 1024
}

fun addImageFile(file: File) {
val requestFile = RequestBody.create("image/*".toMediaTypeOrNull(), file)
val body = MultipartBody.Part.createFormData("image", file.name, requestFile)
imageList.add(body)
calculateTotalFileSize() //파일 사이즈 계산
fileAddedEvent.value = true
}

Expand Down Expand Up @@ -93,9 +103,20 @@ class CareerAddActivityViewModel : ViewModel() {
val filePart = MultipartBody.Part.createFormData("image", fileName, requestFile)

imageList.add(filePart)
calculateTotalFileSize() //파일 사이즈 계산
fileAddedEvent.value = true
}

//파일 사이즈 측정
fun calculateTotalFileSize() {
var totalSize: Long = 0
for (filePart in imageList) {
val file = filePart.body
totalSize += file?.contentLength() ?: 0
}
totalFileSize.value = totalSize
}

//API에 전송할 데이터를 포함하는 RequestDto 생성 함수
fun createRequestDto(): ActivityDto? {
val startDateString = startDate.value
Expand Down Expand Up @@ -135,18 +156,6 @@ class CareerAddActivityViewModel : ViewModel() {
val requestDtoPart: RequestBody =
requestDtoJson.toRequestBody("application/json".toMediaTypeOrNull())

//파일 사이즈 측정
fun calculateTotalFileSize(): Long {
var totalSize: Long = 0
for (filePart in imageList) {
val file = filePart.body
totalSize += file?.contentLength() ?: 0
}
return totalSize
}

val totalFileSize = calculateTotalFileSize()
Log.d("File Size", "Total File Size: $totalFileSize bytes")
careerApiService.addCareer(imageList, requestDtoPart)
.enqueue(object : Callback<AddCareerResponse> {
override fun onResponse(
Expand All @@ -158,7 +167,7 @@ class CareerAddActivityViewModel : ViewModel() {
if (addCareerResponse != null) {
_addedCareerInfo.postValue(addCareerResponse)
Log.d("addCareer:Extras 성공", "${response.body()}")
Log.d("addSuccessFileName", imageList.toString())
Log.d("totalFileSize", totalFileSize.value.toString())
} else {
_error.postValue("서버 응답이 올바르지 않습니다.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CareerEditActivityViewModel : ViewModel() {
val addFilesLive: MutableLiveData<Boolean> = MutableLiveData<Boolean>().apply {
value = addFiles.isNotEmpty()
}
val totalFileSize = MutableLiveData<Long>(0)

init {
studentId.value = 0
Expand All @@ -44,6 +45,7 @@ class CareerEditActivityViewModel : ViewModel() {
startDate.value = ""
endDate.value = ""
addFiles.clear()
totalFileSize.value = 0
}

fun init() {
Expand All @@ -53,6 +55,7 @@ class CareerEditActivityViewModel : ViewModel() {
endDate.value = ""
addFiles.clear()
addFilesLive.value = addFiles.isNotEmpty()
totalFileSize.value = 0
}

val isFilledAnyOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
Expand All @@ -61,21 +64,28 @@ class CareerEditActivityViewModel : ViewModel() {
addSource(startDate) { value = isAnyFieldFilled() }
addSource(endDate) { value = isAnyFieldFilled() }
addSource(addFilesLive) { value = isAnyFieldFilled() }
addSource(totalFileSize) { value = isAnyFieldFilled() }
}

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

private fun isDateValid(date: String?): Boolean {
return date.isNullOrBlank() || date.length == 8
}

//파일 사이즈가 30MB를 넘지 않는지 체크
private fun isFileSizeValid(): Boolean {
return totalFileSize.value ?: 0 <= 30 * 1024 * 1024
}

fun addImageFile(file: File) {
val requestFile = RequestBody.create("image/*".toMediaTypeOrNull(), file)
val body = MultipartBody.Part.createFormData("addFiles", file.name, requestFile)
addFiles.add(body)
addFilesLive.value = addFiles.isNotEmpty()
calculateTotalFileSize() //파일 사이즈 계산
fileAddedEvent.value = true
}

Expand Down Expand Up @@ -105,9 +115,20 @@ class CareerEditActivityViewModel : ViewModel() {
val filePart = MultipartBody.Part.createFormData("addFiles", fileName, requestFile)
addFiles.add(filePart)
addFilesLive.value = addFiles.isNotEmpty()
calculateTotalFileSize() //파일 사이즈 계산
fileAddedEvent.value = true
}

//파일 사이즈 측정
fun calculateTotalFileSize() {
var totalSize: Long = 0
for (filePart in addFiles) {
val file = filePart.body
totalSize += file?.contentLength() ?: 0
}
totalFileSize.value = totalSize
}

private val careerApiService = ApiClient.createService<CareerApi>()

private val _activityDetailInfo: MutableLiveData<CareerDetailResponse?> = MutableLiveData()
Expand Down

0 comments on commit 4f6a65f

Please sign in to comment.