Skip to content

Commit ec21787

Browse files
committed
Add OmegaBindView
Minor fixes Add sample
1 parent 1c55869 commit ec21787

File tree

15 files changed

+112
-35
lines changed

15 files changed

+112
-35
lines changed

app/src/main/java/com/omega_r/bind/app/MainActivity.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,41 @@ package com.omega_r.bind.app
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.util.SparseArray
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
9+
import com.omega_r.bind.OmegaAutoAdapter
10+
import com.omega_r.bind.OmegaBindView
511
import com.omega_r.bind.model.BindModel
6-
import com.omega_r.bind.model.binders.bindString
12+
import com.omega_r.bind.model.binders.*
713

814
class MainActivity : AppCompatActivity() {
915

1016
private val bindModel = BindModel.create<String> {
1117
bindString(R.id.NO_DEBUG)
18+
bindVisible(R.id.NO_DEBUG) {
19+
true
20+
}
21+
bindMultiCustom(R.id.NO_DEBUG, R.id.NO_DEBUG) { sparseArray: SparseArray<View>, s: String ->
22+
sparseArray[R.id.NO_DEBUG]
23+
}
1224
}
1325

26+
private val adapter = OmegaAutoAdapter.create(R.layout.activity_main, bindModel)
27+
1428
override fun onCreate(savedInstanceState: Bundle?) {
1529
super.onCreate(savedInstanceState)
1630
setContentView(R.layout.activity_main)
31+
32+
val bindView = OmegaBindView.create<String>(this, R.layout.activity_main) {
33+
bindString(R.id.textview_hello)
34+
}
35+
36+
37+
bindView.bind("Bind: Hello World!")
38+
39+
addContentView(bindView, ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT))
1740
}
41+
1842
}

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
tools:context=".MainActivity">
88

99
<TextView
10+
android:id="@+id/textview_hello"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13+
tools:text="Hello World!"
1314
app:layout_constraintBottom_toBottomOf="parent"
1415
app:layout_constraintLeft_toLeftOf="parent"
1516
app:layout_constraintRight_toRightOf="parent"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.omega_r.bind
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import android.widget.FrameLayout
6+
import androidx.annotation.LayoutRes
7+
import com.omega_r.bind.model.BindModel
8+
9+
class OmegaBindView<M> @JvmOverloads constructor(
10+
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
11+
) : FrameLayout(context, attrs, defStyleAttr) {
12+
13+
companion object {
14+
15+
fun <M> create(context: Context, @LayoutRes layoutRes: Int, bindModel: BindModel<M>): OmegaBindView<M> {
16+
return OmegaBindView<M>(context).apply {
17+
setBindModel(layoutRes, bindModel)
18+
}
19+
}
20+
21+
fun <M> create(
22+
context: Context,
23+
@LayoutRes layoutRes: Int,
24+
parentBindModel: BindModel<M>? = null,
25+
builder: BindModel.Builder<M>.() -> Unit,
26+
): OmegaBindView<M> {
27+
val bindModel = BindModel.create(parentBindModel, builder)
28+
return create(context, layoutRes, bindModel)
29+
}
30+
31+
}
32+
33+
private var bindModel: BindModel<M>? = null
34+
35+
fun setBindModel(@LayoutRes layoutRes: Int, bindModel: BindModel<M>) {
36+
removeAllViews()
37+
inflate(context, layoutRes, this)
38+
bindModel.onViewCreated(this)
39+
this.bindModel = bindModel
40+
}
41+
42+
fun bind(item: M) {
43+
bindModel?.bind(this, item)
44+
}
45+
46+
}

library/src/main/java/com/omega_r/bind/model/BindModel.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.omega_r.bind.model
22

3+
import android.app.Activity
34
import android.util.SparseArray
45
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.FrameLayout
8+
import androidx.annotation.LayoutRes
59
import com.omega_r.bind.R
610
import com.omega_r.bind.model.binders.*
711

