Skip to content

Commit

Permalink
feat(graphing): automatically select distinct function names, e.g. f(…
Browse files Browse the repository at this point in the history
…x), g(x), ...
  • Loading branch information
Bnyro committed Jun 30, 2024
1 parent 04d3128 commit dffe704
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 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,9 @@ fun GraphingScreen(graphViewModel: GraphViewModel = viewModel()) {
graphViewModel.updateSelectedFunction(-1)
showAddFunctionDialog = false
},
functionName = remember(graphViewModel.selectedFunctionIndex) { graphViewModel.functionName }.ifEmpty {
Defaults.defaultFuncNameChars[graphViewModel.functions.size % Defaults.defaultFuncNameChars.size].toString()
},
initialColor =
remember(graphViewModel.selectedFunctionIndex) { graphViewModel.functionColor },
initialExpression =
Expand All @@ -100,6 +104,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 +117,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 +127,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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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,6 +30,8 @@ class GraphViewModel(private val application: Application) : AndroidViewModel(ap

var selectedFunctionIndex by mutableIntStateOf(-1)
private set
var functionName by mutableStateOf("")

var functionColor by mutableStateOf(Color.Red)

var expression by mutableStateOf("")
Expand All @@ -41,6 +43,7 @@ class GraphViewModel(private val application: Application) : AndroidViewModel(ap
return
}
val function = functions[index]
functionName = function.name
functionColor = function.color
expression = function.expression
}
Expand All @@ -64,13 +67,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 dffe704

Please sign in to comment.