2021-10-27 13:34:09 +13:00
|
|
|
package io.heckel.ntfy.data
|
|
|
|
|
2021-11-12 13:41:29 +13:00
|
|
|
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
|
2021-11-12 16:14:28 +13:00
|
|
|
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) {
|
2021-11-12 13:41:29 +13:00
|
|
|
init {
|
|
|
|
Log.d(TAG, "Created $this")
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getSubscriptionsLiveData(): LiveData<List<Subscription>> {
|
2021-11-12 16:14:28 +13:00
|
|
|
return subscriptionDao
|
|
|
|
.listFlow()
|
|
|
|
.asLiveData()
|
|
|
|
.map { list -> toSubscriptionList(list) }
|
2021-11-12 13:41:29 +13:00
|
|
|
}
|
|
|
|
|
2021-11-14 13:26:37 +13:00
|
|
|
fun getSubscriptionIdsLiveData(): LiveData<Set<Long>> {
|
|
|
|
return subscriptionDao
|
|
|
|
.listFlow()
|
|
|
|
.asLiveData()
|
|
|
|
.map { list -> list.map { it.id }.toSet() }
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:41:29 +13:00
|
|
|
fun getSubscriptions(): List<Subscription> {
|
2021-11-12 16:14:28 +13:00
|
|
|
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? {
|
2021-11-12 16:14:28 +13:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:41:29 +13:00
|
|
|
fun getNotificationsLiveData(subscriptionId: Long): LiveData<List<Notification>> {
|
2021-11-01 08:19:25 +13:00
|
|
|
return notificationDao.list(subscriptionId).asLiveData()
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:41:29 +13:00
|
|
|
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 {
|
2021-11-12 13:41:29 +13:00
|
|
|
val maybeExistingNotification = notificationDao.get(notification.id)
|
2021-11-12 16:14:28 +13:00
|
|
|
if (maybeExistingNotification == null) {
|
|
|
|
notificationDao.add(notification)
|
2021-11-14 13:26:37 +13:00
|
|
|
return true
|
2021-11-12 13:41:29 +13:00
|
|
|
}
|
2021-11-14 13:26:37 +13:00
|
|
|
return false
|
2021-11-01 08:19:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("RedundantSuspendModifier")
|
|
|
|
@WorkerThread
|
2021-11-12 16:14:28 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-12 16:14:28 +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,
|
2021-11-12 16:14:28 +13:00
|
|
|
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,
|
2021-11-12 16:14:28 +13:00
|
|
|
lastActive = s.lastActive,
|
|
|
|
notifications = s.notifications
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:34:09 +13:00
|
|
|
companion object {
|
2021-11-12 13:41:29 +13:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|