Skip to content

Commit

Permalink
feat: allow the selection of multiple online sources for the wallpape…
Browse files Browse the repository at this point in the history
…r changer
  • Loading branch information
Bnyro committed Dec 8, 2023
1 parent 9ded3ea commit f6c4697
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bnyro.wallpaper.ui.components

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -23,12 +24,13 @@ fun BlockButton(
selected: Boolean,
onClick: () -> Unit
) {
Log.e(text, selected.toString())
Column(
modifier = modifier
.height(50.dp)
.clip(RoundedCornerShape(12.dp))
.background(
if (selected) {
if (!selected) {
MaterialTheme.colorScheme.surfaceVariant.copy(
0.5f
)
Expand All @@ -46,7 +48,7 @@ fun BlockButton(
style = MaterialTheme.typography.labelLarge.copy(
textAlign = TextAlign.Center
),
color = if (selected) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.inverseSurface
color = if (!selected) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.inverseSurface
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
Expand All @@ -25,11 +26,10 @@ 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.BlockPreference
import com.bnyro.wallpaper.ui.components.prefs.MultiSelectionBlockPreference
import com.bnyro.wallpaper.ui.components.prefs.ListPreference
import com.bnyro.wallpaper.ui.components.prefs.SettingsCategory
import com.bnyro.wallpaper.ui.nav.DrawerScreens
import com.bnyro.wallpaper.util.LocalWallpaperHelper
import com.bnyro.wallpaper.util.PickFolderContract

@Composable
Expand Down Expand Up @@ -84,19 +84,20 @@ fun WallpaperChangerPref(config: WallpaperConfig, onChange: (WallpaperConfig) ->
Crossfade(targetState = wallpaperSource, label = "wallpaper_source") { state ->
when (state) {
WallpaperSource.ONLINE -> {
var currentIndex by remember {
mutableIntStateOf(
DrawerScreens.apiScreens.indexOfFirst { it.route == config.apiRoute }
)
var currentSelections = remember {
config.apiRoute?.split(",").orEmpty().map { route ->
DrawerScreens.apiScreens.indexOfFirst { it.route == route }
}
}
BlockPreference(
MultiSelectionBlockPreference(
preferenceKey = null,
entries = DrawerScreens.apiScreens.map { stringResource(it.titleResource) },
values = DrawerScreens.apiScreens.map { it.route },
defaultSelection = currentIndex
) { index ->
config.apiRoute = DrawerScreens.apiScreens[index].route
currentIndex = index
defaultSelections = currentSelections
) { selections ->
config.apiRoute =
selections.joinToString(",") { DrawerScreens.apiScreens[it].route }
currentSelections = selections
onChange(config)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,42 @@ package com.bnyro.wallpaper.ui.components.prefs

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.bnyro.wallpaper.ui.components.BlockButton
import com.bnyro.wallpaper.util.Preferences

@Composable
fun BlockPreference(
fun MultiSelectionBlockPreference(
preferenceKey: String?,
entries: List<String>,
values: List<String>,
defaultSelection: Int = 0,
onSelectionChange: (Int) -> Unit = {}
defaultSelections: List<Int> = listOf(0),
onSelectionChange: (List<Int>) -> Unit = {}
) {
var selected by remember {
mutableIntStateOf(defaultSelection)
}

LaunchedEffect(Unit) {
if (preferenceKey == null) return@LaunchedEffect
val pref = Preferences.getString(preferenceKey, "")
if (pref.orEmpty().isNotEmpty()) selected = values.indexOf(pref)
val selected = remember {
val pref = preferenceKey?.let { Preferences.getString(it, "").split(",") }
val sel = if (!pref.isNullOrEmpty()) pref.map { values.indexOf(it) } else defaultSelections
sel.toMutableStateList()
}

LazyRow(
modifier = Modifier.padding(horizontal = 5.dp)
) {
items(entries) {
val index = entries.indexOf(it)
itemsIndexed(entries) { index, it ->
BlockButton(
modifier = Modifier.padding(2.dp, 0.dp),
text = it,
selected = index != selected
selected = selected.contains(index)
) {
Preferences.edit { putString(preferenceKey, values[index]) }
selected = index
if (selected.contains(index)) selected.remove(index) else selected.add(index)
if (preferenceKey != null) Preferences.edit {
putString(preferenceKey, values.joinToString(","))
}
onSelectionChange.invoke(selected)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class BackgroundWorker(
}

private suspend fun getOnlineWallpaper(config: WallpaperConfig): Bitmap? {
val source = config.apiRoute?.split(",")?.randomOrNull() ?: return null

return withContext(Dispatchers.IO) {
val url = runCatching {
Preferences.getApiByRoute(config.apiRoute.orEmpty()).getRandomWallpaperUrl()
Preferences.getApiByRoute(source).getRandomWallpaperUrl()
}.getOrElse { return@withContext null }
ImageHelper.getSuspend(applicationContext, url, true)
}
Expand Down

0 comments on commit f6c4697

Please sign in to comment.