restructure
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
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
This commit is contained in:
67
android/src/main/java/com/rssuper/viewmodel/FeedViewModel.kt
Normal file
67
android/src/main/java/com/rssuper/viewmodel/FeedViewModel.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.rssuper.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.rssuper.repository.SubscriptionRepository
|
||||
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 SubscriptionViewModel(
|
||||
private val subscriptionRepository: SubscriptionRepository
|
||||
) : ViewModel() {
|
||||
|
||||
private val _subscriptionsState = MutableStateFlow<State<List<com.rssuper.database.entities.SubscriptionEntity>>>(State.Idle)
|
||||
val subscriptionsState: StateFlow<State<List<com.rssuper.database.entities.SubscriptionEntity>>> = _subscriptionsState.asStateFlow()
|
||||
|
||||
private val _enabledSubscriptionsState = MutableStateFlow<State<List<com.rssuper.database.entities.SubscriptionEntity>>>(State.Idle)
|
||||
val enabledSubscriptionsState: StateFlow<State<List<com.rssuper.database.entities.SubscriptionEntity>>> = _enabledSubscriptionsState.asStateFlow()
|
||||
|
||||
fun loadAllSubscriptions() {
|
||||
viewModelScope.launch {
|
||||
subscriptionRepository.getAllSubscriptions().collect { state ->
|
||||
_subscriptionsState.value = state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun loadEnabledSubscriptions() {
|
||||
viewModelScope.launch {
|
||||
subscriptionRepository.getEnabledSubscriptions().collect { state ->
|
||||
_enabledSubscriptionsState.value = state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setEnabled(id: String, enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
subscriptionRepository.setEnabled(id, enabled)
|
||||
loadEnabledSubscriptions()
|
||||
} catch (e: Exception) {
|
||||
_enabledSubscriptionsState.value = State.Error("Failed to update subscription enabled state", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setError(id: String, error: String?) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
subscriptionRepository.setError(id, error)
|
||||
} catch (e: Exception) {
|
||||
_subscriptionsState.value = State.Error("Failed to set subscription error", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateLastFetchedAt(id: String, lastFetchedAt: Long) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
subscriptionRepository.updateLastFetchedAt(id, lastFetchedAt)
|
||||
} catch (e: Exception) {
|
||||
_subscriptionsState.value = State.Error("Failed to update last fetched time", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateNextFetchAt(id: String, nextFetchAt: Long) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
subscriptionRepository.updateNextFetchAt(id, nextFetchAt)
|
||||
} catch (e: Exception) {
|
||||
_subscriptionsState.value = State.Error("Failed to update next fetch time", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun refreshSubscriptions() {
|
||||
loadAllSubscriptions()
|
||||
loadEnabledSubscriptions()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user