Skip to content

Commit

Permalink
Rewrite LineColor to be a mapper (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob authored Jan 14, 2024
1 parent cb6cb1d commit 563277b
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.annotation.QueryValue
import io.micronaut.views.View
import net.twisterrob.blt.data.StaticData
import net.twisterrob.blt.io.feeds.trackernet.LineStatusFeed
import net.twisterrob.travel.domain.london.status.Feed
import net.twisterrob.travel.domain.london.status.api.ParsedStatusItem
import net.twisterrob.travel.domain.london.status.api.StatusHistoryRepository
import net.twisterrob.travel.statushistory.viewmodel.LineColor
import net.twisterrob.travel.statushistory.viewmodel.LineStatusModel
import net.twisterrob.travel.statushistory.viewmodel.LineStatusModelMapper
import net.twisterrob.travel.statushistory.viewmodel.Result
import net.twisterrob.travel.statushistory.viewmodel.ResultChangeModelMapper
import net.twisterrob.travel.statushistory.viewmodel.ResultChangesCalculator
import java.util.Date

@Controller
class LineStatusHistoryController(
private val useCase: StatusHistoryRepository,
private val staticData: StaticData,
private val modelMapper: LineStatusModelMapper,
) {

@Get("/LineStatusHistory")
Expand All @@ -40,12 +37,7 @@ class LineStatusHistoryController(
.map(ParsedStatusItem::toResult)
val changes = ResultChangesCalculator().getChanges(results)

return HttpResponse.ok(
LineStatusModel(
changes.map(ResultChangeModelMapper()::map),
LineColor.AllColors(staticData.lineColors)
)
)
return HttpResponse.ok(modelMapper.map(changes))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.twisterrob.blt.data.StaticData
import net.twisterrob.blt.io.feeds.LocalhostUrlBuilder
import net.twisterrob.blt.io.feeds.TFLUrlBuilder
import net.twisterrob.blt.io.feeds.URLBuilder
import net.twisterrob.blt.model.LineColors
import net.twisterrob.travel.domain.london.status.DomainStatusHistoryRepository
import net.twisterrob.travel.domain.london.status.DomainRefreshUseCase
import net.twisterrob.travel.domain.london.status.api.FeedParser
Expand Down Expand Up @@ -53,6 +54,10 @@ class Dependencies {
fun staticData(): StaticData =
SharedStaticData()

@Singleton
fun lineColors(staticData: StaticData): LineColors =
staticData.lineColors

@Singleton
@Requires(notEnv = ["development"])
fun urlBuilder(): URLBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,7 @@ object HandlebarsHelpers {
fun formatDate(value: Date, pattern: String): String =
SimpleDateFormat(pattern, Locale.getDefault()).format(value)

/**
* Workaround for `myEnumMap.[Foo]` and `lookup myEnumMap Foo` not working.
* Usage: Replace `(lookup someEnumMap someEnumKey)` with `(lookupEnumMap someEnumMap someEnumKey)`.
* See https://github.com/jknack/handlebars.java/issues/1083.
*/
@JvmStatic
fun <E : Enum<E>> lookupEnumMap(map: EnumMap<E, *>, key: E): Any? =
map[key]

/**
* Workaround for `myMap.[Foo]` and `lookup myMap Foo` not working, because Handlebars toStrings the keys.
* Usage: Replace `(lookup someEnumMap someEnumKey)` with `(lookupMap someMap someKey)`.
*/
@JvmStatic
fun <K> lookupMap(map: Map<K, *>, key: K): Any? =
map[key]
fun formatCssColor(value: Int): String =
"#%06X".format(Locale.ROOT, value and @Suppress("detekt.MagicNumber") 0xFFFFFF)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.twisterrob.travel.statushistory.viewmodel

import net.twisterrob.blt.model.Line

class LineColorsModel(
val line: Line,
val foregroundColor: Int,
val backgroundColor: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.twisterrob.travel.statushistory.viewmodel

import jakarta.inject.Inject
import net.twisterrob.blt.model.Line
import net.twisterrob.blt.model.LineColors

class LineColorsModelMapper @Inject constructor(
private val colors: LineColors,
) {

@OptIn(ExperimentalStdlibApi::class)
fun map(): List<LineColorsModel> =
Line.entries.map { line ->
LineColorsModel(
line = line,
foregroundColor = line.getForeground(colors),
backgroundColor = line.getBackground(colors),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.twisterrob.travel.statushistory.viewmodel

import jakarta.inject.Inject

class LineStatusModelMapper @Inject constructor(
private val resultChangeModelMapper: ResultChangeModelMapper,
private val lineColorsModelMapper: LineColorsModelMapper,
) {

fun map(changes: List<Changes>): LineStatusModel =
LineStatusModel(
changes.map(resultChangeModelMapper::map),
lineColorsModelMapper.map()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.Date

class LineStatusModel(
val feedChanges: List<ResultChangeModel>,
val colors: Iterable<LineColor>,
val colors: List<LineColorsModel>,
)

class ResultChangeModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.twisterrob.travel.statushistory.viewmodel

import jakarta.inject.Inject
import net.twisterrob.blt.diff.HtmlDiff
import net.twisterrob.blt.io.feeds.trackernet.model.LineStatus
import net.twisterrob.blt.model.Line
import net.twisterrob.travel.statushistory.viewmodel.ResultChangeModel.LineStatusModel

class ResultChangeModelMapper {
class ResultChangeModelMapper @Inject constructor(
) {

fun map(changes: Changes): ResultChangeModel =
ResultChangeModel(
Expand Down
4 changes: 2 additions & 2 deletions web/status-history/src/main/resources/views/LineStatus.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
{{#each colors as | lineColor | }}
.line-{{lineColor.line}} {
color: {{lineColor.foregroundColor}};
background-color: {{lineColor.backgroundColor}};
color: {{formatCssColor lineColor.foregroundColor}};
background-color: {{formatCssColor lineColor.backgroundColor}};
}
{{/each}}
</style>
Expand Down
Loading

0 comments on commit 563277b

Please sign in to comment.