Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit ef50f48

Browse files
authored
Merge pull request #33 from igalata/feature/adapter
adapter implemented
2 parents 985a171 + 15e3363 commit ef50f48

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

app/src/main/java/com/igalata/bubblepickerdemo/DemoActivity.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import android.support.v4.content.ContextCompat
77
import android.support.v7.app.AppCompatActivity
88
import android.widget.Toast
99
import com.igalata.bubblepicker.BubblePickerListener
10+
import com.igalata.bubblepicker.adapter.BubblePickerAdapter
1011
import com.igalata.bubblepicker.model.BubbleGradient
1112
import com.igalata.bubblepicker.model.PickerItem
1213
import kotlinx.android.synthetic.main.activity_demo.*
13-
import java.util.*
1414

1515
/**
1616
* Created by irinagalata on 1/19/17.
@@ -43,15 +43,20 @@ class DemoActivity : AppCompatActivity() {
4343
val colors = resources.obtainTypedArray(R.array.colors)
4444
val images = resources.obtainTypedArray(R.array.images)
4545

46-
picker.items = ArrayList()
46+
picker.adapter = object : BubblePickerAdapter {
4747

48-
titles.forEachIndexed { i, country ->
49-
picker.items?.add(PickerItem(country,
50-
gradient = BubbleGradient(colors.getColor((i * 2) % 8, 0), colors.getColor((i * 2) % 8 + 1, 0),
51-
BubbleGradient.VERTICAL),
52-
typeface = mediumTypeface,
53-
textColor = ContextCompat.getColor(this, android.R.color.white),
54-
backgroundImage = ContextCompat.getDrawable(this, images.getResourceId(i, 0))))
48+
override val totalCount = titles.size
49+
50+
override fun getItem(position: Int): PickerItem {
51+
return PickerItem().apply {
52+
title = titles[position]
53+
gradient = BubbleGradient(colors.getColor((position * 2) % 8, 0),
54+
colors.getColor((position * 2) % 8 + 1, 0), BubbleGradient.VERTICAL)
55+
typeface = mediumTypeface
56+
textColor = ContextCompat.getColor(this@DemoActivity, android.R.color.white)
57+
backgroundImage = ContextCompat.getDrawable(this@DemoActivity, images.getResourceId(position, 0))
58+
}
59+
}
5560
}
5661

5762
colors.recycle()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.igalata.bubblepicker.adapter
2+
3+
import com.igalata.bubblepicker.model.PickerItem
4+
5+
/**
6+
* Created by irinagalata on 5/22/17.
7+
*/
8+
interface BubblePickerAdapter {
9+
10+
val totalCount: Int
11+
12+
fun getItem(position: Int): PickerItem
13+
14+
}

bubblepicker/src/main/java/com/igalata/bubblepicker/model/PickerItem.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import android.support.annotation.ColorInt
77
/**
88
* Created by irinagalata on 1/19/17.
99
*/
10-
data class PickerItem @JvmOverloads constructor(val title: String? = null,
11-
val icon: Drawable? = null,
12-
val iconOnTop: Boolean = true,
13-
@ColorInt val color: Int? = null,
14-
val gradient: BubbleGradient? = null,
15-
val overlayAlpha: Float = 0.5f,
16-
val typeface: Typeface = Typeface.DEFAULT,
17-
@ColorInt val textColor: Int? = null,
18-
val textSize: Float = 40f,
19-
val backgroundImage: Drawable? = null,
20-
val isSelected: Boolean = false)
10+
data class PickerItem @JvmOverloads constructor(var title: String? = null,
11+
var icon: Drawable? = null,
12+
var iconOnTop: Boolean = true,
13+
@ColorInt var color: Int? = null,
14+
var gradient: BubbleGradient? = null,
15+
var overlayAlpha: Float = 0.5f,
16+
var typeface: Typeface = Typeface.DEFAULT,
17+
@ColorInt var textColor: Int? = null,
18+
var textSize: Float = 40f,
19+
var backgroundImage: Drawable? = null,
20+
var isSelected: Boolean = false)

bubblepicker/src/main/java/com/igalata/bubblepicker/rendering/BubblePicker.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import android.util.AttributeSet
88
import android.view.MotionEvent
99
import com.igalata.bubblepicker.BubblePickerListener
1010
import com.igalata.bubblepicker.R
11-
import com.igalata.bubblepicker.exception.EmptyPickerException
11+
import com.igalata.bubblepicker.adapter.BubblePickerAdapter
1212
import com.igalata.bubblepicker.model.Color
1313
import com.igalata.bubblepicker.model.PickerItem
14-
import java.util.*
1514

1615
/**
1716
* Created by irinagalata on 1/19/17.
@@ -23,11 +22,21 @@ class BubblePicker : GLSurfaceView {
2322
field = value
2423
renderer.backgroundColor = Color(value)
2524
}
25+
@Deprecated(level = DeprecationLevel.WARNING,
26+
message = "Use BubblePickerAdapter for the view setup instead")
2627
var items: ArrayList<PickerItem>? = null
2728
set(value) {
2829
field = value
2930
renderer.items = value ?: ArrayList()
3031
}
32+
var adapter: BubblePickerAdapter? = null
33+
set(value) {
34+
field = value
35+
if (value != null) {
36+
renderer.items = ArrayList((0..value.totalCount - 1)
37+
.map { value.getItem(it) }.toList())
38+
}
39+
}
3140
var maxSelectedCount: Int? = null
3241
set(value) {
3342
renderer.maxSelectedCount = value
@@ -68,11 +77,6 @@ class BubblePicker : GLSurfaceView {
6877
attrs?.let { retrieveAttrubutes(attrs) }
6978
}
7079

71-
override fun onResume() {
72-
super.onResume()
73-
if (items?.isEmpty() ?: false) throw EmptyPickerException()
74-
}
75-
7680
override fun onTouchEvent(event: MotionEvent): Boolean {
7781
when (event.action) {
7882
MotionEvent.ACTION_DOWN -> {

bubblepicker/src/main/java/com/igalata/bubblepicker/rendering/Item.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ data class Item(val pickerItem: PickerItem, val circleBody: CircleBody) {
8585
private fun drawBackground(canvas: Canvas, withImage: Boolean) {
8686
val bgPaint = Paint()
8787
bgPaint.style = Paint.Style.FILL
88-
pickerItem.color?.let { bgPaint.color = pickerItem.color }
88+
pickerItem.color?.let { bgPaint.color = pickerItem.color!! }
8989
pickerItem.gradient?.let { bgPaint.shader = gradient }
9090
if (withImage) bgPaint.alpha = (pickerItem.overlayAlpha * 255).toInt()
9191
canvas.drawRect(0f, 0f, bitmapSize, bitmapSize, bgPaint)
@@ -95,7 +95,7 @@ data class Item(val pickerItem: PickerItem, val circleBody: CircleBody) {
9595
if (pickerItem.title == null || pickerItem.textColor == null) return
9696

9797
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
98-
color = pickerItem.textColor
98+
color = pickerItem.textColor!!
9999
textSize = pickerItem.textSize
100100
typeface = pickerItem.typeface
101101
}

0 commit comments

Comments
 (0)