e-ntfy-android/app/src/main/java/io/heckel/ntfy/ui/MainViewModel.kt

45 lines
1.5 KiB
Kotlin
Raw Normal View History

2021-10-28 05:04:14 +02:00
package io.heckel.ntfy.ui
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
2021-10-30 03:13:58 +02:00
import androidx.lifecycle.viewModelScope
import io.heckel.ntfy.data.*
2021-10-30 03:13:58 +02:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.collections.List
2021-10-30 03:13:58 +02:00
class SubscriptionsViewModel(private val repository: Repository) : ViewModel() {
fun list(): LiveData<List<Subscription>> {
return repository.getSubscriptionsLiveData()
}
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-31 20:19:25 +01:00
fun remove(subscriptionId: Long) = viewModelScope.launch(Dispatchers.IO) {
repository.removeAllNotifications(subscriptionId)
2021-10-31 20:19:25 +01:00
repository.removeSubscription(subscriptionId)
}
suspend fun get(baseUrl: String, topic: String): Subscription? {
return repository.getSubscription(baseUrl, topic)
}
}
2021-10-30 03:13:58 +02:00
class SubscriptionsViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
@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
else -> throw IllegalArgumentException("Unknown viewModel class $modelClass")
}
}
}