3408: Only support iconics (get rid of "discouraged api") (#3476)
* 3408: Only support iconics (get rid of "discouraged api") * 3408: Remove unused import
This commit is contained in:
parent
7f7afb3e46
commit
8c519af611
3 changed files with 9 additions and 21 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package com.keylesspalace.tusky.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Build
|
||||
import android.text.Spannable
|
||||
import android.text.SpannableStringBuilder
|
||||
|
|
@ -11,7 +10,6 @@ import android.text.style.DynamicDrawableSpan
|
|||
import android.text.style.ForegroundColorSpan
|
||||
import android.text.style.ImageSpan
|
||||
import android.text.style.URLSpan
|
||||
import androidx.appcompat.content.res.AppCompatResources
|
||||
import com.mikepenz.iconics.IconicsDrawable
|
||||
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
|
||||
import java.util.regex.Pattern
|
||||
|
|
@ -97,34 +95,24 @@ fun highlightSpans(text: Spannable, colour: Int) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Replaces text of the form [drawabale name] or [iconics name] with their spanned counterparts (ImageSpan).
|
||||
* Replaces text of the form [iconics name] with their spanned counterparts (ImageSpan).
|
||||
*/
|
||||
fun addDrawables(text: CharSequence, color: Int, size: Int, context: Context): Spannable {
|
||||
val alignment = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) DynamicDrawableSpan.ALIGN_CENTER else DynamicDrawableSpan.ALIGN_BASELINE
|
||||
|
||||
val builder = SpannableStringBuilder(text)
|
||||
|
||||
val pattern = Pattern.compile("\\[(drawable|iconics) ([0-9a-z_]+)\\]")
|
||||
val pattern = Pattern.compile("\\[iconics ([0-9a-z_]+)\\]")
|
||||
val matcher = pattern.matcher(builder)
|
||||
while (matcher.find()) {
|
||||
val resourceType = matcher.group(1)
|
||||
val resourceName = matcher.group(2)
|
||||
val resourceName = matcher.group(1)
|
||||
?: continue
|
||||
|
||||
val drawable: Drawable? = when (resourceType) {
|
||||
"iconics" -> IconicsDrawable(context, GoogleMaterial.getIcon(resourceName))
|
||||
else -> {
|
||||
val drawableResourceId = context.resources.getIdentifier(resourceName, "drawable", context.packageName)
|
||||
if (drawableResourceId != 0) AppCompatResources.getDrawable(context, drawableResourceId) else null
|
||||
}
|
||||
}
|
||||
val drawable = IconicsDrawable(context, GoogleMaterial.getIcon(resourceName))
|
||||
drawable.setBounds(0, 0, size, size)
|
||||
drawable.setTint(color)
|
||||
|
||||
if (drawable != null) {
|
||||
drawable.setBounds(0, 0, size, size)
|
||||
drawable.setTint(color)
|
||||
|
||||
builder.setSpan(ImageSpan(drawable, alignment), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
builder.setSpan(ImageSpan(drawable, alignment), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
|
||||
return builder
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue