From 553c65f7bc581f12b669a3dd30015816ab1f6151 Mon Sep 17 00:00:00 2001 From: fgerber Date: Thu, 3 Nov 2022 23:12:54 +0100 Subject: [PATCH] Refactor redraft as functional code --- .../pixeldroid/app/posts/StatusViewHolder.kt | 62 +++++-------------- 1 file changed, 16 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt b/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt index ff8e7054..75a08e0d 100644 --- a/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt +++ b/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt @@ -9,7 +9,6 @@ import android.graphics.Typeface import android.graphics.drawable.AnimatedVectorDrawable import android.graphics.drawable.Drawable import android.net.Uri -import android.os.Handler import android.os.Looper import android.text.method.LinkMovementMethod import android.util.Log @@ -40,7 +39,6 @@ import com.karumi.dexter.listener.PermissionDeniedResponse import com.karumi.dexter.listener.PermissionGrantedResponse import com.karumi.dexter.listener.single.BasePermissionListener import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import okhttp3.* import okio.BufferedSink import okio.buffer @@ -480,25 +478,16 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold val postDescription = status?.content ?: "" val postAttachments = status?.media_attachments!! // Catch possible exception from !! (?) - val imageUris: MutableList = mutableListOf() - val imageNames: MutableList = mutableListOf() - val imageDescriptions: MutableList = - mutableListOf() val postNSFW = status?.sensitive - for (currentAttachment in postAttachments) { - val imageUri = currentAttachment.url ?: "" - val imageName = - Uri.parse(imageUri).lastPathSegment.toString() - val imageDescription = - currentAttachment.description ?: "" - val downloadedFile = - File(context.cacheDir, imageName) - val downloadedUri = Uri.fromFile(downloadedFile) - - imageUris.add(downloadedUri) - imageNames.add(imageName) - imageDescriptions.add(imageDescription) + val imageNames = postAttachments.map { postAttachment -> + Uri.parse(postAttachment.url ?: "").lastPathSegment.toString() + } + val imageUris = imageNames.map { imageName -> + Uri.fromFile(File(context.cacheDir, imageName)) + } + val imageDescriptions = postAttachments.map { postAttachment -> + fromHtml(postAttachment.description ?: "").toString() } val counter = AtomicInteger(0) @@ -509,10 +498,8 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold if (counter.incrementAndGet() == imageUris.size) { if (allFilesExist(imageNames)) { // Delete original post - Handler(Looper.getMainLooper()).post { - runBlocking { - deletePost(apiHolder.api ?: apiHolder.setToCurrentUser(), db) - } + lifecycleScope.launch { + deletePost(apiHolder.api ?: apiHolder.setToCurrentUser(), db) } val counterInt = counter.get() @@ -527,23 +514,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold ).show() // Pass downloaded images to new post creation activity intent.apply { - assert(imageUris.size == imageDescriptions.size) - - for (i in 0 until imageUris.size) { - val imageUri = imageUris[i] - val imageDescription = - fromHtml(imageDescriptions[i]).toString() - val imageItem = ClipData.Item( - imageDescription, - null, - imageUri - ) + imageUris.zip(imageDescriptions).map { (imageUri, imageDescription) -> + val imageItem = ClipData.Item(imageDescription, null, imageUri) if (clipData == null) { - clipData = ClipData( - "", - emptyArray(), - imageItem - ) + clipData = ClipData("", emptyArray(), imageItem) } else { clipData!!.addItem(imageItem) } @@ -837,14 +811,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold } } - private fun allFilesExist(listOfNames: MutableList): Boolean { - for (name in listOfNames) { - val file = File(binding.root.context.cacheDir, name) - if (!file.exists()) { - return false - } + private fun allFilesExist(listOfNames: List): Boolean { + return listOfNames.all { + File(binding.root.context.cacheDir, it).exists() } - return true } companion object {