Skip to content

Commit e52db66

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 999e896 commit e52db66

2 files changed

Lines changed: 69 additions & 11 deletions

File tree

app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,12 @@ private void bindSharedAvatars(ListItemViewHolder holder, OCFile file) {
606606
if (sharedAvatars.getChildCount() > 0) {
607607
sharedAvatars.removeAllViews();
608608
}
609-
final var avatars = helper.getAvatarSharees(file, userId);
610-
sharedAvatars.setAvatars(user, avatars, viewThemeUtils);
611-
sharedAvatars.setOnClickListener(view -> ocFileListFragmentInterface.onShareIconClick(file));
609+
610+
helper.getAvatarSharees(file, user, userId, avatars -> {
611+
sharedAvatars.setAvatars(user, avatars, viewThemeUtils);
612+
sharedAvatars.setOnClickListener(view -> ocFileListFragmentInterface.onShareIconClick(file));
613+
return Unit.INSTANCE;
614+
});
612615
}
613616

614617
private void bindListItemViewHolder(ListItemViewHolder holder, OCFile file) {

app/src/main/java/com/owncloud/android/ui/adapter/helper/OCFileListAdapterHelper.kt

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77

88
package com.owncloud.android.ui.adapter.helper
99

10+
import com.nextcloud.android.common.ui.network.auth.ServerCredentials
11+
import com.nextcloud.android.common.ui.share.avatar.ShareAvatarRepository
12+
import com.nextcloud.android.common.ui.share.model.api.share.Share
13+
import com.nextcloud.client.account.User
1014
import com.nextcloud.client.database.entity.FileEntity
1115
import com.nextcloud.client.preferences.AppPreferences
1216
import com.nextcloud.utils.extensions.filterFilenames
1317
import com.nextcloud.utils.extensions.isTempFile
18+
import com.nextcloud.utils.extensions.toServerCredentials
1419
import com.owncloud.android.MainApp
1520
import com.owncloud.android.datamodel.OCFile
21+
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
22+
import com.owncloud.android.lib.common.utils.Log_OC
1623
import com.owncloud.android.lib.resources.shares.ShareType
1724
import com.owncloud.android.lib.resources.shares.ShareeUser
25+
import com.owncloud.android.lib.resources.status.NextcloudVersion
1826
import com.owncloud.android.utils.FileSortOrder
1927
import com.owncloud.android.utils.MimeTypeUtil
2028
import kotlinx.coroutines.CoroutineScope
@@ -23,11 +31,14 @@ import kotlinx.coroutines.Job
2331
import kotlinx.coroutines.SupervisorJob
2432
import kotlinx.coroutines.launch
2533
import kotlinx.coroutines.withContext
34+
import java.util.concurrent.ConcurrentHashMap
2635

2736
class OCFileListAdapterHelper {
2837
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
2938
private var job: Job? = null
3039

40+
private val remoteSharees = ConcurrentHashMap<String, List<ShareeUser>>()
41+
3142
@Suppress("LongParameterList")
3243
fun prepareFileList(
3344
directory: OCFile,
@@ -56,17 +67,56 @@ class OCFileListAdapterHelper {
5667
}
5768
}
5869

59-
fun getAvatarSharees(file: OCFile, userId: String?): List<ShareeUser> {
60-
val sharees = file.sharees
61-
val ownerId = file.ownerId
70+
fun getAvatarSharees(
71+
file: OCFile,
72+
user: User?,
73+
userId: String?,
74+
onComplete: (List<ShareeUser>) -> Unit
75+
) {
76+
scope.launch {
77+
val result = if (supportsUnifiedShare(user) && user != null) {
78+
val credentials = getServerCredentials(user) ?: return@launch
79+
val sourceId = file.remoteId
80+
val repository = ShareAvatarRepository(credentials).fetchShareAvatars(sourceId)
81+
repository?.toAvatarSharees() ?: listOf()
82+
} else {
83+
val sharees = file.sharees
84+
val ownerId = file.ownerId
85+
86+
val ownerSharee = if (!ownerId.isNullOrEmpty() && ownerId != userId) {
87+
ShareeUser(ownerId, file.ownerDisplayName, ShareType.USER).takeIf { it !in sharees }
88+
} else {
89+
null
90+
}
91+
92+
listOfNotNull(ownerSharee) + sharees.asReversed()
93+
}
6294

63-
val ownerSharee = if (!ownerId.isNullOrEmpty() && ownerId != userId) {
64-
ShareeUser(ownerId, file.ownerDisplayName, ShareType.USER).takeIf { it !in sharees }
65-
} else {
66-
null
95+
withContext(Dispatchers.Main) {
96+
onComplete(result)
97+
}
6798
}
99+
}
68100

69-
return listOfNotNull(ownerSharee) + sharees.asReversed()
101+
private fun supportsUnifiedShare(user: User?): Boolean {
102+
return user?.server?.version?.isNewerOrEqual(NextcloudVersion.nextcloud_34) == true
103+
}
104+
105+
private fun List<Share>.toAvatarSharees(): List<ShareeUser> = asSequence()
106+
.flatMap { share -> share.invitedRecipients }
107+
.distinctBy { recipient -> recipient.value }
108+
.map { recipient -> ShareeUser(recipient.value, recipient.displayName, ShareType.USER) }
109+
.toList()
110+
111+
@Suppress("TooGenericExceptionCaught")
112+
private fun getServerCredentials(user: User): ServerCredentials? = try {
113+
OwnCloudClientManagerFactory
114+
.getDefaultSingleton()
115+
.getClientFor(user.toOwnCloudAccount(), MainApp.getAppContext())
116+
.toServerCredentials(user.server.uri.toString())
117+
} catch (e: Exception) {
118+
Log_OC.e(TAG, "Failed to create client for share avatars", e)
119+
null
70120
}
71121

72122
suspend fun prepareFileList(
@@ -199,5 +249,10 @@ class OCFileListAdapterHelper {
199249
fun cleanup() {
200250
job?.cancel()
201251
job = null
252+
remoteSharees.clear()
253+
}
254+
255+
companion object {
256+
private val TAG = OCFileListAdapterHelper::class.java.simpleName
202257
}
203258
}

0 commit comments

Comments
 (0)