Sort authors in onboarding by number of news resources

jv/author-item-improvements-sorting
Jolanda Verhoef 2 years ago
parent 58ffd8781a
commit e4a81eea78

@ -17,6 +17,7 @@
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.NewsRepository
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor
import javax.inject.Inject
@ -29,11 +30,12 @@ import kotlinx.coroutines.flow.map
* data.
*/
class GetPersistentSortedFollowableAuthorsStreamUseCase @Inject constructor(
newsRepository: NewsRepository,
authorsRepository: AuthorsRepository,
private val userDataRepository: UserDataRepository
) {
private val getSortedFollowableAuthorsStream =
GetSortedFollowableAuthorsStreamUseCase(authorsRepository)
GetSortedFollowableAuthorsStreamUseCase(newsRepository, authorsRepository)
/**
* Returns a list of authors with their associated followed state sorted alphabetically by name.

@ -17,33 +17,40 @@
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.NewsRepository
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor
import javax.inject.Inject
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
* state.
*/
class GetSortedFollowableAuthorsStreamUseCase @Inject constructor(
private val newsRepository: NewsRepository,
private val authorsRepository: AuthorsRepository
) {
/**
* 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>> {
return authorsRepository.getAuthorsStream().map { authors ->
return combine(
newsRepository.getNewsResourcesStream(),
authorsRepository.getAuthorsStream()
) { resources, authors ->
val authorResourceCount =
resources.flatMap { it.authors }.groupingBy { it.id }.eachCount()
authors
.sortedByDescending { authorResourceCount[it.id] }
.map { author ->
FollowableAuthor(
author = author,
isFollowed = author.id in followedAuthorIds
)
}
.sortedBy { it.author.name }
}
}
}

Loading…
Cancel
Save