Bug fix redraft and refactor

This commit is contained in:
Matthieu 2022-10-27 14:12:15 +02:00
parent 91819dd4db
commit 069c11478a
5 changed files with 156 additions and 138 deletions

View File

@ -64,6 +64,12 @@ data class PhotoData(
class PostCreationActivity : BaseThemedWithoutBarActivity() {
companion object {
internal const val PICTURE_DESCRIPTION = "picture_description"
internal const val TEMP_FILES = "temp_files"
internal const val POST_REDRAFT = "post_redraft"
}
private var user: UserDatabaseEntity? = null
private lateinit var instance: InstanceDatabaseEntity
@ -105,9 +111,6 @@ class PostCreationActivity : BaseThemedWithoutBarActivity() {
)
}
//Get initial text value from model (for template)
binding.newPostDescriptionInputField.setText(model.uiState.value.newPostDescriptionText)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
model.uiState.collect { uiState ->
@ -167,9 +170,13 @@ class PostCreationActivity : BaseThemedWithoutBarActivity() {
model.newPostDescriptionChanged(binding.newPostDescriptionInputField.text)
}
// Fetch existing description passed through putExtra (if any)
val existingDescription = intent.getStringExtra(PhotoEditActivity.PICTURE_DESCRIPTION)
binding.newPostDescriptionInputField.setText(existingDescription)
val existingDescription: String? = intent.getStringExtra(PICTURE_DESCRIPTION)
binding.newPostDescriptionInputField.setText(
// Set description from redraft if any, otherwise from the template
existingDescription ?: model.uiState.value.newPostDescriptionText
)
binding.postTextInputLayout.counterMaxLength = instance.maxStatusChars
@ -219,15 +226,15 @@ class PostCreationActivity : BaseThemedWithoutBarActivity() {
}
// Clean up temporary files, if any
val tempFiles = intent.getStringArrayExtra(PhotoEditActivity.TEMP_FILES)
tempFiles?.asList()?.map { uri -> uri.toString() }?.forEach {
val tempFiles = intent.getStringArrayExtra(TEMP_FILES)
tempFiles?.asList()?.forEach {
val file = File(binding.root.context.cacheDir, it)
file.delete()
model.trackTempFile(file)
}
}
override fun onBackPressed() {
val redraft = intent.getBooleanExtra(PhotoEditActivity.POST_REDRAFT, false)
val redraft = intent.getBooleanExtra(POST_REDRAFT, false)
if (redraft) {
val builder = AlertDialog.Builder(binding.root.context)
builder.apply {

View File

@ -1,6 +1,5 @@
package org.pixeldroid.app.postCreation
import android.R.attr.orientation
import android.app.Application
import android.content.ClipData
import android.content.Intent

View File

@ -80,9 +80,6 @@ class PhotoEditActivity : BaseThemedWithBarActivity() {
companion object{
internal const val PICTURE_URI = "picture_uri"
internal const val PICTURE_POSITION = "picture_position"
internal const val PICTURE_DESCRIPTION = "picture_description"
internal const val TEMP_FILES = "temp_files"
internal const val POST_REDRAFT = "post_redraft"
private var executor: ExecutorService = newSingleThreadExecutor()
private var future: Future<*>? = null

View File

@ -473,18 +473,19 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
setPositiveButton(android.R.string.ok) { _, _ ->
lifecycleScope.launch {
val user = db.userDao().getActiveUser()!!
status?.id?.let { id ->
try {
// Create new post creation activity
val intent = Intent(context, PostCreationActivity::class.java)
val intent =
Intent(context, PostCreationActivity::class.java)
// Get descriptions and images from original post
val postDescription = status?.content ?: ""
val postAttachments = status?.media_attachments!! // Catch possible exception from !! (?)
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 imageDescriptions: MutableList<String> =
mutableListOf()
for (currentAttachment in postAttachments) {
val imageUri = currentAttachment.url ?: ""
@ -509,20 +510,15 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
if (counter.incrementAndGet() == imageUris.size) {
if (allFilesExist(imageNames)) {
val counterInt = counter.get()
if (counterInt < 2) {
Toast.makeText(
binding.root.context,
binding.root.context.getString(R.string.item_load_success, counterInt),
binding.root.context.resources.getQuantityString(
R.plurals.items_load_success,
counterInt,
counterInt
),
Toast.LENGTH_SHORT
).show()
} else {
Toast.makeText(
binding.root.context,
binding.root.context.getString(R.string.items_load_success, counterInt),
Toast.LENGTH_SHORT
).show()
}
// Pass downloaded images to new post creation activity
intent.apply {
assert(imageUris.size == imageDescriptions.size)
@ -552,19 +548,20 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
// Pass post description of existing post to new post creation activity
if (postDescription != null) {
intent.putExtra(
PhotoEditActivity.PICTURE_DESCRIPTION,
fromHtml(postDescription!!).toString()
PostCreationActivity.PICTURE_DESCRIPTION,
fromHtml(postDescription).toString()
)
if (imageNames.isNotEmpty()) {
intent.putExtra(
PostCreationActivity.TEMP_FILES,
imageNames.toTypedArray()
)
}
if (imageUris.isNotEmpty()) {
intent.putExtra(
PhotoEditActivity.TEMP_FILES,
imageUris.map{ uri -> uri.toString() }.toTypedArray()
PostCreationActivity.POST_REDRAFT,
true
)
}
intent.putExtra(PhotoEditActivity.POST_REDRAFT, true)
// Launch post creation activity
binding.root.context.startActivity(intent)
@ -584,14 +581,21 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
// Iterate through all pictures of the original post
for (currentAttachment in postAttachments) {
val imageUri = currentAttachment.url ?: ""
val imageName = Uri.parse(imageUri).lastPathSegment.toString()
val downloadedFile = File(context.cacheDir, imageName)
val downloadRequest: Request = Request.Builder().url(imageUri).build()
val imageName =
Uri.parse(imageUri).lastPathSegment.toString()
val downloadedFile =
File(context.cacheDir, imageName)
val downloadRequest: Request =
Request.Builder().url(imageUri).build()
// Check whether image is in cache directory already (maybe rather do so using Glide in the future?)
if (!downloadedFile.exists()) {
OkHttpClient().newCall(downloadRequest).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
OkHttpClient().newCall(downloadRequest)
.enqueue(object : Callback {
override fun onFailure(
call: Call,
e: IOException
) {
Looper.prepare()
downloadedFile.delete()
Toast.makeText(
@ -602,7 +606,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
override fun onResponse(
call: Call,
response: Response
) {
val sink: BufferedSink =
downloadedFile.sink().buffer()
sink.writeAll(response.body!!.source())
@ -619,7 +626,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
} catch (exception: HttpException) {
Toast.makeText(
binding.root.context,
binding.root.context.getString(R.string.redraft_post_failed_error, exception.code()),
binding.root.context.getString(
R.string.redraft_post_failed_error,
exception.code()
),
Toast.LENGTH_SHORT
).show()
} catch (exception: IOException) {
@ -631,8 +641,11 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
// Delete original post
deletePost(apiHolder.api ?: apiHolder.setToCurrentUser(), db)
}
deletePost(
apiHolder.api ?: apiHolder.setToCurrentUser(),
db
)
}
}
setNegativeButton(android.R.string.cancel) { _, _ -> }

View File

@ -148,8 +148,10 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="image_download_failed">Download has failed, please try again</string>
<string name="image_download_downloading">Downloading…</string>
<string name="image_download_success">Image downloaded successfully</string>
<string name="item_load_success">%1$d item loaded successfully</string>
<string name="items_load_success">%1$d items loaded successfully</string>
<plurals name="items_load_success">
<item quantity="one">%d item loaded successfully</item>
<item quantity="other">%d items loaded successfully</item>
</plurals>
<!-- Post attributes -->
<string name="no_description">No description</string>
<plurals name="likes">