Skip to content

Commit

Permalink
wallpaper changer: option to set different walls for lockscreen than …
Browse files Browse the repository at this point in the history
…homescreen (closes #110)
  • Loading branch information
Bnyro committed Jul 26, 2023
1 parent 4d69664 commit 5c8a2df
Show file tree
Hide file tree
Showing 41 changed files with 231 additions and 165 deletions.
7 changes: 0 additions & 7 deletions app/src/main/java/com/bnyro/wallpaper/constants/ThemeMode.kt

This file was deleted.

This file was deleted.

7 changes: 7 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/enums/ThemeMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bnyro.wallpaper.enums

enum class ThemeMode(val value: Int) {
AUTO(0),
LIGHT(1),
DARK(2)
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/enums/WallpaperConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bnyro.wallpaper.enums

import com.bnyro.wallpaper.ui.nav.DrawerScreens

data class WallpaperConfig(
var target: WallpaperTarget = WallpaperTarget.BOTH,
var source: WallpaperSource = WallpaperSource.ONLINE,
var apiRoute: String? = DrawerScreens.apiScreens.firstOrNull()?.route,
var localFolderUri: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.bnyro.wallpaper.enums
enum class WallpaperSource(val value: Int) {
ONLINE(0),
FAVORITES(1),
LOCAL(2);
LOCAL(2),
NONE(3);

companion object {
fun fromInt(value: Int) = WallpaperSource.values().first { it.value == value }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bnyro.wallpaper.enums

enum class WallpaperTarget(val value: Int) {
BOTH(0),
HOME(1),
LOCK (2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import com.bnyro.wallpaper.constants.ThemeMode
import com.bnyro.wallpaper.enums.ThemeMode
import com.bnyro.wallpaper.ui.models.MainModel
import com.bnyro.wallpaper.ui.theme.WallYouTheme

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.bnyro.wallpaper.ui.components

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.animation.Crossfade
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
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.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
import com.bnyro.wallpaper.util.WorkerHelper

@Composable
fun WallpaperChangerPref(config: WallpaperConfig, onChange: (WallpaperConfig) -> Unit) {
val context = LocalContext.current

val localWallpaperDirChooser = rememberLauncherForActivityResult(PickFolderContract()) {
val uri = it ?: return@rememberLauncherForActivityResult
config.localFolderUri = uri.toString()
onChange(config)
}

SettingsCategory(
title = stringResource(
when (config.target) {
WallpaperTarget.BOTH -> R.string.both
WallpaperTarget.HOME -> R.string.home
WallpaperTarget.LOCK -> R.string.lockscreen
}
)
)
val wallpaperSources = listOf(R.string.online, R.string.favorites, R.string.local)
var wallpaperSource by remember { mutableStateOf(config.source) }
ListPreference(
prefKey = null,
title = stringResource(R.string.wallpaper_changer_source),
entries = wallpaperSources.map { stringResource(it) },
values = List(wallpaperSources.size) { index -> index.toString() },
defaultValue = wallpaperSource.value.toString()
) { newValue ->
config.source = WallpaperSource.fromInt(newValue.toInt())
wallpaperSource = config.source
onChange(config)
WorkerHelper.enqueue(context, true)
}

Crossfade(targetState = wallpaperSource) { state ->
when (state) {
WallpaperSource.ONLINE -> {
var currentIndex by remember {
mutableIntStateOf(DrawerScreens.apiScreens.indexOfFirst { it.route == config.apiRoute })
}
BlockPreference(
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
onChange(config)
WorkerHelper.enqueue(context, true)
}
}

WallpaperSource.LOCAL -> Button(
onClick = {
val currentFolder = LocalWallpaperHelper.getDirectory(config)
localWallpaperDirChooser.launch(currentFolder)
}
) {
Text(stringResource(R.string.choose_dir))
}

else -> Unit
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ 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.enums.WallpaperTarget
import com.bnyro.wallpaper.ext.awaitQuery
import com.bnyro.wallpaper.ext.query
import com.bnyro.wallpaper.util.*
Expand All @@ -50,7 +51,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun WallpaperPreview(
wallpaper: Wallpaper,
Expand Down Expand Up @@ -226,7 +226,7 @@ fun WallpaperPreview(
onDismissRequest = {
showModeSelection = false
},
onClick = {
onClick = { index ->
if (Preferences.getBoolean(Preferences.autoAddToFavoritesKey, false)) {
liked = true
awaitQuery {
Expand All @@ -237,7 +237,7 @@ fun WallpaperPreview(
WallpaperHelper.setWallpaper(
context.applicationContext,
bitmap!!,
it
WallpaperTarget.values()[index]
)
}
showModeSelection = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
Expand All @@ -16,18 +16,20 @@ import com.bnyro.wallpaper.util.Preferences

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

LaunchedEffect(Unit) {
if (preferenceKey == null) return@LaunchedEffect
val pref = Preferences.getString(preferenceKey, "")
if (pref != "") selected = values.indexOf(pref)
if (pref.orEmpty().isNotEmpty()) selected = values.indexOf(pref)
}

LazyRow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.bnyro.wallpaper.util.Preferences

@Composable
fun ListPreference(
prefKey: String,
prefKey: String?,
title: String,
entries: List<String>,
values: List<String>,
Expand All @@ -28,11 +28,11 @@ fun ListPreference(
mutableStateOf(false)
}

val indexOfCurrent = values.indexOf(Preferences.getString(prefKey, defaultValue))
val currentValue = prefKey?.let {
Preferences.getString(it, defaultValue)
} ?: defaultValue
var summary by remember {
mutableStateOf(
if (indexOfCurrent != -1) entries[indexOfCurrent] else null
)
mutableStateOf(entries.getOrNull(values.indexOf(currentValue)))
}

PreferenceItem(
Expand All @@ -55,7 +55,7 @@ fun ListPreference(
},
onClick = {
summary = entries[it]
Preferences.edit {
if (prefKey != null) Preferences.edit {
putString(prefKey, values[it])
}
onChange.invoke(values[it])
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@ package com.bnyro.wallpaper.ui.models

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.constants.ThemeMode
import com.bnyro.wallpaper.enums.ThemeMode
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.ApiHolder
import com.bnyro.wallpaper.util.Preferences
import kotlinx.coroutines.launch

class MainModel : ViewModel() {
var themeMode by mutableStateOf(
Preferences.getString(
Preferences.themeModeKey,
ThemeMode.AUTO.toString()
)!!.toInt()
)
private val themeModeIndex = Preferences.getString(
Preferences.themeModeKey,
ThemeMode.AUTO.value.toString()
).toInt()
var themeMode by mutableStateOf(ThemeMode.values()[themeModeIndex])

var api: Api = ApiHolder.whApi
var wallpapers by mutableStateOf(
listOf<Wallpaper>()
)
var titleResource by mutableStateOf(
R.string.app_name
)
var titleResource by mutableIntStateOf(R.string.app_name)

var page: Int = 1

Expand Down
Loading

0 comments on commit 5c8a2df

Please sign in to comment.