ntfy-android/app/src/main/java/io/heckel/ntfy/data/Repository.kt

123 lines
3.8 KiB
Kotlin
Raw Normal View History

2021-10-27 13:34:09 +13:00
package io.heckel.ntfy.data
import android.util.Log
2021-10-30 14:13:58 +13:00
import androidx.annotation.WorkerThread
2021-10-27 13:34:09 +13:00
import androidx.lifecycle.LiveData
2021-10-30 14:13:58 +13:00
import androidx.lifecycle.asLiveData
import androidx.lifecycle.map
import kotlinx.coroutines.flow.map
2021-10-27 15:41:19 +13:00
2021-11-01 08:19:25 +13:00
class Repository(private val subscriptionDao: SubscriptionDao, private val notificationDao: NotificationDao) {
init {
Log.d(TAG, "Created $this")
}
fun getSubscriptionsLiveData(): LiveData<List<Subscription>> {
return subscriptionDao
.listFlow()
.asLiveData()
.map { list -> toSubscriptionList(list) }
}
2021-11-14 13:26:37 +13:00
fun getSubscriptionIdsLiveData(): LiveData<Set<Long>> {
return subscriptionDao
.listFlow()
.asLiveData()
.map { list -> list.map { it.id }.toSet() }
}
fun getSubscriptions(): List<Subscription> {
return toSubscriptionList(subscriptionDao.list())
2021-10-27 15:41:19 +13:00
}
2021-10-30 14:13:58 +13:00
@Suppress("RedundantSuspendModifier")
@WorkerThread
2021-11-01 08:19:25 +13:00
suspend fun getSubscription(baseUrl: String, topic: String): Subscription? {
return toSubscription(subscriptionDao.get(baseUrl, topic))
2021-10-27 13:34:09 +13:00
}
2021-10-30 14:13:58 +13:00
@Suppress("RedundantSuspendModifier")
@WorkerThread
2021-11-01 08:19:25 +13:00
suspend fun addSubscription(subscription: Subscription) {
2021-10-30 14:13:58 +13:00
subscriptionDao.add(subscription)
2021-10-27 13:34:09 +13:00
}
2021-10-30 14:13:58 +13:00
@Suppress("RedundantSuspendModifier")
@WorkerThread
2021-11-01 08:19:25 +13:00
suspend fun removeSubscription(subscriptionId: Long) {
subscriptionDao.remove(subscriptionId)
}
fun getNotificationsLiveData(subscriptionId: Long): LiveData<List<Notification>> {
2021-11-01 08:19:25 +13:00
return notificationDao.list(subscriptionId).asLiveData()
}
fun onlyNewNotifications(subscriptionId: Long, notifications: List<Notification>): List<Notification> {
val existingIds = notificationDao.listIds(subscriptionId)
return notifications.filterNot { existingIds.contains(it.id) }
2021-11-08 07:13:32 +13:00
}
2021-11-01 08:19:25 +13:00
@Suppress("RedundantSuspendModifier")
@WorkerThread
2021-11-14 13:26:37 +13:00
suspend fun addNotification(notification: Notification): Boolean {
val maybeExistingNotification = notificationDao.get(notification.id)
if (maybeExistingNotification == null) {
notificationDao.add(notification)
2021-11-14 13:26:37 +13:00
return true
}
2021-11-14 13:26:37 +13:00
return false
2021-11-01 08:19:25 +13:00
}
@Suppress("RedundantSuspendModifier")
@WorkerThread
suspend fun removeNotification(notificationId: String) {
2021-11-04 06:56:08 +13:00
notificationDao.remove(notificationId)
2021-11-01 08:19:25 +13:00
}
@Suppress("RedundantSuspendModifier")
@WorkerThread
fun removeAllNotifications(subscriptionId: Long) {
notificationDao.removeAll(subscriptionId)
2021-10-27 13:34:09 +13:00
}
private fun toSubscriptionList(list: List<SubscriptionWithMetadata>): List<Subscription> {
return list.map { s ->
Subscription(
id = s.id,
baseUrl = s.baseUrl,
topic = s.topic,
2021-11-14 13:26:37 +13:00
instant = s.instant,
lastActive = s.lastActive,
notifications = s.notifications
)
}
}
private fun toSubscription(s: SubscriptionWithMetadata?): Subscription? {
if (s == null) {
return null
}
return Subscription(
id = s.id,
baseUrl = s.baseUrl,
topic = s.topic,
2021-11-14 13:26:37 +13:00
instant = s.instant,
lastActive = s.lastActive,
notifications = s.notifications
)
}
2021-10-27 13:34:09 +13:00
companion object {
private val TAG = "NtfyRepository"
2021-10-27 14:44:12 +13:00
private var instance: Repository? = null
2021-10-27 13:34:09 +13:00
2021-11-01 08:19:25 +13:00
fun getInstance(subscriptionDao: SubscriptionDao, notificationDao: NotificationDao): Repository {
2021-10-27 14:44:12 +13:00
return synchronized(Repository::class) {
2021-11-01 08:19:25 +13:00
val newInstance = instance ?: Repository(subscriptionDao, notificationDao)
2021-10-27 13:34:09 +13:00
instance = newInstance
newInstance
}
}
}
}