diff --git a/app/src/main/java/app/suhasdissa/memerize/MemerizeApplication.kt b/app/src/main/java/app/suhasdissa/memerize/MemerizeApplication.kt index 35029b9..ef5e325 100644 --- a/app/src/main/java/app/suhasdissa/memerize/MemerizeApplication.kt +++ b/app/src/main/java/app/suhasdissa/memerize/MemerizeApplication.kt @@ -10,8 +10,14 @@ package app.suhasdissa.memerize import android.app.Application import app.suhasdissa.memerize.backend.database.MemeDatabase import app.suhasdissa.memerize.utils.UpdateUtil +import app.suhasdissa.memerize.utils.defaultImageCacheSize +import app.suhasdissa.memerize.utils.imageCacheKey +import app.suhasdissa.memerize.utils.preferences +import coil.ImageLoader +import coil.ImageLoaderFactory +import coil.disk.DiskCache -class MemerizeApplication : Application() { +class MemerizeApplication : Application(), ImageLoaderFactory { private val database by lazy { MemeDatabase.getDatabase(this) } lateinit var container: AppContainer @@ -20,4 +26,18 @@ class MemerizeApplication : Application() { container = DefaultAppContainer(database) UpdateUtil.getCurrentVersion(this.applicationContext) } + + override fun newImageLoader(): ImageLoader { + return ImageLoader.Builder(this) + .crossfade(true) + .respectCacheHeaders(false) + .diskCache( + DiskCache.Builder() + .directory(cacheDir.resolve("coil")) + .maxSizeBytes( + preferences.getInt(imageCacheKey, defaultImageCacheSize) * 1024 * 1024L + ) + .build() + ).build() + } } diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/components/CacheSizeDialog.kt b/app/src/main/java/app/suhasdissa/memerize/ui/components/CacheSizeDialog.kt new file mode 100644 index 0000000..4b0e8c7 --- /dev/null +++ b/app/src/main/java/app/suhasdissa/memerize/ui/components/CacheSizeDialog.kt @@ -0,0 +1,60 @@ +/******************************************************************************* +Created By Suhas Dissanayake on 8/9/23, 9:56 PM +Copyright (c) 2023 +https://github.com/SuhasDissa/ +All Rights Reserved + ******************************************************************************/ + +package app.suhasdissa.memerize.ui.components + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.lazy.grid.GridCells +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid +import androidx.compose.foundation.lazy.grid.items +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.FilterChip +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue +import androidx.compose.ui.unit.dp +import app.suhasdissa.memerize.utils.defaultImageCacheSize +import app.suhasdissa.memerize.utils.imageCacheKey +import app.suhasdissa.memerize.utils.rememberPreference + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun CacheSizeDialog(onDismissRequest: () -> Unit) { + val cacheSizes = listOf(16, 32, 64, 128, 256, 512, 1024, 2048) + var prefSize by rememberPreference(key = imageCacheKey, defaultValue = defaultImageCacheSize) + AlertDialog( + onDismissRequest, + title = { Text("Change Image Cache Size") }, + confirmButton = { + Button(onClick = { + onDismissRequest.invoke() + }) { + Text(text = "Ok") + } + }, + text = { + LazyVerticalGrid( + columns = GridCells.Fixed(3), + verticalArrangement = Arrangement.spacedBy(8.dp), + horizontalArrangement = Arrangement.spacedBy(8.dp) + ) { + items(items = cacheSizes) { + FilterChip( + selected = prefSize == it, + onClick = { prefSize = it }, + label = { + Text("$it MB") + } + ) + } + } + } + ) +} diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/components/HighlightCard.kt b/app/src/main/java/app/suhasdissa/memerize/ui/components/HighlightCard.kt index 0aecc6c..a5853bc 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/components/HighlightCard.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/components/HighlightCard.kt @@ -9,7 +9,6 @@ package app.suhasdissa.memerize.ui.components import android.view.SoundEffectConstants import androidx.compose.foundation.ExperimentalFoundationApi -import androidx.compose.foundation.Image import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row @@ -30,7 +29,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.painterResource @@ -39,14 +37,12 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import app.suhasdissa.memerize.R import coil.compose.AsyncImage -import coil.request.ImageRequest @OptIn(ExperimentalFoundationApi::class) @Composable fun HighlightCard( onClick: () -> Unit, name: String, - thumbnail: Int? = null, thumbnail_url: String? = null, highlighted: Boolean = false, onLongClick: () -> Unit = {} @@ -84,18 +80,9 @@ fun HighlightCard( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceEvenly ) { - if (thumbnail != null) { - Image( - modifier = Modifier - .size(90.dp) - .clip(CircleShape), - painter = painterResource(thumbnail), - contentDescription = null - ) - } else if (thumbnail_url != null) { + if (thumbnail_url != null) { AsyncImage( - model = ImageRequest.Builder(context = LocalContext.current).data(thumbnail_url) - .crossfade(true).build(), + model = thumbnail_url, contentDescription = null, modifier = Modifier .size(90.dp) @@ -119,5 +106,5 @@ fun HighlightCard( @Preview() @Composable fun HighlightCardPreview() { - HighlightCard(onClick = {}, name = "Preview", thumbnail = R.drawable.reddit_placeholder) + HighlightCard(onClick = {}, name = "Preview") } diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/components/ImageCard.kt b/app/src/main/java/app/suhasdissa/memerize/ui/components/ImageCard.kt index 9fcc4c5..7dcd3fb 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/components/ImageCard.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/components/ImageCard.kt @@ -21,7 +21,6 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource @@ -29,7 +28,6 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import app.suhasdissa.memerize.R import coil.compose.AsyncImage -import coil.request.ImageRequest @Composable fun ImageCard( @@ -60,8 +58,7 @@ fun ImageCard( ) Spacer(modifier = Modifier.height(20.dp)) AsyncImage( - model = ImageRequest.Builder(context = LocalContext.current).data(photoUrl) - .crossfade(true).build(), + model = photoUrl, contentDescription = stringResource(R.string.meme_photo), contentScale = ContentScale.FillWidth, modifier = Modifier.fillMaxWidth(), diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/components/SubredditCard.kt b/app/src/main/java/app/suhasdissa/memerize/ui/components/SubredditCard.kt index abd6ac0..12d5bbb 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/components/SubredditCard.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/components/SubredditCard.kt @@ -23,12 +23,10 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalView import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import coil.compose.AsyncImage -import coil.request.ImageRequest @Composable fun SubredditCardCompact( @@ -54,8 +52,7 @@ fun SubredditCardCompact( .padding(8.dp) .aspectRatio(1f) .clip(CircleShape), - model = ImageRequest.Builder(context = LocalContext.current) - .data(thumbnail).crossfade(true).build(), + model = thumbnail, contentDescription = null, contentScale = ContentScale.Crop ) diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/CommunityScreen.kt b/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/CommunityScreen.kt index 114993c..7d7ca83 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/CommunityScreen.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/CommunityScreen.kt @@ -47,7 +47,6 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp @@ -57,7 +56,6 @@ import app.suhasdissa.memerize.backend.viewmodels.LemmyCommunityViewModel import app.suhasdissa.memerize.backend.viewmodels.state.AboutCommunityState import app.suhasdissa.memerize.ui.components.SubredditCardCompact import coil.compose.AsyncImage -import coil.request.ImageRequest @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -223,8 +221,7 @@ fun CommunityScreen( .padding(8.dp) .aspectRatio(1f) .clip(CircleShape), - model = ImageRequest.Builder(context = LocalContext.current) - .data(state.community.iconUrl).crossfade(true).build(), + model = state.community.iconUrl, contentDescription = null, contentScale = ContentScale.Crop ) diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/SubredditScreen.kt b/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/SubredditScreen.kt index 7295b64..0d1124c 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/SubredditScreen.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/screens/home/SubredditScreen.kt @@ -47,7 +47,6 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp @@ -56,7 +55,6 @@ import app.suhasdissa.memerize.backend.viewmodels.RedditCommunityViewModel import app.suhasdissa.memerize.backend.viewmodels.state.AboutCommunityState import app.suhasdissa.memerize.ui.components.SubredditCardCompact import coil.compose.AsyncImage -import coil.request.ImageRequest @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -208,8 +206,7 @@ fun SubredditScreen( .padding(8.dp) .aspectRatio(1f) .clip(CircleShape), - model = ImageRequest.Builder(context = LocalContext.current) - .data(state.community.iconUrl).crossfade(true).build(), + model = state.community.iconUrl, contentDescription = null, contentScale = ContentScale.Crop ) diff --git a/app/src/main/java/app/suhasdissa/memerize/ui/screens/settings/SettingsScreen.kt b/app/src/main/java/app/suhasdissa/memerize/ui/screens/settings/SettingsScreen.kt index 07ab877..6b2df51 100644 --- a/app/src/main/java/app/suhasdissa/memerize/ui/screens/settings/SettingsScreen.kt +++ b/app/src/main/java/app/suhasdissa/memerize/ui/screens/settings/SettingsScreen.kt @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Menu +import androidx.compose.material.icons.filled.Storage import androidx.compose.material.icons.outlined.Folder import androidx.compose.material.icons.outlined.Info import androidx.compose.material3.CenterAlignedTopAppBar @@ -28,10 +29,15 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import androidx.core.content.edit +import app.suhasdissa.memerize.ui.components.CacheSizeDialog import app.suhasdissa.memerize.ui.components.SettingItem import app.suhasdissa.memerize.utils.SaveDirectoryKey import app.suhasdissa.memerize.utils.preferences @@ -53,6 +59,7 @@ fun SettingsScreen( Log.d("FIle path", it.toString()) context.preferences.edit { putString(SaveDirectoryKey, it.toString()) } } + var showImageCacheDialog by remember { mutableStateOf(false) } Scaffold(modifier = Modifier.fillMaxSize(), topBar = { CenterAlignedTopAppBar(navigationIcon = { IconButton(onClick = { @@ -86,6 +93,16 @@ fun SettingsScreen( icon = Icons.Outlined.Folder ) } + item { + SettingItem( + title = "Image Cache Limit", + description = "Set Image Cache Limit", + onClick = { + showImageCacheDialog = true + }, + icon = Icons.Default.Storage + ) + } item { SettingItem( title = "About", @@ -96,4 +113,10 @@ fun SettingsScreen( } } } + + if (showImageCacheDialog) { + CacheSizeDialog { + showImageCacheDialog = false + } + } } diff --git a/app/src/main/java/app/suhasdissa/memerize/utils/Preferences.kt b/app/src/main/java/app/suhasdissa/memerize/utils/Preferences.kt index a1e0555..18bc11d 100644 --- a/app/src/main/java/app/suhasdissa/memerize/utils/Preferences.kt +++ b/app/src/main/java/app/suhasdissa/memerize/utils/Preferences.kt @@ -18,6 +18,9 @@ import androidx.compose.ui.platform.LocalContext import androidx.core.content.edit const val SaveDirectoryKey = "saveDirectory" +const val imageCacheKey = "imageCacheLimit" + +const val defaultImageCacheSize: Int = 256 inline fun > SharedPreferences.getEnum( key: String,