Skip to content

Commit cca41aa

Browse files
committed
Clean up the API a bit
1 parent 0b25786 commit cca41aa

3 files changed

Lines changed: 58 additions & 40 deletions

File tree

togglebuttonlayout/src/main/java/com/savvyapps/togglebuttonlayout/Toggle.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@ class Toggle(
2727
*/
2828
var isSelected: Boolean = false
2929

30-
internal var parentRef: ToggleButtonLayout? = null
3130

32-
fun getToggleButtonLayout(): ToggleButtonLayout? {
33-
return parentRef
34-
}
31+
/**
32+
* Get the parent view of the toggle. Null if not yet inflated
33+
*/
34+
var toggleButtonLayout: ToggleButtonLayout? = null
35+
internal set
36+
37+
/**
38+
* The current toggle view. Null if not yet inflated
39+
*/
40+
var toggleView: ToggleView? = null
41+
internal set
3542

3643
init {
3744
if (id == 0) {

togglebuttonlayout/src/main/java/com/savvyapps/togglebuttonlayout/ToggleButtonLayout.kt

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import android.widget.ImageView
1919
import android.widget.LinearLayout
2020
import android.widget.TextView
2121

22-
@Suppress("MemberVisibilityCanBePrivate", "unused")
2322
/**
2423
* ToggleButtonLayout is a layout used to group related options. Layout and spacing is arranged to
2524
* convey that certain toggle buttons are part of a group.
2625
*/
26+
@Suppress("MemberVisibilityCanBePrivate", "unused")
2727
class ToggleButtonLayout : CardView {
2828

2929
companion object {
@@ -116,7 +116,7 @@ class ToggleButtonLayout : CardView {
116116
/**
117117
* Listen for when toggles get selected and deselected
118118
*/
119-
var onToggledListener: ((v: View?, toggle: Toggle, selected: Boolean) -> Unit)? = null
119+
var onToggledListener: ((toggleButtonLayout: ToggleButtonLayout?, toggle: Toggle, selected: Boolean) -> Unit)? = null
120120

121121
constructor(context: Context) : super(context) {
122122
init(context, null)
@@ -173,9 +173,7 @@ class ToggleButtonLayout : CardView {
173173
MenuInflater(context).inflate(menuId, menu)
174174
for (i in 0 until menu.size()) {
175175
val item = menu.getItem(i)
176-
val toggle = Toggle(item.itemId, item.icon, item.title).apply {
177-
this.parentRef = this@ToggleButtonLayout
178-
}
176+
val toggle = Toggle(item.itemId, item.icon, item.title)
179177
addToggle(toggle)
180178
}
181179
}
@@ -212,6 +210,8 @@ class ToggleButtonLayout : CardView {
212210
toggleView.layoutParams = params
213211
}
214212
linearLayout.addView(toggleView)
213+
toggle.toggleView = toggleView
214+
toggle.toggleButtonLayout = this
215215
}
216216

217217
/**
@@ -278,35 +278,4 @@ class ToggleButtonLayout : CardView {
278278
view.background = null
279279
}
280280
}
281-
282-
/**
283-
* Default view for Toggle
284-
*/
285-
@SuppressLint("ViewConstructor")
286-
internal class ToggleView(context: Context, toggle: Toggle, @LayoutRes layoutRes: Int?) : FrameLayout(context) {
287-
private val textView: TextView?
288-
private val imageView: ImageView?
289-
290-
init {
291-
id = toggle.id
292-
if (layoutRes != null) {
293-
View.inflate(context, layoutRes, this)
294-
textView = findViewById(android.R.id.text1)
295-
imageView = findViewById(android.R.id.icon)
296-
} else {
297-
textView = TextView(context)
298-
imageView = ImageView(context)
299-
addView(imageView)
300-
addView(textView)
301-
val eightDp = Utils.dpToPx(getContext(), 8)
302-
setPadding(eightDp, eightDp, eightDp, eightDp)
303-
}
304-
setTag(R.id.tb_toggle_id, toggle)
305-
textView?.text = toggle.title
306-
if (toggle.icon != null) {
307-
imageView?.setImageDrawable(toggle.icon)
308-
}
309-
foreground = Utils.getThemeAttrDrawable(getContext(), R.attr.selectableItemBackground)
310-
}
311-
}
312281
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.savvyapps.togglebuttonlayout
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.support.annotation.LayoutRes
6+
import android.view.View
7+
import android.widget.FrameLayout
8+
import android.widget.ImageView
9+
import android.widget.TextView
10+
11+
/**
12+
* Default view for Toggle. Be sure to use references [android.R.id.text1] and
13+
* [android.R.id.icon] if using your own layout
14+
*/
15+
@Suppress("MemberVisibilityCanBePrivate")
16+
@SuppressLint("ViewConstructor")
17+
class ToggleView(context: Context, toggle: Toggle, @LayoutRes layoutRes: Int?) : FrameLayout(context) {
18+
val textView: TextView?
19+
val imageView: ImageView?
20+
21+
init {
22+
id = toggle.id
23+
if (layoutRes != null) {
24+
View.inflate(context, layoutRes, this)
25+
textView = findViewById(android.R.id.text1)
26+
imageView = findViewById(android.R.id.icon)
27+
} else {
28+
textView = TextView(context)
29+
imageView = ImageView(context)
30+
addView(imageView)
31+
addView(textView)
32+
val eightDp = Utils.dpToPx(getContext(), 8)
33+
setPadding(eightDp, eightDp, eightDp, eightDp)
34+
}
35+
setTag(R.id.tb_toggle_id, toggle)
36+
textView?.text = toggle.title
37+
if (toggle.icon != null) {
38+
imageView?.setImageDrawable(toggle.icon)
39+
}
40+
foreground = Utils.getThemeAttrDrawable(getContext(), R.attr.selectableItemBackground)
41+
}
42+
}

0 commit comments

Comments
 (0)