Merge pull request #5443 from RafaelsRamos/imp/feed

fix: Improve home feed layout and fix issue where incorrect cache size was defaulted.
This commit is contained in:
Bnyro 2024-01-06 14:03:31 +01:00 committed by GitHub
commit 9c2abeeca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View File

@ -20,44 +20,52 @@ import com.github.libretube.util.DataSaverMode
import java.nio.file.Path import java.nio.file.Path
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File
object ImageHelper { object ImageHelper {
lateinit var imageLoader: ImageLoader lateinit var imageLoader: ImageLoader
private val Context.coilFile get() = cacheDir.resolve("coil")
/** /**
* Initialize the image loader * Initialize the image loader
*/ */
fun initializeImageLoader(context: Context) { fun initializeImageLoader(context: Context) {
val maxImageCacheSize = PreferenceHelper.getString( val maxCacheSize = PreferenceHelper.getString(PreferenceKeys.MAX_IMAGE_CACHE, "128")
PreferenceKeys.MAX_IMAGE_CACHE,
""
)
imageLoader = ImageLoader.Builder(context) imageLoader = ImageLoader.Builder(context)
.callFactory(CronetHelper.callFactory) .callFactory(CronetHelper.callFactory)
.crossfade(true)
.apply { .apply {
when (maxImageCacheSize) { if (maxCacheSize.isEmpty()) {
"" -> { diskCachePolicy(CachePolicy.DISABLED)
diskCachePolicy(CachePolicy.DISABLED) } else {
} diskCachePolicy(CachePolicy.ENABLED)
memoryCachePolicy(CachePolicy.ENABLED)
else -> diskCache( val diskCache = generateDiskCache(
DiskCache.Builder() directory = context.coilFile,
.directory(context.cacheDir.resolve("coil")) size = maxCacheSize.toInt()
.maxSizeBytes(maxImageCacheSize.toInt() * 1024 * 1024L)
.build()
) )
diskCache(diskCache)
} }
} }
.build() .build()
} }
private fun generateDiskCache(directory: File, size: Int): DiskCache {
return DiskCache.Builder()
.directory(directory)
.maxSizeBytes(size * 1024 * 1024L)
.build()
}
/** /**
* load an image from a url into an imageView * load an image from a url into an imageView
*/ */
fun loadImage(url: String?, target: ImageView) { fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled // only load the image if the data saver mode is disabled
if (DataSaverMode.isEnabled(target.context) || url == null) return if (DataSaverMode.isEnabled(target.context) || url.isNullOrEmpty()) return
val urlToLoad = ProxyHelper.unwrapImageUrl(url) val urlToLoad = ProxyHelper.unwrapImageUrl(url)
val request = ImageRequest.Builder(target.context) val request = ImageRequest.Builder(target.context)

View File

@ -167,6 +167,7 @@ class HomeFragment : Fragment() {
filteredFeed.take(20).toMutableList(), filteredFeed.take(20).toMutableList(),
forceMode = VideosAdapter.Companion.LayoutMode.RELATED_COLUMN forceMode = VideosAdapter.Companion.LayoutMode.RELATED_COLUMN
) )
binding.featuredRV.setHasFixedSize(true)
} }
private suspend fun loadBookmarks() { private suspend fun loadBookmarks() {

View File

@ -35,7 +35,8 @@
android:id="@+id/featuredRV" android:id="@+id/featuredRV"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp" android:paddingHorizontal="10dp"
android:clipToPadding="false"
android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false"
android:visibility="gone" /> android:visibility="gone" />
@ -48,7 +49,8 @@
android:id="@+id/watchingRV" android:id="@+id/watchingRV"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp" android:paddingHorizontal="10dp"
android:clipToPadding="false"
android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false"
android:visibility="gone" /> android:visibility="gone" />