Skip to content

Commit

Permalink
UI improvements (#948)
Browse files Browse the repository at this point in the history
* UI improvements
  • Loading branch information
Altonss authored Jan 5, 2025
1 parent b89392d commit ed094aa
Show file tree
Hide file tree
Showing 37 changed files with 202 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import de.grobox.transportr.ui.LineView
import de.grobox.transportr.utils.DateUtils.formatDelay
import de.grobox.transportr.utils.DateUtils.formatTime
import de.grobox.transportr.utils.DateUtils.formatRelativeTime
import de.grobox.transportr.utils.TransportrUtils.getLineColor
import de.grobox.transportr.utils.TransportrUtils.getLocationName
import de.schildbach.pte.dto.Departure
import kotlinx.android.synthetic.main.list_item_departure.view.*
Expand Down Expand Up @@ -77,8 +78,9 @@ internal class DepartureViewHolder(v: View) : RecyclerView.ViewHolder(v) {
}
} ?: run { delay.visibility = GONE }

val lineColor = getLineColor(line.context, dep.line)
// line icon and name
line.setLine(dep.line)
line.setLine(dep.line, lineColor)
lineName.text = dep.line.name

// line destination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import de.grobox.transportr.R
import de.grobox.transportr.ui.LineView
import de.grobox.transportr.utils.TransportrUtils
import de.schildbach.pte.dto.Line


Expand Down Expand Up @@ -51,7 +52,9 @@ internal class LineAdapter : RecyclerView.Adapter<LineViewHolder>() {
}

