Skip to content

Commit

Permalink
Merge pull request wordpress-mobile#20960 from wordpress-mobile/conve…
Browse files Browse the repository at this point in the history
…rt-blockprocessorfactory-to-kotlin

Convert BlockProcessor's child classes to Kotlin
  • Loading branch information
notandyvee authored Jun 18, 2024
2 parents e6dd7b3 + 4d4abeb commit d6f3777
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 518 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public static boolean shouldShowGutenbergEditor(boolean isNewPost, String postCo

public static String replaceMediaFileWithUrlInGutenbergPost(@NonNull String postContent,
@NonNull String localMediaId, MediaFile mediaFile,
String siteUrl) {
@NonNull String siteUrl) {
if (mediaFile != null && contentContainsGutenbergBlocks(postContent)) {
MediaUploadCompletionProcessor processor = new MediaUploadCompletionProcessor(localMediaId, mediaFile,
siteUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import org.jsoup.nodes.Document
import org.wordpress.android.util.helpers.MediaFile

class AudioBlockProcessor(localId: String, mediaFile: MediaFile) : BlockProcessor(localId, mediaFile) {
override fun processBlockContentDocument(document: Document?): Boolean {
val audioElements = document?.select(AUDIO_TAG)
override fun processBlockContentDocument(document: Document): Boolean {
val audioElements = document.select(AUDIO_TAG)

audioElements?.let { elements ->
for (element in elements) {
// replaces the src attribute's local url with the remote counterpart.
element.attr(SRC_ATTRIBUTE, remoteUrl)
}
return true
for (element in audioElements) {
// replaces the src attribute's local url with the remote counterpart.
element.attr(SRC_ATTRIBUTE, remoteUrl)
}
return false
return true
}

override fun processBlockJsonAttributes(jsonAttributes: JsonObject?): Boolean {
val id = jsonAttributes?.get(ID_ATTRIBUTE)
override fun processBlockJsonAttributes(jsonAttributes: JsonObject): Boolean {
val id = jsonAttributes.get(ID_ATTRIBUTE)

return if (id != null && !id.isJsonNull && id.asString == localId) {
jsonAttributes.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,37 @@ abstract class BlockProcessor internal constructor(@JvmField var localId: String
* @return A string containing content with ids and urls replaced
*/
@JvmOverloads
fun processBlock(block: String, isSelfClosingTag: Boolean = false) = if (splitBlock(block, isSelfClosingTag)) {
if (processBlockJsonAttributes(jsonAttributes)) {
if (isSelfClosingTag) {
// return injected block
StringBuilder()
.append("<!-- wp:")
.append(blockName)
.append(" ")
.append(jsonAttributes) // json parser output
.append(" /-->")
.toString()
} else if (processBlockContentDocument(blockContentDocument)) {
// return injected block
StringBuilder()
.append("<!-- wp:")
.append(blockName)
.append(" ")
.append(jsonAttributes) // json parser output
.append(" -->\n")
.append(blockContentDocument?.body()?.html()) // HTML parser output
.append(closingComment)
.toString()
} else {
block
}
} else {
processInnerBlock(block) // delegate to inner blocks if needed
fun processBlock(block: String, isSelfClosingTag: Boolean = false) = when {
!splitBlock(block, isSelfClosingTag) -> block // leave block unchanged
jsonAttributes?.let { !processBlockJsonAttributes(it) } == true -> {
// delegate to inner blocks if needed
processInnerBlock(block)
}
isSelfClosingTag -> {
// return injected block
StringBuilder()
.append("<!-- wp:")
.append(blockName)
.append(" ")
.append(jsonAttributes) // json parser output
.append(" /-->")
.toString()
}
} else {
// leave block unchanged
block

blockContentDocument?.let { processBlockContentDocument(it) } == true -> {
// return injected block
StringBuilder()
.append("<!-- wp:")
.append(blockName)
.append(" ")
.append(jsonAttributes) // json parser output
.append(" -->\n")
.append(blockContentDocument?.body()?.html()) // HTML parser output
.append(closingComment)
.toString()
}

else -> block // leave block unchanged
}

fun addIntPropertySafely(jsonAttributes: JsonObject, propertyName: String, value: String) = try {
Expand All @@ -121,7 +121,7 @@ abstract class BlockProcessor internal constructor(@JvmField var localId: String
* @param document The document to be mutated to make the necessary replacements
* @return A boolean value indicating whether or not the block contents should be replaced
*/
abstract fun processBlockContentDocument(document: Document?): Boolean
abstract fun processBlockContentDocument(document: Document): Boolean

/**
* All concrete implementations must implement this method for the particular block type. The jsonAttributes object
Expand All @@ -134,7 +134,7 @@ abstract class BlockProcessor internal constructor(@JvmField var localId: String
* @param jsonAttributes the attributes object used to check for a match with the local id, and mutated if necessary
* @return
*/
abstract fun processBlockJsonAttributes(jsonAttributes: JsonObject?): Boolean
abstract fun processBlockJsonAttributes(jsonAttributes: JsonObject): Boolean

/**
* This method can be optionally overridden by concrete implementations to delegate further processing via recursion
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.wordpress.android.ui.posts.mediauploadcompletionprocessors

import org.wordpress.android.util.helpers.MediaFile

/**
* This factory initializes block processors for all media block types and provides a method to retrieve a block
* processor instance for a given block type.
* @param localId The local media id that needs replacement
* @param mediaFile The mediaFile containing the remote id and remote url`
* @param siteUrl The site url - used to generate the attachmentPage url
* @return The factory instance - useful for chaining this method upon instantiation
*/
internal class BlockProcessorFactory(
mediaUploadCompletionProcessor: MediaUploadCompletionProcessor,
localId: String,
mediaFile: MediaFile,
siteUrl: String
) {
private val mediaBlockTypeBlockProcessorMap = hashMapOf(
MediaBlockType.IMAGE to ImageBlockProcessor(localId, mediaFile),
MediaBlockType.VIDEOPRESS to VideoPressBlockProcessor(localId, mediaFile),
MediaBlockType.VIDEO to VideoBlockProcessor(localId, mediaFile),
MediaBlockType.MEDIA_TEXT to MediaTextBlockProcessor(localId, mediaFile),
MediaBlockType.GALLERY to GalleryBlockProcessor(localId, mediaFile, siteUrl, mediaUploadCompletionProcessor),
MediaBlockType.COVER to CoverBlockProcessor(localId, mediaFile, mediaUploadCompletionProcessor),
MediaBlockType.FILE to FileBlockProcessor(localId, mediaFile),
MediaBlockType.AUDIO to AudioBlockProcessor(localId, mediaFile)
)

/**
* Retrieves the block processor instance for the given media block type.
*
* @param blockType The media block type for which to provide a [BlockProcessor]
* @return The [BlockProcessor] for the given media block type
*/
fun getProcessorForMediaBlockType(blockType: MediaBlockType) = mediaBlockTypeBlockProcessorMap[blockType]
}

This file was deleted.

Loading

0 comments on commit d6f3777

Please sign in to comment.