@@ -35,6 +39,9 @@ class BindModel<M>(private val list: List<Binder<*, M>>) {
3539

3640
constructor(vararg binder: Binder<*, M>) : this(binder.toList())
3741

42+
fun onViewCreated(activity: Activity) {
43+
onViewCreated(activity.window.decorView)
44+
}
3845

3946
fun onViewCreated(view: View) {
4047
val viewCache = SparseArray<View>()
@@ -57,12 +64,16 @@ class BindModel<M>(private val list: List<Binder<*, M>>) {
5764
}
5865
} else {
5966
viewCache.put(id, childView)
60-
array[id].forEach { it.dispatchOnCreateView(childView, viewCache) }
67+
array[id].forEach { it.dispatchOnViewCreated(childView, viewCache) }
6168
}
6269
}
6370

6471
}
6572

73+
fun bind(activity: Activity, item: M) {
74+
bind(activity.window.decorView, item)
75+
}
76+
6677
fun bind(view: View, item: M) {
6778
@Suppress("UNCHECKED_CAST")
6879
val viewCache = view.getTag(R.id.omega_autobind) as SparseArray<View>

library/src/main/java/com/omega_r/bind/model/binders/Binder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ abstract class Binder<V : View, M> {
1010

1111
var viewOptionally: Boolean = false
1212

13-
open fun dispatchOnCreateView(view: View, viewCache: SparseArray<View>) {
13+
internal open fun dispatchOnViewCreated(view: View, viewCache: SparseArray<View>) {
1414
@Suppress("UNCHECKED_CAST")
15-
onCreateView(view as V)
15+
onViewCreated(view as V)
1616
}
1717

18-
protected open fun onCreateView(itemView: V) {
18+
protected open fun onViewCreated(itemView: V) {
1919
// nothing
2020
}
2121

library/src/main/java/com/omega_r/bind/model/binders/BinderTextWatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class BinderTextWatcher<E> : TextWatcher {
4545
// nothing
4646
}
4747

48-
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
48+
override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) {
4949
if (enabled) {
5050
item?.let { item ->
51-
callbacks.forEach { it(item, s.toString()) }
51+
callbacks.forEach { it(item, text.toString()) }
5252
}
5353
}
5454
}

library/src/main/java/com/omega_r/bind/model/binders/ClickBinder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open class ClickBinder<M>(
1111
private val block: (M) -> Unit
1212
) : Binder<View, M>() {
1313

14-
override fun onCreateView(itemView: View) {
14+
override fun onViewCreated(itemView: View) {
1515
val tag = itemView.getTag(R.id.omega_click_bind) as? ClickManager
1616
if (tag == null) {
1717
itemView.setTag(R.id.omega_click_bind, ClickManager())

library/src/main/java/com/omega_r/bind/model/binders/MultiBinder.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import android.view.View
55

66
abstract class MultiBinder<V : View, M>(final override val id: Int, vararg ids: Int) : Binder<V, M>() {
77

8-
val ids = listOf(id, *ids.toTypedArray())
9-
8+
private val ids = listOf(id, *ids.toTypedArray())
109

1110
@Suppress("UNCHECKED_CAST")
1211
override fun dispatchBind(viewCache: SparseArray<View>, item: M) {

library/src/main/java/com/omega_r/bind/model/binders/RecyclerViewListBinder.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ open class RecyclerViewListBinder<M, SM>(
1616
private val block: BindModel.Builder<SM>.() -> Unit
1717
) : Binder<RecyclerView, M>() {
1818

19-
override fun onCreateView(itemView: RecyclerView) {
20-
itemView.adapter = OmegaAutoAdapter.create(layoutRes, callback?.let { Callback(callback) }, parentModel, block)
19+
override fun onViewCreated(itemView: RecyclerView) {
20+
itemView.adapter = OmegaAutoAdapter.create(
21+
layoutRes = layoutRes,
22+
callback = callback?.let { Callback(callback) },
23+
parentModel = parentModel,
24+
block = block
25+
)
2126
}
2227

2328
@Suppress("UNCHECKED_CAST")

library/src/main/java/com/omega_r/bind/model/binders/SpinnerListBinder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ open class SpinnerListBinder<M, SM>(
1818
private val converter: (Context, SM, isDropDown: Boolean) -> CharSequence
1919
) : Binder<Spinner, M>() {
2020

21-
override fun onCreateView(itemView: Spinner) {
21+
override fun onViewCreated(itemView: Spinner) {
2222
itemView.adapter = OmegaSpinnerAdapter.CustomAdapter(itemView.context, layoutRes, converter).also {
2323
it.nonSelectedItem = nonSelectedItem
2424
}

0 commit comments

Comments
 (0)