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 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.data.repository.UserDataRepository import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
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
@ -29,11 +30,12 @@ import kotlinx.coroutines.flow.map
* data. * data.
*/ */
class GetPersistentSortedFollowableAuthorsStreamUseCase @Inject constructor( class GetPersistentSortedFollowableAuthorsStreamUseCase @Inject constructor(
newsRepository: NewsRepository,
authorsRepository: AuthorsRepository, authorsRepository: AuthorsRepository,
private val userDataRepository: UserDataRepository private val userDataRepository: UserDataRepository
) { ) {
private val getSortedFollowableAuthorsStream = private val getSortedFollowableAuthorsStream =
GetSortedFollowableAuthorsStreamUseCase(authorsRepository) GetSortedFollowableAuthorsStreamUseCase(newsRepository, 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.

@ -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 }
} }
} }
} }

Loading…
Cancel
Save