Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Apr 15, 2024
1 parent 5ce56fc commit 611d23b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class SelectionMapFragment(

val pointIds = map.addMarkers(markerDescriptions)
val lineIds = lines.fold(listOf<Int>()) { ids, item ->
ids + map.addPolyLine(LineDescription(item.points, item.strokeWidth, item.strokeColor, false, false))
ids + map.addPolyLine(LineDescription(item.points, item.strokeWidth, item.strokeColor))
}
val polygonIds = polygons.fold(listOf<Int>()) { ids, item ->
ids + map.addPolygon(PolygonDescription(item.points, item.strokeWidth, item.strokeColor, item.fillColor))
Expand Down
10 changes: 5 additions & 5 deletions maps/src/main/java/org/odk/collect/maps/LineDescription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package org.odk.collect.maps
import org.odk.collect.androidshared.utils.toColorInt

data class LineDescription(
val points: List<MapPoint>,
private val strokeWidth: String?,
private val strokeColor: String?,
val draggable: Boolean,
val closed: Boolean
val points: List<MapPoint> = emptyList(),
private val strokeWidth: String? = null,
private val strokeColor: String? = null,
val draggable: Boolean = false,
val closed: Boolean = false
) {
@JvmOverloads
fun getStrokeWidth(forMapbox: Boolean = false): Float {
Expand Down
8 changes: 4 additions & 4 deletions maps/src/main/java/org/odk/collect/maps/PolygonDescription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import androidx.core.graphics.ColorUtils
import org.odk.collect.androidshared.utils.toColorInt

data class PolygonDescription(
val points: List<MapPoint>,
private val strokeWidth: String?,
private val strokeColor: String?,
private val fillColor: String?
val points: List<MapPoint> = emptyList(),
private val strokeWidth: String? = null,
private val strokeColor: String? = null,
private val fillColor: String? = null
) {
fun getStrokeWidth(): Float {
return try {
Expand Down
16 changes: 8 additions & 8 deletions maps/src/test/java/org/odk/collect/maps/LineDescriptionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ import org.junit.runner.RunWith
class LineDescriptionTest {
@Test
fun `getStrokeWidth returns the default value when the passed one is null`() {
val lineDescription = LineDescription(emptyList(), null, null, false, false)
val lineDescription = LineDescription()
assertThat(lineDescription.getStrokeWidth(), equalTo(MapConsts.DEFAULT_STROKE_WIDTH))
}

@Test
fun `getStrokeWidth returns the default value when the passed one is invalid`() {
val lineDescription = LineDescription(emptyList(), "blah", null, false, false)
val lineDescription = LineDescription(strokeWidth = "blah")
assertThat(lineDescription.getStrokeWidth(), equalTo(MapConsts.DEFAULT_STROKE_WIDTH))
}

@Test
fun `getStrokeWidth returns the default value when the passed one is not greater than or equal to zero`() {
val lineDescription = LineDescription(emptyList(), "-1", null, false, false)
val lineDescription = LineDescription(strokeWidth = "-1")
assertThat(lineDescription.getStrokeWidth(), equalTo(MapConsts.DEFAULT_STROKE_WIDTH))
}

@Test
fun `getStrokeWidth returns custom value when the passed one is a valid int number`() {
val lineDescription = LineDescription(emptyList(), "10", null, false, false)
val lineDescription = LineDescription(strokeWidth = "10")
assertThat(lineDescription.getStrokeWidth(), equalTo(10f))
}

@Test
fun `getStrokeWidth returns custom value when the passed one is a valid float number`() {
val lineDescription = LineDescription(emptyList(), "10.5", null, false, false)
val lineDescription = LineDescription(strokeWidth = "10.5")
assertThat(lineDescription.getStrokeWidth(), equalTo(10.5f))
}

@Test
fun `getStrokeColor returns the default color when the passed one is null`() {
val lineDescription = LineDescription(emptyList(), null, null, false, false)
val lineDescription = LineDescription()
assertThat(lineDescription.getStrokeColor(), equalTo(MapConsts.DEFAULT_STROKE_COLOR))
}

@Test
fun `getStrokeColor returns the default color when the passed one is invalid`() {
val lineDescription = LineDescription(emptyList(), null, "blah", false, false)
val lineDescription = LineDescription(strokeColor = "blah")
assertThat(lineDescription.getStrokeColor(), equalTo(MapConsts.DEFAULT_STROKE_COLOR))
}

@Test
fun `getStrokeColor returns custom color when it is valid`() {
val lineDescription = LineDescription(emptyList(), null, "#aaccee", false, false)
val lineDescription = LineDescription(strokeColor = "#aaccee")
assertThat(lineDescription.getStrokeColor(), equalTo(-5583634))
}
}
22 changes: 11 additions & 11 deletions maps/src/test/java/org/odk/collect/maps/PolygonDescriptionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,67 @@ import org.junit.runner.RunWith
class PolygonDescriptionTest {
@Test
fun `getStrokeWidth returns the default value when the passed one is null`() {
val polygonDescription = PolygonDescription(emptyList(), null, null, null)
val polygonDescription = PolygonDescription()
assertThat(polygonDescription.getStrokeWidth(), equalTo(MapConsts.DEFAULT_STROKE_WIDTH))
}

@Test
fun `getStrokeWidth returns the default value when the passed one is invalid`() {
val polygonDescription = PolygonDescription(emptyList(), "blah", null, null)
val polygonDescription = PolygonDescription(strokeWidth = "blah")
assertThat(polygonDescription.getStrokeWidth(), equalTo(MapConsts.DEFAULT_STROKE_WIDTH))
}

@Test
fun `getStrokeWidth returns the default value when the passed one is not greater than or equal to zero`() {
val polygonDescription = PolygonDescription(emptyList(), "-1", null, null)
val polygonDescription = PolygonDescription(strokeWidth = "-1")
assertThat(polygonDescription.getStrokeWidth(), equalTo(MapConsts.DEFAULT_STROKE_WIDTH))
}

@Test
fun `getStrokeWidth returns custom value when the passed one is a valid int number`() {
val polygonDescription = PolygonDescription(emptyList(), "10", null, null)
val polygonDescription = PolygonDescription(strokeWidth = "10")
assertThat(polygonDescription.getStrokeWidth(), equalTo(10f))
}

@Test
fun `getStrokeWidth returns custom value when the passed one is a valid float number`() {
val polygonDescription = PolygonDescription(emptyList(), "10.5", null, null)
val polygonDescription = PolygonDescription(strokeWidth = "10.5")
assertThat(polygonDescription.getStrokeWidth(), equalTo(10.5f))
}

@Test
fun `getStrokeColor returns the default color when the passed one is null`() {
val polygonDescription = PolygonDescription(emptyList(), "0", null, null)
val polygonDescription = PolygonDescription()
assertThat(polygonDescription.getStrokeColor(), equalTo(MapConsts.DEFAULT_STROKE_COLOR))
}

@Test
fun `getStrokeColor returns the default color when the passed one is invalid`() {
val polygonDescription = PolygonDescription(emptyList(), "0", "blah", null)
val polygonDescription = PolygonDescription(strokeColor = "blah")
assertThat(polygonDescription.getStrokeColor(), equalTo(MapConsts.DEFAULT_STROKE_COLOR))
}

@Test
fun `getStrokeColor returns custom color when it is valid`() {
val polygonDescription = PolygonDescription(emptyList(), "0", "#aaccee", null)
val polygonDescription = PolygonDescription(strokeColor = "#aaccee")
assertThat(polygonDescription.getStrokeColor(), equalTo(-5583634))
}

@Test
fun `getFillColor returns the default color when the passed one is null`() {
val polygonDescription = PolygonDescription(emptyList(), "0", null, null)
val polygonDescription = PolygonDescription()
assertThat(polygonDescription.getFillColor(), equalTo(1157562368))
}

@Test
fun `getFillColor returns the default color when the passed one is invalid`() {
val polygonDescription = PolygonDescription(emptyList(), "0", null, "blah")
val polygonDescription = PolygonDescription(fillColor = "blah")
assertThat(polygonDescription.getFillColor(), equalTo(1157562368))
}

@Test
fun `getFillColor returns custom color when it is valid`() {
val polygonDescription = PolygonDescription(emptyList(), "0", null, "#aaccee")
val polygonDescription = PolygonDescription(fillColor = "#aaccee")
assertThat(polygonDescription.getFillColor(), equalTo(1152044270))
}
}

0 comments on commit 611d23b

Please sign in to comment.