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

Syntax highlight parsed transactions #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 86 additions & 69 deletions app/src/main/java/be/chvp/nanoledger/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -44,7 +46,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
Expand Down Expand Up @@ -215,74 +216,7 @@ fun MainContent(
LazyColumn(modifier = Modifier.fillMaxSize()) {
items(transactions?.size ?: 0) {
val index = transactions!!.size - it - 1
Card(
colors =
if (index == selected) {
CardDefaults.outlinedCardColors()
} else {
CardDefaults.cardColors()
},
elevation =
if (index == selected) {
CardDefaults.outlinedCardElevation()
} else {
CardDefaults.cardElevation()
},
border =
if (index == selected) {
CardDefaults.outlinedCardBorder(true)
} else {
null
},
modifier =
Modifier.fillMaxWidth().padding(
8.dp,
if (it == 0) 8.dp else 4.dp,
8.dp,
if (it == transactions!!.size - 1) 8.dp else 4.dp,
),
) {
Box(modifier = Modifier.clickable { mainViewModel.toggleSelect(index) }) {
val tr = transactions!![index]
Column(modifier = Modifier.fillMaxWidth().padding(8.dp)) {
Text(
transactionHeader(tr),
softWrap = false,
style =
MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
),
overflow = TextOverflow.Ellipsis,
)
for (p in tr.postings) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth(),
) {
Text(
" ${p.account}",
softWrap = false,
style =
MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
),
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
Text(
p.amount ?: "",
softWrap = false,
style =
MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
),
modifier = Modifier.padding(start = 2.dp),
)
}
}
}
}
}
TransactionCard(selected, index, transactions, mainViewModel, it)
}
}
} else {
Expand Down Expand Up @@ -328,6 +262,89 @@ fun MainContent(
}
}

@Composable
fun TransactionCard(selected: Int?, index: Int, transactions: List<Transaction>?, mainViewModel: MainViewModel, it: Int){

val isDarkTheme = isSystemInDarkTheme()

val backgroundColor = if (index == selected) {
if (isDarkTheme) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.tertiaryContainer
} else {
if (isDarkTheme) MaterialTheme.colorScheme.inverseOnSurface
else MaterialTheme.colorScheme.surfaceVariant
}

val contentColor = if (index == selected) {
MaterialTheme.colorScheme.onPrimaryContainer
} else {
if (isDarkTheme) MaterialTheme.colorScheme.onSurface
else MaterialTheme.colorScheme.onSurface
}

val elevation = if (index == selected) {
8.dp
} else {
2.dp
}

Card(
colors = CardDefaults.cardColors(
containerColor = backgroundColor,
contentColor = contentColor
),
elevation = CardDefaults.cardElevation(elevation),
border =if (index == selected) BorderStroke(1.dp, MaterialTheme.colorScheme.outline) else null,
modifier =
Modifier.fillMaxWidth().padding(
8.dp,
if (it == 0) 8.dp else 4.dp,
8.dp,
if (it == transactions!!.size - 1) 8.dp else 4.dp,
),
) {
Box(modifier = Modifier.clickable { mainViewModel.toggleSelect(index) }) {
val tr = transactions[index]
Column(modifier = Modifier.fillMaxWidth().padding(8.dp)) {
Text(
transactionHeader(tr),
softWrap = false,
style =
MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
),
overflow = TextOverflow.Ellipsis,
)
for (p in tr.postings) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth(),
) {
Text(
" ${p.account}",
softWrap = false,
style =
MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
),
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
Text(
p.amount ?: "",
softWrap = false,
style =
MaterialTheme.typography.bodySmall.copy(
fontFamily = FontFamily.Monospace,
),
modifier = Modifier.padding(start = 2.dp),
)
}
}
}
}
}
}

@Composable
fun MainBar(mainViewModel: MainViewModel = viewModel()) {
val context = LocalContext.current
Expand Down