앱 내에서 기본 카테고리가 여러 개 있고, 그 카테고리에 맞는 색과 이미지도 있었다. 이를 하드코딩으로 color, string, drawable 리소스를 각각 참조했었지만 카테고리에 대응하는 배열을 만들어서 쓴다면 유지보수에 편할 것 같았다.
카테고리 색 Integer-array로 대체
<!-- 카테고리 색 array-->
<array name="categoryColorArr">
<item name="business">@color/category_business</item>
<item name="health">@color/category_health</item>
<item name="study">@color/category_study</item>
<item name="career">@color/category_career</item>
<item name="dailylife">@color/category_dailylife</item>
<item name="habit">@color/category_habit</item>
<item name="etc">@color/category_etc</item>
</array>
private lateinit var categoryColorArr: IntArray
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatisticsRVAdapter.ViewHolder {
val binding = ItemStatisticsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
context = parent.context
categoryColorArr = context.resources.getIntArray(R.array.categoryColorArr)
return ViewHolder(binding)
}
inner class ViewHolder(val binding: ItemStatisticsBinding) : RecyclerView.ViewHolder(binding.root) {
@SuppressLint("SetTextI18n")
fun bind(item: Statistics) {
with(binding)
{
// 카테고리
val categoryId = item.category.id
itemStatisticsCategoryCv.setCardBackgroundColor(categoryColorArr[categoryId-2])
// (혹은)
itemStatisticsCategoryCv.background.setTint(categoryColorArr[categoryId-2])
...
}
}
카테고리 이름 string-array로 대체
<!-- 카테고리 이름 array -->
<string-array name="CATEGORY">
<item name="business">@string/category_business</item>
<item name="health">@string/category_health</item>
<item name="study">@string/category_study</item>
<item name="career">@string/category_career</item>
<item name="dailylife">@string/category_dailylife</item>
<item name="habit">@string/category_habit</item>
<item name="etc">@string/category_etc</item>
</string-array>
private lateinit var categoryNameArr : Array<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
categoryNameArr = resources.getStringArray(R.array.CATEGORY)
}
private fun initCategoryDropdown() {
// 카테고리 고정 데이터 넣어주기
categoryList.apply {
for (categoryId: Int in categoryNameArr.indices) {
add(CategoryList(categoryId + 2, categoryNameArr[categoryId], categoryImgList.getResourceId(categoryId, -1)))
}
}
...
}
카테고리 이미지 drawable array로 변경
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="categorySelectedImgArr">
<item name="business">@drawable/ic_category_business_on</item>
<item name="health">@drawable/ic_category_health_on</item>
<item name="study">@drawable/ic_category_study_on</item>
<item name="career">@drawable/ic_category_career_on</item>
<item name="dailylife">@drawable/ic_category_daylife_on</item>
<item name="habit">@drawable/ic_category_habit_on</item>
<item name="etc">@drawable/ic_category_etc_on</item>
</array>
<array name="categoryNotSelectedImgArr">
<item name="business">@drawable/ic_category_business_off</item>
<item name="health">@drawable/ic_category_health_off</item>
<item name="study">@drawable/ic_category_study_off</item>
<item name="career">@drawable/ic_category_career_off</item>
<item name="dailylife">@drawable/ic_category_daylife_off</item>
<item name="habit">@drawable/ic_category_habit_off</item>
<item name="etc">@drawable/ic_category_etc_off</item>
</array>
</resources>
private lateinit var categoryDrawableOnList : TypedArray
private lateinit var categoryDrawableOffList : TypedArray
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
categoryDrawableOnList = requireContext().resources.obtainTypedArray(R.array.categorySelectedImgArr)
categoryDrawableOffList = requireContext().resources.obtainTypedArray(R.array.categoryNotSelectedImgArr)
}
private fun clickGoalItem(initialList: ArrayList<Boolean>) {
...
// 클릭 시 이미지 변경
for (i: Int in categoryImgList.indices) {
categoryImgList[i].setOnClickListener {
if (editList[i]) {
categoryImgList[i].setImageResource(categoryDrawableOffList.getResourceId(i, -1))
editList[i] = false
} else {
categoryImgList[i].setImageResource(categoryDrawableOnList.getResourceId(i, -1))
editList[i] = true
}
...
}
}
이로써 원래 더러웠던
private val categoryDrawableOnList = listOf(
R.drawable.ic_category_business_on, R.drawable.ic_category_health_on, R.drawable.ic_category_study_on,
R.drawable.ic_category_career_on, R.drawable.ic_category_daylife_on, R.drawable.ic_category_habit_on,
R.drawable.ic_category_etc_on
)
private val categoryDrawableOffList = listOf(
R.drawable.ic_category_business_off, R.drawable.ic_category_health_off, R.drawable.ic_category_study_off,
R.drawable.ic_category_career_off, R.drawable.ic_category_daylife_off, R.drawable.ic_category_habit_off,
R.drawable.ic_category_etc_off
)
같은 경우 위의
private lateinit var categoryDrawableOnList : TypedArray
private lateinit var categoryDrawableOffList : TypedArray
categoryDrawableOnList = requireContext().resources.obtainTypedArray(R.array.categorySelectedImgArr)
categoryDrawableOffList = requireContext().resources.obtainTypedArray(R.array.categoryNotSelectedImgArr)
식으로 훨씬 간단하게 바꿀 수 있었다.
여러 화면에 걸쳐 사용되는 리소스였기에 색, 이름, 이미지를 배열로 저장해 사용하자 훨씬 알아보기 쉬워졌고, 확장성 또한 높아졌다. 즐겁게 리펙토링할 수 있었던 순간이었다.