-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
226 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.bnyro.translate.ext | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
fun String.hexToColor() = Color(android.graphics.Color.parseColor("#$this")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
app/src/main/java/com/bnyro/translate/ui/components/prefs/AccentColorPref.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.bnyro.translate.ui.components.prefs | ||
|
||
import android.os.Build | ||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
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.height | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.core.content.edit | ||
import com.bnyro.translate.R | ||
import com.bnyro.translate.ext.hexToColor | ||
import com.bnyro.translate.ui.MainActivity | ||
import com.bnyro.translate.ui.components.DialogButton | ||
import com.bnyro.translate.ui.theme.defaultAccentColor | ||
import com.bnyro.translate.util.Preferences | ||
import okhttp3.internal.toHexString | ||
|
||
@Composable | ||
fun AccentColorPrefDialog( | ||
onDismissRequest: () -> Unit | ||
) { | ||
val context = LocalContext.current | ||
val supportsDynamicColors = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S | ||
|
||
var color by remember { | ||
mutableStateOf( | ||
Preferences.getAccentColor() ?: run { | ||
if (supportsDynamicColors) null else defaultAccentColor | ||
} | ||
) | ||
} | ||
|
||
AlertDialog( | ||
onDismissRequest = onDismissRequest, | ||
confirmButton = { | ||
DialogButton(text = stringResource(R.string.okay)) { | ||
Preferences.prefs.edit(true) { putString(Preferences.accentColorKey, color) } | ||
(context as MainActivity).accentColor = color | ||
onDismissRequest.invoke() | ||
} | ||
}, | ||
dismissButton = { | ||
DialogButton(text = stringResource(R.string.cancel)) { | ||
onDismissRequest.invoke() | ||
} | ||
}, | ||
title = { | ||
Text(stringResource(R.string.accent_color)) | ||
}, | ||
text = { | ||
Column( | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
if (supportsDynamicColors) { | ||
Row( | ||
modifier = Modifier.fillMaxWidth(), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text( | ||
modifier = Modifier.weight(1f), | ||
text = stringResource(R.string.dynamic_colors) | ||
) | ||
Switch( | ||
checked = color == null, | ||
onCheckedChange = { newValue -> | ||
color = defaultAccentColor.takeIf { !newValue } | ||
} | ||
) | ||
} | ||
} | ||
Row( | ||
modifier = Modifier.height(250.dp) | ||
) { | ||
AnimatedVisibility( | ||
visible = color != null | ||
) { | ||
Column( | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
listOf("R", "G", "B").forEachIndexed { index, c -> | ||
val startIndex = index * 2 | ||
color?.let { | ||
ColorSlider( | ||
label = c, | ||
value = it.substring(startIndex, startIndex + 2).toInt(16), | ||
onChange = { colorInt -> | ||
var newHex = colorInt.toHexString() | ||
if (newHex.length == 1) newHex = "0$newHex" | ||
color = StringBuilder(it).apply { | ||
setCharAt(startIndex, newHex[0]) | ||
setCharAt(startIndex + 1, newHex[1]) | ||
}.toString() | ||
} | ||
) | ||
} | ||
} | ||
Spacer(modifier = Modifier.height(20.dp)) | ||
color?.let { | ||
Row( | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Box( | ||
Modifier.size(50.dp).background( | ||
MaterialTheme.colorScheme.primary, | ||
CircleShape | ||
) | ||
) | ||
Text(text = " => ", fontSize = 27.sp) | ||
Box( | ||
modifier = Modifier.size(50.dp).background( | ||
it.hexToColor(), | ||
CircleShape | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/bnyro/translate/ui/components/prefs/ColorSlider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.bnyro.translate.ui.components.prefs | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Slider | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun ColorSlider( | ||
label: String, | ||
value: Int, | ||
onChange: (Int) -> Unit | ||
) { | ||
Row( | ||
modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text(label) | ||
Slider( | ||
modifier = Modifier.padding(horizontal = 10.dp).weight(1f), | ||
value = value.toFloat(), | ||
valueRange = 0f..255f, | ||
steps = 256, | ||
onValueChange = { | ||
onChange(it.toInt()) | ||
} | ||
) | ||
Text(value.toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.