Skip to content

Commit 9c4e1e7

Browse files
committed
Added getString(), getDrawable(), getColor() and getColorStateList() to DSL's ViewHolder
1 parent 709601d commit 9c4e1e7

File tree

2 files changed

+218
-3
lines changed

2 files changed

+218
-3
lines changed

kotlin-dsl-layoutcontainer/src/main/java/com/hannesdorfmann/adapterdelegates4/dsl/LayoutContainerListAdapterDelegateDsl.kt

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.hannesdorfmann.adapterdelegates4.dsl
22

3+
import android.content.Context
4+
import android.content.res.ColorStateList
5+
import android.graphics.drawable.Drawable
6+
import android.os.Build
37
import android.view.LayoutInflater
48
import android.view.View
59
import android.view.ViewGroup
10+
import androidx.annotation.ColorInt
11+
import androidx.annotation.ColorRes
12+
import androidx.annotation.DrawableRes
613
import androidx.annotation.IdRes
714
import androidx.annotation.LayoutRes
15+
import androidx.annotation.NonNull
16+
import androidx.annotation.StringRes
817
import androidx.recyclerview.widget.RecyclerView
918
import com.hannesdorfmann.adapterdelegates4.AbsListItemAdapterDelegate
1019
import com.hannesdorfmann.adapterdelegates4.AdapterDelegate
@@ -104,7 +113,9 @@ class DslLayoutContainerListAdapterDelegate<I : T, T>(
104113
}
105114

106115
/**
107-
* ViewHolder that is used internally if you use [adapterDelegate] DSL to create your Adapter
116+
* ViewHolder that is used internally if you use [adapterDelegateLayoutContainer] DSL to create your AdapterDelegate
117+
*
118+
* @since 4.1.0
108119
*/
109120
class AdapterDelegateLayoutContainerViewHolder<T>(
110121
override val containerView: View
@@ -137,8 +148,103 @@ class AdapterDelegateLayoutContainerViewHolder<T>(
137148

138149
/**
139150
* Get the context.
151+
*
152+
* @since 4.1.1
153+
*/
154+
val context: Context = containerView.context
155+
156+
/**
157+
* Returns a localized string from the application's package's
158+
* default string table.
159+
*
160+
* @param resId Resource id for the string
161+
* @return The string data associated with the resource, stripped of styled
162+
* text information.
163+
*
164+
* @since 4.1.1
165+
*/
166+
fun getString(@StringRes resId: Int): String {
167+
return context.getString(resId)
168+
}
169+
170+
/**
171+
* Returns a localized formatted string from the application's package's
172+
* default string table, substituting the format arguments as defined in
173+
* [java.util.Formatter] and [java.lang.String.format].
174+
*
175+
* @param resId Resource id for the format string
176+
* @param formatArgs The format arguments that will be used for
177+
* substitution.
178+
* @return The string data associated with the resource, formatted and
179+
* stripped of styled text information.
180+
*
181+
* @since 4.1.1
182+
*/
183+
fun getString(@StringRes resId: Int, vararg formatArgs: Any): String {
184+
return context.getString(resId, *formatArgs)
185+
}
186+
187+
/**
188+
* Returns a color associated with a particular resource ID and styled for
189+
* the current theme.
190+
*
191+
* @param id The desired resource identifier, as generated by the aapt
192+
* tool. This integer encodes the package, type, and resource
193+
* entry. The value 0 is an invalid identifier.
194+
* @return A single color value in the form 0xAARRGGBB.
195+
* @throws android.content.res.Resources.NotFoundException if the given ID
196+
* does not exist.
197+
*
198+
* @since 4.1.1
199+
*/
200+
@ColorInt
201+
fun getColor(@ColorRes id: Int): Int {
202+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
203+
context.getColor(id)
204+
} else {
205+
context.resources.getColor(id)
206+
}
207+
}
208+
209+
/**
210+
* Returns a drawable object associated with a particular resource ID and
211+
* styled for the current theme.
212+
*
213+
* @param id The desired resource identifier, as generated by the aapt
214+
* tool. This integer encodes the package, type, and resource
215+
* entry. The value 0 is an invalid identifier.
216+
* @return An object that can be used to draw this resource.
217+
* @throws android.content.res.Resources.NotFoundException if the given ID
218+
* does not exist.
219+
*
220+
* @since 4.1.1
221+
*/
222+
fun getDrawable(@DrawableRes id: Int): Drawable {
223+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
224+
context.getDrawable(id)
225+
} else {
226+
context.resources.getDrawable(id)
227+
}
228+
}
229+
230+
/**
231+
* Returns a color state list associated with a particular resource ID and
232+
* styled for the current theme.
233+
*
234+
* @param id The desired resource identifier, as generated by the aapt
235+
* tool. This integer encodes the package, type, and resource
236+
* entry. The value 0 is an invalid identifier.
237+
* @return A color state list.
238+
* @throws android.content.res.Resources.NotFoundException if the given ID
239+
* does not exist.
140240
*/
141-
val context = containerView.context
241+
fun getColorStateList(@ColorRes id: Int): ColorStateList {
242+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
243+
context.getColorStateList(id)
244+
} else {
245+
context.resources.getColorStateList(id)
246+
}
247+
}
142248

