Skip to content

Commit 6680cb5

Browse files
committed
Refactor chart adapters to use reusable ChartListAdapter
Introduced a generic ChartListAdapter to reduce code duplication across chart adapters. Updated CustomStyleChartAdapter, MixedChartAdapter, and SpecialChartAdapter to extend ChartListAdapter, simplifying their implementations. Adjusted related activities and unified chart item data classes for consistency.
1 parent 094f1d6 commit 6680cb5

File tree

7 files changed

+86
-124
lines changed

7 files changed

+86
-124
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.aachartmodel.aainfographics.demo.basiccontent
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.TextView
7+
import androidx.annotation.LayoutRes
8+
import androidx.recyclerview.widget.RecyclerView
9+
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartModel
10+
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartView
11+
import com.github.aachartmodel.aainfographics.demo.R
12+
13+
/**
14+
* Reusable RecyclerView adapter for chart demo lists.
15+
*/
16+
open class ChartListAdapter(
17+
@LayoutRes private val layoutResId: Int,
18+
private val chartItems: List<ChartItem>,
19+
private val chartModelProvider: (ChartItem) -> AAChartModel,
20+
private val onBindAdditionalViews: (holder: ViewHolder, item: ChartItem, position: Int) -> Unit = { _, _, _ -> }
21+
) : RecyclerView.Adapter<ChartListAdapter.ViewHolder>() {
22+
23+
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
24+
val nameTextView: TextView? = view.findViewById(R.id.tv_chart_name)
25+
val indexTextView: TextView? = view.findViewById(R.id.tv_chart_index)
26+
val aaChartView: AAChartView = view.findViewById(R.id.aa_chart_view)
27+
}
28+
29+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
30+
val view = LayoutInflater.from(parent.context).inflate(layoutResId, parent, false)
31+
return ViewHolder(view)
32+
}
33+
34+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
35+
val item = chartItems[position]
36+
holder.nameTextView?.text = item.name
37+
holder.indexTextView?.let { indexView ->
38+
indexView.text = "${position + 1}."
39+
}
40+
holder.aaChartView.aa_drawChartWithChartModel(chartModelProvider(item))
41+
onBindAdditionalViews(holder, item, position)
42+
}
43+
44+
override fun getItemCount(): Int = chartItems.size
45+
}
46+
47+
/**
48+
* Represents a chart demo entry with a display name and internal type.
49+
*/
50+
data class ChartItem(val name: String, val chartType: String)

sample/src/main/java/com/github/aachartmodel/aainfographics/demo/basiccontent/CustomStyleChartAdapter.kt

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
11
package com.github.aachartmodel.aainfographics.demo.basiccontent
22

3-
import android.content.Context
4-
import android.view.LayoutInflater
5-
import android.view.View
6-
import android.view.ViewGroup
7-
import android.widget.TextView
8-
import androidx.recyclerview.widget.RecyclerView
9-
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartModel
10-
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartView
113
import com.github.aachartmodel.aainfographics.demo.R
124
import com.github.aachartmodel.aainfographics.demo.chartcomposer.CustomStyleChartComposer
135

