Skip to content

Convert ImageSizeMap to Kotlin #21888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 67 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
7ba68b8
Rename .java to .kt
nbradbury May 13, 2025
f7cc8fe
Automated conversion of ReaderPostRenderer to Kotlin
nbradbury May 13, 2025
dec3ba0
Renamed variables to Kotlin format
nbradbury May 13, 2025
2400452
Fixed most warnings
nbradbury May 13, 2025
865561f
Put params on separate lines
nbradbury May 13, 2025
3837c7d
Fixed comment wrapping in formatPostContentForWebView
nbradbury May 13, 2025
b3f8798
Converted beginRender to use a coroutine instead of a handler
nbradbury May 13, 2025
3c4627c
Make renderBuilder non-null
nbradbury May 13, 2025
74f08d7
Reformatted stringbuilder appends for readability
nbradbury May 13, 2025
98e8a59
Fixed line length warnings
nbradbury May 13, 2025
d99c7f8
Fixed detekt warnings
nbradbury May 13, 2025
26f70aa
Rename .java to .kt
nbradbury May 13, 2025
d753d34
Auto conversion of ReaderResourceVars
nbradbury May 13, 2025
4fb8341
Renamed ReaderResourceVars variables to Kotlin format
nbradbury May 13, 2025
16e69bd
Fixed MagicNumbers
nbradbury May 13, 2025
011383c
Use "as MaterialR" import to reduce line lengths
nbradbury May 13, 2025
ff698b3
Removed unused suppression
nbradbury May 13, 2025
9d8dd0e
Changed coroutine scope
nbradbury May 13, 2025
d44a63a
Changed postContent to a fun
nbradbury May 13, 2025
0f32773
Removed unused renderedHtml variable
nbradbury May 13, 2025
68084aa
Minor formatting changes
nbradbury May 13, 2025
e9d027b
Removed unused variables from ReaderResourceVars
nbradbury May 14, 2025
d213f0f
Replace for loops with forEach
nbradbury May 14, 2025
dbc8da7
Rename .java to .kt
nbradbury May 14, 2025
5eaa7f2
Auto-convert reader scanners to Kotlin
nbradbury May 14, 2025
b730d0a
Renamed variables to Kotlin style
nbradbury May 14, 2025
7252bc9
Fixed issues in ReaderEmbedScanner
nbradbury May 14, 2025
53ea4b8
Fixed issues in ReaderIframeScanner
nbradbury May 14, 2025
899c6b2
Fixed issues in ReaderImageScanner
nbradbury May 14, 2025
6000050
Don't allow null content in ReaderImageScanner
nbradbury May 14, 2025
c8944fc
Changed getter to a fun
nbradbury May 14, 2025
fdc35a3
Suppressed warning
nbradbury May 14, 2025
690c6c3
Updated comment
nbradbury May 14, 2025
9b19f79
Use Kotlin "?.let" instead of null check
nbradbury May 14, 2025
3fb2d20
Removed unused val
nbradbury May 14, 2025
64d1868
Merge branch 'trunk' of https://github.com/wordpress-mobile/WordPress…
nbradbury May 14, 2025
40e1c53
Rename .java to .kt
nbradbury May 15, 2025
3e30830
Converted ReaderHtmlUtils to Kotlin and fixed nullability issues
nbradbury May 15, 2025
2a5e135
Fixed issues with ReaderUtils.java before conversion
nbradbury May 15, 2025
fe98c0e
First pass at tackling nullability issues with ReaderUtils.java
nbradbury May 15, 2025
15302b9
Second pass at tackling nullability issues with ReaderUtils.java
nbradbury May 15, 2025
a8916e7
Third pass at tackling nullability issues with ReaderUtils.java
nbradbury May 15, 2025
3d5584a
Fourth pass at tackling nullability issues with ReaderUtils.java
nbradbury May 15, 2025
ef090d2
Rename .java to .kt
nbradbury May 15, 2025
34c1a24
Auto-converted ReaderUtils to Kotlin
nbradbury May 15, 2025
8ba92b1
First pass at cleaning up ReaderUtils after conversion
nbradbury May 15, 2025
cd4cbf0
Second pass at cleaning up ReaderUtils after conversion
nbradbury May 15, 2025
127f2d7
Fixed Detekt warnings
nbradbury May 15, 2025
5260c77
Allow for null recycler view to fix failing tests
nbradbury May 15, 2025
c99e784
Fixed nullability in ReaderUtilsWrapper
nbradbury May 15, 2025
b17b8b5
Use Kotlin forEach instead of for loops
nbradbury May 15, 2025
7495e2a
Drop TextUtils
nbradbury May 15, 2025
86cc38c
Don't allow null recycler
nbradbury May 15, 2025
f57123c
Removed unnecessary fun
nbradbury May 15, 2025
516ab9f
Merge branch 'trunk' into issue/CMM-351-reader-utils-kotlin
nbradbury May 16, 2025
114e0a7
Merge remote-tracking branch 'origin/issue/CMM-351-reader-utils-kotli…
nbradbury May 16, 2025
2eb5538
Adjusted isValidUrlEncodedString to clarify return
nbradbury May 16, 2025
653ee26
Merge branch 'trunk' into issue/CMM-351-reader-utils-kotlin
nbradbury May 16, 2025
ba2202b
Rename .java to .kt
nbradbury May 16, 2025
3be7ddd
Auto-conversion of ImageSizeMap to Kotlin
nbradbury May 16, 2025
56a4f03
Fixed issues with auto-conversion
nbradbury May 16, 2025
1f16c83
Added getKeyFromImageUrl
nbradbury May 16, 2025
1501df8
Merge branch 'trunk' into issue/CMM-351-reader-imagesizemap
nbradbury May 16, 2025
914003d
Added null check on path for safety
nbradbury May 16, 2025
fafdd88
Updated ReaderUtils from trunk to fix merge conflict
nbradbury May 16, 2025
8f3640d
Merge branch 'trunk' into issue/CMM-351-reader-imagesizemap
nbradbury May 16, 2025
56b4f9d
Merge branch 'trunk' into issue/CMM-351-reader-imagesizemap
adalpari May 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.wordpress.android.ui.reader.utils

