Skip to content

Commit

Permalink
Release 2.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Jul 22, 2020
2 parents d92373f + 990225c commit aa2866d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.5.1

# Fixed
- Filter unexpected items from the hierarchical tree results

# 2.5.0

## Added
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Add this proguard rule to solve it.
-keep class com.algolia.search.model.** { *; }
```

## Troubleshooting

Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/guides/building-search-ui/troubleshooting/faq/android/) where you will find answers for the most common issues and gotchas with the library.

# License

InstantSearch Android is [licensed under Apache V2](LICENSE).
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/dependency/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Library: Dependency {

override val group = "com.algolia"
override val artifact = "instantsearch"
override val version = "2.5.0"
override val version = "2.5.1"

val packageName = "$group:$artifact-android"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.algolia.instantsearch.core.connection.ConnectionImpl
import com.algolia.instantsearch.helper.searcher.SearcherSingleIndex
import com.algolia.instantsearch.helper.searcher.addFacet
import com.algolia.search.model.response.ResponseSearch
import com.algolia.search.model.search.Facet

internal data class HierarchicalConnectionSearcher(
private val viewModel: HierarchicalViewModel,
Expand All @@ -17,12 +18,33 @@ internal data class HierarchicalConnectionSearcher(
?: response.facetsOrNull?.filter { it.key == viewModel.hierarchicalAttributes.first() } ?: mapOf()

viewModel.tree.value = viewModel.hierarchicalAttributes
.mapNotNull { facets[it] }
.mapNotNull { facets[it]?.toMutableList() }
.filterUnprefixed()
.flatten()
.toNodes(selectedHierarchicalValue)
}
}

/**
* Removes results not matching the naming pattern.
* This is a workaround to remove unexpected categories in results.
*
* Let's consider an item with the following filters:
* Level 0: [Clothing, Top]
* Level 1: [Clothing > Men, Clothing > Women, Top > T-shirts]
*
* In case of selecting 'Clothing' the tree will be:
* Level 0: [Clothing, Furniture]
* Level 1: [Clothing > Men, Clothing > Women]
*/
private fun List<MutableList<Facet>>.filterUnprefixed(): List<MutableList<Facet>> {
viewModel.hierarchicalPath.value.forEachIndexed { index, (_, item) ->
getOrNull(index + 1)?. // Get next level (sub-category)
removeAll { !it.value.startsWith(item) } // Remove the items not respecting the prefix convention
}
return this
}

private val selectedHierarchicalValue: String?
get() {
val value = viewModel.selections.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public open class HierarchicalViewModel(
public val selections = SubscriptionValue<List<String>>(listOf())
public val eventHierarchicalPath = SubscriptionEvent<HierarchicalPath>()

internal val hierarchicalPath = SubscriptionValue<HierarchicalPath>(listOf())

init {
if (hierarchicalAttributes.isEmpty())
throw IllegalArgumentException("HierarchicalAttributes should not be empty")
Expand All @@ -37,10 +39,11 @@ public open class HierarchicalViewModel(
selections.getOrNull(index)?.let { item to it }
}.filterNotNull()

this.hierarchicalPath.value = hierarchicalPath
eventHierarchicalPath.send(hierarchicalPath)
}

private fun String.toSelectionList(): List<String> = split(separator).fold(listOf()) { acc, s ->
acc + if (acc.isEmpty()) s else acc.last() + separator + s
}
}
}

0 comments on commit aa2866d

Please sign in to comment.