refactor: Extract notification type icon code (#1126)

This code will be used elsewhere in an upcoming change, so extract it
now to minimise the diffs.

While I'm here, provide an icon for mentions, and an attribute for the
"favourite" colour.
This commit is contained in:
Nik Clayton 2024-11-22 15:05:06 +01:00 committed by GitHub
parent 79a69513ae
commit acdbf1e52f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 113 additions and 7 deletions

View File

@ -71,7 +71,7 @@ open class FilterableStatusViewHolder<T : IStatusViewData>(
binding.statusFilteredPlaceholder.statusFilterLabel.text = label
binding.statusFilteredPlaceholder.statusFilterShowAnyway.setOnClickListener {
listener.clearWarningAction(pachliAccountId, status)
listener.clearContentFilter(pachliAccountId, status)
}
binding.statusFilteredPlaceholder.statusFilterEditFilter.setOnClickListener {
listener.onEditFilterById(pachliAccountId, result.filter.id)

View File

@ -0,0 +1,93 @@
/*
* Copyright 2024 Pachli Association
*
* This file is a part of Pachli.
*
* This program 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.
*
* Pachli 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 Pachli; if not,
* see <http://www.gnu.org/licenses>.
*/
package app.pachli.components.notifications
import android.content.Context
import android.graphics.drawable.Drawable
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import app.pachli.R
import app.pachli.core.network.model.Notification
import app.pachli.util.setDrawableTint
/**
* @return An icon for the given [Notification.Type], appropriately coloured.
*/
fun Notification.Type.icon(context: Context) = when (this) {
Notification.Type.UNKNOWN -> getIconWithColor(
context,
R.drawable.ic_home_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.MENTION -> getIconWithColor(
context,
R.drawable.ic_mention_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.REBLOG -> getIconWithColor(
context,
R.drawable.ic_repeat_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.FAVOURITE -> getIconWithColor(
context,
R.drawable.ic_star_24dp,
app.pachli.core.designsystem.R.attr.favoriteIconColor,
)
Notification.Type.FOLLOW -> getIconWithColor(
context,
app.pachli.core.ui.R.drawable.ic_person_add_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.FOLLOW_REQUEST -> getIconWithColor(
context,
app.pachli.core.ui.R.drawable.ic_person_add_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.POLL -> getIconWithColor(
context,
R.drawable.ic_poll_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.STATUS -> getIconWithColor(
context,
R.drawable.ic_home_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
Notification.Type.UPDATE -> getIconWithColor(
context,
R.drawable.ic_edit_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
else -> getIconWithColor(
context,
R.drawable.ic_home_24dp,
androidx.appcompat.R.attr.colorPrimary,
)
}
private fun getIconWithColor(
context: Context,
@DrawableRes drawable: Int,
@AttrRes color: Int,
): Drawable? {
return AppCompatResources.getDrawable(context, drawable)?.apply {
setDrawableTint(context, this, color)
}
}

View File

@ -249,26 +249,21 @@ internal class StatusNotificationViewHolder(
val type = viewData.type
val context = binding.notificationTopText.context
val format: String
val icon: Drawable?
val icon = type.icon(context)
when (type) {
Notification.Type.FAVOURITE -> {
icon = getIconWithColor(context, R.drawable.ic_star_24dp, DR.color.tusky_orange)
format = context.getString(R.string.notification_favourite_format)
}
Notification.Type.REBLOG -> {
icon = getIconWithColor(context, R.drawable.ic_repeat_24dp, DR.color.tusky_blue)
format = context.getString(R.string.notification_reblog_format)
}
Notification.Type.STATUS -> {
icon = getIconWithColor(context, R.drawable.ic_home_24dp, DR.color.tusky_blue)
format = context.getString(R.string.notification_subscription_format)
}
Notification.Type.UPDATE -> {
icon = getIconWithColor(context, R.drawable.ic_edit_24dp, DR.color.tusky_blue)
format = context.getString(R.string.notification_update_format)
}
else -> {
icon = getIconWithColor(context, R.drawable.ic_star_24dp, DR.color.tusky_orange)
format = context.getString(R.string.notification_favourite_format)
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="?colorPrimary"
android:pathData="M22.2 10.2q0 2-.8 3.8-.7 1.7-2 2.7-1.3 1-2.8 1-1 0-1.6-.5-.6-.6-.6-1.5v-.8q-.6 1.2-1.7 2t-2.3.8q-1.8 0-2.9-1.2-1-1.2-1-3.3 0-1.8.8-3.5t2-2.6q1.4-1 3-1 2.3 0 3 2h.1l.5-1.7h1.8l-1.3 5.9q-.5 2-.5 2.7 0 .6.3.8.3.3.6.3 1 0 1.8-.8t1.3-2.2q.5-1.3.5-3 0-2-.9-3.5Q18.7 5 17 4.2q-1.7-.8-3.9-.8-2.8 0-4.9 1.2Q6.2 5.8 5 8q-1.2 2.3-1.2 5.1 0 2.2 1 4 .8 1.7 2.5 2.5 1.7 1 3.9 1 1.7 0 3.4-.5t3.4-1.4l.7 1.4q-2 1.1-3.8 1.6-1.8.5-3.8.5-2.7 0-4.8-1.1Q4.2 20 3 18q-1.2-2.1-1.2-4.8 0-3.3 1.5-6 1.4-2.6 4-4 2.6-1.5 5.8-1.5 2.8 0 4.9 1 2 1.1 3 3 1.2 2 1.2 4.5zm-7.5 0q0-1.1-.7-1.8t-1.8-.7q-1 0-1.9.7-.9.8-1.4 2-.4 1.4-.4 2.8 0 1.3.5 2 .6.8 1.7.8.8 0 1.5-.4.8-.5 1.3-1.5l.7-1.3.4-1.4.1-1.2z" />
</vector>

View File

@ -28,4 +28,8 @@
<!-- Height of a toolbar that contains a title and a subtitle -->
<!-- Some custom fonts require this to be larger than the default -->
<attr name="actionBarSizeWithSubtitle" format="dimension|reference" />
<!-- Colour to use for the "favourite" icon. Also displayed next to
favourite notifications. -->
<attr name="favoriteIconColor" format="color" />
</resources>

View File

@ -85,6 +85,10 @@
<item name="snackbarTextViewStyle">@style/snackbar_text</item>
<item name="linearProgressIndicatorStyle">@style/Pachli.Widget.Material3.LinearProgressIndicator</item>
<!-- TODO: Change this to pachli_orange, but also need a lighter version for the
sparkbutton effect first. -->
<item name="favoriteIconColor">@color/tusky_orange</item>
</style>
<style name="AppTheme" parent="Theme.Pachli" />