diff --git a/WordPress/src/main/java/org/wordpress/android/support/ZendeskUploadHelper.kt b/WordPress/src/main/java/org/wordpress/android/support/ZendeskUploadHelper.kt index 6aa507c19b4d..d40eec6cd3eb 100644 --- a/WordPress/src/main/java/org/wordpress/android/support/ZendeskUploadHelper.kt +++ b/WordPress/src/main/java/org/wordpress/android/support/ZendeskUploadHelper.kt @@ -4,6 +4,9 @@ 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 @@ -11,14 +14,16 @@ import org.wordpress.android.util.extensions.mimeType import zendesk.support.Support import zendesk.support.UploadResponse import java.io.File +import javax.inject.Inject /** * https://zendesk.github.io/mobile_sdk_javadocs/supportv2/v301/index.html?zendesk/support/UploadProvider.html */ -class ZendeskUploadHelper { - fun uploadFile( +class ZendeskUploadHelper @Inject constructor() { + private fun uploadAttachment( context: Context, uri: Uri, + callback: ZendeskCallback, ): zendesk.support.Attachment? { val uploadProvider = Support.INSTANCE.provider()?.uploadProvider() if (uploadProvider == null) { @@ -36,28 +41,34 @@ class ZendeskUploadHelper { file.name, file, uri.mimeType(context), - object : ZendeskCallback() { - override fun onSuccess(result: UploadResponse) { - // return result.attachment - } - - override fun onError(errorResponse: ErrorResponse?) { - AppLog.v( - T.SUPPORT, "Uploading to Zendesk failed with" + - " error: ${errorResponse?.reason}" - ) - } - }) + callback + ) return null } - fun uploadFiles( + suspend fun uploadAttachments( context: Context, + scope: CoroutineScope, uris: List, ): zendesk.support.Attachment? { - uris.forEach { - uploadFile(context, it) + val callback = object : ZendeskCallback() { + override fun onSuccess(result: UploadResponse) { + // return result.attachment + } + + 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 null } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt index 995ee6400e0a..14b5923b9302 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt @@ -16,6 +16,7 @@ import org.wordpress.android.R import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.modules.UI_THREAD import org.wordpress.android.support.ZendeskHelper +import org.wordpress.android.support.ZendeskUploadHelper import org.wordpress.android.ui.accounts.HelpActivity import org.wordpress.android.ui.media.MediaBrowserType import org.wordpress.android.ui.mysite.SelectedSiteRepository @@ -36,6 +37,7 @@ import javax.inject.Named class FeedbackFormViewModel @Inject constructor( @Named(UI_THREAD) mainDispatcher: CoroutineDispatcher, private val zendeskHelper: ZendeskHelper, + private val zendeskUploadHelper: ZendeskUploadHelper, private val selectedSiteRepository: SelectedSiteRepository, private val appLogWrapper: AppLogWrapper, private val toastUtilsWrapper: ToastUtilsWrapper, @@ -66,6 +68,16 @@ class FeedbackFormViewModel @Inject constructor( // identity if it hasn't been previously set zendeskHelper.createAnonymousIdentityIfNeeded() + if (_attachments.value.isNotEmpty()) { + launch { + zendeskUploadHelper.uploadAttachments( + context = context, + scope = viewModelScope, + uris = _attachments.value.map { it.uri } + ) + } + } + _isProgressShowing.value = true createZendeskFeedbackRequest( context = context,