Implemented AbsSpinnerAttrAdapter

This commit is contained in:
Akash Yadav
2022-11-06 21:37:52 +05:30
parent 43903e9a8f
commit f132ee9ce9
4 changed files with 129 additions and 6 deletions
@@ -53,6 +53,16 @@ abstract class AbstractParser {
protected open fun parseInteger(value: String, def: Int = 0): Int {
return com.itsaky.androidide.inflater.internal.utils.parseInteger(value = value, def = def)
}
/**
* Parses the given string value representing a reference to an integer array resource to its actual
* value. Returns an empty array if the resource reference cannot be resolved.
*
* @param value The string value. Usually value from attributes.
*/
protected open fun parseIntegerArray(value: String): IntArray {
return com.itsaky.androidide.inflater.internal.utils.parseIntegerArray(value) ?: intArrayOf()
}
/**
* Parses the given string value representing a boolean or reference to an boolean resource to its
@@ -67,7 +77,7 @@ abstract class AbstractParser {
}
/**
* Parses the given string value representing a string or reference to an string resource to its
* Parses the given string value representing a string or reference to a string resource to its
* actual value. Returns [value] itself if it cannot be parsed.
*
* @param value The string value. Usually value from attributes.
@@ -76,6 +86,16 @@ abstract class AbstractParser {
return com.itsaky.androidide.inflater.internal.utils.parseString(value)
}
/**
* Parses the given string value representing a reference to a string array resource to its actual
* value. Returns an empty array if the resource reference cannot be resolved.
*
* @param value The string value. Usually value from attributes.
*/
protected open fun parseStringArray(value: String): Array<String> {
return com.itsaky.androidide.inflater.internal.utils.parseStringArray(value) ?: emptyArray()
}
/**
* Parses the given string value representing a color or reference to a drawable resource to an
* actual drawable which can be rendered.
@@ -0,0 +1,61 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.inflater.internal.adapters
import android.R.layout
import android.content.Context
import android.view.ViewGroup.LayoutParams
import android.widget.AbsSpinner
import com.itsaky.androidide.inflater.IAttribute
import com.itsaky.androidide.inflater.INamespace
import com.itsaky.androidide.inflater.IView
import com.itsaky.androidide.inflater.internal.LayoutFile
/**
* Attribute adapter for [AbsSpinner].
*
* @author Akash Yadav
*/
abstract class AbsSpinnerAttrAdapter : AdapterViewAttrAdapter() {
override fun apply(view: IView, attribute: IAttribute): Boolean {
return doApply<AbsSpinner>(view, attribute) {
_: LayoutFile,
context: Context,
_: LayoutParams,
_: INamespace,
name: String,
value: String ->
var applied = true
when (name) {
"entries" -> {
val array = parseStringArray(value)
val adapter = newSimpleAdapter(context, array)
adapter.setDropDownViewResource(layout.simple_spinner_dropdown_item)
setAdapter(adapter)
}
else -> applied = false
}
if (!applied) {
applied = super.apply(view, attribute)
}
return@doApply applied
}
}
}
@@ -0,0 +1,42 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.inflater.internal.adapters
import android.R.layout
import android.content.Context
import android.widget.ArrayAdapter
/**
* Attribute adapter for [AdapterView][android.widget.AdapterView]
*
* @author Akash Yadav
*/
abstract class AdapterViewAttrAdapter : ViewAttrAdapter() {
protected open fun newSimpleAdapter(ctx: Context): ArrayAdapter<String> {
return newSimpleAdapter(ctx, newAdapterItems(4))
}
protected open fun newSimpleAdapter(ctx: Context, items: Array<String>): ArrayAdapter<String> {
return ArrayAdapter<String>(ctx, layout.simple_list_item_1, items)
}
protected open fun newAdapterItems(size: Int): Array<String> {
return Array(size) { "Item $it" }
}
}
@@ -40,21 +40,21 @@ abstract class CompoundButtonAttrAdapter : ButtonAttrAdapter() {
_: INamespace,
name: String,
value: String ->
var handled = true
var applied = true
when (name) {
"buttonTint" -> buttonTintList = parseColorStateList(context, value)
"button" -> buttonDrawable = parseDrawable(context, value)
"buttonTintMode" -> buttonTintMode = parsePorterDuffMode(value)
"checked" -> isChecked = parseBoolean(value = value, def = true)
else -> handled = false
else -> applied = false
}
if (!handled) {
handled = super.apply(view, attribute)
if (!applied) {
applied = super.apply(view, attribute)
}
return@doApply handled
return@doApply applied
}
}