143249
/**
144250
* This should never be called directly.

kotlin-dsl/src/main/java/com/hannesdorfmann/adapterdelegates4/dsl/ListAdapterDelegateDsl.kt

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.hannesdorfmann.adapterdelegates4.dsl
22

3+
import android.content.Context
4+
import android.content.res.ColorStateList
5+
import android.graphics.drawable.Drawable
6+
import android.os.Build
37
import android.view.LayoutInflater
48
import android.view.View
59
import android.view.ViewGroup
10+
import androidx.annotation.ColorInt
11+
import androidx.annotation.ColorRes
12+
import androidx.annotation.DrawableRes
613
import androidx.annotation.IdRes
714
import androidx.annotation.LayoutRes
15+
import androidx.annotation.StringRes
816
import androidx.recyclerview.widget.RecyclerView
917
import com.hannesdorfmann.adapterdelegates4.AbsListItemAdapterDelegate
1018
import com.hannesdorfmann.adapterdelegates4.AdapterDelegate
@@ -43,6 +51,10 @@ inline fun <reified I : T, T> adapterDelegate(
4351
)
4452
}
4553

54+
/**
55+
* Delegate used internally in combination with [adapterDelegate]
56+
* @since 4.1.0
57+
*/
4658
class DslListAdapterDelegate<I : T, T>(
4759
@LayoutRes private val layout: Int,
4860
private val on: (item: T, items: List<T>, position: Int) -> Boolean,
@@ -103,6 +115,8 @@ class DslListAdapterDelegate<I : T, T>(
103115

104116
/**
105117
* ViewHolder that is used internally if you use [adapterDelegate] DSL to create your Adapter
118+
*
119+
* @since 4.1.0
106120
*/
107121
class AdapterDelegateViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) {
108122

@@ -133,8 +147,103 @@ class AdapterDelegateViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) {
133147

134148
/**
135149
* Get the context.
150+
*
151+
* @since 4.1.1
152+
*/
153+
val context: Context = view.context
154+
155+
/**
156+
* Returns a localized string from the application's package's
157+
* default string table.
158+
*
159+
* @param resId Resource id for the string
160+
* @return The string data associated with the resource, stripped of styled
161+
* text information.
162+
*
163+
* @since 4.1.1
164+
*/
165+
fun getString(@StringRes resId: Int): String {
166+
return context.getString(resId)
167+
}
168+
169+
/**
170+
* Returns a localized formatted string from the application's package's
171+
* default string table, substituting the format arguments as defined in
172+
* [java.util.Formatter] and [java.lang.String.format].
173+
*
174+
* @param resId Resource id for the format string
175+
* @param formatArgs The format arguments that will be used for
176+
* substitution.
177+
* @return The string data associated with the resource, formatted and
178+
* stripped of styled text information.
179+
*
180+
* @since 4.1.1
181+
*/
182+
fun getString(@StringRes resId: Int, vararg formatArgs: Any): String {
183+
return context.getString(resId, *formatArgs)
184+
}
185+
186+
/**
187+
* Returns a color associated with a particular resource ID and styled for
188+
* the current theme.
189+
*
190+
* @param id The desired resource identifier, as generated by the aapt
191+
* tool. This integer encodes the package, type, and resource
192+
* entry. The value 0 is an invalid identifier.
193+
* @return A single color value in the form 0xAARRGGBB.
194+
* @throws android.content.res.Resources.NotFoundException if the given ID
195+
* does not exist.
196+
*
197+
* @since 4.1.1
136198
*/
137-
val context = view.context
199+
@ColorInt
200+
fun getColor(@ColorRes id: Int): Int {
201+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
202+
context.getColor(id)
203+
} else {
204+
context.resources.getColor(id)
205+
}
206+
}
207+
208+
/**
209+
* Returns a drawable object associated with a particular resource ID and
210+
* styled for the current theme.
211+
*
212+
* @param id The desired resource identifier, as generated by the aapt
213+
* tool. This integer encodes the package, type, and resource
214+
* entry. The value 0 is an invalid identifier.
215+
* @return An object that can be used to draw this resource.
216+
* @throws android.content.res.Resources.NotFoundException if the given ID
217+
* does not exist.
218+
*
219+
* @since 4.1.1
220+
*/
221+
fun getDrawable(@DrawableRes id: Int): Drawable {
222+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
223+
context.getDrawable(id)
224+
} else {
225+
context.resources.getDrawable(id)
226+
}
227+
}
228+
229+
/**
230+
* Returns a color state list associated with a particular resource ID and
231+
* styled for the current theme.
232+
*
233+
* @param id The desired resource identifier, as generated by the aapt
234+
* tool. This integer encodes the package, type, and resource
235+
* entry. The value 0 is an invalid identifier.
236+
* @return A color state list.
237+
* @throws android.content.res.Resources.NotFoundException if the given ID
238+
* does not exist.
239+
*/
240+
fun getColorStateList(@ColorRes id: Int): ColorStateList {
241+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
242+
context.getColorStateList(id)
243+
} else {
244+
context.resources.getColorStateList(id)
245+
}
246+
}
138247

139248
/**
140249
* This should never be called directly.

0 commit comments

Comments
 (0)