Skip to content

Commit badec2f

Browse files
committed
Feature: Thumbnail Loading Fix & Caching Improvements
1 parent 4b68373 commit badec2f

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

opencloudApp/src/main/java/eu/opencloud/android/presentation/thumbnails/ThumbnailsRequester.kt

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package eu.opencloud.android.presentation.thumbnails
2222

2323
import android.accounts.Account
24+
import android.accounts.AccountManager
2425
import android.net.Uri
2526
import coil.ImageLoader
2627
import coil.disk.DiskCache
@@ -53,18 +54,48 @@ object ThumbnailsRequester : KoinComponent {
5354
private val clientManager: ClientManager by inject()
5455

5556
private const val SPACE_SPECIAL_PREVIEW_URI = "%s?scalingup=0&a=1&x=%d&y=%d&c=%s&preview=1"
56-
private const val FILE_PREVIEW_URI = "%s%s?x=%d&y=%d&c=%s&preview=1&id=%s"
57+
private const val FILE_PREVIEW_URI = "%s/remote.php/webdav%s?x=%d&y=%d&c=%s&preview=1"
5758

58-
private const val DISK_CACHE_SIZE: Long = 1024 * 1024 * 10 // 10MB
59+
private const val DISK_CACHE_SIZE: Long = 1024 * 1024 * 100 // 100MB
5960

6061
private var imageLoader: ImageLoader? = null
6162
private var currentAccountName: String? = null
6263

64+
fun getAvatarUri(account: Account): String {
65+
val accountManager = AccountManager.get(appContext)
66+
val baseUrl = accountManager.getUserData(account, eu.opencloud.android.lib.common.accounts.AccountUtils.Constants.KEY_OC_BASE_URL)
67+
val username = AccountUtils.getUsernameOfAccount(account.name)
68+
return "$baseUrl/index.php/avatar/${android.net.Uri.encode(username)}/384"
69+
}
70+
71+
fun getPreviewUriForFile(file: OCFile, account: Account, etag: String? = null): String {
72+
return getPreviewUri(file.remotePath, etag ?: file.etag, account)
73+
}
74+
75+
fun getPreviewUriForFile(fileWithSyncInfo: OCFileWithSyncInfo, account: Account): String {
76+
return getPreviewUriForFile(fileWithSyncInfo.file, account)
77+
}
78+
79+
fun getPreviewUriForSpaceSpecial(spaceSpecial: SpaceSpecial): String {
80+
return String.format(Locale.US, SPACE_SPECIAL_PREVIEW_URI, spaceSpecial.webDavUrl, 1024, 1024, spaceSpecial.eTag)
81+
}
82+
83+
private fun getPreviewUri(remotePath: String?, etag: String?, account: Account): String {
84+
val accountManager = AccountManager.get(appContext)
85+
val baseUrl = accountManager.getUserData(account, eu.opencloud.android.lib.common.accounts.AccountUtils.Constants.KEY_OC_BASE_URL)
86+
87+
val path = if (remotePath?.startsWith("/") == true) remotePath else "/$remotePath"
88+
val encodedPath = Uri.encode(path, "/")
89+
90+
return String.format(Locale.US, FILE_PREVIEW_URI, baseUrl, encodedPath, 1024, 1024, etag)
91+
}
92+
6393
fun getCoilImageLoader(): ImageLoader {
6494
val accountName = AccountUtils.getCurrentOpenCloudAccount(appContext).name
6595
if (imageLoader == null || currentAccountName != accountName) {
96+
imageLoader?.shutdown()
6697
currentAccountName = accountName
67-
val openCloudClient = getOpenCloudClient()
98+
val openCloudClient = clientManager.getClientForCoilThumbnails(accountName)
6899

69100
val coilRequestHeaderInterceptor = CoilRequestHeaderInterceptor(
70101
requestHeaders = hashMapOf(
@@ -89,7 +120,11 @@ object ThumbnailsRequester : KoinComponent {
89120
.directory(appContext.cacheDir.resolve("thumbnails_coil_cache"))
90121
.maxSizeBytes(DISK_CACHE_SIZE)
91122
.build()
92-
spacesThumbnailSize,
123+
}
124+
.build()
125+
}
126+
return imageLoader!!
127+
}
93128

94129
private class CoilRequestHeaderInterceptor(
95130
private val requestHeaders: HashMap<String, String>
@@ -99,7 +134,7 @@ object ThumbnailsRequester : KoinComponent {
99134
val request = chain.request().newBuilder()
100135
requestHeaders.toHeaders().forEach { request.addHeader(it.first, it.second) }
101136
return chain.proceed(request.build()).newBuilder().removeHeader("Cache-Control")
102-
.addHeader("Cache-Control", "max-age=5000 , must-revalidate, value").build().also { Timber.d("Header :" + it.headers) }
137+
.addHeader("Cache-Control", "max-age=5000, must-revalidate").build().also { Timber.d("Header :" + it.headers) }
103138
}
104139
}
105140
}

opencloudApp/src/main/java/eu/opencloud/android/ui/adapter/ReceiveExternalFilesAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
158158
.build();
159159
imageLoader.enqueue(request);
160160
} else {
161+
ImageLoader imageLoader = ThumbnailsRequester.INSTANCE.getCoilImageLoader();
162+
coil.request.ImageRequest request = new coil.request.ImageRequest.Builder(mContext)
163+
.data(null)
164+
.target(fileIcon)
165+
.build();
166+
imageLoader.enqueue(request);
161167
fileIcon.setImageResource(
162168
MimetypeIconUtil.getFileTypeIconId(file.getMimeType(), file.getFileName())
163169
);

0 commit comments

Comments
 (0)