1
0
mirror of https://gitlab.shinice.net/pixeldroid/PixelDroid synced 2025-01-31 07:44:49 +01:00

Refactor redraft as functional code

This commit is contained in:
fgerber 2022-11-03 23:12:54 +01:00
parent c726bbb448
commit 1c8a7d8b7d

View File

@ -9,7 +9,6 @@ import android.graphics.Typeface
import android.graphics.drawable.AnimatedVectorDrawable import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.os.Handler
import android.os.Looper import android.os.Looper
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
import android.util.Log 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.PermissionGrantedResponse
import com.karumi.dexter.listener.single.BasePermissionListener import com.karumi.dexter.listener.single.BasePermissionListener
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.* import okhttp3.*
import okio.BufferedSink import okio.BufferedSink
import okio.buffer import okio.buffer
@ -480,25 +478,16 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
val postDescription = status?.content ?: "" val postDescription = status?.content ?: ""
val postAttachments = val postAttachments =
status?.media_attachments!! // Catch possible exception from !! (?) status?.media_attachments!! // Catch possible exception from !! (?)
val imageUris: MutableList<Uri> = mutableListOf()
val imageNames: MutableList<String> = mutableListOf()
val imageDescriptions: MutableList<String> =
mutableListOf()
val postNSFW = status?.sensitive val postNSFW = status?.sensitive
for (currentAttachment in postAttachments) { val imageNames = postAttachments.map { postAttachment ->
val imageUri = currentAttachment.url ?: "" Uri.parse(postAttachment.url ?: "").lastPathSegment.toString()
val imageName = }
Uri.parse(imageUri).lastPathSegment.toString() val imageUris = imageNames.map { imageName ->
val imageDescription = Uri.fromFile(File(context.cacheDir, imageName))
currentAttachment.description ?: "" }
val downloadedFile = val imageDescriptions = postAttachments.map { postAttachment ->
File(context.cacheDir, imageName) fromHtml(postAttachment.description ?: "").toString()
val downloadedUri = Uri.fromFile(downloadedFile)
imageUris.add(downloadedUri)
imageNames.add(imageName)
imageDescriptions.add(imageDescription)
} }
val counter = AtomicInteger(0) val counter = AtomicInteger(0)
@ -509,10 +498,8 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
if (counter.incrementAndGet() == imageUris.size) { if (counter.incrementAndGet() == imageUris.size) {
if (allFilesExist(imageNames)) { if (allFilesExist(imageNames)) {
// Delete original post // Delete original post
Handler(Looper.getMainLooper()).post { lifecycleScope.launch {
runBlocking { deletePost(apiHolder.api ?: apiHolder.setToCurrentUser(), db)
deletePost(apiHolder.api ?: apiHolder.setToCurrentUser(), db)
}
} }
val counterInt = counter.get() val counterInt = counter.get()
@ -527,23 +514,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
).show() ).show()
// Pass downloaded images to new post creation activity // Pass downloaded images to new post creation activity
intent.apply { intent.apply {
assert(imageUris.size == imageDescriptions.size) imageUris.zip(imageDescriptions).map { (imageUri, imageDescription) ->
val imageItem = ClipData.Item(imageDescription, null, imageUri)
for (i in 0 until imageUris.size) {
val imageUri = imageUris[i]
val imageDescription =
fromHtml(imageDescriptions[i]).toString()
val imageItem = ClipData.Item(
imageDescription,
null,
imageUri
)
if (clipData == null) { if (clipData == null) {
clipData = ClipData( clipData = ClipData("", emptyArray(), imageItem)
"",
emptyArray(),
imageItem
)
} else { } else {
clipData!!.addItem(imageItem) clipData!!.addItem(imageItem)
} }
@ -837,14 +811,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
} }
} }
private fun allFilesExist(listOfNames: MutableList<String>): Boolean { private fun allFilesExist(listOfNames: List<String>): Boolean {
for (name in listOfNames) { return listOfNames.all {
val file = File(binding.root.context.cacheDir, name) File(binding.root.context.cacheDir, it).exists()
if (!file.exists()) {
return false
}
} }
return true
} }
companion object { companion object {