Skip to content
Snippets Groups Projects
Verified Commit 035daae0 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Fixes #44

parent 63d74287
No related branches found
No related tags found
No related merge requests found
...@@ -126,7 +126,7 @@ class QuasselNotificationBackend @Inject constructor( ...@@ -126,7 +126,7 @@ class QuasselNotificationBackend @Inject constructor(
Message_Type.Notice).toInt(), Message_Type.Notice).toInt(),
0 0
) { ) {
processMessages(session, *it.toTypedArray()) processMessages(session, false, *it.toTypedArray())
false false
} }
} }
...@@ -140,7 +140,7 @@ class QuasselNotificationBackend @Inject constructor( ...@@ -140,7 +140,7 @@ class QuasselNotificationBackend @Inject constructor(
Message_Type.Notice).toInt(), Message_Type.Notice).toInt(),
Message_Flag.of(Message_Flag.Highlight).toInt() Message_Flag.of(Message_Flag.Highlight).toInt()
) { ) {
processMessages(session, *it.toTypedArray()) processMessages(session, false, *it.toTypedArray())
false false
} }
} }
...@@ -150,6 +150,15 @@ class QuasselNotificationBackend @Inject constructor( ...@@ -150,6 +150,15 @@ class QuasselNotificationBackend @Inject constructor(
} }
} }
} }
// Cleanup
val removedBuffers = database.notifications().buffers().minus(buffers.map(BufferInfo::bufferId))
for (removedBuffer in removedBuffers) {
database.notifications().markRead(removedBuffer, MsgId.MAX_VALUE)
}
// Update notifications to have actions
showConnectedNotifications()
} }
} }
...@@ -178,7 +187,7 @@ class QuasselNotificationBackend @Inject constructor( ...@@ -178,7 +187,7 @@ class QuasselNotificationBackend @Inject constructor(
} }
@Synchronized @Synchronized
override fun processMessages(session: Session, vararg messages: Message) { override fun processMessages(session: Session, show: Boolean, vararg messages: Message) {
val now = Instant.now() val now = Instant.now()
val results = messages.filter { val results = messages.filter {
val level = it.bufferInfo.type.let { val level = it.bufferInfo.type.let {
...@@ -229,18 +238,33 @@ class QuasselNotificationBackend @Inject constructor( ...@@ -229,18 +238,33 @@ class QuasselNotificationBackend @Inject constructor(
) )
} }
database.notifications().save(*results.toTypedArray()) database.notifications().save(*results.toTypedArray())
if (show) {
executor.schedule( executor.schedule(
{ {
results.map(QuasselDatabase.NotificationData::bufferId).distinct() results.map(QuasselDatabase.NotificationData::bufferId).distinct().forEach { buffer ->
.forEach(this::showNotification) this.showNotification(buffer)
}
}, },
session.lag.value * 2, session.lag.value * 2,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS
) )
} }
}
fun showConnectedNotifications() {
database.notifications().buffers().forEach { buffer ->
this.showNotification(buffer, true)
}
}
fun showDisconnectedNotifications() {
database.notifications().buffers().forEach { buffer ->
this.showNotification(buffer, false)
}
}
@Synchronized @Synchronized
private fun showNotification(buffer: BufferId) { private fun showNotification(buffer: BufferId, isConnected: Boolean = true) {
val data = database.notifications().all(buffer) val data = database.notifications().all(buffer)
data.lastOrNull()?.let { data.lastOrNull()?.let {
// Only send a loud notification if it has any new messages // Only send a loud notification if it has any new messages
...@@ -310,7 +334,7 @@ class QuasselNotificationBackend @Inject constructor( ...@@ -310,7 +334,7 @@ class QuasselNotificationBackend @Inject constructor(
) )
} }
val notification = notificationHandler.notificationMessage( val notification = notificationHandler.notificationMessage(
notificationSettings, bufferInfo, notificationData, isLoud notificationSettings, bufferInfo, notificationData, isLoud, isConnected
) )
notificationHandler.notify(notification) notificationHandler.notify(notification)
} ?: notificationHandler.remove(buffer) } ?: notificationHandler.remove(buffer)
......
...@@ -366,6 +366,7 @@ class QuasselService : DaggerLifecycleService(), ...@@ -366,6 +366,7 @@ class QuasselService : DaggerLifecycleService(),
if (it == ConnectionState.CLOSED) { if (it == ConnectionState.CLOSED) {
scheduleReconnect() scheduleReconnect()
notificationBackend.showDisconnectedNotifications()
} }
} }
}) })
......
...@@ -108,7 +108,8 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C ...@@ -108,7 +108,8 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C
} }
fun notificationMessage(notificationSettings: NotificationSettings, bufferInfo: BufferInfo, fun notificationMessage(notificationSettings: NotificationSettings, bufferInfo: BufferInfo,
notifications: List<NotificationMessage>, isLoud: Boolean): Handle { notifications: List<NotificationMessage>, isLoud: Boolean,
isConnected: Boolean): Handle {
val pendingIntentOpen = PendingIntent.getActivity( val pendingIntentOpen = PendingIntent.getActivity(
context.applicationContext, context.applicationContext,
System.currentTimeMillis().toInt(), System.currentTimeMillis().toInt(),
...@@ -198,7 +199,8 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C ...@@ -198,7 +199,8 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C
} }
} }
) )
.addAction(0, translatedLocale.getString(R.string.label_mark_read), markReadPendingIntent) .letIf(isConnected) {
it.addAction(0, translatedLocale.getString(R.string.label_mark_read), markReadPendingIntent)
.letIf(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { .letIf(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
it.addAction( it.addAction(
NotificationCompat.Action.Builder( NotificationCompat.Action.Builder(
...@@ -208,6 +210,7 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C ...@@ -208,6 +210,7 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C
).addRemoteInput(remoteInput).build() ).addRemoteInput(remoteInput).build()
) )
} }
}
.setWhen(notifications.last().time.toEpochMilli()) .setWhen(notifications.last().time.toEpochMilli())
.apply { .apply {
if (bufferInfo.type.hasFlag(Buffer_Type.QueryBuffer)) { if (bufferInfo.type.hasFlag(Buffer_Type.QueryBuffer)) {
......
...@@ -60,7 +60,7 @@ class RpcHandler( ...@@ -60,7 +60,7 @@ class RpcHandler(
override fun displayMsg(message: Message) { override fun displayMsg(message: Message) {
session.bufferSyncer.bufferInfoUpdated(message.bufferInfo) session.bufferSyncer.bufferInfoUpdated(message.bufferInfo)
backlogStorage.storeMessages(session, message) backlogStorage.storeMessages(session, message)
notificationManager?.processMessages(session, message) notificationManager?.processMessages(session, true, message)
} }
override fun createIdentity(identity: Identity, additional: QVariantMap) = override fun createIdentity(identity: Identity, additional: QVariantMap) =
......
...@@ -25,6 +25,6 @@ import de.kuschku.libquassel.protocol.MsgId ...@@ -25,6 +25,6 @@ import de.kuschku.libquassel.protocol.MsgId
interface NotificationManager { interface NotificationManager {
fun init(session: Session) fun init(session: Session)
fun processMessages(session: Session, vararg messages: Message) fun processMessages(session: Session, show: Boolean, vararg messages: Message)
fun clear(buffer: BufferId, lastRead: MsgId = MsgId.MAX_VALUE) fun clear(buffer: BufferId, lastRead: MsgId = MsgId.MAX_VALUE)
} }
...@@ -216,6 +216,9 @@ abstract class QuasselDatabase : RoomDatabase() { ...@@ -216,6 +216,9 @@ abstract class QuasselDatabase : RoomDatabase() {
@Insert(onConflict = OnConflictStrategy.IGNORE) @Insert(onConflict = OnConflictStrategy.IGNORE)
fun save(vararg entities: NotificationData) fun save(vararg entities: NotificationData)
@Query("SELECT DISTINCT bufferId FROM notification")
fun buffers(): List<BufferId>
@Query("SELECT * FROM notification ORDER BY time ASC") @Query("SELECT * FROM notification ORDER BY time ASC")
fun all(): List<NotificationData> fun all(): List<NotificationData>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment