Skip to content

Commit 67d7a8c

Browse files
authored
Use properties delegates on states and minor fixes (#50)
1 parent b86ca4b commit 67d7a8c

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

app/src/main/java/com/godaddy/android/colorpicker/HarmonyColorPickerScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fun HarmonyColorPickerScreen(navController: NavController) {
3535
}
3636
)
3737
var currentColor by remember {
38-
mutableStateOf(HsvColor.from(Color.Black))
38+
mutableStateOf(HsvColor.from(Color.Red))
3939
}
4040
var extraColors by remember {
4141
mutableStateOf(emptyList<HsvColor>())

app/src/main/java/com/godaddy/android/colorpicker/SampleColorPickerActivity.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import androidx.compose.material.Text
1818
import androidx.compose.material.TextButton
1919
import androidx.compose.material.TopAppBar
2020
import androidx.compose.runtime.Composable
21+
import androidx.compose.runtime.getValue
2122
import androidx.compose.runtime.mutableStateOf
2223
import androidx.compose.runtime.remember
24+
import androidx.compose.runtime.setValue
2325
import androidx.compose.ui.Modifier
2426
import androidx.compose.ui.graphics.Color
2527
import androidx.compose.ui.graphics.ExperimentalGraphicsApi
@@ -38,8 +40,8 @@ class SampleColorPickerActivity : ComponentActivity() {
3840
super.onCreate(savedInstanceState)
3941
setContent {
4042
ComposeColorPickerTheme {
41-
val openDialog = remember { mutableStateOf(false) }
42-
val currentColor = remember {
43+
var openDialog by remember { mutableStateOf(false) }
44+
var currentColor by remember {
4345
mutableStateOf(HsvColor.from(Color.Black))
4446
}
4547
Surface(color = MaterialTheme.colors.background) {
@@ -54,32 +56,32 @@ class SampleColorPickerActivity : ComponentActivity() {
5456
Text(
5557
text = "Dialog",
5658
modifier = Modifier.clickable {
57-
openDialog.value = true
59+
openDialog = true
5860
}.padding(8.dp).fillMaxWidth()
5961
)
6062
}
6163
}
62-
if (openDialog.value) {
64+
if (openDialog) {
6365
AlertDialog(
6466
onDismissRequest = {
65-
openDialog.value = false
67+
openDialog = false
6668
},
6769
text = {
6870
ClassicColorPicker(
69-
color = currentColor.value,
71+
color = currentColor,
7072
modifier = Modifier
7173
.height(300.dp)
7274
.padding(16.dp),
7375
onColorChanged = { hsvColor: HsvColor ->
7476
// Triggered when the color changes, do something with the newly picked color here!
75-
currentColor.value = hsvColor
77+
currentColor = hsvColor
7678
}
7779
)
7880
},
7981
confirmButton = {
8082
TextButton(
8183
onClick = {
82-
openDialog.value = false
84+
openDialog = false
8385
}
8486
) {
8587
Text("Confirm")
@@ -88,7 +90,7 @@ class SampleColorPickerActivity : ComponentActivity() {
8890
dismissButton = {
8991
TextButton(
9092
onClick = {
91-
openDialog.value = false
93+
openDialog = false
9294
}
9395
) {
9496
Text("Dismiss")

color-picker/src/commonMain/kotlin/com/godaddy/android/colorpicker/harmony/HarmonyColorPicker.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private fun HarmonyColorPickerWithMagnifiers(
131131

132132
) {
133133
val updatedOnColorChanged by rememberUpdatedState(onColorChanged)
134-
val diameterPx = remember(constraints.maxWidth) {
134+
val diameterPx by remember(constraints.maxWidth) {
135135
mutableStateOf(constraints.maxWidth)
136136
}
137137

@@ -145,7 +145,7 @@ private fun HarmonyColorPickerWithMagnifiers(
145145
fun updateColorWheel(newPosition: Offset, animate: Boolean) {
146146
// Work out if the new position is inside the circle we are drawing, and has a
147147
// valid color associated to it. If not, keep the current position
148-
val newColor = colorForPosition(newPosition, IntSize(diameterPx.value, diameterPx.value), hsvColorUpdated.value)
148+
val newColor = colorForPosition(newPosition, IntSize(diameterPx, diameterPx), hsvColorUpdated.value)
149149
if (newColor != null) {
150150
animateChanges = animate
151151
updatedOnColorChanged(newColor)
@@ -168,9 +168,9 @@ private fun HarmonyColorPickerWithMagnifiers(
168168
}
169169

170170
Box(inputModifier.fillMaxSize()) {
171-
ColorWheel(hsvColor = hsvColor, diameter = diameterPx.value)
171+
ColorWheel(hsvColor = hsvColor, diameter = diameterPx)
172172
HarmonyColorMagnifiers(
173-
diameterPx.value,
173+
diameterPx,
174174
hsvColor,
175175
animateChanges,
176176
currentlyChangingInput,

desktop/src/jvmMain/kotlin/ClassicColorPickerScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.godaddy.android.colorpicker.HsvColor
1616
fun ClassicColorPickerScreen() {
1717
Column {
1818
var currentColor by remember {
19-
mutableStateOf(HsvColor.from(Color.Black))
19+
mutableStateOf(HsvColor.from(Color.Red))
2020
}
2121
ColorPreviewInfo(currentColor = currentColor.toColor())
2222
ClassicColorPicker(

desktop/src/jvmMain/kotlin/HarmonyColorPickerScreen.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,43 @@ fun HarmonyColorPickerScreen() {
3333
var currentColor by remember {
3434
mutableStateOf(HsvColor.from(Color.Red))
3535
}
36-
val extraColors = remember {
36+
var extraColors by remember {
3737
mutableStateOf(emptyList<HsvColor>())
3838
}
3939
ColorPaletteBar(
4040
modifier = Modifier.fillMaxWidth().wrapContentHeight(),
41-
colors = listOf(currentColor).plus(extraColors.value)
41+
colors = listOf(currentColor).plus(extraColors)
4242
)
43-
val expanded = remember {
43+
var expanded by remember {
4444
mutableStateOf(false)
4545
}
46-
val harmonyMode = remember {
46+
var harmonyMode by remember {
4747
mutableStateOf(ColorHarmonyMode.ANALOGOUS)
4848
}
4949
TextButton(onClick = {
50-
expanded.value = true
50+
expanded = true
5151
}) {
52-
Text(harmonyMode.value.name)
52+
Text(harmonyMode.name)
5353
}
54-
DropdownMenu(expanded.value, onDismissRequest = {
55-
expanded.value = false
54+
DropdownMenu(expanded, onDismissRequest = {
55+
expanded = false
5656
}) {
5757
ColorHarmonyMode.values().forEach {
5858
DropdownMenuItem(onClick = {
59-
harmonyMode.value = it
60-
expanded.value = false
59+
harmonyMode = it
60+
expanded = false
6161
}) {
6262
Text(it.name)
6363
}
6464
}
6565
}
6666
HarmonyColorPicker(
67-
harmonyMode = harmonyMode.value,
67+
harmonyMode = harmonyMode,
6868
modifier = Modifier.defaultMinSize(minHeight = 300.dp, minWidth = 300.dp),
6969
color = currentColor,
7070
onColorChanged = { hsvColor ->
7171
currentColor = hsvColor
72-
extraColors.value = hsvColor.getColors(harmonyMode.value)
72+
extraColors = hsvColor.getColors(harmonyMode)
7373
}
7474
)
7575
}

desktop/src/jvmMain/kotlin/Main.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import androidx.compose.foundation.background
32
import androidx.compose.foundation.clickable
43
import androidx.compose.foundation.layout.Column
@@ -14,8 +13,10 @@ import androidx.compose.material.TabRow
1413
import androidx.compose.material.Text
1514
import androidx.compose.material.TopAppBar
1615
import androidx.compose.runtime.Composable
16+
import androidx.compose.runtime.getValue
1717
import androidx.compose.runtime.mutableStateOf
1818
import androidx.compose.runtime.remember
19+
import androidx.compose.runtime.setValue
1920
import androidx.compose.ui.Alignment
2021
import androidx.compose.ui.Modifier
2122
import androidx.compose.ui.graphics.Color
@@ -34,29 +35,29 @@ fun main() = application {
3435
})
3536
// Here is how to add a Color Picker to your compose tree:
3637

37-
val currentColorPicker = remember { mutableStateOf(ColorPicker.CLASSIC) }
38+
var currentColorPicker by remember { mutableStateOf(ColorPicker.CLASSIC) }
3839
TabRow(
39-
currentColorPicker.value.ordinal,
40+
currentColorPicker.ordinal,
4041
tabs = {
4142
Text(
4243
"Classic Picker",
4344
modifier =
4445
Modifier.clickable {
45-
currentColorPicker.value = ColorPicker.CLASSIC
46+
currentColorPicker = ColorPicker.CLASSIC
4647
}.padding(16.dp),
4748
textAlign = TextAlign.Center
4849
)
4950
Text(
5051
"Harmony Picker",
5152
modifier = Modifier.clickable {
52-
currentColorPicker.value = ColorPicker.HARMONY
53+
currentColorPicker = ColorPicker.HARMONY
5354
}.padding(16.dp),
5455
textAlign = TextAlign.Center
5556
)
5657
},
5758
contentColor = Color.White
5859
)
59-
when (currentColorPicker.value) {
60+
when (currentColorPicker) {
6061
ColorPicker.CLASSIC -> {
6162
ClassicColorPickerScreen()
6263
}

0 commit comments

Comments
 (0)