77
88package 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
1014import com.nextcloud.client.database.entity.FileEntity
1115import com.nextcloud.client.preferences.AppPreferences
1216import com.nextcloud.utils.extensions.filterFilenames
1317import com.nextcloud.utils.extensions.isTempFile
18+ import com.nextcloud.utils.extensions.toServerCredentials
1419import com.owncloud.android.MainApp
1520import com.owncloud.android.datamodel.OCFile
21+ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
22+ import com.owncloud.android.lib.common.utils.Log_OC
1623import com.owncloud.android.lib.resources.shares.ShareType
1724import com.owncloud.android.lib.resources.shares.ShareeUser
25+ import com.owncloud.android.lib.resources.status.NextcloudVersion
1826import com.owncloud.android.utils.FileSortOrder
1927import com.owncloud.android.utils.MimeTypeUtil
2028import kotlinx.coroutines.CoroutineScope
@@ -23,11 +31,14 @@ import kotlinx.coroutines.Job
2331import kotlinx.coroutines.SupervisorJob
2432import kotlinx.coroutines.launch
2533import kotlinx.coroutines.withContext
34+ import java.util.concurrent.ConcurrentHashMap
2635
2736class 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