Loading app/src/main/java/foundation/e/notificationsreceiver/repositories/MessagesRepositoryImpl.kt +27 −19 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ import foundation.e.notificationsreceiver.domain.entities.Topic import foundation.e.notificationsreceiver.domain.repositories.MessagesRepository import foundation.e.notificationsreceiver.domain.utils.runSuspendCatching import io.heckel.ntfy.db.Database import io.heckel.ntfy.db.Notification import io.heckel.ntfy.db.NotificationDao import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow Loading @@ -18,10 +19,8 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton class MessagesRepositoryImpl @Inject constructor( @ApplicationContext private val appContext: Context, ) : MessagesRepository { class MessagesRepositoryImpl @Inject constructor(@ApplicationContext private val appContext: Context) : MessagesRepository { private val notificationsDao: NotificationDao init { Loading @@ -31,22 +30,25 @@ class MessagesRepositoryImpl @Inject constructor( override fun messages(subscriptions: List<Topic>): Flow<List<Message>> { return notificationsDao.listFlow(subscriptions.map { it.localId }).map { notifications -> notifications.mapNotNull { with(it) { runSuspendCatching { notifications.mapNotNull(::notificationToMessage) } } internal fun notificationToMessage(notification: Notification): Message? { return runSuspendCatching { with(notification) { Message( id = id, id = tags.split(",").firstOrNull().takeIf { !it.isNullOrBlank() } ?: id, timestamp = Instant.ofEpochSecond(timestamp), title = title, content = message, icon = IconType.EOS, read = deleted, ntfyId = id, notificationId = notificationId, ) }.getOrNull() } } } }.getOrNull() } override suspend fun markAsRead(localId: String) { Loading @@ -54,4 +56,10 @@ class MessagesRepositoryImpl @Inject constructor( notificationsDao.markAsDeleted(notificationId = localId) } } override suspend fun removeAll(localIds: List<String>) { withContext(Dispatchers.IO) { notificationsDao.removeAll(notificationIds = localIds) } } } app/src/main/java/io/heckel/ntfy/db/Database.kt +3 −0 Original line number Diff line number Diff line Loading @@ -477,6 +477,9 @@ interface NotificationDao { @Query("DELETE FROM notification WHERE subscriptionId = :subscriptionId") fun removeAll(subscriptionId: Long) @Query("DELETE FROM notification WHERE id IN (:notificationIds)") fun removeAll(notificationIds: List<String>) @Query("SELECT * FROM notification WHERE subscriptionId IN (:subscriptionIds) ORDER BY timestamp DESC") fun listFlow(subscriptionIds: List<Long>): Flow<List<Notification>> } Loading build.gradle +1 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ spotless { "ktlint_standard_property-naming": "disabled", "ktlint_standard_discouraged-comment-location": "disabled", "ktlint_standard_function-expression-body": "disabled", "max_line_length": 120 ]) target( "app/src/main/java/foundation/e/notificationsreceiver/**/*.kt", Loading notificationsreceiver-domain/entitiesfixtures/src/main/java/foundation/e/notificationreceiver/domain/entities/fixtures/MessageFixtures.kt +1 −0 Original line number Diff line number Diff line Loading @@ -26,5 +26,6 @@ val messageFixture = Message( title = "Message Fixture", content = "Simple content of the base fixture of a Message\n", read = false, ntfyId = "ntfyid_1", notificationId = 36 ) notificationsreceiver-domain/src/main/java/foundation/e/notificationsreceiver/domain/entities/Message.kt +1 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ data class Message( val content: String, val icon: IconType = IconType.EOS, val read: Boolean = false, val ntfyId: String, val notificationId: Int, ) { enum class IconType { Loading Loading
app/src/main/java/foundation/e/notificationsreceiver/repositories/MessagesRepositoryImpl.kt +27 −19 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ import foundation.e.notificationsreceiver.domain.entities.Topic import foundation.e.notificationsreceiver.domain.repositories.MessagesRepository import foundation.e.notificationsreceiver.domain.utils.runSuspendCatching import io.heckel.ntfy.db.Database import io.heckel.ntfy.db.Notification import io.heckel.ntfy.db.NotificationDao import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow Loading @@ -18,10 +19,8 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton class MessagesRepositoryImpl @Inject constructor( @ApplicationContext private val appContext: Context, ) : MessagesRepository { class MessagesRepositoryImpl @Inject constructor(@ApplicationContext private val appContext: Context) : MessagesRepository { private val notificationsDao: NotificationDao init { Loading @@ -31,22 +30,25 @@ class MessagesRepositoryImpl @Inject constructor( override fun messages(subscriptions: List<Topic>): Flow<List<Message>> { return notificationsDao.listFlow(subscriptions.map { it.localId }).map { notifications -> notifications.mapNotNull { with(it) { runSuspendCatching { notifications.mapNotNull(::notificationToMessage) } } internal fun notificationToMessage(notification: Notification): Message? { return runSuspendCatching { with(notification) { Message( id = id, id = tags.split(",").firstOrNull().takeIf { !it.isNullOrBlank() } ?: id, timestamp = Instant.ofEpochSecond(timestamp), title = title, content = message, icon = IconType.EOS, read = deleted, ntfyId = id, notificationId = notificationId, ) }.getOrNull() } } } }.getOrNull() } override suspend fun markAsRead(localId: String) { Loading @@ -54,4 +56,10 @@ class MessagesRepositoryImpl @Inject constructor( notificationsDao.markAsDeleted(notificationId = localId) } } override suspend fun removeAll(localIds: List<String>) { withContext(Dispatchers.IO) { notificationsDao.removeAll(notificationIds = localIds) } } }
app/src/main/java/io/heckel/ntfy/db/Database.kt +3 −0 Original line number Diff line number Diff line Loading @@ -477,6 +477,9 @@ interface NotificationDao { @Query("DELETE FROM notification WHERE subscriptionId = :subscriptionId") fun removeAll(subscriptionId: Long) @Query("DELETE FROM notification WHERE id IN (:notificationIds)") fun removeAll(notificationIds: List<String>) @Query("SELECT * FROM notification WHERE subscriptionId IN (:subscriptionIds) ORDER BY timestamp DESC") fun listFlow(subscriptionIds: List<Long>): Flow<List<Notification>> } Loading
build.gradle +1 −0 Original line number Diff line number Diff line Loading @@ -41,6 +41,7 @@ spotless { "ktlint_standard_property-naming": "disabled", "ktlint_standard_discouraged-comment-location": "disabled", "ktlint_standard_function-expression-body": "disabled", "max_line_length": 120 ]) target( "app/src/main/java/foundation/e/notificationsreceiver/**/*.kt", Loading
notificationsreceiver-domain/entitiesfixtures/src/main/java/foundation/e/notificationreceiver/domain/entities/fixtures/MessageFixtures.kt +1 −0 Original line number Diff line number Diff line Loading @@ -26,5 +26,6 @@ val messageFixture = Message( title = "Message Fixture", content = "Simple content of the base fixture of a Message\n", read = false, ntfyId = "ntfyid_1", notificationId = 36 )
notificationsreceiver-domain/src/main/java/foundation/e/notificationsreceiver/domain/entities/Message.kt +1 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ data class Message( val content: String, val icon: IconType = IconType.EOS, val read: Boolean = false, val ntfyId: String, val notificationId: Int, ) { enum class IconType { Loading