Skip to content

Commit

Permalink
Pass attachment IDs when creating request
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Aug 5, 2024
1 parent be10c4f commit 4dc33ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ class ZendeskUploadHelper @Inject constructor() {
context: Context,
uri: Uri,
callback: ZendeskCallback<UploadResponse>,
): zendesk.support.Attachment? {
) {
val uploadProvider = Support.INSTANCE.provider()?.uploadProvider()
if (uploadProvider == null) {
AppLog.e(T.SUPPORT, "Upload provider is null")
return null
return
}

val file = uri.fileName(context)?.let { File(it) }
if (file == null) {
AppLog.e(T.SUPPORT, "Upload file is null")
return null
return
}

uploadProvider.uploadAttachment(
Expand All @@ -43,18 +43,20 @@ class ZendeskUploadHelper @Inject constructor() {
uri.mimeType(context),
callback
)

return null
}

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

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

override fun onError(errorResponse: ErrorResponse?) {
Expand All @@ -70,6 +72,7 @@ class ZendeskUploadHelper @Inject constructor() {
}
job.join()
}
return null

return attachments
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,22 @@ class FeedbackFormViewModel @Inject constructor(
// identity if it hasn't been previously set
zendeskHelper.createAnonymousIdentityIfNeeded()

val zendeskAttachments = ArrayList<zendesk.support.Attachment>()
if (_attachments.value.isNotEmpty()) {
launch {
zendeskUploadHelper.uploadAttachments(
zendeskAttachments.addAll(zendeskUploadHelper.uploadAttachments(
context = context,
scope = viewModelScope,
uris = _attachments.value.map { it.uri }
)
)
}
}

_isProgressShowing.value = true
createZendeskFeedbackRequest(
context = context,
attachmentIds = zendeskAttachments.map { it.id.toString() },
callback = object : ZendeskHelper.CreateRequestCallback() {
override fun onSuccess() {
_isProgressShowing.value = false
Expand All @@ -96,6 +99,7 @@ class FeedbackFormViewModel @Inject constructor(

private fun createZendeskFeedbackRequest(
context: Context,
attachmentIds: List<String>,
callback: ZendeskHelper.CreateRequestCallback
) {
zendeskHelper.createRequest(
Expand All @@ -104,9 +108,7 @@ class FeedbackFormViewModel @Inject constructor(
selectedSite = selectedSiteRepository.getSelectedSite(),
extraTags = listOf("in_app_feedback"),
requestDescription = _messageText.value,
attachmentIds = _attachments.value.filter { !it.zendeskId.isNullOrEmpty() }.map {
it.zendeskId!!
},
attachmentIds = attachmentIds,
callback = callback
)
}
Expand Down

0 comments on commit 4dc33ab

Please sign in to comment.