Skip to content

Commit

Permalink
Option to automatically add to favorites (closes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Dec 21, 2022
1 parent 4630147 commit 1c90da3
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 27 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bnyro.wallpaper.ext

fun <T> awaitQuery(block: () -> T): T {
var result: T? = null
Thread {
result = block.invoke()
}.apply {
start()
join()
}
return result!!
}
32 changes: 16 additions & 16 deletions app/src/main/java/com/bnyro/wallpaper/ext/FormatBinarySize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/ext/Query.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.bnyro.wallpaper.ext

fun Query(query: () -> Unit) {
fun query(query: () -> Unit) {
Thread(query).start()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -62,7 +62,7 @@ fun WallpaperGrid(
}

LaunchedEffect(true) {
Query {
query {
it.imgSrc.let { liked = Database.favoritesDao().exists(it) }
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,7 +88,7 @@ fun WallpaperPreview(
}

LaunchedEffect(true) {
Query {
query {
wallpaper.imgSrc.let {
liked = Database.favoritesDao().exists(it)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ fun FavoritesPage() {
}

LaunchedEffect(true) {
Query {
query {
favorites = Database.favoritesDao().getAll()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

<!-- Favorites -->
<string name="no_favorites">No favorites yet</string>
<string name="auto_add_to_favorites">Add to favorites when applying</string>

<!-- About -->
<string name="version">Version</string>
Expand Down

0 comments on commit 1c90da3

Please sign in to comment.