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

View File

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

View File

@ -80,9 +80,6 @@ class PhotoEditActivity : BaseThemedWithBarActivity() {
companion object{ companion object{
internal const val PICTURE_URI = "picture_uri" internal const val PICTURE_URI = "picture_uri"
internal const val PICTURE_POSITION = "picture_position" 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 executor: ExecutorService = newSingleThreadExecutor()
private var future: Future<*>? = null private var future: Future<*>? = null

View File

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