Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ private suspend fun ApplicationViewModel.handleApplicationSideEffect(
).show()
}

is ApplicationSideEffect.AttachmentUploadFailure -> {
JobisToast.create(
context = context,
message = context.getString(R.string.toast_attachment_upload_failure),
drawable = JobisIcon.Error,
).show()
}

is ApplicationSideEffect.SuccessApply -> {
JobisToast.create(
context = context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ internal class ApplicationViewModel @Inject constructor(

private val urls: SnapshotStateList<String> = mutableStateListOf()

private val attachments: MutableList<Pair<String, AttachmentType>> = mutableListOf()

internal fun getFiles() = files

internal fun getUrls() = urls
Expand Down Expand Up @@ -118,8 +116,11 @@ internal class ApplicationViewModel @Inject constructor(
setButtonEnabled()
}

private fun setButtonEnabled() = setState {
state.value.copy(buttonEnabled = files.isNotEmpty())
private fun setButtonEnabled(isSubmitting: Boolean = state.value.isSubmitting) = setState {
state.value.copy(
buttonEnabled = files.isNotEmpty() && !isSubmitting,
isSubmitting = isSubmitting,
)
}

internal fun addUrl() {
Expand All @@ -137,7 +138,7 @@ internal class ApplicationViewModel @Inject constructor(
urls[index] = url
}

private suspend fun applyCompany() {
private suspend fun applyCompany(attachments: List<Pair<String, AttachmentType>>) {
applyCompanyUseCase(
recruitmentId = state.value.recruitmentId,
attachments = attachments,
Expand All @@ -146,7 +147,7 @@ internal class ApplicationViewModel @Inject constructor(
}.onApplyCompanyFailure(isReApply = false)
}

private suspend fun reApplyCompany() {
private suspend fun reApplyCompany(attachments: List<Pair<String, AttachmentType>>) {
reApplyCompanyUseCase(
applicationId = state.value.applicationId,
attachments = attachments,
Expand All @@ -157,6 +158,9 @@ internal class ApplicationViewModel @Inject constructor(

private fun Result<Unit>.onApplyCompanyFailure(isReApply: Boolean) {
onFailure {
if (it !is KotlinNullPointerException) {
setButtonEnabled(isSubmitting = false)
}
when (it) {
is KotlinNullPointerException -> {
postSideEffect(
Expand All @@ -183,18 +187,23 @@ internal class ApplicationViewModel @Inject constructor(
}

internal fun createPresignedUrl() {
setState { state.value.copy(buttonEnabled = false) }
if (state.value.isSubmitting) return
setButtonEnabled(isSubmitting = true)
viewModelScope.launch(Dispatchers.IO) {
createPresignedUseCase(files = files.toFileNames()).onSuccess {
addPresignedUrlOrUrlToAttachments(urls = it.urls.toFileTypeAttachments())
addPresignedUrlOrUrlToAttachments(urls = urls.toUrlTypeAttachments())
if (state.value.isReApply) {
reApplyCompany()
if (uploadFiles(urls = it.urls)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
val attachments = it.urls.toFileTypeAttachments() + urls.toUrlTypeAttachments()
if (state.value.isReApply) {
reApplyCompany(attachments = attachments)
} else {
applyCompany(attachments = attachments)
}
} else {
applyCompany()
setButtonEnabled(isSubmitting = false)
postSideEffect(ApplicationSideEffect.AttachmentUploadFailure)
}
uploadFile(presignedUrls = it.urls.toPresignedUrls())
}.onFailure {
setButtonEnabled(isSubmitting = false)
when (it) {
is BadRequestException -> {
postSideEffect(ApplicationSideEffect.InvalidFileExtension)
Expand All @@ -204,41 +213,36 @@ internal class ApplicationViewModel @Inject constructor(
}
}

private suspend fun uploadFile(presignedUrls: List<String>) {
presignedUrls.forEachIndexed { index, presignedUrl ->
uploadFileUseCase(
presignedUrl = presignedUrl,
file = files[index],
)
}
}
private suspend fun uploadFiles(urls: List<PresignedUrlEntity.UrlEntity>): Boolean =
urls.size == files.size &&
urls.zip(files).all { (url, file) ->
uploadFileUseCase(
presignedUrl = url.preSignedUrl,
file = file,
).isSuccess
}

private fun List<File>.toFileNames() = map { it.name }

private fun List<PresignedUrlEntity.UrlEntity>.toFileTypeAttachments() =
map { it.filePath to AttachmentType.FILE }

private fun List<PresignedUrlEntity.UrlEntity>.toPresignedUrls() =
map { it.preSignedUrl }

private fun List<String>.toUrlTypeAttachments() =
map { it to AttachmentType.URL }

private fun addPresignedUrlOrUrlToAttachments(urls: List<Pair<String, AttachmentType>>) {
attachments.addAll(urls)
}
}

internal data class ApplicationState(
val recruitmentId: Long,
val buttonEnabled: Boolean,
val isSubmitting: Boolean,
val isReApply: Boolean,
val applicationId: Long,
) {
companion object {
fun getInitialState() = ApplicationState(
recruitmentId = 0,
buttonEnabled = false,
isSubmitting = false,
isReApply = false,
applicationId = 0L,
)
Expand All @@ -248,6 +252,7 @@ internal data class ApplicationState(
internal sealed interface ApplicationSideEffect {
data object ExceedFileCount : ApplicationSideEffect
data object InvalidFileExtension : ApplicationSideEffect
data object AttachmentUploadFailure : ApplicationSideEffect
data object SuccessApply : ApplicationSideEffect
data object SuccessReApply : ApplicationSideEffect
data object ConflictApply : ApplicationSideEffect
Expand Down
1 change: 1 addition & 0 deletions feature/application/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<string name="toast_max_count">μ΅œλŒ€ 3κ°œκΉŒμ§€λ§Œ 등둝할 수 μžˆμ–΄μš”</string>
<string name="toast_invalid_file_extension">μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ 파일 ν™•μž₯μžμ—μš”</string>
<string name="toast_attachment_upload_failure">첨뢀 파일 μ—…λ‘œλ“œμ— μ‹€νŒ¨ν–ˆμ–΄μš”. λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”</string>
<string name="toast_success_apply">μ„±κ³΅μ μœΌλ‘œ 지원이 μ™„λ£Œλ˜μ—ˆμ–΄μš”!</string>
<string name="toast_success_re_apply">μ„±κ³΅μ μœΌλ‘œ μž¬μ§€μ› λ˜μ—ˆμ–΄μš”!</string>
<string name="toast_conflict_apply">이미 μ§€μ›ν•œ κΈ°μ—…μ΄μ—μš”</string>
Expand Down
Loading