I am trying to use Exposed in a Compose Multi Plat...
# exposed
n
I am trying to use Exposed in a Compose Multi Platform app. I am only running it on a desktop target. It works fine except that using a transaction in response to a button click doesn't seem to run the Exposed code, by which I mean no SQL statements are logged even when I step through the transaction and no actual changes re made to the database. Am I using it incorrectly?
Copy code
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

object Activities : IntIdTable() {
    val description = varchar("description", 255).index()
    val completed = bool("age")
}

class Activity(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<Activity>(Activities)

    var description by Activities.description
    var completed by Activities.completed
}

@Composable
fun App(localDir: String) {

    Database.connect("jdbc:h2:/$localDir/activity", driver = "org.h2.Driver")
    var activityList: List<Activity> = emptyList()
    var activities: SnapshotStateList<Activity>
    transaction {
        addLogger(StdOutSqlLogger)
        SchemaUtils.drop(Activities)
        SchemaUtils.create(Activities)

        Activity.new { description = "Walk the dog"; completed = false }
        Activity.new { description = "Buy groceries"; completed = false }
        Activity.new { description = "Call a friend"; completed = false }

        activityList = Activity.all().toList()

    }

    transaction {
        Activity.new { description = "New Activity"; completed = false }
        activityList = Activity.all().toList()
    }

    activities = remember {
        mutableStateListOf(*activityList.toTypedArray())
    }

    MaterialTheme {
        val materialBlue700 = Color(0xFF1976D2)
        val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Open))
        Scaffold(
            scaffoldState = scaffoldState,
            topBar = { TopAppBar(title = { Text("TopAppBar") }, backgroundColor = materialBlue700) },
            floatingActionButtonPosition = FabPosition.End,
            floatingActionButton = {
                FloatingActionButton(onClick = {
                    transaction {
                        Activity.new { description = "Another New Activity"; completed = false }
                    }
                }) {
                    Text("+")
                }
            },
            content = {
                LazyColumn(Modifier.fillMaxWidth()) {
                    items(activities) { activity ->
                        ActivityItem(activity)
                    }
                }
            },
        )
    }
}

@Composable
fun ActivityItem(activity: Activity) {
    val descriptionState = remember { mutableStateOf(activity.description) }
    Column(Modifier.fillMaxWidth()) {
        Text(text = "${activity.id}")
        TextField(modifier = Modifier.fillMaxWidth(), value = descriptionState.value, onValueChange = { newValue ->
            descriptionState.value = newValue
            activity.description = newValue
        })
    }
}