Skip to content

Commit 21beee4

Browse files
committed
add OCShare ext
Signed-off-by: alperozturk <[email protected]>
1 parent 3e52ee0 commit 21beee4

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.junit.Before
1919
import org.junit.Test
2020

2121
class CreateShareRemoteOperationIT : AbstractIT() {
22-
2322
@Before
2423
fun before() {
2524
val result = GetStatusRemoteOperation(context).execute(client)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
package com.owncloud.android.lib.resources.shares.extensions
9+
10+
import com.owncloud.android.lib.resources.shares.OCShare
11+
import org.json.JSONArray
12+
import org.json.JSONObject
13+
14+
private const val KEY = "key"
15+
private const val SCOPE_KEY = "scope"
16+
private const val DOWNLOAD_KEY = "download"
17+
private const val PERMISSIONS_KEY = "permissions"
18+
private const val VALUE_KEY = "value"
19+
private const val ENABLED_KEY = "enabled"
20+
21+
fun OCShare?.toggleAllowDownloadAndSync(
22+
isChecked: Boolean,
23+
useV2DownloadAttributes: Boolean
24+
): String? {
25+
val jsonArray =
26+
if (this?.attributes?.isEmpty() == true) {
27+
JSONArray()
28+
} else {
29+
JSONArray(this?.attributes)
30+
}
31+
val downloadAttr = jsonArray.findDownloadAttribute()
32+
val enabledKey = getEnabledKey(useV2DownloadAttributes)
33+
34+
if (downloadAttr != null) {
35+
downloadAttr.put(enabledKey, isChecked)
36+
} else {
37+
jsonArray.put(
38+
JSONObject().apply {
39+
put(KEY, DOWNLOAD_KEY)
40+
put(SCOPE_KEY, PERMISSIONS_KEY)
41+
put(enabledKey, isChecked)
42+
}
43+
)
44+
}
45+
46+
return jsonArray.toString()
47+
}
48+
49+
@Suppress("ReturnCount")
50+
fun OCShare?.isAllowDownloadAndSyncEnabled(useV2DownloadAttributes: Boolean): Boolean {
51+
if (this?.attributes.isNullOrEmpty()) return false
52+
53+
val jsonArray = JSONArray(this.attributes)
54+
val downloadAttr = jsonArray.findDownloadAttribute() ?: return false
55+
val enabledKey = getEnabledKey(useV2DownloadAttributes)
56+
57+
return downloadAttr.optBoolean(enabledKey, false)
58+
}
59+
60+
private fun JSONArray.findDownloadAttribute(): JSONObject? =
61+
(0 until length())
62+
.asSequence()
63+
.map { getJSONObject(it) }
64+
.find {
65+
it.optString(KEY) == DOWNLOAD_KEY &&
66+
it.optString(SCOPE_KEY) == PERMISSIONS_KEY
67+
}
68+
69+
private fun getEnabledKey(isV2: Boolean): String = if (isV2) VALUE_KEY else ENABLED_KEY

0 commit comments

Comments
 (0)