146
class CustomStyleChartAdapter(
15-
private val context: Context,
16-
private val chartItems: List<ChartItem>
17-
) : RecyclerView.Adapter<CustomStyleChartAdapter.ViewHolder>() {
18-
19-
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
20-
val nameTextView: TextView = view.findViewById(R.id.tv_chart_name)
21-
val aaChartView: AAChartView = view.findViewById(R.id.aa_chart_view)
22-
}
23-
24-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
25-
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_custom_style_chart, parent, false)
26-
return ViewHolder(view)
27-
}
28-
29-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
30-
val item = chartItems[position]
31-
holder.nameTextView.text = item.name
32-
val aaChartModel = getChartModel(item.chartType)
33-
holder.aaChartView.aa_drawChartWithChartModel(aaChartModel)
34-
}
35-
36-
override fun getItemCount(): Int = chartItems.size
37-
38-
private fun getChartModel(chartType: String): AAChartModel {
39-
return when (chartType) {
7+
chartItems: List<ChartItem>
8+
) : ChartListAdapter(
9+
layoutResId = R.layout.item_custom_style_chart,
10+
chartItems = chartItems,
11+
chartModelProvider = { item ->
12+
when (item.chartType) {
4013
"configureColorfulChart" -> CustomStyleChartComposer.configureColorfulChart()
4114
"configureColorfulGradientColorChart" -> CustomStyleChartComposer.configureColorfulGradientColorChart()
4215
"configureDiscontinuousDataChart" -> CustomStyleChartComposer.configureDiscontinuousDataChart()
@@ -87,4 +60,4 @@ class CustomStyleChartAdapter(
8760
else -> CustomStyleChartComposer.configureColorfulChart()
8861
}
8962
}
90-
}
63+
)

sample/src/main/java/com/github/aachartmodel/aainfographics/demo/basiccontent/CustomStyleChartListActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CustomStyleChartListActivity : AppCompatActivity() {
1515
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
1616
val chartItems = getChartItems()
1717
recyclerView.layoutManager = LinearLayoutManager(this)
18-
recyclerView.adapter = CustomStyleChartAdapter(this, chartItems)
18+
recyclerView.adapter = CustomStyleChartAdapter(chartItems)
1919
}
2020

2121
private fun getChartItems(): List<ChartItem> {
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
11
package com.github.aachartmodel.aainfographics.demo.basiccontent
22

3-
import android.content.Context
4-
import android.view.LayoutInflater
5-
import android.view.View
6-
import android.view.ViewGroup
7-
import android.widget.TextView
8-
import androidx.recyclerview.widget.RecyclerView
9-
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartModel
10-
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartView
113
import com.github.aachartmodel.aainfographics.demo.R
124
import com.github.aachartmodel.aainfographics.demo.chartcomposer.MixedChartComposer
135

14-
data class MixedChartItem(val name: String, val chartType: String)
15-
166
class MixedChartAdapter(
17-
private val context: Context,
18-
private val chartItems: List<MixedChartItem>
19-
) : RecyclerView.Adapter<MixedChartAdapter.ViewHolder>() {
20-
21-
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
22-
val nameTextView: TextView = view.findViewById(R.id.tv_chart_name)
23-
val aaChartView: AAChartView = view.findViewById(R.id.aa_chart_view)
24-
}
25-
26-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
27-
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_mixed_chart, parent, false)
28-
return ViewHolder(view)
29-
}
30-
31-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
32-
val item = chartItems[position]
33-
holder.nameTextView.text = item.name
34-
val aaChartModel = getChartModel(item.chartType)
35-
holder.aaChartView.aa_drawChartWithChartModel(aaChartModel)
36-
}
37-
38-
override fun getItemCount(): Int = chartItems.size
39-
40-
private fun getChartModel(chartType: String): AAChartModel {
41-
return when (chartType) {
7+
chartItems: List<ChartItem>
8+
) : ChartListAdapter(
9+
layoutResId = R.layout.item_mixed_chart,
10+
chartItems = chartItems,
11+
chartModelProvider = { item ->
12+
when (item.chartType) {
4213
"arearangeMixedLine" -> MixedChartComposer.arearangeMixedLine()
4314
"columnrangeMixedLine" -> MixedChartComposer.configureColumnrangeMixedLineChart()
4415
"stackingColumnMixedLine" -> MixedChartComposer.configureStackingColumnMixedLineChart()
@@ -53,4 +24,4 @@ class MixedChartAdapter(
5324
else -> MixedChartComposer.arearangeMixedLine()
5425
}
5526
}
56-
}
27+
)
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.aachartmodel.aainfographics.demo.basiccontent
22

3-
import android.content.Intent
43
import android.os.Bundle
54
import androidx.appcompat.app.AppCompatActivity
65
import androidx.recyclerview.widget.LinearLayoutManager
@@ -16,22 +15,22 @@ class MixedChartListActivity : AppCompatActivity() {
1615
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
1716
val chartItems = getChartItems()
1817
recyclerView.layoutManager = LinearLayoutManager(this)
19-
recyclerView.adapter = MixedChartAdapter(this, chartItems)
18+
recyclerView.adapter = MixedChartAdapter(chartItems)
2019
}
2120

22-
private fun getChartItems(): List<MixedChartItem> {
21+
private fun getChartItems(): List<ChartItem> {
2322
return listOf(
24-
MixedChartItem("Arearange Mixed Line", "arearangeMixedLine"),
25-
MixedChartItem("Columnrange Mixed Line", "columnrangeMixedLine"),
26-
MixedChartItem("Stacking Column Mixed Line", "stackingColumnMixedLine"),
27-
MixedChartItem("Dash Style Type Mixed", "dashStyleTypeMixed"),
28-
MixedChartItem("Negative Color Mixed", "negativeColorMixed"),
29-
MixedChartItem("Scatter Mixed Line", "scatterMixedLine"),
30-
MixedChartItem("Negative Color Mixed Bubble", "negativeColorMixedBubble"),
31-
MixedChartItem("Polygon Mixed Scatter", "polygonMixedScatter"),
32-
MixedChartItem("Polar Chart Mixed", "polarChartMixed"),
33-
MixedChartItem("Pie Mixed Line Mixed Column", "configurePieMixedLineMixedColumnChart"),
34-
MixedChartItem("Negative Color Mixed Areaspline", "configureNegativeColorMixedAreasplineChart")
23+
ChartItem("Arearange Mixed Line", "arearangeMixedLine"),
24+
ChartItem("Columnrange Mixed Line", "columnrangeMixedLine"),
25+
ChartItem("Stacking Column Mixed Line", "stackingColumnMixedLine"),
26+
ChartItem("Dash Style Type Mixed", "dashStyleTypeMixed"),
27+
ChartItem("Negative Color Mixed", "negativeColorMixed"),
28+
ChartItem("Scatter Mixed Line", "scatterMixedLine"),
29+
ChartItem("Negative Color Mixed Bubble", "negativeColorMixedBubble"),
30+
ChartItem("Polygon Mixed Scatter", "polygonMixedScatter"),
31+
ChartItem("Polar Chart Mixed", "polarChartMixed"),
32+
ChartItem("Pie Mixed Line Mixed Column", "configurePieMixedLineMixedColumnChart"),
33+
ChartItem("Negative Color Mixed Areaspline", "configureNegativeColorMixedAreasplineChart")
3534
)
3635
}
3736
}
Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
package com.github.aachartmodel.aainfographics.demo.basiccontent
22

3-
import android.content.Context
4-
import android.view.LayoutInflater
5-
import android.view.View
6-
import android.view.ViewGroup
7-
import android.widget.TextView
8-
import androidx.recyclerview.widget.RecyclerView
9-
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartModel
10-
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartView
113
import com.github.aachartmodel.aainfographics.demo.R
124
import com.github.aachartmodel.aainfographics.demo.chartcomposer.SpecialChartComposer
135

14-
data class ChartItem(val name: String, val chartType: String)
15-
166
class SpecialChartAdapter(
17-
private val context: Context,
18-
private val chartItems: List<ChartItem>
19-
) : RecyclerView.Adapter<SpecialChartAdapter.ViewHolder>() {
20-
21-
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
22-
val indexTextView: TextView = view.findViewById(R.id.tv_chart_index)
23-
val nameTextView: TextView = view.findViewById(R.id.tv_chart_name)
24-
val aaChartView: AAChartView = view.findViewById(R.id.aa_chart_view)
25-
}
26-
27-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
28-
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_special_chart, parent, false)
29-
return ViewHolder(view)
30-
}
31-
32-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
33-
val item = chartItems[position]
34-
holder.indexTextView.text = "${position + 1}."
35-
holder.nameTextView.text = item.name
36-
val aaChartModel = getChartModel(item.chartType)
37-
holder.aaChartView.aa_drawChartWithChartModel(aaChartModel)
38-
}
39-
40-
override fun getItemCount(): Int = chartItems.size
41-
42-
private fun getChartModel(chartType: String): AAChartModel {
43-
return when (chartType) {
7+
chartItems: List<ChartItem>
8+
) : ChartListAdapter(
9+
layoutResId = R.layout.item_special_chart,
10+
chartItems = chartItems,
11+
chartModelProvider = { item ->
12+
when (item.chartType) {
4413
"Column" -> SpecialChartComposer.configurePolarColumnChart()
4514
"Bar" -> SpecialChartComposer.configurePolarBarChart()
4615
"Line" -> SpecialChartComposer.configurePolarLineChart()
@@ -63,4 +32,4 @@ class SpecialChartAdapter(
6332
else -> SpecialChartComposer.configurePolarColumnChart()
6433
}
6534
}
66-
}
35+
)

sample/src/main/java/com/github/aachartmodel/aainfographics/demo/basiccontent/SpecialChartListActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SpecialChartListActivity : AppCompatActivity() {
1515
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
1616
val chartItems = getChartItems()
1717
recyclerView.layoutManager = LinearLayoutManager(this)
18-
recyclerView.adapter = SpecialChartAdapter(this, chartItems)
18+
recyclerView.adapter = SpecialChartAdapter(chartItems)
1919
}
2020

2121
private fun getChartItems(): List<ChartItem> {

0 commit comments

Comments
 (0)