2021-10-28 05:04:14 +02:00
|
|
|
package io.heckel.ntfy.ui
|
2021-10-28 01:50:44 +02:00
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2021-10-30 03:13:58 +02:00
|
|
|
import androidx.lifecycle.viewModelScope
|
2021-10-28 01:50:44 +02:00
|
|
|
import io.heckel.ntfy.data.*
|
2021-10-30 03:13:58 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
2021-10-28 01:50:44 +02:00
|
|
|
import kotlin.collections.List
|
|
|
|
|
2021-10-30 03:13:58 +02:00
|
|
|
class SubscriptionsViewModel(private val repository: Repository) : ViewModel() {
|
2021-10-28 01:50:44 +02:00
|
|
|
fun list(): LiveData<List<Subscription>> {
|
2021-11-12 01:41:29 +01:00
|
|
|
return repository.getSubscriptionsLiveData()
|
2021-10-28 01:50:44 +02:00
|
|
|
}
|
|
|
|
|
2021-11-14 01:26:37 +01:00
|
|
|
fun listIds(): LiveData<Set<Long>> {
|
|
|
|
return repository.getSubscriptionIdsLiveData()
|
|
|
|
}
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
fun add(subscription: Subscription) = viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
repository.addSubscription(subscription)
|
2021-10-28 01:50:44 +02:00
|
|
|
}
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
fun remove(subscriptionId: Long) = viewModelScope.launch(Dispatchers.IO) {
|
2021-11-12 01:41:29 +01:00
|
|
|
repository.removeAllNotifications(subscriptionId)
|
2021-10-31 20:19:25 +01:00
|
|
|
repository.removeSubscription(subscriptionId)
|
2021-10-28 01:50:44 +02:00
|
|
|
}
|
2021-11-01 13:58:12 +01:00
|
|
|
|
|
|
|
suspend fun get(baseUrl: String, topic: String): Subscription? {
|
|
|
|
return repository.getSubscription(baseUrl, topic)
|
|
|
|
}
|
2021-10-28 01:50:44 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 03:13:58 +02:00
|
|
|
class SubscriptionsViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
|
2021-10-28 01:50:44 +02:00
|
|
|
@Suppress("UNCHECKED_CAST")
|
|
|
|
override fun <T : ViewModel?> create(modelClass: Class<T>) =
|
|
|
|
with(modelClass){
|
|
|
|
when {
|
2021-10-30 03:13:58 +02:00
|
|
|
isAssignableFrom(SubscriptionsViewModel::class.java) -> SubscriptionsViewModel(repository) as T
|
2021-10-28 01:50:44 +02:00
|
|
|
else -> throw IllegalArgumentException("Unknown viewModel class $modelClass")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|