I’m trying to structure a simple application in a way that makes it easier to refactor by importing all values into a composable using data classes.
All the values are passed through successfully. However, whenever I try to add functionally into the composable it doesn’t work despite showing no errors.
Here is the file that contains the main activity and composable titled “Test1”
It displays a text widget that says “Hello World” with a red font and a blue button underneath it.
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Test1(Test1Properties(
textProperties = TextProperties(),
buttonProperties = ButtonProperties()
),
Test1Functions())
}
}
}
@Composable
fun Test1(
test1Properties: Test1Properties,
test1Functions: Test1Functions
){
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxSize()
) {
Text(
text = test1Properties.textProperties.text,
color = test1Properties.textProperties.color,
fontSize = test1Properties.textProperties.fontSize,
fontWeight = test1Properties.textProperties.fontWeight
)
Button(
onClick = {test1Functions.changeTextPropertiesText(test1Properties)},
colors = ButtonDefaults.buttonColors(
containerColor = test1Properties.buttonProperties.containerColor
)
) {
Text(text = test1Properties.buttonProperties.text)
}
}
}
Here is the file containing all of the data classes that hold all of the values for the composable.
There are three data classes. one for the text widget, one for the button, and one to hold them all together. There is one class to hold the functionally of the composable called “Test1Functions”. It only has one function to change the color of the text widget to blue.
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
data class Test1Properties(
val textProperties: TextProperties,
val buttonProperties: ButtonProperties
)
data class TextProperties(
var text: String = "Hello World",
var color: Color = Color.Red,
val fontSize: TextUnit = 30.sp,
val fontWeight: FontWeight = FontWeight.Bold
)
data class ButtonProperties(
val onClick: () -> Unit = {},
val text: String = "Button",
val containerColor: Color = Color.Blue
)
class Test1Functions {
fun changeTextPropertiesText(test1Properties: Test1Properties): Color {
test1Properties.textProperties.color = Color.Blue
return test1Properties.textProperties.color
}
}
Everything seems fine, no errors occurring, but when pressing the button the intended functionally does not occur. The intended functionally being turning the text in text widget blue.
And if you have any problems with this way of structuring your app, please recommend a better way.