Skip to content

Commit

Permalink
- saving category colours cause its pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo03v committed Apr 11, 2024
1 parent a587521 commit ad79b47
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "bb6e146c83819d370d9c303a8dc57a8b",
"identityHash": "5786d9fcd0545b1af0077201c347aca5",
"entities": [
{
"tableName": "categories",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `color` TEXT NOT NULL, `emoji` TEXT NOT NULL, `isDefault` INTEGER NOT NULL, `name` TEXT NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `color` INTEGER NOT NULL, `emoji` TEXT NOT NULL, `isDefault` INTEGER NOT NULL, `name` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
Expand All @@ -17,7 +17,7 @@
{
"fieldPath": "color",
"columnName": "color",
"affinity": "TEXT",
"affinity": "INTEGER",
"notNull": true
},
{
Expand Down Expand Up @@ -103,7 +103,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'bb6e146c83819d370d9c303a8dc57a8b')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5786d9fcd0545b1af0077201c347aca5')"
]
}
}
34 changes: 15 additions & 19 deletions app/src/main/java/cloud/pablos/overload/data/Converters.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package cloud.pablos.overload.data

class Converters {
/*@TypeConverter
fun iconToString(icon: ImageVector): String {
return icon.name.split(".")[1]
}
@TypeConverter
fun stringToIcon(name: String): ImageVector {
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt")
val method = cl.declaredMethods.first()
return method.invoke(null, Icons.Filled) as ImageVector
}
import androidx.compose.ui.graphics.Color

class Converters {
companion object {
fun iconToString(icon: ImageVector): String {
return icon.name.split(".")[1]
fun convertColorToLong(color: Color): Long {
val alpha = 255 // (color.alpha * 255).toInt()
val red = (color.red * 255).toInt()
val green = (color.green * 255).toInt()
val blue = (color.blue * 255).toInt()
return (alpha.toLong() shl 24) or (red.toLong() shl 16) or (green.toLong() shl 8) or blue.toLong()
}

fun stringToIcon(name: String): ImageVector {
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt")
val method = cl.declaredMethods.first()
return method.invoke(null, Icons.Filled) as ImageVector
fun convertLongToColor(value: Long): Color {
val alpha = 1f // (value shr 24 and 0xFF).toFloat() / 255f
val red = (value shr 16 and 0xFF).toFloat() / 255f
val green = (value shr 8 and 0xFF).toFloat() / 255f
val blue = (value and 0xFF).toFloat() / 255f
return Color(red, green, blue, alpha)
}
}*/
}
}
57 changes: 57 additions & 0 deletions app/src/main/java/cloud/pablos/overload/data/Helpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cloud.pablos.overload.data

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import cloud.pablos.overload.data.category.CategoryState
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow

