Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onclick on cells #21

Merged
merged 9 commits into from
Aug 22, 2024
8 changes: 1 addition & 7 deletions BeeTablesCompose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("maven-publish")
id("org.jetbrains.kotlin.plugin.compose")
}

android {
Expand Down Expand Up @@ -46,13 +47,6 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}

packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import com.breens.beetablescompose.components.TableHeaderComponentWithoutColumnD
import com.breens.beetablescompose.components.TableRowComponent
import com.breens.beetablescompose.components.TableRowComponentWithoutDividers
import com.breens.beetablescompose.utils.extractMembers
import com.breens.beetablescompose.utils.lightColor
import com.breens.beetablescompose.utils.lightGray

/**
* 🐝 A Compose UI data table library.
Expand Down Expand Up @@ -61,25 +63,29 @@ inline fun <reified T : Any> BeeTablesCompose(
data: List<T>,
enableTableHeaderTitles: Boolean = true,
headerTableTitles: List<String>,
headerTitlesBorderColor: Color = Color.LightGray,
headerTitlesBorderColor: Color = lightGray(),
headerTitlesTextStyle: TextStyle = MaterialTheme.typography.bodySmall,
headerTitlesBackGroundColor: Color = Color.White,
tableRowColors: List<Color> = listOf(Color.White, Color.White),
rowBorderColor: Color = Color.LightGray,
headerTitlesBackGroundColor: Color = lightColor(),
tableRowColors: List<Color> = listOf(
lightColor(),
lightColor(),
),
rowBorderColor: Color = lightGray(),
rowTextStyle: TextStyle = MaterialTheme.typography.bodySmall,
tableElevation: Dp = 0.dp,
shape: RoundedCornerShape = RoundedCornerShape(4.dp),
borderStroke: BorderStroke = BorderStroke(
width = 1.dp,
color = Color.LightGray,
color = lightGray(),
),
disableVerticalDividers: Boolean = false,
dividerThickness: Dp = 1.dp,
horizontalDividerColor: Color = Color.LightGray,
horizontalDividerColor: Color = lightGray(),
contentAlignment: Alignment = Alignment.Center,
textAlign: TextAlign = TextAlign.Center,
tablePadding: Dp = 0.dp,
columnToIndexIncreaseWidth: Int? = null,
crossinline onRowClick: (Int) -> Unit = {},
) {
OutlinedCard(
elevation = CardDefaults.cardElevation(defaultElevation = tableElevation),
Expand Down Expand Up @@ -137,6 +143,7 @@ inline fun <reified T : Any> BeeTablesCompose(
textAlign = textAlign,
tablePadding = tablePadding,
columnToIndexIncreaseWidth = columnToIndexIncreaseWidth,
onRowClick = { onRowClick(it) },
)
} else {
TableRowComponent(
Expand All @@ -149,6 +156,8 @@ inline fun <reified T : Any> BeeTablesCompose(
textAlign = textAlign,
tablePadding = tablePadding,
columnToIndexIncreaseWidth = columnToIndexIncreaseWidth,
onRowClick = { onRowClick(it) },

)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.breens.beetablescompose.utils.darkColor
import com.breens.beetablescompose.utils.lightGray

@Composable
fun TableHeaderComponent(
Expand Down Expand Up @@ -85,9 +87,9 @@ fun TableHeaderComponentPreview() {
val titles = listOf("Team", "Home", "Away", "Points")
TableHeaderComponent(
headerTableTitles = titles,
headerTitlesBorderColor = Color.Black,
headerTitlesBorderColor = darkColor(),
headerTitlesTextStyle = MaterialTheme.typography.labelMedium,
headerTitlesBackGroundColor = Color.LightGray,
headerTitlesBackGroundColor = lightGray(),
contentAlignment = Alignment.Center,
textAlign = TextAlign.Center,
tablePadding = 0.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.breens.beetablescompose.utils.lightColor

@Composable
fun TableHeaderComponentWithoutColumnDividers(
Expand Down Expand Up @@ -91,7 +92,7 @@ fun TableHeaderComponentWithoutColumnDividersPreview() {
TableHeaderComponentWithoutColumnDividers(
headerTableTitles = titles,
headerTitlesTextStyle = MaterialTheme.typography.bodySmall,
headerTitlesBackGroundColor = Color.White,
headerTitlesBackGroundColor = lightColor(),
dividerThickness = 1.dp,
contentAlignment = Alignment.Center,
textAlign = TextAlign.Center,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package com.breens.beetablescompose.components

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -29,12 +31,15 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.breens.beetablescompose.utils.lightColor
import com.breens.beetablescompose.utils.lightGray

@Composable
fun TableRowComponent(
Expand All @@ -47,6 +52,7 @@ fun TableRowComponent(
tablePadding: Dp,
columnToIndexIncreaseWidth: Int?,
dividerThickness: Dp,
onRowClick: (Int) -> Unit = {},
) {
Row(
Modifier
Expand All @@ -62,7 +68,8 @@ fun TableRowComponent(
.border(
width = dividerThickness,
color = rowBorderColor,
),
)
.clickable { onRowClick(index) },
contentAlignment = contentAlignment,
) {
Text(
Expand All @@ -83,13 +90,15 @@ fun TableRowComponent(
@Composable
@Preview(showBackground = true)
fun TableRowComponentPreview() {
val context = LocalContext.current
val titles = listOf("Man Utd", "26", "7", "95")

TableRowComponent(
data = titles,
rowBorderColor = Color.LightGray,
rowBorderColor = lightGray(),
onRowClick = { Toast.makeText(context, "Row ...${titles[it]},", Toast.LENGTH_SHORT).show() },
rowTextStyle = MaterialTheme.typography.bodySmall,
rowBackGroundColor = Color.White,
rowBackGroundColor = lightColor(),
contentAlignment = Alignment.Center,
textAlign = TextAlign.Center,
tablePadding = 0.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.breens.beetablescompose.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -36,6 +37,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.breens.beetablescompose.utils.lightGray

@Composable
fun TableRowComponentWithoutDividers(
Expand All @@ -48,6 +50,7 @@ fun TableRowComponentWithoutDividers(
textAlign: TextAlign,
tablePadding: Dp,
columnToIndexIncreaseWidth: Int?,
onRowClick: (Int) -> Unit = {},
) {
Column(
modifier = Modifier.padding(horizontal = tablePadding),
Expand All @@ -61,8 +64,10 @@ fun TableRowComponentWithoutDividers(
val weight = if (index == columnToIndexIncreaseWidth) 8f else 2f
Box(
modifier = Modifier
.weight(weight),
.weight(weight)
.clickable { onRowClick(index) },
contentAlignment = contentAlignment,

) {
Text(
text = title,
Expand Down Expand Up @@ -94,9 +99,9 @@ fun TableRowComponentWithoutDividersPreview() {
TableRowComponentWithoutDividers(
data = titles,
rowTextStyle = MaterialTheme.typography.bodySmall,
rowBackGroundColor = Color.White,
rowBackGroundColor = MaterialTheme.colorScheme.background,
dividerThickness = 1.dp,
horizontalDividerColor = Color.LightGray,
horizontalDividerColor = lightGray(),
contentAlignment = Alignment.Center,
textAlign = TextAlign.Center,
tablePadding = 0.dp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 Breens Mbaka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.breens.beetablescompose.utils

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable

@Composable
fun lightGray() = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.12f)

@Composable
fun darkColor() = MaterialTheme.colorScheme.onBackground

@Composable
fun lightColor() = MaterialTheme.colorScheme.background
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ repositories {

```gradle
dependencies {
implementation("com.github.Breens-Mbaka:BeeTablesCompose:1.0.5")
implementation("com.github.Breens-Mbaka:BeeTablesCompose:1.2.0")
}
```

# Usage

- Import the library: Import the BeeTablesCompose function into your Compose-based project.

- Prepare your data: Organize your data in a list of objects to be displayed in the table.
Expand All @@ -66,6 +68,17 @@ dependencies {
<p align="center">
<img src="https://github.com/Breens-Mbaka/BeeTablesCompose/assets/72180010/f5ea94a1-df41-472c-91e2-03d6cb7dc82f" />

# NOTE ⚠️🚨
- Before building a release APK of your app, if you have enabled minification, make sure to annotate your table data classes with @Keep. This will prevent the class from being removed at runtime.
```kotlin
@Keep
data class User(
val name: String,
val email: Int,
val city: String
)
```

# Want a new shiny feature 🪩✨
- If you want to request a new feature please first read this [short guide](https://github.com/Breens-Mbaka/BeeTablesCompose/blob/master/.github/ISSUE_TEMPLATE/feature_request.md)

Expand All @@ -85,7 +98,7 @@ Also __[follow](https://github.com/Breens-Mbaka)__ me for my next creations!

# License
```xml
Designed and developed by 2023 Breens Mbaka
Designed and developed by 2024 Breens Mbaka

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
7 changes: 1 addition & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.compose")
}

android {
Expand Down Expand Up @@ -51,12 +52,6 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
Expand Down
Loading