Skip to content

Commit

Permalink
Upload multiple attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Aug 5, 2024
1 parent bd19684 commit be10c4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ 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
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<UploadResponse>,
): zendesk.support.Attachment? {
val uploadProvider = Support.INSTANCE.provider()?.uploadProvider()
if (uploadProvider == null) {
Expand All @@ -36,28 +41,34 @@ class ZendeskUploadHelper {
file.name,
file,
uri.mimeType(context),
object : ZendeskCallback<UploadResponse>() {
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<Uri>,
): zendesk.support.Attachment? {
uris.forEach {
uploadFile(context, it)
val callback = object : ZendeskCallback<UploadResponse>() {
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit be10c4f

Please sign in to comment.