Created abstract class for parsing APIs

This commit is contained in:
Akash Yadav
2022-11-06 08:33:04 +05:30
parent c67cbb634c
commit a891b8d609
5 changed files with 144 additions and 40 deletions
@@ -0,0 +1,134 @@
/*
* 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
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import com.itsaky.androidide.inflater.internal.utils.defaultGravity
import com.itsaky.androidide.inflater.internal.utils.unknownDrawable
/**
* Abstract class which provides access to the internal parsing utility methods to its subclasses.
*
* @author Akash Yadav
*/
abstract class AbstractParser {
/**
* Parses the given string value representing an integer or reference to an integer resource to
* its actual value.
*
* @param value The string value. Usually value from attributes.
* @param def The default value.
*/
@JvmOverloads
protected open fun parseFloat(value: String, def: Float = 0f): Float {
return com.itsaky.androidide.inflater.internal.utils.parseFloat(value = value, def = def)
}
/**
* Parses the given string value representing an integer or reference to an integer resource to
* its actual value.
*
* @param value The string value. Usually value from attributes.
* @param def The default value.
*/
@JvmOverloads
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 boolean or reference to an boolean resource to its
* actual value.
*
* @param value The string value. Usually value from attributes.
* @param def The default value.
*/
@JvmOverloads
protected open fun parseBoolean(value: String, def: Boolean = false): Boolean {
return com.itsaky.androidide.inflater.internal.utils.parseBoolean(value = value, def = def)
}
/**
* Parses the given string value representing a color or reference to a drawable resource to an
* actual drawable which can be rendered.
*
* @param value The string value. Usually value from attributes.
* @param def The default value.
*/
@JvmOverloads
protected open fun parseDrawable(
context: Context,
value: String,
def: Drawable = unknownDrawable()
): Drawable {
return com.itsaky.androidide.inflater.internal.utils.parseDrawable(
context = context,
value = value,
def = def
)
}
/**
* Parses the gravity flags which can be single flag value like `center` or a multiple combined
* flag values like `start|top`.
*
* @param value The gravity string.
* @param def The default gravity flag value.
*/
@JvmOverloads
protected open fun parseGravity(value: String, def: Int = defaultGravity()): Int {
return com.itsaky.androidide.inflater.internal.utils.parseGravity(value = value, def = def)
}
/**
* Parses the given string value representing a dimension value or reference to a dimension
* resource to its actual value.
*
* @param value The string value. Usually value from attributes.
* @param def The default value.
*/
@JvmOverloads
protected open fun parseDimension(context: Context, value: String, def: Int = 0): Int {
return com.itsaky.androidide.inflater.internal.utils
.parseDimension(context = context, value = value, def = def.toFloat())
.toInt()
}
/**
* Parses the given string value representing a color code or reference to a color resource to its
* actual value.
*
* @param value The string value. Usually value from attributes.
* @param def The default value.
*/
@JvmOverloads
protected open fun parseColor(
context: Context,
value: String,
def: Int = Color.TRANSPARENT
): Int {
return com.itsaky.androidide.inflater.internal.utils.parseColor(
context = context,
value = value,
def = def
)
}
}
@@ -22,7 +22,7 @@ package com.itsaky.androidide.inflater
*
* @author Akash Yadav
*/
abstract class IAttributeAdapter {
abstract class IAttributeAdapter : AbstractParser() {
/**
* Apply the given attribute to the given view.
@@ -21,6 +21,7 @@ import android.graphics.drawable.Drawable
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import androidx.annotation.ChecksSdkIntAtLeast
import com.itsaky.androidide.inflater.AbstractParser
import org.xmlpull.v1.XmlPullParser
/**
@@ -29,7 +30,7 @@ import org.xmlpull.v1.XmlPullParser
* @author Akash Yadav
*/
abstract class IDrawableParser
protected constructor(protected open val parser: XmlPullParser?, open var minDepth: Int) {
protected constructor(protected open val parser: XmlPullParser?, open var minDepth: Int) : AbstractParser() {
/**
* Parse the drawable using the already provided parser and data.
*
@@ -61,33 +62,7 @@ protected constructor(protected open val parser: XmlPullParser?, open var minDep
}
return drawable
}
protected fun parseInteger(value: String, def: Int): Int {
return com.itsaky.androidide.inflater.internal.utils.parseInteger(value, def)
}
protected fun parseBoolean(value: String): Boolean {
return com.itsaky.androidide.inflater.internal.utils.parseBoolean(value)
}
protected fun parseDrawable(context: Context, value: String): Drawable {
return com.itsaky.androidide.inflater.internal.utils.parseDrawable(context, value)
}
protected fun parseGravity(value: String): Int {
return com.itsaky.androidide.inflater.internal.utils.parseGravity(value)
}
protected fun parseDimension(context: Context?, value: String?): Int {
return com.itsaky.androidide.inflater.internal.utils
.parseDimension(context!!, value, 0f)
.toInt()
}
protected fun parseColor(context: Context, value: String): Int {
return com.itsaky.androidide.inflater.internal.utils.parseColor(context, value)
}
/**
* Actual implementation of the parse logic.
*
@@ -51,18 +51,13 @@ open class ViewAttrAdapter : IAttributeAdapter() {
"alpha" -> alpha = parseFloat(value)
else -> handled = false
}
this.layoutParams = layoutParams
if (handled) {
this.layoutParams = layoutParams
}
}
}
protected fun parseDimension(context: Context, value: String): Int {
return com.itsaky.androidide.inflater.internal.utils.parseDimension(context, value).roundToInt()
}
protected fun parseFloat(value: String, def: Float = 1f): Float {
return com.itsaky.androidide.inflater.internal.utils.parseFloat(value, def)
}
override fun applyBasic(view: IView) {
TODO("Not yet implemented")
}
@@ -284,11 +284,11 @@ fun parseDimension(
}
}
fun parseFloat(value: String, defValue: Float): Float {
fun parseFloat(value: String, def: Float): Float {
return try {
value.toFloat()
} catch (err: Throwable) {
defValue
def
}
}