lebonprix.apk/java/io/xdrm/lebonprix/adapter/CategoryAdapter.kt

82 lines
2.5 KiB
Kotlin

package io.xdrm.todoleast.adapter
import android.graphics.PorterDuff
import android.opengl.Visibility
import android.support.v4.content.res.ResourcesCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import io.xdrm.lebonprix.R
import io.xdrm.lebonprix.model.Category
import io.xdrm.lebonprix.model.CategoryItem
import kotlinx.android.synthetic.main.category_item.view.*
import kotlin.math.round
class CategoryAdapter(private val dataSet: List<CategoryItem>) : BaseAdapter() {
override fun getView(i: Int, convertView: View?, parent: ViewGroup) : View?{
if( i >= dataSet.size )
return null
val item = dataSet[i]
// inflate
val repr: View = LayoutInflater.from(parent.context).inflate(R.layout.category_item, parent, false)
// 1. status (selected or not)
repr.selected.isChecked = item.selected
repr.setOnClickListener { view ->
item.selected = !item.selected
view.selected.setChecked(item.selected)
if( view.selected.isChecked ) setSelected(view)
else setUnselected(view)
}
// 2. label
repr.label.text = item.category.label
// 3. count
repr.count.text = round(item.count*100).toInt().toString() + " %"
// hide if "ALL"
if( item.category == Category.ALL )
repr.count.visibility = View.INVISIBLE
// 4. infer icon
val category = Category.fromLabel(item.category.label)
if( category != null )
repr.icon.setImageResource(category!!.iconId)
// 5. manage selected or not
if( item.selected ) setSelected(repr)
else setUnselected(repr)
return repr
}
override fun getCount() = dataSet.size
override fun getItem(i: Int) = dataSet[i]
override fun getItemId(i: Int) = i.toLong()
private fun setSelected(v: View){
val whiteColor = ResourcesCompat.getColor(v.resources, android.R.color.white, null)
v.setBackgroundResource(R.drawable.rounded_category_item_bg_selected)
v.label.setTextColor(whiteColor)
v.icon.setColorFilter(0xffffffff.toInt(), PorterDuff.Mode.SRC_IN)
}
private fun setUnselected(v: View){
v.setBackgroundResource(R.drawable.rounded_category_item_bg)
val blackColor = ResourcesCompat.getColor(v.resources, android.R.color.black, null)
v.label.setTextColor(blackColor)
v.icon.clearColorFilter()
}
}