Some checks failed
CI - Multi-Platform Native / Build iOS (RSSuper) (push) Has been cancelled
CI - Multi-Platform Native / Build macOS (push) Has been cancelled
CI - Multi-Platform Native / Build Android (push) Has been cancelled
CI - Multi-Platform Native / Build Linux (push) Has been cancelled
CI - Multi-Platform Native / Build Summary (push) Has been cancelled
68 lines
2.3 KiB
Kotlin
68 lines
2.3 KiB
Kotlin
package com.rssuper.viewmodel
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
import androidx.lifecycle.viewModelScope
|
|
import com.rssuper.repository.FeedRepository
|
|
import com.rssuper.state.State
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
import kotlinx.coroutines.launch
|
|
|
|
class FeedViewModel(
|
|
private val feedRepository: FeedRepository
|
|
) : ViewModel() {
|
|
|
|
private val _feedState = MutableStateFlow<State<List<com.rssuper.database.entities.FeedItemEntity>>>(State.Idle)
|
|
val feedState: StateFlow<State<List<com.rssuper.database.entities.FeedItemEntity>>> = _feedState.asStateFlow()
|
|
|
|
private val _unreadCount = MutableStateFlow<State<Int>>(State.Idle)
|
|
val unreadCount: StateFlow<State<Int>> = _unreadCount.asStateFlow()
|
|
|
|
fun loadFeedItems(subscriptionId: String? = null) {
|
|
viewModelScope.launch {
|
|
feedRepository.getFeedItems(subscriptionId).collect { state ->
|
|
_feedState.value = state
|
|
}
|
|
}
|
|
}
|
|
|
|
fun loadUnreadCount(subscriptionId: String? = null) {
|
|
viewModelScope.launch {
|
|
_unreadCount.value = State.Loading
|
|
try {
|
|
val count = feedRepository.getUnreadCount(subscriptionId)
|
|
_unreadCount.value = State.Success(count)
|
|
} catch (e: Exception) {
|
|
_unreadCount.value = State.Error("Failed to load unread count", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
fun markAsRead(id: String, isRead: Boolean) {
|
|
viewModelScope.launch {
|
|
try {
|
|
feedRepository.markAsRead(id, isRead)
|
|
loadUnreadCount()
|
|
} catch (e: Exception) {
|
|
_unreadCount.value = State.Error("Failed to update read state", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
fun markAsStarred(id: String, isStarred: Boolean) {
|
|
viewModelScope.launch {
|
|
try {
|
|
feedRepository.markAsStarred(id, isStarred)
|
|
} catch (e: Exception) {
|
|
_feedState.value = State.Error("Failed to update starred state", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
fun refreshFeed(subscriptionId: String? = null) {
|
|
loadFeedItems(subscriptionId)
|
|
loadUnreadCount(subscriptionId)
|
|
}
|
|
}
|