Skip to content

Commit

Permalink
Merge pull request #199 from Bible-Translation-Tools/mm-languages-list
Browse files Browse the repository at this point in the history
properly sort and filter languages on Languages page (#199)
  • Loading branch information
mXaln authored May 20, 2024
2 parents f379bed + 15059d5 commit 3e9eadb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,58 @@ class FetchLanguageViewData(
private const val MATCHING_RESULT_TAKE = 20
}

private val comparator = compareBy(LanguageViewData::isGateway)
.then(compareByDescending { it.url != null })

fun getViewDataList(
currentPath: String
): List<LanguageViewData> {
val languages = languageRepo.getAll()

val listViewData = languages.sortedBy { it.isGateway }.map {
val available = storage.hasLanguageContent(it.code)

LanguageViewData(
code = it.code,
anglicizedName = it.anglicizedName,
localizedName = it.localizedName,
url = if (available) {
"$currentPath/${it.code}"
} else {
null
}
)
}

val availableLanguages = listViewData.filter { it.url != null }
val unavailableLanguages = listViewData.filter { it.url == null }

val allLanguages = availableLanguages + unavailableLanguages
return languages
.map {
val available = storage.hasLanguageContent(it.code)

return allLanguages.take(DISPLAY_ITEMS_LIMIT)
LanguageViewData(
code = it.code,
anglicizedName = it.anglicizedName,
localizedName = it.localizedName,
isGateway = it.isGateway,
url = if (available) {
"$currentPath/${it.code}"
} else {
null
}
)
}
.sortedWith(comparator)
.take(DISPLAY_ITEMS_LIMIT)
}

fun filterLanguages(
query: String,
currentPath: String,
currentIndex: Int = 0
): List<LanguageViewData> {
val result = getMatchingLanguages(query, languageRepo.getAll().sortedBy { it.isGateway })

return result
.drop(currentIndex)
.take(DISPLAY_ITEMS_LIMIT)
return getMatchingLanguages(query, languageRepo.getAll())
.map {
val available = storage.hasLanguageContent(it.code)

LanguageViewData(
code = it.code,
anglicizedName = it.anglicizedName,
localizedName = it.localizedName,
isGateway = it.isGateway,
url = if (available) {
"$currentPath/${it.code}"
} else {
null
}
)
}
.sortedWith(comparator)
.drop(currentIndex)
.take(DISPLAY_ITEMS_LIMIT)
}

fun loadMoreLanguages(
Expand All @@ -74,25 +74,25 @@ class FetchLanguageViewData(
): List<LanguageViewData> {
if (currentIndex < 0) return listOf()

// load more (default) is only applied to HL
val languages = languageRepo.getAll().sortedBy { it.isGateway }

return languages
.drop(currentIndex + DISPLAY_ITEMS_LIMIT - 1)
.take(DISPLAY_ITEMS_LIMIT)
return languageRepo.getAll()
.map {
val available = storage.hasLanguageContent(it.code)

LanguageViewData(
code = it.code,
anglicizedName = it.anglicizedName,
localizedName = it.localizedName,
isGateway = it.isGateway,
url = if (available) {
"$currentPath/${it.code}"
} else {
null
}
)
}
.sortedWith(comparator)
.drop(currentIndex + DISPLAY_ITEMS_LIMIT)
.take(DISPLAY_ITEMS_LIMIT)
}

private fun getMatchingLanguages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ data class LanguageViewData(
val code: String,
val anglicizedName: String,
val localizedName: String,
val isGateway: Boolean,
val url: String?
)

0 comments on commit 3e9eadb

Please sign in to comment.