Skip to content

Commit

Permalink
Moved uploading attachments to the view model
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Aug 6, 2024
1 parent 2887a7c commit 5bd4a3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package org.wordpress.android.support

import android.content.Context
import android.net.Uri
import com.zendesk.service.ErrorResponse
import com.zendesk.service.ZendeskCallback
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T
import org.wordpress.android.util.extensions.fileName
Expand All @@ -20,7 +16,7 @@ import javax.inject.Inject
* https://zendesk.github.io/mobile_sdk_javadocs/supportv2/v301/index.html?zendesk/support/UploadProvider.html
*/
class ZendeskUploadHelper @Inject constructor() {
private fun uploadAttachment(
fun uploadAttachment(
context: Context,
uri: Uri,
callback: ZendeskCallback<UploadResponse>,
Expand All @@ -44,35 +40,4 @@ class ZendeskUploadHelper @Inject constructor() {
callback
)
}

suspend fun uploadAttachments(
context: Context,
scope: CoroutineScope,
uris: List<Uri>,
): List<zendesk.support.Attachment> {
val attachments = mutableListOf<zendesk.support.Attachment>()

val callback = object : ZendeskCallback<UploadResponse>() {
override fun onSuccess(result: UploadResponse) {
result.attachment?.let {
attachments.add(it)
}
}

override fun onError(errorResponse: ErrorResponse?) {
AppLog.v(
T.SUPPORT, "Uploading to Zendesk failed with" +
" error: ${errorResponse?.reason}"
)
}
}
uris.forEach { uri ->
val job = scope.launch(context = Dispatchers.Default) {
uploadAttachment(context, uri, callback)
}
job.join()
}

return attachments
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import android.net.Uri
import androidx.annotation.StringRes
import androidx.lifecycle.viewModelScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zendesk.service.ErrorResponse
import com.zendesk.service.ZendeskCallback
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
Expand All @@ -23,13 +26,15 @@ import org.wordpress.android.ui.mysite.SelectedSiteRepository
import org.wordpress.android.ui.photopicker.MediaPickerConstants
import org.wordpress.android.ui.photopicker.MediaPickerLauncher
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T
import org.wordpress.android.util.NetworkUtils
import org.wordpress.android.util.ToastUtilsWrapper
import org.wordpress.android.util.extensions.copyToTempFile
import org.wordpress.android.util.extensions.fileSize
import org.wordpress.android.util.extensions.mimeType
import org.wordpress.android.util.extensions.sizeFmt
import org.wordpress.android.viewmodel.ScopedViewModel
import zendesk.support.UploadResponse
import javax.inject.Inject
import javax.inject.Named

Expand Down Expand Up @@ -71,11 +76,8 @@ class FeedbackFormViewModel @Inject constructor(
val zendeskAttachments = ArrayList<zendesk.support.Attachment>()
if (_attachments.value.isNotEmpty()) {
launch {
zendeskAttachments.addAll(zendeskUploadHelper.uploadAttachments(
context = context,
scope = viewModelScope,
uris = _attachments.value.map { it.uri }
)
zendeskAttachments.addAll(
uploadAttachments(context)
)
}
}
Expand Down Expand Up @@ -141,7 +143,7 @@ class FeedbackFormViewModel @Inject constructor(
}

private fun onFailure(errorMessage: String? = null) {
appLogWrapper.e(AppLog.T.SUPPORT, "Failed to submit feedback form: $errorMessage")
appLogWrapper.e(T.SUPPORT, "Failed to submit feedback form: $errorMessage")
showToast(R.string.feedback_form_failure)
}

Expand Down Expand Up @@ -225,6 +227,36 @@ class FeedbackFormViewModel @Inject constructor(
}
}

private suspend fun uploadAttachments(
context: Context,
): List<zendesk.support.Attachment> {
val uploadedAttachments = mutableListOf<zendesk.support.Attachment>()
val uris = _attachments.value.map { it.uri }

val callback = object : ZendeskCallback<UploadResponse>() {
override fun onSuccess(result: UploadResponse) {
result.attachment?.let {
uploadedAttachments.add(it)
}
}

override fun onError(errorResponse: ErrorResponse?) {
AppLog.v(
T.SUPPORT, "Uploading to Zendesk failed with" +
" error: ${errorResponse?.reason}"
)
}
}
uris.forEach { uri ->
val job = viewModelScope.launch(context = Dispatchers.Default) {
zendeskUploadHelper.uploadAttachment(context, uri, callback)
}
job.join()
}

return uploadedAttachments
}

companion object {
private const val MAX_SINGLE_ATTACHMENT_SIZE = 50000000
private const val MAX_TOTAL_ATTACHMENT_SIZE = MAX_SINGLE_ATTACHMENT_SIZE * 3
Expand Down

0 comments on commit 5bd4a3c

Please sign in to comment.