Skip to content

Commit

Permalink
feat(graphing): automatically select distinct function names and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jun 30, 2024
1 parent 04d3128 commit 3206fcb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object Defaults {
)
val defaultVarNameChars: Set<Char> =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$".toCharArray().toSet()
val defaultFuncNameChars: List<Char> = "fghijklmnopqrstuvw".toList()

fun getDefaultGenericFunctions(): Map<String, EvalFunctionBlock> {
return mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,5 @@ class EvalConfiguration(
this.varNameChars = Defaults.defaultVarNameChars
this.genericConstants = Defaults.defaultGenericConstants.toMutableMap()
this.genericFunctions = Defaults.getDefaultGenericFunctions().toMutableMap()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import androidx.compose.ui.graphics.Color
class Function(
val expression: String,
val color: Color,
val name: String,
val function: (Float) -> Float?
) {

companion object {
fun create(expression: String, color: Color): Function {
fun create(expression: String, color: Color, functionName: String): Function {
val compiled: CompiledExpression = Evaluator.compile(expression)
return Function(
expression, color
expression, color, functionName
) { value ->
compiled.execute("x" to value.toDouble())?.toFloat()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ import net.youapps.calcyou.viewmodels.GraphViewModel
@Composable
fun AddNewFunctionDialog(
graphViewModel: GraphViewModel,
functionName: String,
initialExpression: String,
initialColor: Color,
onDismissRequest: () -> Unit
) {
Dialog(onDismissRequest) {
DialogContent(
onConfirm = { expression, color ->
graphViewModel.addFunction(expression, color)
graphViewModel.addFunction(expression, color, functionName)
onDismissRequest()
},
onCancel = onDismissRequest,
checkExpression = graphViewModel::checkExpression,
isError = graphViewModel.isError,
errorMessage = graphViewModel.errorText,
functionName = functionName,
initialExpression = initialExpression,
initialColor = initialColor
)
Expand All @@ -67,6 +69,7 @@ private fun DialogContent(
checkExpression: (String) -> Unit,
isError: Boolean,
errorMessage: String,
functionName: String,
initialExpression: String = "",
initialColor: Color = rainbowColors.first(),
) {
Expand All @@ -92,7 +95,7 @@ private fun DialogContent(
isError = isError,
prefix = {
Text(
text = "f(x) = ", style = TextStyle(
text = "${functionName}(x) = ", style = TextStyle(
fontFamily = FontFamily.Serif,
fontWeight = FontWeight.Medium,
fontSize = MaterialTheme.typography.bodyLarge.fontSize,
Expand Down Expand Up @@ -149,6 +152,7 @@ private fun DialogContentPreview() {
onCancel = {},
checkExpression = {},
isError = true,
errorMessage = "Invalid token at index 0"
errorMessage = "Invalid token at index 0",
functionName = "f"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import net.youapps.calcyou.data.graphing.Defaults
import net.youapps.calcyou.data.graphing.Function
import net.youapps.calcyou.ui.components.AddNewFunctionDialog
import net.youapps.calcyou.viewmodels.GraphViewModel
Expand Down Expand Up @@ -82,6 +83,7 @@ fun GraphingScreen(graphViewModel: GraphViewModel = viewModel()) {
graphViewModel.updateSelectedFunction(-1)
showAddFunctionDialog = false
},
functionName = remember(graphViewModel.selectedFunctionIndex) { graphViewModel.functionName },
initialColor =
remember(graphViewModel.selectedFunctionIndex) { graphViewModel.functionColor },
initialExpression =
Expand All @@ -100,6 +102,7 @@ fun FunctionList(
Column(modifier = modifier) {
functions.forEachIndexed { index, function ->
FunctionRow(
functionName = function.name,
text = function.expression,
color = function.color,
onClick = { onClickFunction(index) },
Expand All @@ -112,7 +115,7 @@ fun FunctionList(
}

@Composable
fun FunctionRow(text: String, color: Color, onClick: () -> Unit, onClickRemove: () -> Unit) {
fun FunctionRow(functionName: String, text: String, color: Color, onClick: () -> Unit, onClickRemove: () -> Unit) {
Row(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -122,7 +125,7 @@ fun FunctionRow(text: String, color: Color, onClick: () -> Unit, onClickRemove:
) {

Text(
text = "f(x) = ", style = TextStyle(
text = "${functionName}(x) = ", style = TextStyle(
fontFamily = FontFamily.Serif,
fontWeight = FontWeight.Bold,
fontSize = MaterialTheme.typography.titleMedium.fontSize,
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/net/youapps/calcyou/viewmodels/GraphViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.AndroidViewModel
import net.youapps.calcyou.R
import net.youapps.calcyou.data.graphing.Defaults
import net.youapps.calcyou.data.graphing.Evaluator
import net.youapps.calcyou.data.graphing.Function
import net.youapps.calcyou.data.graphing.Window
import net.youapps.calcyou.ui.components.rainbowColors
import java.text.ParseException
import kotlin.random.Random

class GraphViewModel(private val application: Application) : AndroidViewModel(application) {
val context: Context
private val context: Context
get() = application.applicationContext
var window by mutableStateOf(Window(), neverEqualPolicy())
val functions = mutableStateListOf<Function>()
Expand All @@ -30,17 +32,24 @@ class GraphViewModel(private val application: Application) : AndroidViewModel(ap

var selectedFunctionIndex by mutableIntStateOf(-1)
private set
var functionColor by mutableStateOf(Color.Red)
var functionName by mutableStateOf(Defaults.defaultFuncNameChars.first().toString())

var functionColor by mutableStateOf(rainbowColors.first())

var expression by mutableStateOf("")

fun updateSelectedFunction(index: Int) {
selectedFunctionIndex = index
if (index == -1) {
expression = ""
functionName = Defaults.defaultFuncNameChars[
functions.size % Defaults.defaultFuncNameChars.size
].toString()
functionColor = rainbowColors[functions.size % rainbowColors.size]
return
}
val function = functions[index]
functionName = function.name
functionColor = function.color
expression = function.expression
}
Expand All @@ -64,13 +73,13 @@ class GraphViewModel(private val application: Application) : AndroidViewModel(ap
}
}

fun addFunction(expression: String, color: Color) {
fun addFunction(expression: String, color: Color, functionName: String) {
if (selectedFunctionIndex != -1) {
functions[selectedFunctionIndex] = Function.create(expression, color)
functions[selectedFunctionIndex] = Function.create(expression, color, functionName)
updateSelectedFunction(-1)
return
}
functions.add(Function.create(expression, color))
functions.add(Function.create(expression, color, functionName))
}

fun removeFunction(index: Int) {
Expand Down

0 comments on commit 3206fcb

Please sign in to comment.