From 0885cf07f682ce880decd6ee7506422aa967d4f0 Mon Sep 17 00:00:00 2001 From: Mahdi Hosseinzadeh Date: Mon, 16 Aug 2021 21:01:29 +0430 Subject: [PATCH] Add resource version of some properties --- .../ir/mahozad/android/PieChartAPITest.kt | 25 +++++++++++++++++++ .../src/androidTest/res/values/colors.xml | 1 + .../src/androidTest/res/values/dimens.xml | 1 + .../src/androidTest/res/values/strings.xml | 3 +++ .../kotlin/ir/mahozad/android/PieChart.kt | 3 +++ .../kotlin/ir/mahozad/android/Properties.kt | 4 +++ 6 files changed, 37 insertions(+) create mode 100644 piechart/src/androidTest/res/values/strings.xml diff --git a/piechart/src/androidTest/kotlin/ir/mahozad/android/PieChartAPITest.kt b/piechart/src/androidTest/kotlin/ir/mahozad/android/PieChartAPITest.kt index 9745fc4f..a3c6ff9d 100644 --- a/piechart/src/androidTest/kotlin/ir/mahozad/android/PieChartAPITest.kt +++ b/piechart/src/androidTest/kotlin/ir/mahozad/android/PieChartAPITest.kt @@ -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 diff --git a/piechart/src/androidTest/res/values/colors.xml b/piechart/src/androidTest/res/values/colors.xml index fcd33ebc..71677ceb 100644 --- a/piechart/src/androidTest/res/values/colors.xml +++ b/piechart/src/androidTest/res/values/colors.xml @@ -1,5 +1,6 @@ #a38f02 + #669148 #f0f #ff00AE28 diff --git a/piechart/src/androidTest/res/values/dimens.xml b/piechart/src/androidTest/res/values/dimens.xml index d30b4e95..59f14669 100644 --- a/piechart/src/androidTest/res/values/dimens.xml +++ b/piechart/src/androidTest/res/values/dimens.xml @@ -5,6 +5,7 @@ 23sp 23sp 27dp + 31dp 88 43% 73% diff --git a/piechart/src/androidTest/res/values/strings.xml b/piechart/src/androidTest/res/values/strings.xml new file mode 100644 index 00000000..0c145feb --- /dev/null +++ b/piechart/src/androidTest/res/values/strings.xml @@ -0,0 +1,3 @@ + + aTestyO + diff --git a/piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt b/piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt index 4376d79e..3fe67548 100644 --- a/piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt +++ b/piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt @@ -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) } @@ -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) diff --git a/piechart/src/main/kotlin/ir/mahozad/android/Properties.kt b/piechart/src/main/kotlin/ir/mahozad/android/Properties.kt index e910bd53..52950c96 100644 --- a/piechart/src/main/kotlin/ir/mahozad/android/Properties.kt +++ b/piechart/src/main/kotlin/ir/mahozad/android/Properties.kt @@ -52,6 +52,10 @@ class DimensionResource(override val mainProperty: KMutableProperty0) override fun resolveResourceValue(context: Context) = PX(context.resources.getDimension(resId)) } +class StringResource(override val mainProperty: KMutableProperty0) : PropertyResource() { + override fun resolveResourceValue(context: Context) = context.resources.getString(resId) +} + // class EnumResource>(override val mainProperty: KMutableProperty0) : PropertyResource() { // override fun resolveResourceValue(context: Context): T { // context.withStyledAttributes(resId, R.styleable.PieChart) {