Skip to content

Commit

Permalink
Add resource version of some properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mahozad committed Aug 16, 2021
1 parent ff040d4 commit 0885cf0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ class PieChartAPITest {
assertThat(pieChart.legendIconsHeight.px).isEqualTo(expected)
}

@Test fun changeLegendsTitleResourceShouldChangeLegendsTitleAsWell() {
val resourceId = ir.mahozad.android.test.R.string.testLegendsTitle
val expected = resources.getString(resourceId)
pieChart.legendsTitleResource = resourceId
assertThat(pieChart.legendsTitle).isEqualTo(expected)
}

@Test fun changeChartLegendsMarginResourceShouldChangeLegendsMarginAsWell() {
val resourceId = ir.mahozad.android.test.R.dimen.testLegendsMargin
val expected = resources.getDimension(resourceId)
pieChart.legendsMarginResource = resourceId
assertThat(pieChart.legendsMargin.px).isEqualTo(expected)
}

@Test fun changeChartLegendsColorResourceShouldChangeLegendsColorAsWell() {
val resourceId = ir.mahozad.android.test.R.color.testLegendsColor
val expected = resources.getColor(resourceId, null)
pieChart.legendsColorResource = resourceId
assertThat(pieChart.legendsColor).isEqualTo(expected)
}





@Test fun changeChartCenterBackgroundStatusResourceShouldChangeCenterBackgroundStatusAsWell() {
val resourceId1 = ir.mahozad.android.test.R.bool.testCenterBackgroundEnabled
val resourceId2 = ir.mahozad.android.test.R.bool.testCenterBackgroundDisabled
Expand Down
1 change: 1 addition & 0 deletions piechart/src/androidTest/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<resources>
<color name="testCenterBackgroundColor">#a38f02</color>
<color name="testLegendsColor">#669148</color>
<color name="testColor">#f0f</color>
<array name="testColorArray">
<item>#ff00AE28</item>
Expand Down
1 change: 1 addition & 0 deletions piechart/src/androidTest/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<dimen name="testLegendsSize">23sp</dimen>
<dimen name="testLegendsPercentageSize">23sp</dimen>
<dimen name="testLegendIconsHeight">27dp</dimen>
<dimen name="testLegendsMargin">31dp</dimen>
<integer name="testStartAngle">88</integer>
<fraction name="testHoleRatio">43%</fraction>
<fraction name="testOverlayRatio">73%</fraction>
Expand Down
3 changes: 3 additions & 0 deletions piechart/src/androidTest/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="testLegendsTitle">aTestyO</string>
</resources>
3 changes: 3 additions & 0 deletions piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class PieChart @JvmOverloads constructor(
onSizeChanged(width, height, width, height)
}

var legendsTitleResource by StringResource(::legendsTitle)
var legendsTitle by Property(DEFAULT_LEGENDS_TITLE) {
onSizeChanged(width, height, width, height)
}
Expand All @@ -357,10 +358,12 @@ class PieChart @JvmOverloads constructor(
onSizeChanged(width, height, width, height)
}

var legendsMarginResource by DimensionResource(::legendsMargin)
var legendsMargin by Property(DEFAULT_LEGENDS_MARGIN) {
onSizeChanged(width, height, width, height)
}

var legendsColorResource by ColorResource(::legendsColor)
var legendsColor by Property(DEFAULT_LEGENDS_COLOR) {
// TODO: No need to recalculate everything; provide a method in legend box for this
onSizeChanged(width, height, width, height)
Expand Down
4 changes: 4 additions & 0 deletions piechart/src/main/kotlin/ir/mahozad/android/Properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class DimensionResource(override val mainProperty: KMutableProperty0<Dimension>)
override fun resolveResourceValue(context: Context) = PX(context.resources.getDimension(resId))
}

class StringResource(override val mainProperty: KMutableProperty0<String>) : PropertyResource<String>() {
override fun resolveResourceValue(context: Context) = context.resources.getString(resId)
}

// class EnumResource<T : Enum<T>>(override val mainProperty: KMutableProperty0<T>) : PropertyResource<T>() {
// override fun resolveResourceValue(context: Context): T {
// context.withStyledAttributes(resId, R.styleable.PieChart) {
Expand Down

0 comments on commit 0885cf0

Please sign in to comment.