Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
refactor: add switch preference and make thumbnail color fallback opt…
Browse files Browse the repository at this point in the history
…ional
  • Loading branch information
Bnyro committed Oct 20, 2023
1 parent 3223af1 commit 88c4586
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/app/suhasdissa/vibeyou/Destination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sealed class Destination(val route: String) {
object About : Destination("about")
object NetworkSettings : Destination("net_settings")
object DatabaseSettings : Destination("database_settings")
object AppearanceSettings : Destination("appearance_settings")
object Playlists : Destination("playlist_screen")
object LocalPlaylists : Destination("local_playlist_screen")
object Artist : Destination("artist")
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/app/suhasdissa/vibeyou/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import app.suhasdissa.vibeyou.ui.screens.search.ArtistScreen
import app.suhasdissa.vibeyou.ui.screens.search.LocalSearchScreen
import app.suhasdissa.vibeyou.ui.screens.search.SearchScreen
import app.suhasdissa.vibeyou.ui.screens.settings.AboutScreen
import app.suhasdissa.vibeyou.ui.screens.settings.AppearanceSettingsScreen
import app.suhasdissa.vibeyou.ui.screens.settings.DatabaseSettingsScreen
import app.suhasdissa.vibeyou.ui.screens.settings.NetworkSettingsScreen
import app.suhasdissa.vibeyou.ui.screens.settings.SettingsScreen
Expand Down Expand Up @@ -72,6 +73,10 @@ fun AppNavHost(navHostController: NavHostController) {
DatabaseSettingsScreen()
}

composable(route = Destination.AppearanceSettings.route) {
AppearanceSettingsScreen()
}

composable(Destination.Playlists.route) {
CompositionLocalProvider(LocalViewModelStoreOwner provides viewModelStoreOwner) {
val searchViewModel: PipedSearchViewModel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.media.audiofx.LoudnessEnhancer
Expand Down Expand Up @@ -128,7 +127,7 @@ class PlayerService : MediaSessionService(), MediaSession.Callback, Player.Liste
val bitmap = BitmapFactory.decodeFile(uri.path)
future.set(bitmap)
} catch (e: Exception) {
future.set(createOneColorImage(appInstance.accentColor))
handleBitmapLoadFailure(future, e)
}
} else {
val imageLoader = ImageLoader.Builder(context).build()
Expand All @@ -139,13 +138,21 @@ class PlayerService : MediaSessionService(), MediaSession.Callback, Player.Liste
if (result is SuccessResult) {
future.set(result.drawable.toBitmap())
} else if (result is ErrorResult) {
future.set(createOneColorImage(appInstance.accentColor))
handleBitmapLoadFailure(future, result.throwable)
}
}
}
return future
}

private fun handleBitmapLoadFailure(future: SettableFuture<Bitmap>, error: Throwable) {
if (Pref.sharedPreferences.getBoolean(Pref.thumbnailColorFallbackKey, false)) {
future.set(createOneColorImage(appInstance.accentColor))
} else {
future.setException(error)
}
}

private fun createOneColorImage(@ColorInt color: Int): Bitmap {
val rect = Rect(0, 0, 1, 1)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package app.suhasdissa.vibeyou.ui.screens.settings

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import app.suhasdissa.vibeyou.R
import app.suhasdissa.vibeyou.utils.Pref

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppearanceSettingsScreen() {
val topBarBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()

Scaffold(modifier = Modifier.fillMaxSize(), topBar = {
LargeTopAppBar(
title = { Text(stringResource(R.string.appearance_settings)) },
scrollBehavior = topBarBehavior
)
}) { innerPadding ->
LazyColumn(
Modifier
.fillMaxSize()
.padding(innerPadding)
.nestedScroll(topBarBehavior.nestedScrollConnection)
) {
item {
SwitchPref(
prefKey = Pref.thumbnailColorFallbackKey,
title = stringResource(R.string.fallback_thumnail_accent),
summary = stringResource(R.string.fallback_thumnail_accent_description)
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Landscape
import androidx.compose.material.icons.filled.SettingsBackupRestore
import androidx.compose.material.icons.filled.Storage
import androidx.compose.material.icons.filled.Web
Expand Down Expand Up @@ -69,6 +70,14 @@ fun SettingsScreen(
icon = Icons.Default.Web
)
}
item {
SettingItem(
title = stringResource(R.string.appearance_settings),
description = stringResource(R.string.appearance_settings_description),
onClick = { onNavigate(Destination.AppearanceSettings.route) },
icon = Icons.Default.Landscape
)
}
item {
SettingItem(
title = stringResource(R.string.music_cache_limit),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package app.suhasdissa.vibeyou.ui.screens.settings

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.suhasdissa.vibeyou.utils.rememberPreference

@Composable
fun SwitchPref(
prefKey: String,
title: String,
summary: String? = null,
defaultValue: Boolean = false,
onCheckedChange: (Boolean) -> Unit = {}
) {
var checked by rememberPreference(key = prefKey, defaultValue = defaultValue)
val interactionSource = remember { MutableInteractionSource() }

Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 12.dp)
.clickable(
interactionSource = interactionSource,
indication = null
) {
checked = !checked
},
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Column(
modifier = Modifier.weight(1f),
verticalArrangement = Arrangement.Center
) {
Text(fontSize = 18.sp, text = title)
if (summary != null) {
Text(modifier = Modifier.padding(top = 6.dp), text = summary)
}
}
Spacer(modifier = Modifier.width(6.dp))
Switch(
checked = checked,
onCheckedChange = {
checked = it
onCheckedChange.invoke(it)
}
)
}
}
1 change: 1 addition & 0 deletions app/src/main/java/app/suhasdissa/vibeyou/utils/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object Pref {
private const val pipedInstanceKey = "SelectedPipedInstanceKey"
const val authTokenKey = "AuthTokenKey"
const val exoCacheKey = "ExoCacheKey"
const val thumbnailColorFallbackKey = "ThumbnailColorFallbackef"

lateinit var sharedPreferences: SharedPreferences

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@
<string name="change_music_cache_size">Change music cache size</string>
<string name="music_cache_limit">Music cache limit</string>
<string name="unlimited">Unlimited</string>
<string name="appearance_settings">Appearance Settings</string>
<string name="appearance_settings_description">Customize the appearance to fit your needs.</string>
<string name="fallback_thumnail_accent">Notification thumbnail fallback</string>
<string name="fallback_thumnail_accent_description">Fall back to a using the system/theme accent color as a notification thumbnail if there\'s none.</string>
</resources>

0 comments on commit 88c4586

Please sign in to comment.