internal class LineViewHolder(private val lineView: LineView) : RecyclerView.ViewHolder(lineView) {

fun bind(line: Line) = lineView.setLine(line)
fun bind(line: Line) {
val lineColor = TransportrUtils.getLineColor(lineView.context, line)
lineView.setLine(line, lineColor)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ internal abstract class BaseViewHolder(v: View) : RecyclerView.ViewHolder(v) {

@SuppressLint("SetTextI18n")
protected fun TextView.addPlatform(position: Position?) {
if (position == null) return
text = "$text ${context.getString(R.string.platform, position.toString())}"
if (position == null) {
visibility = GONE
} else {
text = context.getString(R.string.platform, position.toString())
visibility = VISIBLE
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import android.view.View.VISIBLE
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat.getColor
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -38,6 +37,7 @@ import de.grobox.transportr.trips.detail.LegViewHolder.LegType.*
import de.grobox.transportr.ui.LineView
import de.grobox.transportr.utils.DateUtils
import de.grobox.transportr.utils.DateUtils.formatDuration
import de.grobox.transportr.utils.TransportrUtils.getLineColor
import de.grobox.transportr.utils.TransportrUtils.getLocationName
import de.schildbach.pte.dto.Line
import de.schildbach.pte.dto.Stop
Expand All @@ -58,6 +58,7 @@ internal class LegViewHolder(v: View, private val listener: LegClickListener, pr

private val fromCircle: ImageView = v.fromCircle
private val fromLocation: TextView = v.fromLocation
private val fromPlatform: TextView = v.fromPlatform
private val fromButton: ImageButton = v.fromButton

private val lineBar: ImageView = v.lineBar
Expand All @@ -71,14 +72,17 @@ internal class LegViewHolder(v: View, private val listener: LegClickListener, pr

private val toCircle: ImageView = v.toCircle
private val toLocation: TextView = v.toLocation
private val toPlatform: TextView = v.toPlatform
private val toButton: ImageButton = v.toButton

private val adapter = StopAdapter(listener)

fun bind(leg: Leg, legType: LegType) {
// Locations
fromLocation.text = getLocationName(leg.departure)
toLocation.text = getLocationName(leg.arrival)
// Hide platforms by default
fromPlatform.visibility = GONE
toPlatform.visibility = GONE

fromLocation.setOnClickListener { listener.onLocationClick(leg.departure) }
toLocation.setOnClickListener { listener.onLocationClick(leg.arrival) }
Expand Down Expand Up @@ -119,11 +123,14 @@ internal class LegViewHolder(v: View, private val listener: LegClickListener, pr
setArrivalTimes(toTime, toDelay, leg.arrivalStop)

// Departure and Arrival Platform
fromLocation.addPlatform(leg.departurePosition)
toLocation.addPlatform(leg.arrivalPosition)
fromPlatform.addPlatform(leg.departurePosition)
toPlatform.addPlatform(leg.arrivalPosition)

// Get Line color
val lineColor = getLineColor(lineBar.context, leg.line)

// Line
lineView.setLine(leg.line)
lineView.setLine(leg.line, lineColor)
if (showLineName && !isNullOrEmpty(leg.line.name)) {
lineDestination.text = leg.line.name
} else if (leg.destination != null) {
Expand All @@ -133,7 +140,6 @@ internal class LegViewHolder(v: View, private val listener: LegClickListener, pr
}

// Line bar
val lineColor = getLineColor(leg.line)
fromCircle.setColorFilter(lineColor)
lineBar.setColorFilter(lineColor)
toCircle.setColorFilter(lineColor)
Expand Down Expand Up @@ -212,13 +218,4 @@ internal class LegViewHolder(v: View, private val listener: LegClickListener, pr
stopsList.visibility = GONE
}

@ColorInt
private fun getLineColor(line: Line): Int {
if (line.style == null) return DEFAULT_LINE_COLOR
if (line.style!!.backgroundColor != 0) return line.style!!.backgroundColor
if (line.style!!.backgroundColor2 != 0) return line.style!!.backgroundColor2
if (line.style!!.foregroundColor != 0) return line.style!!.foregroundColor
return if (line.style!!.borderColor != 0) line.style!!.borderColor else DEFAULT_LINE_COLOR
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class StopViewHolder(v: View, private val listener : LegClickListener)

private val circle: ImageView = v.circle
private val stopLocation: TextView = v.stopLocation
private val stopPlatform: TextView = v.stopPlatform
private val stopButton: ImageButton = v.stopButton

fun bind(stop: Stop, color: Int) {
Expand Down Expand Up @@ -71,7 +72,7 @@ internal class StopViewHolder(v: View, private val listener : LegClickListener)

stopLocation.text = getLocationName(stop.location)
stopLocation.setOnClickListener { listener.onLocationClick(stop.location) }
stopLocation.addPlatform(stop.arrivalPosition)
stopPlatform.addPlatform(stop.arrivalPosition)

// show popup on button click
stopButton.setOnClickListener { LegPopupMenu(stopButton.context, stopButton, stop).show() }
Expand Down
23 changes: 3 additions & 20 deletions app/src/main/java/de/grobox/transportr/trips/detail/TripDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap
import de.grobox.transportr.R
import de.grobox.transportr.map.MapDrawer
import de.grobox.transportr.utils.DateUtils.formatTime
import de.grobox.transportr.utils.TransportrUtils
import de.grobox.transportr.utils.hasLocation
import de.schildbach.pte.dto.Location
import de.schildbach.pte.dto.Point
Expand All @@ -60,7 +61,7 @@ internal class TripDrawer(context: Context) : MapDrawer(context) {

// get colors
val backgroundColor = getBackgroundColor(leg)
val foregroundColor = getForegroundColor(leg)
val foregroundColor = TransportrUtils.getTextColorBasedOnBackground(backgroundColor)

// draw leg path first, so it is always at the bottom
val points = ArrayList<LatLng>(leg.path.size)
Expand Down Expand Up @@ -134,29 +135,11 @@ internal class TripDrawer(context: Context) : MapDrawer(context) {
@ColorInt
private fun getBackgroundColor(leg: Leg): Int {
if (leg is Public) {
val line = leg.line
return if (line?.style != null && line.style!!.backgroundColor != 0) {
line.style!!.backgroundColor
} else {
ContextCompat.getColor(context, R.color.accent)
}
return TransportrUtils.getLineColor(context, leg.line)
}
return ContextCompat.getColor(context, R.color.walking)
}

@ColorInt
private fun getForegroundColor(leg: Leg): Int {
if (leg is Public) {
val line = leg.line
return if (line?.style != null && line.style!!.foregroundColor != 0) {
line.style!!.foregroundColor
} else {
ContextCompat.getColor(context, android.R.color.white)
}
}
return ContextCompat.getColor(context, android.R.color.black)
}

private fun markLocation(map: MapboxMap, location: Location, icon: Icon, text: String) {
markLocation(map, location, icon, location.uniqueShortName(), text)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal object TripUtils {
if (leg is Trip.Public) {
// show departure position if existing
if (leg.departurePosition != null) {
str += " " + context.getString(R.string.platform, leg.departurePosition.toString())
str += " (${context.getString(R.string.platform, leg.departurePosition.toString())})"
}
str += "\n ${getEmojiForProduct(leg.line?.product)} "
leg.line?.label?.let {
Expand All @@ -126,7 +126,7 @@ internal object TripUtils {

// add arrival position if existing
if (leg is Trip.Public && leg.arrivalPosition != null) {
str += " ${context.getString(R.string.platform, leg.arrivalPosition.toString())}"
str += " (${context.getString(R.string.platform, leg.arrivalPosition.toString())})"
}
return str
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import de.grobox.transportr.ui.LineView
import de.grobox.transportr.utils.DateUtils.formatDuration
import de.grobox.transportr.utils.DateUtils.formatTime
import de.grobox.transportr.utils.DateUtils.formatRelativeTime
import de.grobox.transportr.utils.TransportrUtils
import de.grobox.transportr.utils.TransportrUtils.getLocationName
import de.schildbach.pte.dto.Trip
import de.schildbach.pte.dto.Trip.Individual
Expand Down Expand Up @@ -81,7 +82,7 @@ internal class TripViewHolder(private val v: View) : BaseViewHolder(v) {
for (leg in trip.legs) {
val lineView = LayoutInflater.from(context).inflate(R.layout.list_item_line, lines, false) as LineView
when (leg) {
is Public -> lineView.setLine(leg.line)
is Public -> lineView.setLine(leg.line, TransportrUtils.getLineColor(lineView.context, leg.line))
is Individual -> lineView.setWalk()
else -> throw RuntimeException()
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/de/grobox/transportr/ui/LineView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import android.util.AttributeSet
import de.grobox.transportr.R
import de.grobox.transportr.utils.TransportrUtils.dpToPx
import de.grobox.transportr.utils.TransportrUtils.getDrawableForProduct
import de.grobox.transportr.utils.TransportrUtils.getTextColorBasedOnBackground
import de.schildbach.pte.dto.Line

class LineView(context: Context, attr: AttributeSet?) : AppCompatTextView(context, attr) {

fun setLine(line: Line) {
fun setLine(line: Line, backgroundColor: Int) {
// get colors
val foregroundColor = line.style?.foregroundColor
val backgroundColor = line.style?.backgroundColor
val foregroundColor = getTextColorBasedOnBackground(backgroundColor)

// set colored background
val backgroundDrawable = getDrawable(context, R.drawable.line_box) as GradientDrawable
Expand All @@ -61,7 +61,7 @@ class LineView(context: Context, attr: AttributeSet?) : AppCompatTextView(contex

// set colored label
text = line.label
if (foregroundColor != null) setTextColor(foregroundColor)
setTextColor(foregroundColor)
}

fun setWalk() {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/de/grobox/transportr/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public interface Constants {
int LOADER_DEPARTURES = 1;
int LOADER_NEARBY_STATIONS = 2;

// Contrast value for line color
double MIN_CONTRAST_RATIO = 1.3;

}
42 changes: 42 additions & 0 deletions app/src/main/java/de/grobox/transportr/utils/TransportrUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import android.content.ClipboardManager
import android.content.Context
import android.content.Context.CLIPBOARD_SERVICE
import android.content.Context.CONNECTIVITY_SERVICE
import android.graphics.Color
import android.net.ConnectivityManager
import android.util.DisplayMetrics.DENSITY_DEFAULT
import android.util.TypedValue
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import de.grobox.transportr.R
import de.grobox.transportr.trips.detail.LegViewHolder
import de.schildbach.pte.dto.Line
import de.schildbach.pte.dto.Location
import de.schildbach.pte.dto.LocationType
import de.schildbach.pte.dto.Product
Expand Down Expand Up @@ -111,6 +115,44 @@ object TransportrUtils {
return ContextCompat.getColor(this, typedValue.run { if (resourceId != 0) resourceId else data })
}

fun getLineColor(context: Context, line: Line): Int {
val themeBackgroundColor = context.getColorFromAttr(R.attr.material_drawer_background)
val colorsToCheck = listOfNotNull(
line.style?.backgroundColor,
line.style?.backgroundColor2,
line.style?.foregroundColor,
line.style?.borderColor
)

for (color in colorsToCheck) {
if (color != 0) {
return if (isContrastSufficient(color, themeBackgroundColor)) {
color
} else {
invertColor(color)
}
}
}

return LegViewHolder.DEFAULT_LINE_COLOR
}

private fun isContrastSufficient(color1: Int, color2: Int): Boolean {
val contrastRatio = ColorUtils.calculateContrast(color1, color2)
return contrastRatio >= Constants.MIN_CONTRAST_RATIO
}

private fun invertColor(color: Int): Int {
val r = 255 - Color.red(color)
val g = 255 - Color.green(color)
val b = 255 - Color.blue(color)
return Color.rgb(r, g, b)
}

fun getTextColorBasedOnBackground(backgroundColor: Int): Int {
val luminance = ColorUtils.calculateLuminance(backgroundColor)
return if (luminance > 0.5) Color.BLACK else Color.WHITE
}
}

fun Location.hasLocation() = hasCoord() && (latAs1E6 != 0 || lonAs1E6 != 0)
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/platform_box.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid
android:color="@color/md_indigo_900"/>

<corners
android:radius="@dimen/line_box_corner_radius"/>

<padding
android:top="1dp"
android:left="4dp"
android:right="4dp"
android:bottom="1dp"/>
</shape>
21 changes: 21 additions & 0 deletions app/src/main/res/drawable/platform_box_secondary.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid
android:color="@android:color/transparent"/>

<stroke
android:width="1.5dp"
android:color="?android:textColorSecondary"/>

<corners
android:radius="@dimen/line_box_corner_radius"/>

<padding
android:top="1dp"
android:left="4dp"
android:right="4dp"
android:bottom="1dp"/>
</shape>
Loading

0 comments on commit ed094aa

Please sign in to comment.