import org.json.JSONException
import org.json.JSONObject
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.JSONUtils
import org.wordpress.android.util.UrlUtils
import androidx.core.net.toUri

/**
* hash map of sizes of attachments in a reader post - created from the json "attachments" section
* of the post endpoints
*/
class ImageSizeMap(private val postContent: String, private val jsonString: String) :
HashMap<String, ImageSizeMap.ImageSize>() {
init {
if (jsonString.isNotEmpty() && jsonString != EMPTY_JSON) {
parseJson()
}
}

@Suppress("NestedBlockDepth")
private fun parseJson() {
try {
val json = JSONObject(jsonString)
val keys = json.keys()
while (keys.hasNext()) {
json.optJSONObject(keys.next())?.let { jsonAttach ->
val mimeType = JSONUtils.getString(jsonAttach, "mime_type")
if (mimeType.startsWith("image")) {
parseJsonImage(jsonAttach)
}
}
}
} catch (e: JSONException) {
AppLog.e(AppLog.T.READER, e)
}
}

private fun parseJsonImage(jsonImage: JSONObject) {
val imageUrl = JSONUtils.getString(
jsonImage,
"URL"
)
val key = getKeyFromImageUrl(imageUrl)

// make sure this image actually appears in the post content - it's possible for
// an image to be in the attachments but not in the post itself
val path = key.toUri().path
if (path != null && postContent.contains(path)) {
var width = jsonImage.optInt("width")
var height = jsonImage.optInt("height")

// check if data-orig-size is present and use it
val originalSize = jsonImage.optString("data-orig-size")
val sizes = originalSize.split(",".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()
if (sizes.size == 2) {
width = sizes[0].toInt()
height = sizes[1].toInt()
}

this[key] =
ImageSize(
width,
height
)
}
}

fun getImageSize(imageUrl: String): ImageSize? {
return get(getKeyFromImageUrl(imageUrl))
}

private fun getKeyFromImageUrl(imageUrl: String) = UrlUtils.normalizeUrl(UrlUtils.removeQuery(imageUrl))

class ImageSize(val width: Int, val height: Int)

companion object {
private const val EMPTY_JSON = "{}"
}
}