-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract
Dimension
class to its own file
- Loading branch information
Showing
5 changed files
with
115 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
piechart/src/androidTest/kotlin/ir/mahozad/android/unit/DimensionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ir.mahozad.android.unit | ||
|
||
import ir.mahozad.android.unit.Dimension.* | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.util.FloatComparator | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.TestInstance | ||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments.arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
|
||
/** | ||
* NOTE: The dimensions and the tests depend on the device and its display density. | ||
*/ | ||
@TestInstance(PER_CLASS) | ||
class DimensionTest { | ||
|
||
@DisplayName("Dimensions and their conversions should be correct") | ||
@ParameterizedTest(name = "#{index} with Dimension: {0}") | ||
@MethodSource("argumentProvider") | ||
fun dimensionsAndTheirConversionsShouldBeCorrect( | ||
dimension: Dimension, | ||
expectedPxValue: Float, | ||
expectedDpValue: Float, | ||
expectedSpValue: Float, | ||
) { | ||
assertThat(dimension.px) | ||
.usingComparator(FloatComparator(0.01f)) | ||
.isEqualTo(expectedPxValue) | ||
assertThat(dimension.dp) | ||
.usingComparator(FloatComparator(0.01f)) | ||
.isEqualTo(expectedDpValue) | ||
assertThat(dimension.sp) | ||
.usingComparator(FloatComparator(0.01f)) | ||
.isEqualTo(expectedSpValue) | ||
} | ||
|
||
/** | ||
* Could also have used Kotlin [Triple]. | ||
*/ | ||
private fun argumentProvider() = listOf( | ||
arguments(PX(0f), 0f, 0f, 0f), | ||
arguments(PX(10f), 10f, 3.8f, 3.8f), | ||
arguments(PX(47f), 47f, 17.86f, 17.86f), | ||
arguments(DP(0f), 0f, 0f, 0f), | ||
arguments(DP(10f), 26.31f, 10f, 10f), | ||
arguments(DP(47f), 123.67f, 47f, 47f), | ||
arguments(SP(0f), 0f, 0f, 0f), | ||
arguments(SP(10f), 26.31f, 10f, 10f), | ||
arguments(SP(47f), 123.67f, 47f, 47f), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
piechart/src/main/kotlin/ir/mahozad/android/unit/Dimension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ir.mahozad.android.unit | ||
|
||
import android.content.res.Resources | ||
|
||
/** | ||
* See [#57](https://github.com/mahozad/android-pie-chart/issues/57). | ||
*/ | ||
sealed class Dimension { | ||
|
||
// TODO: Is it safe to use `Resources.getSystem().displayMetrics` | ||
// instead of `context.resources.displayMetrics` which requires context? | ||
// See https://stackoverflow.com/a/17880012/ and https://stackoverflow.com/a/62433235 | ||
|
||
protected abstract val value: Float | ||
/** | ||
* The pixel value of the dimension. | ||
*/ | ||
abstract val px: Float | ||
/** | ||
* The dp value of the dimension. | ||
*/ | ||
abstract val dp: Float | ||
/** | ||
* The sp value of the dimension. | ||
*/ | ||
abstract val sp: Float | ||
|
||
override fun toString() = "${this::class.simpleName}: $value${this::class.simpleName?.lowercase()}" | ||
|
||
class PX(override val value: Float) : Dimension() { | ||
override val px = value | ||
override val dp = value / Resources.getSystem().displayMetrics.density | ||
override val sp = value / Resources.getSystem().displayMetrics.scaledDensity | ||
} | ||
|
||
class DP(override val value: Float) : Dimension() { | ||
override val px = value * Resources.getSystem().displayMetrics.density | ||
override val dp = value | ||
override val sp = PX(px).sp | ||
} | ||
|
||
class SP(override val value: Float) : Dimension() { | ||
override val px = value * Resources.getSystem().displayMetrics.scaledDensity | ||
override val dp = PX(px).dp | ||
override val sp = value | ||
} | ||
} | ||
|
||
inline val Int.dp: Dimension get() = Dimension.DP(this.toFloat()) | ||
inline val Float.dp: Dimension get() = Dimension.DP(this) | ||
inline val Double.dp: Dimension get() = Dimension.DP(this.toFloat()) | ||
inline val Int.sp: Dimension get() = Dimension.SP(this.toFloat()) | ||
inline val Float.sp: Dimension get() = Dimension.SP(this) | ||
inline val Double.sp: Dimension get() = Dimension.SP(this.toFloat()) | ||
inline val Int.px: Dimension get() = Dimension.PX(this.toFloat()) | ||
inline val Float.px: Dimension get() = Dimension.PX(this) | ||
inline val Double.px: Dimension get() = Dimension.PX(this.toFloat()) |