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

267 lines
9 KiB
Kotlin
Raw Normal View History

2021-10-27 13:34:09 +13:00
package io.heckel.ntfy.data
2021-11-23 09:45:43 +13:00
import android.content.SharedPreferences
import android.util.Log
2021-10-30 14:13:58 +13:00
import androidx.annotation.WorkerThread
2021-11-15 15:42:41 +13:00
import androidx.lifecycle.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong
2021-10-27 15:41:19 +13:00
2021-11-23 09:45:43 +13:00
class Repository(private val sharedPrefs: SharedPreferences, private val subscriptionDao: SubscriptionDao, private val notificationDao: NotificationDao) {
2021-11-15 15:42:41 +13:00
private val connectionStates = ConcurrentHashMap<Long, ConnectionState>()
private val connectionStatesLiveData = MutableLiveData(connectionStates)
val detailViewSubscriptionId = AtomicLong(0L) // Omg, what a hack ...
2021-11-15 15:42:41 +13:00
init {
Log.d(TAG, "Created $this")
}
fun getSubscriptionsLiveData(): LiveData<List<Subscription>> {
return subscriptionDao
.listFlow()
.asLiveData()
2021-11-15 15:42:41 +13:00
.combineWith(connectionStatesLiveData) { subscriptionsWithMetadata, _ ->
toSubscriptionList(subscriptionsWithMetadata.orEmpty())
}
}
fun getSubscriptionIdsWithInstantStatusLiveData(): LiveData<Set<Pair<Long, Boolean>>> {
2021-11-14 13:26:37 +13:00
return subscriptionDao
.listFlow()
.asLiveData()
.map { list -> list.map { Pair(it.id, it.instant) }.toSet() }
2021-11-14 13:26:37 +13:00
}
fun getSubscriptions(): List<Subscription> {
return toSubscriptionList(subscriptionDao.list())
2021-10-27 15:41:19 +13:00
}
2021-12-30 11:48:06 +13:00
fun getSubscriptionIdsWithInstantStatus(): Set<Pair<Long, Boolean>> {
return subscriptionDao
.list()
.map { Pair(it.id, it.instant) }.toSet()
}
@Suppress("RedundantSuspendModifier")
@WorkerThread
suspend fun getSubscription(subscriptionId: Long): Subscription? {
return toSubscription(subscriptionDao.get(subscriptionId))
}
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
}
@Suppress("RedundantSuspendModifier")
@WorkerThread
suspend fun updateSubscription(subscription: Subscription) {
subscriptionDao.update(subscription)
}
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-23 09:45:43 +13:00
return notificationDao.listFlow(subscriptionId).asLiveData()
}
fun clearAllNotificationIds(subscriptionId: Long) {
return notificationDao.clearAllNotificationIds(subscriptionId)
2021-11-01 08:19:25 +13:00
}
2021-11-15 14:22:02 +13:00
fun getNotification(notificationId: String): Notification? {
return notificationDao.get(notificationId)
}
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
suspend fun addNotification(notification: Notification): Boolean {
val maybeExistingNotification = notificationDao.get(notification.id)
if (maybeExistingNotification != null) {
return false
}
notificationDao.add(notification)
return true
2021-11-23 09:45:43 +13:00
}
2021-11-01 08:19:25 +13:00
@Suppress("RedundantSuspendModifier")
@WorkerThread
suspend fun markAsDeleted(notificationId: String) {
notificationDao.markAsDeleted(notificationId)
}
fun markAllAsDeleted(subscriptionId: Long) {
notificationDao.markAllAsDeleted(subscriptionId)
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-23 09:45:43 +13:00
fun getPollWorkerVersion(): Int {
return sharedPrefs.getInt(SHARED_PREFS_POLL_WORKER_VERSION, 0)
}
fun setPollWorkerVersion(version: Int) {
sharedPrefs.edit()
.putInt(SHARED_PREFS_POLL_WORKER_VERSION, version)
.apply()
}
2021-12-14 14:54:36 +13:00
fun getAutoRestartWorkerVersion(): Int {
return sharedPrefs.getInt(SHARED_PREFS_AUTO_RESTART_WORKER_VERSION, 0)
}
fun setAutoRestartWorkerVersion(version: Int) {
sharedPrefs.edit()
.putInt(SHARED_PREFS_AUTO_RESTART_WORKER_VERSION, version)
.apply()
}
2021-11-23 09:45:43 +13:00
private suspend fun isMuted(subscriptionId: Long): Boolean {
if (isGlobalMuted()) {
return true
}
val s = getSubscription(subscriptionId) ?: return true
return s.mutedUntil == 1L || (s.mutedUntil > 1L && s.mutedUntil > System.currentTimeMillis()/1000)
}
fun isGlobalMuted(): Boolean {
2021-11-23 09:45:43 +13:00
val mutedUntil = getGlobalMutedUntil()
return mutedUntil == 1L || (mutedUntil > 1L && mutedUntil > System.currentTimeMillis()/1000)
}
fun getGlobalMutedUntil(): Long {
return sharedPrefs.getLong(SHARED_PREFS_MUTED_UNTIL_TIMESTAMP, 0L)
}
fun setGlobalMutedUntil(mutedUntilTimestamp: Long) {
sharedPrefs.edit()
.putLong(SHARED_PREFS_MUTED_UNTIL_TIMESTAMP, mutedUntilTimestamp)
.apply()
}
fun checkGlobalMutedUntil(): Boolean {
val mutedUntil = sharedPrefs.getLong(SHARED_PREFS_MUTED_UNTIL_TIMESTAMP, 0L)
val expired = mutedUntil > 1L && System.currentTimeMillis()/1000 > mutedUntil
if (expired) {
sharedPrefs.edit()
.putLong(SHARED_PREFS_MUTED_UNTIL_TIMESTAMP, 0L)
.apply()
return true
}
return false
}
private fun toSubscriptionList(list: List<SubscriptionWithMetadata>): List<Subscription> {
return list.map { s ->
2021-11-15 15:42:41 +13:00
val connectionState = connectionStates.getOrElse(s.id) { ConnectionState.NOT_APPLICABLE }
Subscription(
id = s.id,
baseUrl = s.baseUrl,
topic = s.topic,
2021-11-14 13:26:37 +13:00
instant = s.instant,
2021-11-22 08:54:13 +13:00
mutedUntil = s.mutedUntil,
2021-12-30 08:33:17 +13:00
upAppId = s.upAppId,
upConnectorToken = s.upConnectorToken,
totalCount = s.totalCount,
newCount = s.newCount,
lastActive = s.lastActive,
2021-11-15 15:42:41 +13:00
state = connectionState
)
}
}
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-22 08:54:13 +13:00
mutedUntil = s.mutedUntil,
2021-12-30 08:33:17 +13:00
upAppId = s.upAppId,
upConnectorToken = s.upConnectorToken,
totalCount = s.totalCount,
newCount = s.newCount,
lastActive = s.lastActive,
2021-11-15 15:42:41 +13:00
state = getState(s.id)
)
}
2021-11-17 08:08:52 +13:00
fun updateState(subscriptionIds: Collection<Long>, newState: ConnectionState) {
var changed = false
subscriptionIds.forEach { subscriptionId ->
val state = connectionStates.getOrElse(subscriptionId) { ConnectionState.NOT_APPLICABLE }
if (state !== newState) {
changed = true
if (newState == ConnectionState.NOT_APPLICABLE) {
connectionStates.remove(subscriptionId)
} else {
connectionStates[subscriptionId] = newState
}
2021-11-15 15:42:41 +13:00
}
2021-11-17 08:08:52 +13:00
}
if (changed) {
2021-11-15 15:42:41 +13:00
connectionStatesLiveData.postValue(connectionStates)
}
}
private fun getState(subscriptionId: Long): ConnectionState {
return connectionStatesLiveData.value!!.getOrElse(subscriptionId) { ConnectionState.NOT_APPLICABLE }
}
2021-10-27 13:34:09 +13:00
companion object {
2021-11-23 09:45:43 +13:00
const val SHARED_PREFS_ID = "MainPreferences"
const val SHARED_PREFS_POLL_WORKER_VERSION = "PollWorkerVersion"
2021-12-14 14:54:36 +13:00
const val SHARED_PREFS_AUTO_RESTART_WORKER_VERSION = "AutoRestartWorkerVersion"
2021-11-23 09:45:43 +13:00
const val SHARED_PREFS_MUTED_UNTIL_TIMESTAMP = "MutedUntil"
2021-11-15 15:42:41 +13:00
private const 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-23 09:45:43 +13:00
fun getInstance(sharedPrefs: SharedPreferences, subscriptionDao: SubscriptionDao, notificationDao: NotificationDao): Repository {
2021-10-27 14:44:12 +13:00
return synchronized(Repository::class) {
2021-11-23 09:45:43 +13:00
val newInstance = instance ?: Repository(sharedPrefs, subscriptionDao, notificationDao)
2021-10-27 13:34:09 +13:00
instance = newInstance
newInstance
}
}
}
}
2021-11-15 15:42:41 +13:00
/* https://stackoverflow.com/a/57079290/1440785 */
fun <T, K, R> LiveData<T>.combineWith(
liveData: LiveData<K>,
block: (T?, K?) -> R
): LiveData<R> {
val result = MediatorLiveData<R>()
result.addSource(this) {
result.value = block(this.value, liveData.value)
}
result.addSource(liveData) {
result.value = block(this.value, liveData.value)
}
return result
}