Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Add option to change image cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Aug 9, 2023
1 parent 2ab4b41 commit 5d56363
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 33 deletions.
22 changes: 21 additions & 1 deletion app/src/main/java/app/suhasdissa/memerize/MemerizeApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
)
}
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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)
Expand All @@ -119,5 +106,5 @@ fun HighlightCard(
@Preview()
@Composable
fun HighlightCardPreview() {
HighlightCard(onClick = {}, name = "Preview", thumbnail = R.drawable.reddit_placeholder)
HighlightCard(onClick = {}, name = "Preview")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ 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
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(
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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",
Expand All @@ -96,4 +113,10 @@ fun SettingsScreen(
}
}
}

if (showImageCacheDialog) {
CacheSizeDialog {
showImageCacheDialog = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reified T : Enum<T>> SharedPreferences.getEnum(
key: String,
Expand Down

0 comments on commit 5d56363

Please sign in to comment.