|
1 | 1 | package com.hannesdorfmann.adapterdelegates4.dsl |
2 | 2 |
|
| 3 | +import android.content.Context |
| 4 | +import android.content.res.ColorStateList |
| 5 | +import android.graphics.drawable.Drawable |
| 6 | +import android.os.Build |
3 | 7 | import android.view.LayoutInflater |
4 | 8 | import android.view.View |
5 | 9 | import android.view.ViewGroup |
| 10 | +import androidx.annotation.ColorInt |
| 11 | +import androidx.annotation.ColorRes |
| 12 | +import androidx.annotation.DrawableRes |
6 | 13 | import androidx.annotation.IdRes |
7 | 14 | import androidx.annotation.LayoutRes |
| 15 | +import androidx.annotation.StringRes |
8 | 16 | import androidx.recyclerview.widget.RecyclerView |
9 | 17 | import com.hannesdorfmann.adapterdelegates4.AbsListItemAdapterDelegate |
10 | 18 | import com.hannesdorfmann.adapterdelegates4.AdapterDelegate |
@@ -43,6 +51,10 @@ inline fun <reified I : T, T> adapterDelegate( |
43 | 51 | ) |
44 | 52 | } |
45 | 53 |
|
| 54 | +/** |
| 55 | + * Delegate used internally in combination with [adapterDelegate] |
| 56 | + * @since 4.1.0 |
| 57 | + */ |
46 | 58 | class DslListAdapterDelegate<I : T, T>( |
47 | 59 | @LayoutRes private val layout: Int, |
48 | 60 | private val on: (item: T, items: List<T>, position: Int) -> Boolean, |
@@ -103,6 +115,8 @@ class DslListAdapterDelegate<I : T, T>( |
103 | 115 |
|
104 | 116 | /** |
105 | 117 | * ViewHolder that is used internally if you use [adapterDelegate] DSL to create your Adapter |
| 118 | + * |
| 119 | + * @since 4.1.0 |
106 | 120 | */ |
107 | 121 | class AdapterDelegateViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) { |
108 | 122 |
|
@@ -133,8 +147,103 @@ class AdapterDelegateViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) { |
133 | 147 |
|
134 | 148 | /** |
135 | 149 | * 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 |
136 | 198 | */ |
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 | + } |
138 | 247 |
|
139 | 248 | /** |
140 | 249 | * This should never be called directly. |
|
0 commit comments