|
| 1 | +package com.example.myapplication |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.activity.ComponentActivity |
| 5 | +import androidx.activity.compose.setContent |
| 6 | +import androidx.activity.enableEdgeToEdge |
| 7 | +import androidx.compose.foundation.background |
| 8 | +import androidx.compose.foundation.layout.Column |
| 9 | +import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.Spacer |
| 11 | +import androidx.compose.foundation.layout.fillMaxSize |
| 12 | +import androidx.compose.foundation.layout.height |
| 13 | +import androidx.compose.foundation.layout.padding |
| 14 | +import androidx.compose.foundation.lazy.LazyColumn |
| 15 | +import androidx.compose.foundation.lazy.items |
| 16 | +import androidx.compose.material3.MaterialTheme |
| 17 | +import androidx.compose.material3.Surface |
| 18 | +import androidx.compose.material3.Text |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.ui.Modifier |
| 21 | +import androidx.compose.ui.graphics.Color |
| 22 | +import androidx.compose.ui.tooling.preview.Preview |
| 23 | +import androidx.compose.ui.unit.dp |
| 24 | +import org.jetbrains.kotlinx.dataframe.DataFrame |
| 25 | +import org.jetbrains.kotlinx.dataframe.annotations.DataSchema |
| 26 | +import org.jetbrains.kotlinx.dataframe.api.cast |
| 27 | +import org.jetbrains.kotlinx.dataframe.api.dataFrameOf |
| 28 | +import org.jetbrains.kotlinx.dataframe.api.filter |
| 29 | +import org.jetbrains.kotlinx.dataframe.api.rows |
| 30 | + |
| 31 | +@DataSchema |
| 32 | +data class Person( |
| 33 | + val age: Int, |
| 34 | + val name: String |
| 35 | +) |
| 36 | + |
| 37 | +class MainActivity : ComponentActivity() { |
| 38 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 39 | + super.onCreate(savedInstanceState) |
| 40 | + enableEdgeToEdge() |
| 41 | + |
| 42 | + val df = dataFrameOf( |
| 43 | + "name" to listOf("Andrei", "Nikita", "Jolan"), |
| 44 | + "age" to listOf(22, 16, 37) |
| 45 | + ).cast<Person>() |
| 46 | + |
| 47 | + setContent { |
| 48 | + MaterialTheme { |
| 49 | + Surface(modifier = Modifier.fillMaxSize()) { |
| 50 | + DataFrameScreen(df) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +@Preview(showBackground = true) |
| 58 | +@Composable |
| 59 | +fun DefaultDataFrameScreenPreview() { |
| 60 | + val df = dataFrameOf( |
| 61 | + "name" to listOf("Andrei", "Nikita", "Jolan"), |
| 62 | + "age" to listOf(22, 16, 37) |
| 63 | + ).cast<Person>() |
| 64 | + DataFrameScreen(df) |
| 65 | +} |
| 66 | + |
| 67 | +@Composable |
| 68 | +fun DataFrameScreen(df: DataFrame<Person>) { |
| 69 | + Column( |
| 70 | + modifier = Modifier |
| 71 | + .fillMaxSize() |
| 72 | + .padding(top = 48.dp, start = 16.dp, end = 16.dp) |
| 73 | + ) { |
| 74 | + Text( |
| 75 | + text = "Kotlin DataFrame on Android", |
| 76 | + style = MaterialTheme.typography.headlineSmall, |
| 77 | + modifier = Modifier.padding(bottom = 16.dp) |
| 78 | + ) |
| 79 | + |
| 80 | + Text( |
| 81 | + text = "df", |
| 82 | + modifier = Modifier |
| 83 | + .background(color = Color.LightGray) |
| 84 | + .padding(2.dp) |
| 85 | + ) |
| 86 | + |
| 87 | + DataFrameTable(df) |
| 88 | + |
| 89 | + Text( |
| 90 | + text = "df.filter { age >= 20 }", |
| 91 | + modifier = Modifier |
| 92 | + .background(color = Color.LightGray) |
| 93 | + .padding(2.dp) |
| 94 | + ) |
| 95 | + |
| 96 | + DataFrameTable(df.filter { age >= 20 }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@Preview(showBackground = true) |
| 101 | +@Composable |
| 102 | +fun DefaultDataFrameTablePreview() { |
| 103 | + val df = dataFrameOf( |
| 104 | + "name" to listOf("Andrei", "Nikita", "Jolan"), |
| 105 | + "age" to listOf(22, 16, 37) |
| 106 | + ).cast<Person>() |
| 107 | + DataFrameTable(df) |
| 108 | +} |
| 109 | + |
| 110 | +@Composable |
| 111 | +fun DataFrameTable(df: DataFrame<*>) { |
| 112 | + val columnNames = df.columnNames() |
| 113 | + |
| 114 | + // Header |
| 115 | + Row { |
| 116 | + for (name in columnNames) { |
| 117 | + Text( |
| 118 | + text = name, |
| 119 | + modifier = Modifier |
| 120 | + .weight(1f) |
| 121 | + .padding(4.dp), |
| 122 | + style = MaterialTheme.typography.labelLarge |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Spacer(Modifier.height(4.dp)) |
| 128 | + |
| 129 | + // Rows |
| 130 | + LazyColumn { |
| 131 | + items(df.rows().toList()) { row -> |
| 132 | + Row { |
| 133 | + for (cell in row.values()) { |
| 134 | + Text( |
| 135 | + text = cell.toString(), |
| 136 | + modifier = Modifier |
| 137 | + .weight(1f) |
| 138 | + .padding(4.dp) |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments