Refactor redraft as functional code
This commit is contained in:
parent
8325067566
commit
553c65f7bc
|
@ -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<Uri> = mutableListOf()
|
||||
val imageNames: MutableList<String> = mutableListOf()
|
||||
val imageDescriptions: MutableList<String> =
|
||||
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<String>): Boolean {
|
||||
for (name in listOfNames) {
|
||||
val file = File(binding.root.context.cacheDir, name)
|
||||
if (!file.exists()) {
|
||||
return false
|
||||
}
|
||||
private fun allFilesExist(listOfNames: List<String>): Boolean {
|
||||
return listOfNames.all {
|
||||
File(binding.root.context.cacheDir, it).exists()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
Loading…
Reference in New Issue