class Helpers {
companion object {
private fun calculateRelativeLuminance(color: Color): Double {
val red = if (color.red <= 0.03928) color.red / 12.92 else ((color.red + 0.055) / 1.055).pow(2.4)
val green = if (color.green <= 0.03928) color.green / 12.92 else ((color.green + 0.055) / 1.055).pow(2.4)
val blue = if (color.blue <= 0.03928) color.blue / 12.92 else ((color.blue + 0.055) / 1.055).pow(2.4)
return 0.2126 * red + 0.7152 * green + 0.0722 * blue
}

private fun calculateContrastRatio(
background: Color,
foreground: Color,
): Double {
val lum1 = calculateRelativeLuminance(background)
val lum2 = calculateRelativeLuminance(foreground)
val lighter = max(lum1, lum2)
val darker = min(lum1, lum2)
return (lighter + 0.05) / (darker + 0.05)
}

@Composable
fun decideBackground(categoryState: CategoryState): Color {
return (
categoryState.categories.find { category ->
category.id == categoryState.selectedCategory
}
)
?.let {
if (it.isDefault) {
MaterialTheme.colorScheme.primaryContainer
} else {
Converters.convertLongToColor(it.color)
}
}
?: MaterialTheme.colorScheme.primaryContainer
}

@Composable
fun decideForeground(background: Color): Color {
val white = MaterialTheme.colorScheme.background
val black = MaterialTheme.colorScheme.onBackground
val contrastWithWhite = calculateContrastRatio(background, white)
val contrastWithBlack = calculateContrastRatio(background, black)
return if (contrastWithWhite >= contrastWithBlack) white else black
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cloud.pablos.overload.data.item.Item
@Entity(tableName = "categories")
data class Category(
@PrimaryKey(autoGenerate = true) val id: Int = 1,
val color: String,
val color: Long,
val emoji: String,
val isDefault: Boolean,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sealed interface CategoryEvent {

data class SetId(val id: Int) : CategoryEvent

data class SetColor(val color: String) : CategoryEvent
data class SetColor(val color: Long) : CategoryEvent

data class SetEmoji(val emoji: String) : CategoryEvent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data class CategoryState(
val categoryWithItems: List<CategoryWithItems> = emptyList(),
// --
val id: Int = 1,
val color: String = Color.Unspecified.toString(),
val color: Long = Color.Unspecified.value.toLong(),
val emoji: String = "🕣",
val name: String = "Default",
val isDefault: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud.pablos.overload.data.category
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cloud.pablos.overload.data.Converters.Companion.convertColorToLong
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand Down Expand Up @@ -60,7 +61,7 @@ class CategoryViewModel(
_state.update {
it.copy(
id = 0,
color = Color.Unspecified.toString(),
color = convertColorToLong(Color.Unspecified),
emoji = "🕣",
isDefault = false,
name = "",
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/cloud/pablos/overload/ui/OverloadApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fun OverloadAppContent(
if (adjustEndDialogState) {
AdjustEndDialog(
onClose = { itemEvent(ItemEvent.SetAdjustEndDialogShown(false)) },
categoryState,
itemState,
itemEvent,
)
Expand All @@ -344,6 +345,7 @@ fun OverloadAppContent(
if (spreadAcrossDaysDialogState) {
SpreadAcrossDaysDialog(
onClose = { itemEvent(ItemEvent.SetSpreadAcrossDaysDialogShown(false)) },
categoryState,
itemState,
itemEvent,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import cloud.pablos.overload.R
import cloud.pablos.overload.data.Helpers.Companion.decideBackground
import cloud.pablos.overload.data.Helpers.Companion.decideForeground
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
Expand All @@ -49,6 +51,9 @@ fun OverloadNavigationFab(
itemEvent: (ItemEvent) -> Unit,
onDrawerClicked: () -> Unit = {},
) {
val backgroundColor = decideBackground(categoryState)
val foregroundColor = decideForeground(backgroundColor)

val date = LocalDate.now()

val itemsForToday = getItemsOfDay(date, categoryState, itemState)
Expand Down Expand Up @@ -94,8 +99,8 @@ fun OverloadNavigationFab(
itemEvent(ItemEvent.SetIsFabOpen(false))
},
interactionSource = interactionSource,
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
containerColor = backgroundColor,
contentColor = foregroundColor,
modifier = Modifier.padding(bottom = 10.dp).fillMaxWidth(),
) {
Column(
Expand Down Expand Up @@ -234,8 +239,8 @@ fun OverloadNavigationFab(
}
},
interactionSource = interactionSource,
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
containerColor = backgroundColor,
contentColor = foregroundColor,
modifier = Modifier.fillMaxWidth(),
) {
Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import cloud.pablos.overload.R
import cloud.pablos.overload.data.Helpers
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
Expand All @@ -44,6 +45,9 @@ fun OverloadNavigationFabSmall(
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
) {
val backgroundColor = Helpers.decideBackground(categoryState)
val foregroundColor = Helpers.decideForeground(backgroundColor)

val date = LocalDate.now()

val itemsForToday = getItemsOfDay(date, categoryState, itemState)
Expand Down Expand Up @@ -88,8 +92,8 @@ fun OverloadNavigationFabSmall(
onClick = {
itemEvent(ItemEvent.SetIsFabOpen(false))
},
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
containerColor = backgroundColor,
contentColor = foregroundColor,
) {
Icon(
imageVector = Icons.Default.Close,
Expand Down Expand Up @@ -139,8 +143,8 @@ fun OverloadNavigationFabSmall(
}
},
interactionSource = interactionSource,
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
containerColor = backgroundColor,
contentColor = foregroundColor,
) {
Icon(
imageVector =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import androidx.navigation.NavHostController
import androidx.room.withTransaction
import cloud.pablos.overload.R
import cloud.pablos.overload.data.Backup
import cloud.pablos.overload.data.Converters.Companion.convertColorToLong
import cloud.pablos.overload.data.OverloadDatabase
import cloud.pablos.overload.data.category.Category
import cloud.pablos.overload.data.category.CategoryEvent
Expand Down Expand Up @@ -102,12 +103,12 @@ fun ConfigurationsTab(
val workGoalDialogState = remember { mutableStateOf(false) }
val pauseGoalDialogState = remember { mutableStateOf(false) }

val color = MaterialTheme.colorScheme.onSurfaceVariant.toString()
val color = convertColorToLong(MaterialTheme.colorScheme.onSurfaceVariant)

val categories = categoryState.categories
LaunchedEffect(categories) {
if (categories.isEmpty()) {
categoryEvent(CategoryEvent.SetId(0))
categoryEvent(CategoryEvent.SetId(1))
categoryEvent(CategoryEvent.SetName("Default"))
categoryEvent(CategoryEvent.SetColor(color))
categoryEvent(CategoryEvent.SetEmoji("🕣"))
Expand Down Expand Up @@ -610,10 +611,11 @@ private fun importJsonData(
Log.d("import", "Importing categories")
val categoriesTable = databaseBackup.data["categories"] ?: emptyList()
categoriesTable.forEach { categoriesData ->
Log.d("colorimport", categoriesData["color"].toString())
val category =
Category(
id = (categoriesData["id"] as? Double)?.toInt() ?: 0,
color = categoriesData["color"] as String,
color = (categoriesData["color"] as? Double)?.toLong() ?: 0,
emoji = categoriesData["emoji"] as String,
isDefault = categoriesData["isDefault"] as Boolean,
name = categoriesData["name"] as String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import cloud.pablos.overload.R
import cloud.pablos.overload.data.Converters.Companion.convertColorToLong
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.ui.views.TextView

Expand Down Expand Up @@ -65,7 +66,7 @@ fun ConfigurationsTabCreateCategoryDialog(
onClick = {
categoryEvent(CategoryEvent.SetName(name.text))
categoryEvent(CategoryEvent.SetEmoji("💙"))
categoryEvent(CategoryEvent.SetColor(Color.Blue.toString()))
categoryEvent(CategoryEvent.SetColor(convertColorToLong(Color.Blue)))
categoryEvent(CategoryEvent.SetIsDefault(false))
categoryEvent(CategoryEvent.SaveCategory)
onClose()
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/cloud/pablos/overload/ui/tabs/home/HomeTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import androidx.compose.material3.PrimaryTabRow
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRowDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import cloud.pablos.overload.data.Helpers
import cloud.pablos.overload.data.category.CategoryEvent
import cloud.pablos.overload.data.category.CategoryState
import cloud.pablos.overload.data.item.ItemEvent
Expand All @@ -51,6 +54,8 @@ fun HomeTab(
itemState: ItemState,
itemEvent: (ItemEvent) -> Unit,
) {
val backgroundColor = Helpers.decideBackground(categoryState)

val pagerState =
rememberPagerState(
initialPage = 2,
Expand Down Expand Up @@ -102,6 +107,13 @@ fun HomeTab(
PrimaryTabRow(
selectedTabIndex = pagerState.currentPage,
divider = {},
indicator = {
TabRowDefaults.PrimaryIndicator(
modifier = Modifier.tabIndicatorOffset(pagerState.currentPage, matchContentSize = true),
width = Dp.Unspecified,
color = backgroundColor,
)
},
) {
homeTabItems.forEachIndexed { index, item ->
Tab(
Expand Down
Loading

0 comments on commit ad79b47

Please sign in to comment.