1+ package com.omega_r.bind.delegates
2+
3+ import android.animation.AnimatorInflater
4+ import android.content.res.Resources
5+ import android.view.View
6+ import android.view.animation.AnimationUtils
7+ import androidx.annotation.*
8+ import androidx.core.content.ContextCompat
9+ import androidx.recyclerview.widget.RecyclerView
10+ import com.omega_r.bind.delegates.managers.BindersManager
11+ import com.omega_r.bind.delegates.managers.BindersManager.BindType.RESETTABLE
12+ import com.omega_r.bind.delegates.managers.BindersManager.BindType.RESETTABLE_WITH_AUTO_INIT
13+ import com.omega_r.bind.adapters.OmegaAutoAdapter
14+ import com.omega_r.bind.model.BindModel
15+ import com.omega_r.click.OmegaContextable
16+ import com.omega_r.click.OmegaViewFindable
17+
18+ /* *
19+ * Created by Anton Knyazev on 04.04.2019.
20+ */
21+ @Suppress(" unused" )
22+ interface OmegaBindable : OmegaContextable , OmegaViewFindable {
23+
24+
25+ private val resources: Resources
26+ get() = getContext()?.resources ? : error(" Context is null" )
27+
28+ val bindersManager: BindersManager
29+
30+ private fun <T : View > findView (id : Int ): T {
31+ return findViewById(id)
32+ ? : error(" Bind is not found R.id.${this .getContext()!! .resources.getResourceEntryName(id)} (${this ::class .java.name} )" )
33+ }
34+
35+ private fun <T : View > findViewOrNull (id : Int ): T ? {
36+ return findViewById(id)
37+ }
38+
39+ private fun <T : View > findViews (vararg ids : Int ): List <T > {
40+ return ids.map { findView(it) }
41+ }
42+
43+ private fun <T : View > findViewsToMap (vararg ids : Int ): Map <Int , T > {
44+ return ids.associate { it to findView(it) }
45+ }
46+
47+ private fun <T : View , IH : IdHolder > findViewsToIdHolderMap (ids : Array <out IH >): Map <IH , T > {
48+ return ids.associate { it to findView(it.id) }
49+ }
50+
51+ private fun <T : View , E > findViewsToIdPairMap (ids : Array <out Pair <E , Int >>): Map <E , T > {
52+ return ids.associate { it.first to findView(it.second) }
53+ }
54+
55+ fun <T : RecyclerView > bind (@IdRes res : Int , adapter : RecyclerView .Adapter <* >) =
56+ bindersManager.bind<T >(RESETTABLE_WITH_AUTO_INIT , { findView(res) }) {
57+ this .adapter = adapter
58+ }
59+
60+ fun <T : RecyclerView , M > bind (
61+ @IdRes res : Int ,
62+ @LayoutRes layoutRes : Int ,
63+ parentModel : BindModel <M >? = null,
64+ callback : ((M ) -> Unit )? = null,
65+ builder : BindModel .Builder <M >.() -> Unit
66+ ) =
67+ bindersManager.bind<T >(RESETTABLE_WITH_AUTO_INIT , { findView(res) }) {
68+ adapter =
69+ OmegaAutoAdapter .create(layoutRes, callback, parentModel = parentModel, block = builder)
70+ }
71+
72+ fun <T : RecyclerView > bind (@IdRes res : Int , adapter : RecyclerView .Adapter <* >, initBlock : T .() -> Unit ) =
73+ bindersManager.bind<T >(RESETTABLE_WITH_AUTO_INIT , { findView(res) }) {
74+ this .adapter = adapter
75+ initBlock()
76+ }
77+
78+ fun <T : View > bind (@IdRes res : Int , initBlock : T .() -> Unit ) =
79+ bindersManager.bind(RESETTABLE_WITH_AUTO_INIT , { findView(res) }, initBlock)
80+
81+ fun <T : View > bind (@IdRes res : Int ) = bindersManager.bind<T >(RESETTABLE_WITH_AUTO_INIT , { findView(res) })
82+
83+ fun <T > bind (init : () -> T ) = bindersManager.bind(RESETTABLE , init )
84+
85+ fun <T : View > bind (@IdRes vararg ids : Int ): Lazy <List <T >> {
86+ return bindersManager.bind(RESETTABLE , { findViews(* ids) })
87+ }
88+
89+ fun <T : View > bind (@IdRes vararg ids : Int , initBlock : T .() -> Unit ): Lazy <List <T >> =
90+ bindersManager.bind(RESETTABLE_WITH_AUTO_INIT , { findViews(* ids) }) {
91+ forEach(initBlock)
92+ }
93+
94+ fun <T : View , IH : IdHolder > bind (ids : Array <out IH >): Lazy <Map <IH , T >> =
95+ bindersManager.bind(RESETTABLE , { findViewsToIdHolderMap(ids) })
96+
97+ fun <T : View , IH : IdHolder > bind (ids : Array <out IH >, initBlock : T .(IdHolder ) -> Unit ): Lazy <Map <IH , T >> =
98+ bindersManager.bind(RESETTABLE_WITH_AUTO_INIT , { findViewsToIdHolderMap(ids) }) {
99+ forEach {
100+ initBlock(it.value, it.key)
101+ }
102+ }
103+
104+ fun <T : View , E > bind (vararg idsPair : Pair <E , Int >) =
105+ bindersManager.bind(RESETTABLE , { findViewsToIdPairMap<T , E >(idsPair) })
106+
107+ fun <T : View , E > bind (vararg idsPair : Pair <E , Int >, initBlock : T .(E ) -> Unit ) =
108+ bindersManager.bind(RESETTABLE_WITH_AUTO_INIT , { findViewsToIdPairMap<T , E >(idsPair) }) {
109+ forEach { initBlock(it.value, it.key) }
110+ }
111+
112+ fun <T : View > bindOrNull (@IdRes res : Int ) = bindersManager.bind(RESETTABLE , { findViewOrNull<T >(res) })
113+
114+ fun <T : View > bindOrNull (@IdRes res : Int , initBlock : T .() -> Unit ) =
115+ bindersManager.bind(RESETTABLE_WITH_AUTO_INIT , { findViewOrNull<T >(res) }) {
116+ this ?.let (initBlock)
117+ }
118+
119+ fun bindColor (@ColorRes res : Int ) = bindersManager.bind(findInit = {
120+ ContextCompat .getColor(getContext()!! , res)
121+ })
122+
123+ fun bindInt (@IntegerRes res : Int ) = bindersManager.bind(findInit = {
124+ resources.getInteger(res)
125+ })
126+
127+ fun bindDrawable (@DrawableRes res : Int ) = bindersManager.bind(findInit = {
128+ ContextCompat .getDrawable(getContext()!! , res)
129+ ? : error(" BindDrawable is not found R.drawable.${resources.getResourceEntryName(res)} (${this ::class .java.name} )" )
130+ })
131+
132+ fun bindDimen (@DimenRes res : Int ) = bindersManager.bind(findInit = {
133+ resources.getDimension(res)
134+ })
135+
136+ fun bindDimenPixel (@DimenRes res : Int ) = bindersManager.bind(findInit = {
137+ resources.getDimensionPixelSize(res)
138+ })
139+
140+ fun bindAnimation (@AnimRes res : Int ) = bindersManager.bind(findInit = {
141+ AnimationUtils .loadAnimation(getContext(), res)
142+ })
143+
144+ fun bindAnimator (@AnimatorRes res : Int ) = bindersManager.bind(findInit = {
145+ AnimatorInflater .loadAnimator(getContext(), res)
146+ })
147+
148+ }
0 commit comments