2021-10-27 13:34:09 +13:00
|
|
|
package io.heckel.ntfy.data
|
|
|
|
|
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-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) {
|
|
|
|
fun getAllSubscriptions(): LiveData<List<Subscription>> {
|
2021-10-30 14:13:58 +13:00
|
|
|
return subscriptionDao.list().asLiveData()
|
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-10-30 14:13:58 +13:00
|
|
|
return 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 updateSubscription(subscription: Subscription) {
|
2021-10-30 14:13:58 +13:00
|
|
|
subscriptionDao.update(subscription)
|
2021-10-28 09:15:59 +13:00
|
|
|
}
|
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 removeSubscription(subscriptionId: Long) {
|
|
|
|
subscriptionDao.remove(subscriptionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getAllNotifications(subscriptionId: Long): LiveData<List<Notification>> {
|
|
|
|
return notificationDao.list(subscriptionId).asLiveData()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("RedundantSuspendModifier")
|
|
|
|
@WorkerThread
|
|
|
|
suspend fun addNotification(notification: Notification) {
|
|
|
|
notificationDao.add(notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("RedundantSuspendModifier")
|
|
|
|
@WorkerThread
|
2021-11-04 06:56:08 +13:00
|
|
|
suspend fun removeNotification(notificationId: String) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|