This repository was archived by the owner on Jun 9, 2023. It is now read-only.
forked from saket/Dank
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replicate element coloring with icons (#305)
* fix invalid icon name * CenterAlignedImageSpan * Vote direction icon * Vote icon for comments * OP comment icon * use centering based on font metrics * Add preference * Fix tests * sp to px units conversion * Add vertical offset * Revert c3598f8, 9d13eca (remove preference) * Remove preference gates * Update changelog entry * Improve icon centering for different fonts * Use dimension resource for line spacing * Remove drawable caching * Don't apply extra line spacing correction to last line Co-authored-by: Łukasz Rutkowski <[email protected]>
- Loading branch information
1 parent
d681e29
commit 1592ad5
Showing
13 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/me/saket/dank/utils/ColorReplicationIcons.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package me.saket.dank.utils | ||
|
||
import android.content.Context | ||
import androidx.core.content.ContextCompat | ||
import me.saket.dank.R | ||
import me.saket.dank.widgets.span.CenterAlignedImageSpan | ||
import net.dean.jraw.models.VoteDirection | ||
|
||
object ColorReplicationIcons { | ||
@JvmStatic fun pushIcon(context: Context, builder: Truss, sizeDimenResId: Int, | ||
drawableResId: Int, tintColor: Int, lineSpacingExtra: Int) { | ||
val size = context.resources.getDimensionPixelSize(sizeDimenResId) | ||
val icon = context.resources.getDrawable(drawableResId, null).mutate() | ||
icon.setTint(tintColor) | ||
icon.setBounds(0, 0, size, size) | ||
|
||
builder | ||
.pushSpan(CenterAlignedImageSpan(icon, lineSpacingExtra)) | ||
.append("icon") | ||
.popSpan() | ||
} | ||
|
||
@JvmStatic fun pushVoteIcon(context: Context, builder: Truss, vote: VoteDirection?, color: Int, iconSizeResId: Int, lineSpacingExtra: Int) { | ||
val icon = when (vote) { | ||
VoteDirection.DOWN -> R.drawable.ic_arrow_downward_24dp | ||
VoteDirection.UP -> R.drawable.ic_arrow_upward_24dp | ||
VoteDirection.NONE, null -> return | ||
} | ||
|
||
pushIcon(context, builder, iconSizeResId, icon, color, lineSpacingExtra) | ||
} | ||
|
||
@JvmStatic fun pushOPCommentIcon(context: Context, builder: Truss) { | ||
val color = ContextCompat.getColor(context, R.color.submission_comment_byline_author_op) | ||
pushIcon(context, builder, R.dimen.submission_comment_byline, R.drawable.ic_person_12dp, color, 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/src/main/java/me/saket/dank/widgets/span/CenterAlignedImageSpan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package me.saket.dank.widgets.span | ||
|
||
import android.graphics.Canvas | ||
import android.graphics.Paint | ||
import android.graphics.Paint.FontMetricsInt | ||
import android.graphics.drawable.Drawable | ||
import android.text.style.ImageSpan | ||
import java.lang.ref.WeakReference | ||
|
||
class CenterAlignedImageSpan(drawable: Drawable, private val lineSpacingExtra: Int): ImageSpan(drawable, ALIGN_BASELINE) { | ||
|
||
override fun draw(canvas: Canvas, text: CharSequence?, | ||
start: Int, end: Int, x: Float, | ||
top: Int, y: Int, bottom: Int, paint: Paint) { | ||
val drawableHeight = drawable.bounds.height() | ||
val realExtraSpacing = if (canvas.clipBounds.bottom != bottom) lineSpacingExtra else 0 // extra line spacing is not applied to last line | ||
val translationY = top + (bottom - top - drawableHeight - realExtraSpacing) / 2f | ||
|
||
canvas.save() | ||
canvas.translate(x, translationY) | ||
drawable.draw(canvas) | ||
canvas.restore() | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters