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.Idle) val feedState: StateFlow>> = _feedState.asStateFlow() private val _unreadCount = MutableStateFlow>(State.Idle) val unreadCount: StateFlow> = _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) } }