|
|
@ -17,33 +17,40 @@
|
|
|
|
package com.google.samples.apps.nowinandroid.core.domain
|
|
|
|
package com.google.samples.apps.nowinandroid.core.domain
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.repository.AuthorsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.repository.AuthorsRepository
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor
|
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Inject
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* A use case which obtains a list of authors sorted alphabetically by name with their followed
|
|
|
|
* A use case which obtains a list of authors sorted alphabetically by name with their followed
|
|
|
|
* state.
|
|
|
|
* state.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class GetSortedFollowableAuthorsStreamUseCase @Inject constructor(
|
|
|
|
class GetSortedFollowableAuthorsStreamUseCase @Inject constructor(
|
|
|
|
|
|
|
|
private val newsRepository: NewsRepository,
|
|
|
|
private val authorsRepository: AuthorsRepository
|
|
|
|
private val authorsRepository: AuthorsRepository
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns a list of authors with their associated followed state sorted alphabetically by name.
|
|
|
|
* Returns a list of authors with their associated followed state sorted alphabetically by name.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param followedTopicIds - the set of topic ids which are currently being followed.
|
|
|
|
* @param followedAuthorIds - the set of topic ids which are currently being followed.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
operator fun invoke(followedAuthorIds: Set<String>): Flow<List<FollowableAuthor>> {
|
|
|
|
operator fun invoke(followedAuthorIds: Set<String>): Flow<List<FollowableAuthor>> {
|
|
|
|
return authorsRepository.getAuthorsStream().map { authors ->
|
|
|
|
return combine(
|
|
|
|
|
|
|
|
newsRepository.getNewsResourcesStream(),
|
|
|
|
|
|
|
|
authorsRepository.getAuthorsStream()
|
|
|
|
|
|
|
|
) { resources, authors ->
|
|
|
|
|
|
|
|
val authorResourceCount =
|
|
|
|
|
|
|
|
resources.flatMap { it.authors }.groupingBy { it.id }.eachCount()
|
|
|
|
authors
|
|
|
|
authors
|
|
|
|
|
|
|
|
.sortedByDescending { authorResourceCount[it.id] }
|
|
|
|
.map { author ->
|
|
|
|
.map { author ->
|
|
|
|
FollowableAuthor(
|
|
|
|
FollowableAuthor(
|
|
|
|
author = author,
|
|
|
|
author = author,
|
|
|
|
isFollowed = author.id in followedAuthorIds
|
|
|
|
isFollowed = author.id in followedAuthorIds
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.sortedBy { it.author.name }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|