Skip to content

Commit

Permalink
Remove tags with duplicate slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ashiagr committed Jul 23, 2020
1 parent f16419d commit 2843b0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class ReaderInterestsFragment : Fragment(R.layout.reader_interests_fragment_layo

private fun updateInterests(interestsUiState: List<InterestUiState>) {
interestsUiState.forEachIndexed { index, interestTagUiState ->
val chip = interests_chip_group.findViewWithTag(interestTagUiState.title)
?: createChipView(interestTagUiState.title, index)
val chip = interests_chip_group.findViewWithTag(interestTagUiState.slug)
?: createChipView(interestTagUiState.slug, index)
with(chip) {
text = interestTagUiState.title
isChecked = interestTagUiState.isChecked
Expand All @@ -109,14 +109,14 @@ class ReaderInterestsFragment : Fragment(R.layout.reader_interests_fragment_layo
}
}

private fun createChipView(titleTag: String, index: Int): Chip {
private fun createChipView(slug: String, index: Int): Chip {
val chip = layoutInflater.inflate(
R.layout.reader_interest_filter_chip,
interests_chip_group,
false
) as Chip
with(chip) {
tag = titleTag
tag = slug
setOnCheckedChangeListener { compoundButton, isChecked ->
if (compoundButton.isPressed) {
viewModel.onInterestAtIndexToggled(index, isChecked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ class ReaderInterestsViewModel @Inject constructor(
viewModelScope.launch {
val newUiState: UiState? = when (val result = readerTagRepository.getInterests()) {
is SuccessWithData<*> -> {
val tags = result.data as ReaderTagList
val tags = (result.data as ReaderTagList).distinctBy { it.tagSlug }
val distinctTags = ReaderTagList().apply { addAll(tags) }
ContentUiState(
interestsUiState = transformToInterestsUiState(tags),
interests = tags
interestsUiState = transformToInterestsUiState(distinctTags),
interests = distinctTags
)
}
is NetworkUnavailable -> {
Expand Down Expand Up @@ -104,7 +105,7 @@ class ReaderInterestsViewModel @Inject constructor(

private fun transformToInterestsUiState(interests: ReaderTagList) =
interests.map { interest ->
InterestUiState(interest.tagTitle)
InterestUiState(interest.tagTitle, interest.tagSlug)
}

private fun getUpdatedInterestsUiState(index: Int, isChecked: Boolean): List<InterestUiState> {
Expand Down Expand Up @@ -196,6 +197,7 @@ class ReaderInterestsViewModel @Inject constructor(

data class InterestUiState(
val title: String,
val slug: String,
val isChecked: Boolean = false
)

Expand Down

0 comments on commit 2843b0f

Please sign in to comment.