Update: Improve notifications on empty sync results

This commit is contained in:
machiav3lli 2022-07-09 01:30:47 +02:00
parent 8c88b8e6da
commit 717d653644
2 changed files with 58 additions and 50 deletions

View File

@ -35,62 +35,69 @@ import com.machiav3lli.fdroid.utility.extension.resources.getColorFromAttr
* *
* @param productItems list of apps pending updates * @param productItems list of apps pending updates
*/ */
fun Context.displayUpdatesNotification(productItems: List<ProductItem>) { fun Context.displayUpdatesNotification(
productItems: List<ProductItem>,
enforceNotify: Boolean = false
) {
val maxUpdates = 5 val maxUpdates = 5
fun <T> T.applyHack(callback: T.() -> Unit): T = apply(callback) fun <T> T.applyHack(callback: T.() -> Unit): T = apply(callback)
notificationManager.notify( if (productItems.isNotEmpty() || enforceNotify)
NOTIFICATION_ID_UPDATES, NotificationCompat notificationManager.notify(
.Builder(this, NOTIFICATION_CHANNEL_UPDATES) NOTIFICATION_ID_UPDATES, NotificationCompat
.setSmallIcon(R.drawable.ic_new_releases) .Builder(this, NOTIFICATION_CHANNEL_UPDATES)
.setContentTitle(getString(R.string.new_updates_available)) .setSmallIcon(R.drawable.ic_new_releases)
.setContentText( .setContentTitle(getString(if (productItems.isNotEmpty()) R.string.new_updates_available else R.string.no_updates_available))
resources.getQuantityString( .setContentText(
R.plurals.new_updates_DESC_FORMAT, if (productItems.isNotEmpty())
productItems.size, productItems.size resources.getQuantityString(
R.plurals.new_updates_DESC_FORMAT,
productItems.size, productItems.size
)
else null
) )
) .setColor(
.setColor( ContextThemeWrapper(this, R.style.Theme_Main_Light)
ContextThemeWrapper(this, R.style.Theme_Main_Light) .getColorFromAttr(android.R.attr.colorPrimary).defaultColor
.getColorFromAttr(android.R.attr.colorPrimary).defaultColor
)
.setContentIntent(
PendingIntent.getActivity(
this,
0,
Intent(this, MainActivityX::class.java)
.setAction(MainActivityX.ACTION_UPDATES)
.putExtra(
MainActivityX.EXTRA_UPDATES,
productItems.map { it.packageName }.toTypedArray()
),
if (Android.sdk(23))
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else
PendingIntent.FLAG_UPDATE_CURRENT
) )
) .setContentIntent(
.setStyle(NotificationCompat.InboxStyle().applyHack { PendingIntent.getActivity(
for (productItem in productItems.take(maxUpdates)) { this,
val builder = SpannableStringBuilder(productItem.name) 0,
builder.setSpan( Intent(this, MainActivityX::class.java)
ForegroundColorSpan(Color.BLACK), 0, builder.length, .setAction(MainActivityX.ACTION_UPDATES)
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE .putExtra(
MainActivityX.EXTRA_UPDATES,
productItems.map { it.packageName }.toTypedArray()
),
if (Android.sdk(23))
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else
PendingIntent.FLAG_UPDATE_CURRENT
) )
builder.append(' ').append(productItem.version) )
addLine(builder) .setStyle(NotificationCompat.InboxStyle().applyHack {
} for (productItem in productItems.take(maxUpdates)) {
if (productItems.size > maxUpdates) { val builder = SpannableStringBuilder(productItem.name)
val summary = builder.setSpan(
getString(R.string.plus_more_FORMAT, productItems.size - maxUpdates) ForegroundColorSpan(Color.BLACK), 0, builder.length,
if (Android.sdk(24)) { SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
addLine(summary) )
} else { builder.append(' ').append(productItem.version)
setSummaryText(summary) addLine(builder)
} }
} if (productItems.size > maxUpdates) {
}) val summary =
.build() getString(R.string.plus_more_FORMAT, productItems.size - maxUpdates)
) if (Android.sdk(24)) {
addLine(summary)
} else {
setSummaryText(summary)
}
}
})
.build()
)
else notificationManager.cancel(NOTIFICATION_ID_UPDATES)
} }
fun Context.showNotificationError(repository: Repository, exception: Exception) { fun Context.showNotificationError(repository: Repository, exception: Exception) {

View File

@ -206,4 +206,5 @@
<string name="open">Open</string> <string name="open">Open</string>
<string name="loading_list">Loading list…</string> <string name="loading_list">Loading list…</string>
<string name="releases">Releases</string> <string name="releases">Releases</string>
<string name="no_updates_available">No versions available</string>
</resources> </resources>