Update Image Post

Upload images

If you want to edit an image post to add more images, the images to be added must be uploaded first. The AmityFileRepository class handles the uploading and downloading of images. The repository has an uploadImage method which takes the image's Uri and returns an array of AmityUploadResult for successful or failed uploads.

AmityUploadResult will return four possible types of data:

  • AmityUploadResult.PROGRESS - uploading is in progress. It returns AmityUploadInfo which can be used to track the progress of the upload.

  • AmityUploadResult.COMPLETE - the upload completed successfully. It returns AmityImage that can then be attached to the post.

  • AmityUploadResult.ERROR(exception) - the upload failed. It returns an Exception.

  • AmityUploadResult.CANCELLED - uploading is canceled.

fun uploadImage(imageUri: Uri) {
    AmityCoreClient.newFileRepository()
        .uploadImage(uri = imageUri)
        .build()
        .transfer()
        .doOnNext{ uploadResult: AmityUploadResult<AmityImage> ->
            when (uploadResult) {
                is AmityUploadResult.PROGRESS -> {
                    val progress = uploadResult.getUploadInfo().getProgressPercentage()
                }
                is AmityUploadResult.COMPLETE -> {
                    val amityImageI = uploadResult.getFile()
                }
                is AmityUploadResult.ERROR -> {
                    val exception = uploadResult.getError()
                }
                is AmityUploadResult.CANCELLED -> {
                    //proceed
                }
            }
        }
        .subscribe()
}

You can upload a maximum of ten images in a single post.

Update Image post

To edit an image post, use the edit method as shown in the sample code below. The attachments should contain the existing images and the uploaded images that you want to add to the post which are stored in existingImages and newImages respectively.

You can remove existing images from the post by excluding them from the attachments list. You can also change the order of the images in the list.

fun updateImagePost(post: AmityPost, newImages: List<AmityImage>) {
    val postData: AmityPost.Data = post.getData()
    val existingImages = post?.getChildren()
                    ?.map { it.getData() }
                    ?.filterIsInstance<AmityPost.Data.IMAGE>
    val isImagePost = existingImages
                    ?.isNotEmpty() 
                    ?: false
                    
    val attachments = existingImages + newImages
    
    if (isImagePost) {
        postData.edit()
            .attachments(attachments)
            .text(postData.getText())
            .build()
            .apply()
            .subscribe()
    }
 

To update mentioned users in a post, refer to update mention in post.

Last updated