Skip to content

Commit

Permalink
feat: option to not apply image filters at wallpaper changer (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Apr 21, 2024
1 parent 2f53221 commit 8b8523b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.bnyro.wallpaper.ui.nav.DrawerScreens
data class WallpaperConfig(
var target: WallpaperTarget = WallpaperTarget.BOTH,
var source: WallpaperSource = WallpaperSource.ONLINE,
var selectedApiRoutes: List<String> = listOf(DrawerScreens.apiScreens.first().route),
var applyImageFilters: Boolean = true,
var selectedApiRoutes: List<String> = listOf(DrawerScreens.apiScreens[0].route),
var localFolderUris: List<String> = listOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.enums.WallpaperConfig
import com.bnyro.wallpaper.enums.WallpaperSource
import com.bnyro.wallpaper.enums.WallpaperTarget
import com.bnyro.wallpaper.ui.components.prefs.CheckboxPref
import com.bnyro.wallpaper.ui.components.prefs.MultiSelectionBlockPreference
import com.bnyro.wallpaper.ui.components.prefs.ListPreference
import com.bnyro.wallpaper.ui.components.prefs.SettingsCategory
Expand Down Expand Up @@ -143,4 +144,17 @@ fun WallpaperChangerPref(config: WallpaperConfig, onChange: (WallpaperConfig) ->
else -> Unit
}
}

var applyImageFilters by remember {
mutableStateOf(config.applyImageFilters)
}
CheckboxPref(
prefKey = null,
title = stringResource(id = R.string.apply_image_filters),
defaultValue = applyImageFilters
) { newValue ->
config.applyImageFilters = newValue
applyImageFilters = newValue
onChange(config)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ import com.bnyro.wallpaper.util.Preferences

@Composable
fun CheckboxPref(
prefKey: String,
prefKey: String? = null,
title: String,
summary: String? = null,
defaultValue: Boolean = false,
onCheckedChange: (Boolean) -> Unit = {}
) {
var checked by remember {
mutableStateOf(
Preferences.getBoolean(prefKey, defaultValue)
prefKey?.let { Preferences.getBoolean(it, defaultValue) } ?: defaultValue
)
}
val interactionSource = remember { MutableInteractionSource() }

fun onChange(newValue: Boolean) {
checked = newValue
if (prefKey != null) Preferences.edit { putBoolean(prefKey, checked) }
onCheckedChange.invoke(checked)
}

Row(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -40,8 +46,7 @@ fun CheckboxPref(
interactionSource = interactionSource,
indication = null
) {
checked = !checked
Preferences.edit { putBoolean(prefKey, checked) }
onChange(!checked)
},
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
Expand All @@ -53,11 +58,7 @@ fun CheckboxPref(
)
Checkbox(
checked = checked,
onCheckedChange = {
checked = it
Preferences.edit { putBoolean(prefKey, it) }
onCheckedChange.invoke(it)
}
onCheckedChange = ::onChange
)
}
}
18 changes: 13 additions & 5 deletions app/src/main/java/com/bnyro/wallpaper/util/BackgroundWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ class BackgroundWorker(
else -> return
} ?: return

WallpaperHelper.setWallpaperWithFilters(
applicationContext,
bitmap,
config.target
)
if (config.applyImageFilters) {
WallpaperHelper.setWallpaperWithFilters(
applicationContext,
bitmap,
config.target
)
} else {
WallpaperHelper.setWallpaperWithoutFilters(
applicationContext,
bitmap,
config.target
)
}
}

private suspend fun getOnlineWallpaper(config: WallpaperConfig): Bitmap? {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ object WallpaperHelper {
}
}

suspend fun setWallpaperWithoutFilters(context: Context, src: Bitmap, mode: WallpaperTarget) {
withContext(Dispatchers.IO) {
val bitmap = resizeBitmapByPreference(context, src)
setWallpaper(context, bitmap, mode)
}
}

fun setWallpaper(context: Context, bitmap: Bitmap, mode: WallpaperTarget) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (mode in listOf(WallpaperTarget.BOTH, WallpaperTarget.HOME)) {
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 @@ -65,6 +65,7 @@
<string name="none">None</string>
<string name="invert_wallpaper_by_theme">Invert wallpaper by theme</string>
<string name="invert_wallpaper_by_theme_summary">Invert the wallpaper to be dark on dark themes and light on light themes.</string>
<string name="apply_image_filters">Apply image filters</string>
<!-- Network constraints -->
<string name="network_type">Network type</string>
<string name="all_networks">All networks</string>
Expand Down

0 comments on commit 8b8523b

Please sign in to comment.