Skip to content

Commit 0b15a1b

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent b31060e commit 0b15a1b

3 files changed

Lines changed: 45 additions & 71 deletions

File tree

app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ class RichDocumentsEditorWebView : EditorWebView() {
168168
val url = result.url.toUri()
169169
when (result.format) {
170170
PRINT -> printFile(url)
171+
171172
SLIDESHOW -> showSlideShow(url)
173+
172174
else -> {
173175
downloadFile(url, result.fileName)
174176
}
@@ -211,12 +213,9 @@ class RichDocumentsEditorWebView : EditorWebView() {
211213
}
212214

213215
companion object {
214-
private const val URL = "URL"
215216
private const val HYPERLINK = "Url"
216-
private const val TYPE = "Type"
217217
private const val PRINT = "print"
218218
private const val SLIDESHOW = "slideshow"
219219
private const val NEW_NAME = "NewName"
220-
private const val FILENAME = "filename"
221220
}
222221
}

app/src/main/java/com/owncloud/android/ui/model/DownloadAs.kt

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,4 @@
77

88
package com.owncloud.android.ui.model
99

10-
import com.owncloud.android.lib.common.utils.Log_OC
11-
import kotlinx.serialization.SerialName
12-
import kotlinx.serialization.Serializable
13-
import kotlinx.serialization.SerializationException
14-
import kotlinx.serialization.json.Json
15-
16-
@Serializable
17-
data class DownloadAsV1(
18-
@SerialName("Type") val type: String,
19-
@SerialName("URL") val url: String,
20-
@SerialName("filename") val fileName: String
21-
) {
22-
companion object {
23-
fun tryDeserialize(json: String): DownloadAsResult? {
24-
return try {
25-
val v1 = jsonImpl.decodeFromString<DownloadAsV1>(json)
26-
return DownloadAsResult(
27-
format = v1.type,
28-
fileName = v1.fileName,
29-
url = v1.url
30-
)
31-
} catch (e: SerializationException) {
32-
Log_OC.e("DownloadAsV1", "tryDeserialize: $e")
33-
null
34-
}
35-
}
36-
}
37-
}
38-
39-
@Serializable
40-
data class DownloadAsV2(
41-
@SerialName("format") val format: String,
42-
@SerialName("name") val fileName: String,
43-
@SerialName("url") val url: String
44-
) {
45-
companion object {
46-
fun tryDeserialize(json: String): DownloadAsResult? {
47-
return try {
48-
val v2 = jsonImpl.decodeFromString<DownloadAsV2>(json)
49-
return DownloadAsResult(
50-
format = v2.format,
51-
fileName = v2.fileName,
52-
url = v2.url
53-
)
54-
} catch (e: SerializationException) {
55-
Log_OC.e("DownloadAsV2", "tryDeserialize: $e")
56-
null
57-
}
58-
}
59-
}
60-
}
61-
62-
data class DownloadAsResult(
63-
val format: String,
64-
val fileName: String,
65-
val url: String
66-
)
67-
68-
private val jsonImpl = Json { ignoreUnknownKeys = true }
10+
data class DownloadAs(val format: String, val fileName: String, val url: String)

app/src/main/java/com/owncloud/android/utils/RichDocumentDownloadAsParser.kt

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,52 @@
77

88
package com.owncloud.android.utils
99

10-
import com.owncloud.android.ui.model.DownloadAsResult
11-
import com.owncloud.android.ui.model.DownloadAsV1
12-
import com.owncloud.android.ui.model.DownloadAsV2
10+
import com.owncloud.android.lib.common.utils.Log_OC
11+
import com.owncloud.android.ui.model.DownloadAs
12+
import kotlinx.serialization.json.Json
13+
import kotlinx.serialization.json.JsonObject
14+
import kotlinx.serialization.json.contentOrNull
15+
import kotlinx.serialization.json.jsonObject
16+
import kotlinx.serialization.json.jsonPrimitive
1317

1418
object RichDocumentDownloadAsParser {
15-
fun parse(json: String?): DownloadAsResult? {
16-
if (json.isNullOrBlank()) return null
1719

18-
var result = DownloadAsV2.tryDeserialize(json)
19-
if (result == null) {
20-
result = DownloadAsV1.tryDeserialize(json)
20+
private const val TAG = "RichDocumentDownloadAsParser"
21+
private const val URL_UPPERCASE = "URL"
22+
private const val URL_LOWERCASE = "url"
23+
24+
private const val FORMAT = "format"
25+
private const val NAME = "name"
26+
private const val TYPE = "Type"
27+
private const val FILENAME = "filename"
28+
29+
private val json = Json { ignoreUnknownKeys = true }
30+
31+
fun parse(jsonString: String?): DownloadAs? {
32+
if (jsonString.isNullOrBlank()) return null
33+
34+
return try {
35+
val obj = json.parseToJsonElement(jsonString).jsonObject
36+
val url = obj[URL_LOWERCASE]?.jsonPrimitive?.contentOrNull
37+
?: obj[URL_UPPERCASE]?.jsonPrimitive?.contentOrNull
38+
tryParseV2(obj, url) ?: tryParseV1(obj, url)
39+
} catch (e: Exception) {
40+
Log_OC.e(TAG, "parse failed: $e")
41+
null
2142
}
43+
}
44+
45+
private fun tryParseV2(obj: JsonObject, url: String?): DownloadAs? {
46+
val format = obj[FORMAT]?.jsonPrimitive?.contentOrNull
47+
val name = obj[NAME]?.jsonPrimitive?.contentOrNull
48+
if (format == null || url == null) return null
49+
return DownloadAs(format = format, fileName = name ?: "", url = url)
50+
}
2251

23-
return result
52+
private fun tryParseV1(obj: JsonObject, url: String?): DownloadAs? {
53+
val type = obj[TYPE]?.jsonPrimitive?.contentOrNull
54+
val filename = obj[FILENAME]?.jsonPrimitive?.contentOrNull
55+
if (type == null || url == null) return null
56+
return DownloadAs(format = type, fileName = filename ?: "", url = url)
2457
}
2558
}

0 commit comments

Comments
 (0)