From 1c90da33176df51fda2901eefbe6ab9d6fa9f485 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 21 Dec 2022 16:40:13 +0100 Subject: [PATCH] Option to automatically add to favorites (closes #17) --- .../com/bnyro/wallpaper/ext/AwaitQuery.kt | 12 +++++++ .../bnyro/wallpaper/ext/FormatBinarySize.kt | 32 +++++++++---------- .../java/com/bnyro/wallpaper/ext/Query.kt | 2 +- .../wallpaper/ui/components/BlockButton.kt | 4 ++- .../wallpaper/ui/components/WallpaperGrid.kt | 6 ++-- .../ui/components/WallpaperPreview.kt | 14 ++++++-- .../bnyro/wallpaper/ui/pages/FavoritesPage.kt | 4 +-- .../bnyro/wallpaper/ui/pages/SettingsPage.kt | 4 +++ .../com/bnyro/wallpaper/util/Preferences.kt | 2 +- app/src/main/res/values/strings.xml | 1 + 10 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt diff --git a/app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt b/app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt new file mode 100644 index 00000000..cf6651e3 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt @@ -0,0 +1,12 @@ +package com.bnyro.wallpaper.ext + +fun awaitQuery(block: () -> T): T { + var result: T? = null + Thread { + result = block.invoke() + }.apply { + start() + join() + } + return result!! +} diff --git a/app/src/main/java/com/bnyro/wallpaper/ext/FormatBinarySize.kt b/app/src/main/java/com/bnyro/wallpaper/ext/FormatBinarySize.kt index 439f7930..6a9edbfd 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ext/FormatBinarySize.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ext/FormatBinarySize.kt @@ -10,31 +10,31 @@ fun Long.formatBinarySize(): String { this < kiloByteAsByte -> "${this.toDouble()} B" this >= kiloByteAsByte && this < megaByteAsByte -> "${ - String.format( - "%.2f", - (this / kiloByteAsByte) - ) + String.format( + "%.2f", + (this / kiloByteAsByte) + ) } KB" this >= megaByteAsByte && this < gigaByteAsByte -> "${ - String.format( - "%.2f", - (this / megaByteAsByte) - ) + String.format( + "%.2f", + (this / megaByteAsByte) + ) } MB" this >= gigaByteAsByte && this < teraByteAsByte -> "${ - String.format( - "%.2f", - (this / gigaByteAsByte) - ) + String.format( + "%.2f", + (this / gigaByteAsByte) + ) } GB" this >= teraByteAsByte && this < petaByteAsByte -> "${ - String.format( - "%.2f", - (this / teraByteAsByte) - ) + String.format( + "%.2f", + (this / teraByteAsByte) + ) } TB" else -> "Bigger than 1024 TB" } diff --git a/app/src/main/java/com/bnyro/wallpaper/ext/Query.kt b/app/src/main/java/com/bnyro/wallpaper/ext/Query.kt index 3edcc696..4d15bb8c 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ext/Query.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ext/Query.kt @@ -1,5 +1,5 @@ package com.bnyro.wallpaper.ext -fun Query(query: () -> Unit) { +fun query(query: () -> Unit) { Thread(query).start() } diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/BlockButton.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/BlockButton.kt index 75f4738c..863c0da1 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/BlockButton.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/BlockButton.kt @@ -27,7 +27,9 @@ fun BlockButton( modifier = modifier .height(50.dp) .clip(RoundedCornerShape(12.dp)) - .background(if (selected) MaterialTheme.colorScheme.surfaceVariant.copy(0.5f) else MaterialTheme.colorScheme.primaryContainer) + .background( + if (selected) MaterialTheme.colorScheme.surfaceVariant.copy(0.5f) else MaterialTheme.colorScheme.primaryContainer + ) .clickable(onClick = onClick) .padding(horizontal = 15.dp), verticalArrangement = Arrangement.Center, diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt index 051180a0..81d70f12 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt @@ -29,7 +29,7 @@ import androidx.compose.ui.unit.dp import coil.compose.AsyncImage import com.bnyro.wallpaper.db.DatabaseHolder.Companion.Database import com.bnyro.wallpaper.db.obj.Wallpaper -import com.bnyro.wallpaper.ext.Query +import com.bnyro.wallpaper.ext.query @Composable fun WallpaperGrid( @@ -62,7 +62,7 @@ fun WallpaperGrid( } LaunchedEffect(true) { - Query { + query { it.imgSrc.let { liked = Database.favoritesDao().exists(it) } } } @@ -95,7 +95,7 @@ fun WallpaperGrid( if (liked) Icons.Default.Favorite else Icons.Default.FavoriteBorder ) { liked = !liked - Query { + query { if (!liked) { Database.favoritesDao().delete(it) } else { diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperPreview.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperPreview.kt index 6b12d681..10ddde92 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperPreview.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperPreview.kt @@ -42,10 +42,12 @@ import androidx.palette.graphics.Palette import com.bnyro.wallpaper.R import com.bnyro.wallpaper.db.DatabaseHolder.Companion.Database import com.bnyro.wallpaper.db.obj.Wallpaper -import com.bnyro.wallpaper.ext.Query +import com.bnyro.wallpaper.ext.awaitQuery +import com.bnyro.wallpaper.ext.query import com.bnyro.wallpaper.util.BitmapProcessor import com.bnyro.wallpaper.util.DownloadHelper import com.bnyro.wallpaper.util.ImageHelper +import com.bnyro.wallpaper.util.Preferences import com.bnyro.wallpaper.util.WallpaperHelper import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -86,7 +88,7 @@ fun WallpaperPreview( } LaunchedEffect(true) { - Query { + query { wallpaper.imgSrc.let { liked = Database.favoritesDao().exists(it) } @@ -156,7 +158,7 @@ fun WallpaperPreview( icon = if (liked) Icons.Default.Favorite else Icons.Default.FavoriteBorder ) { liked = !liked - Query { + query { if (!liked) { Database.favoritesDao().delete(wallpaper) } else { @@ -226,6 +228,12 @@ fun WallpaperPreview( showModeSelection = false }, onClick = { + if (Preferences.getBoolean(Preferences.autoAddToFavoritesKey, false)) { + liked = true + awaitQuery { + Database.favoritesDao().insertAll(wallpaper) + } + } CoroutineScope(Dispatchers.IO).launch { WallpaperHelper.setWallpaper( context.applicationContext, diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt b/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt index 53d49989..7bbb960a 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt @@ -26,7 +26,7 @@ import androidx.compose.ui.unit.sp import com.bnyro.wallpaper.R import com.bnyro.wallpaper.db.DatabaseHolder.Companion.Database import com.bnyro.wallpaper.db.obj.Wallpaper -import com.bnyro.wallpaper.ext.Query +import com.bnyro.wallpaper.ext.query import com.bnyro.wallpaper.ui.components.WallpaperGrid @Composable @@ -40,7 +40,7 @@ fun FavoritesPage() { } LaunchedEffect(true) { - Query { + query { favorites = Database.favoritesDao().getAll() } } diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt b/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt index 64b85f43..204fed32 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt @@ -55,6 +55,10 @@ fun SettingsPage( prefKey = Preferences.cropImagesKey, title = stringResource(R.string.crop_images) ) + CheckboxPref( + prefKey = Preferences.autoAddToFavoritesKey, + title = stringResource(R.string.auto_add_to_favorites) + ) } } diff --git a/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt b/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt index 1e95e290..9a632c37 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt @@ -12,7 +12,7 @@ object Preferences { const val wallpaperChangerTargetKey = "wallpaperChangerTarget" const val themeModeKey = "themeModeKey" const val wallpaperChangerApiKey = "wallpaperChangerApi" - + const val autoAddToFavoritesKey = "autoAddToFavorites" const val grayscaleKey = "grayscale" const val blurKey = "blur" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c81a5368..e2b59998 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,6 +50,7 @@ No favorites yet + Add to favorites when applying Version