|
|
|
@ -21,7 +21,7 @@ import androidx.lifecycle.viewModelScope
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.util.SyncStatusMonitor
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.GetFollowedUserNewsResourcesUseCase
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
|
|
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
|
|
@ -29,8 +29,6 @@ import kotlinx.coroutines.flow.Flow
|
|
|
|
|
import kotlinx.coroutines.flow.SharingStarted
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
|
import kotlinx.coroutines.flow.flatMapLatest
|
|
|
|
|
import kotlinx.coroutines.flow.flowOf
|
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
|
import kotlinx.coroutines.flow.stateIn
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
@ -40,7 +38,7 @@ import javax.inject.Inject
|
|
|
|
|
class ForYouViewModel @Inject constructor(
|
|
|
|
|
syncStatusMonitor: SyncStatusMonitor,
|
|
|
|
|
private val userDataRepository: UserDataRepository,
|
|
|
|
|
private val getSaveableNewsResources: GetUserNewsResourcesUseCase,
|
|
|
|
|
getFollowedUserNewsResources: GetFollowedUserNewsResourcesUseCase,
|
|
|
|
|
getFollowableTopics: GetFollowableTopicsUseCase,
|
|
|
|
|
) : ViewModel() {
|
|
|
|
|
|
|
|
|
@ -55,26 +53,9 @@ class ForYouViewModel @Inject constructor(
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
val feedState: StateFlow<NewsFeedUiState> =
|
|
|
|
|
userDataRepository.userData
|
|
|
|
|
.map { userData ->
|
|
|
|
|
// If the user hasn't completed the onboarding and hasn't selected any interests
|
|
|
|
|
// show an empty news list to clearly demonstrate that their selections affect the
|
|
|
|
|
// news articles they will see.
|
|
|
|
|
if (!userData.shouldHideOnboarding &&
|
|
|
|
|
userData.followedTopics.isEmpty()
|
|
|
|
|
) {
|
|
|
|
|
flowOf(NewsFeedUiState.Success(emptyList()))
|
|
|
|
|
} else {
|
|
|
|
|
getSaveableNewsResources(
|
|
|
|
|
filterTopicIds = userData.followedTopics,
|
|
|
|
|
)
|
|
|
|
|
.map<List<UserNewsResource>, NewsFeedUiState>(NewsFeedUiState::Success)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Flatten the feed flows.
|
|
|
|
|
// As the selected topics and topic state changes, this will cancel the old feed
|
|
|
|
|
// monitoring and start the new one.
|
|
|
|
|
.flatMapLatest { it }
|
|
|
|
|
getFollowedUserNewsResources().map {
|
|
|
|
|
NewsFeedUiState.Success(it)
|
|
|
|
|
}
|
|
|
|
|
.stateIn(
|
|
|
|
|
scope = viewModelScope,
|
|
|
|
|
started = SharingStarted.WhileSubscribed(5_000),
|
|
|
|
|