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

✨ Add swiss product icons #399

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 24 additions & 6 deletions app/src/main/kotlin/de/hbch/traewelling/ui/composables/LineIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ fun LineIcon(
}
val borderColor: Color = lineIcon?.getBorderColor() ?: Color.Transparent

val switzerlandString = getSwitzerlandLineName(
lineId = lineId ?: "",
productName = lineName.split(" ").getOrElse(0) { "" }
)

val displayedName =
lineIcon?.displayedName
?: getSwitzerlandLineName(
lineId = lineId ?: "",
productName = lineName.split(" ").getOrElse(0) { "" }
)
?: lineName
lineIcon?.displayedName ?: switzerlandString?.first?.text ?: lineName

Row(
modifier = modifier,
Expand All @@ -92,6 +92,24 @@ fun LineIcon(
fontWeight = FontWeight.Bold
)
}
} else if (switzerlandString?.first != null) {
Box(
modifier = Modifier
.widthIn(48.dp, 144.dp)
.background(
color = Color.Red
)
.padding(2.dp)
) {
Text(
text = switzerlandString.first!!,
color = Color.White,
style = LineIconStyle,
fontWeight = FontWeight.Bold,
inlineContent = switzerlandString.second,
modifier = Modifier.align(Alignment.Center)
)
}
} else {
Text(
text = displayedName,
Expand Down
57 changes: 51 additions & 6 deletions app/src/main/kotlin/de/hbch/traewelling/util/Adapters.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package de.hbch.traewelling.util

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.unit.sp
import com.auth0.android.jwt.JWT
import de.hbch.traewelling.R
import de.hbch.traewelling.api.models.station.Station
Expand Down Expand Up @@ -84,14 +96,47 @@ fun getLastDestination(trip: HafasTrip): String {
}
}

fun getSwitzerlandLineName(productName: String, lineId: String): String? {
fun getSwitzerlandLineName(productName: String, lineId: String): Pair<AnnotatedString?, Map<String, InlineTextContent>>? {
// Switzerland lines start with 85 in the second block of line id
val splitLineId = lineId.split("-")
if (splitLineId.getOrNull(1)?.startsWith("85") == true) {
val lineNumber = lineId.split("-").getOrNull(2) ?: return null
return "$productName ${lineNumber.uppercase()}"
val match = "\\w+-85\\w*-(\\w+)\$".toRegex().find(lineId)
if (match != null) {
val inlineTextContent = mutableMapOf<String, InlineTextContent>()
val builder = AnnotatedString.Builder()

val icon = when(productName) {
"IR" -> R.drawable.ic_ch_ir
"IC" -> R.drawable.ic_ch_ic
"EC" -> R.drawable.ic_ch_ec
else -> null
}
val italicProduct = if (productName == "PE") "PE" else null

if (icon == null && italicProduct == null)
return null

if (icon != null) {
builder.appendInlineContent("product", productName)
inlineTextContent["product"] = InlineTextContent(
Placeholder(28.sp, 12.sp, PlaceholderVerticalAlign.TextCenter)
) {
Icon(
painter = painterResource(id = icon),
contentDescription = null,
tint = Color.White,
modifier = Modifier.fillMaxSize()
)
}
}
if (italicProduct != null) {
builder.pushStyle(SpanStyle(fontStyle = FontStyle.Italic))
builder.append(italicProduct)
builder.pop()
}
builder.append(" ")
builder.append(match.groupValues.getOrNull(1)?.uppercase() ?: "")
return Pair(builder.toAnnotatedString(), inlineTextContent)
}
return null
return Pair(null, mapOf())
}

private fun clarifyRingbahnBerlin(trip: HafasTrip): String {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_ch_ec.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="40.5dp"
android:height="12dp"
android:viewportWidth="10.716"
android:viewportHeight="3.175"
android:tint="?attr/colorControlNormal">
<path
android:pathData="m1.196,3.051c-0.038,0 -0.065,-0.005 -0.086,-0.022 -0.03,0 -0.059,-0.011 -0.086,-0.032l-0.097,-0.086 -0.648,-0.519L0.158,2.295C0.121,2.268 0.104,2.225 0.104,2.165c0,-0.057 0.024,-0.111 0.076,-0.162l1.729,-1.74 0.076,-0.054 0.054,-0.043C2.087,0.131 2.16,0.112 2.255,0.112l3.177,0l-0.692,0.702L2.73,0.814L2.277,1.257L4.297,1.257L3.844,1.711L1.823,1.711l-0.454,0.454L3.368,2.165L4.406,3.051ZM5.076,3.051 L4.989,3.041C4.967,3.035 4.919,3.006 4.849,2.954L4.773,2.889 4.157,2.36l-0.086,-0.076c-0.03,-0.027 -0.043,-0.068 -0.043,-0.119 0,-0.027 0.003,-0.049 0.011,-0.065 0.014,-0.022 0.046,-0.057 0.097,-0.108L5.983,0.112l4.582,0l-0.681,0.702L6.653,0.814L5.313,2.165l3.685,0L8.112,3.051ZM5.076,3.051"
android:fillColor="#211e1e"
android:strokeColor="#00000000"/>
</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_ch_ic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="29dp"
android:height="12dp"
android:viewportWidth="7.6729"
android:viewportHeight="3.175"
android:tint="?attr/colorControlNormal">
<path
android:pathData="M2.422,0.0835 L1.1224,3.0791l-1.0573,0l1.2996,-2.9956zM7.5653,0.0835 L7.1798,0.9756l-3.3481,0l-0.5286,1.2115l3.3481,0L6.2657,3.0791L1.8273,3.0791l1.2996,-2.9956zM7.5653,0.0835"
android:fillColor="#211e1e"
android:strokeColor="#00000000"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_ch_ir.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="20dp" android:viewportHeight="3.175" android:viewportWidth="7.5406" android:width="47.5dp" android:tint="?attr/colorControlNormal">

<path android:fillColor="#211e1e" android:pathData="M2.4152,0.1071 L1.1422,3.092L0.0996,3.092l1.2949,-2.9849zM7.5071,0.1071L6.7609,1.8739 6.6951,2.0166C6.6786,2.0468 6.6676,2.0714 6.6621,2.0934 6.6539,2.1099 6.6457,2.1208 6.6402,2.1263 6.632,2.1345 6.6073,2.1538 6.5634,2.1812 6.5195,2.2031 6.4317,2.2141 6.3,2.2141L5.6964,2.2141c0.1317,0.1043 0.2414,0.192 0.3292,0.2634 0.0933,0.0658 0.1674,0.118 0.2195,0.1536 0.0494,0.0384 0.1207,0.096 0.2085,0.1756 0.0933,0.0741 0.2113,0.1701 0.3512,0.2853L5.4769,3.092c-0.1536,-0.1536 -0.3018,-0.2908 -0.439,-0.417 -0.1317,-0.1235 -0.2497,-0.2359 -0.3512,-0.3402 -0.1043,-0.1015 -0.2195,-0.214 -0.3512,-0.3402C4.2039,1.8629 4.064,1.7203 3.9186,1.5666L5.8391,1.5776 6.1464,0.8533L3.8418,0.8533L2.92,3.092L1.8335,3.092L3.1175,0.1071ZM7.5071,0.1071" android:strokeColor="#00000000"/>

</vector>
Loading