-
Notifications
You must be signed in to change notification settings - Fork 2
/
FeatureChips.kt
43 lines (36 loc) Β· 1.23 KB
/
FeatureChips.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package place.pic.ui.upload
import android.view.LayoutInflater
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import place.pic.data.entity.UsefulTag
import place.pic.ui.tag.ChipFactory
/**
* Created By Malibin
* on 7μ 13, 2020
*/
class FeatureChips(private val chipGroup: ChipGroup) {
private lateinit var features: List<UsefulTag>
private val selectedFeatures = mutableListOf<UsefulTag>()
fun submitFeatures(features: List<UsefulTag>) {
this.features = features
selectedFeatures.clear()
chipGroup.removeAllViews()
this.features
.map { convertToChip(it) }
.forEach { chipGroup.addView(it) }
}
private fun convertToChip(feature: UsefulTag): Chip {
val layoutInflater = LayoutInflater.from(chipGroup.context)
val chip = ChipFactory.createSmallChip(layoutInflater)
chip.text = feature.tagName
chip.setOnClickListener { onFeatureClick(feature) }
return chip
}
private fun onFeatureClick(feature: UsefulTag) {
if (selectedFeatures.contains(feature)) {
selectedFeatures.remove(feature)
return
}
selectedFeatures.add(feature)
}
}