Skip to content

Commit 02d7c41

Browse files
authored
README tweaks (#50)
1 parent 47a03bd commit 02d7c41

File tree

1 file changed

+41
-119
lines changed

1 file changed

+41
-119
lines changed

README.md

Lines changed: 41 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -12,145 +12,75 @@ Please follow the directions at [Jitpack.io](https://jitpack.io/#Constructor-io/
1212

1313
You can find this in your [Constructor.io dashboard](https://constructor.io/dashboard). Contact sales if you'd like to sign up, or support if you believe your company already has an account.
1414

15-
## 3. Implement the Autocomplete UI
16-
17-
In your Application class add the following code with your key:
15+
## 3. Create a Client Instance
1816

1917
```kotlin
20-
override fun onCreate() {
21-
super.onCreate()
22-
ConstructorIo.init(this, "YOUR API KEY")
23-
24-
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_suggestions) as SuggestionsFragment
25-
fragment.setConstructorListener(object : ConstructorListener {
26-
override fun onSuggestionSelected(term: String, group: Group?, autocompleteSection: String?) {
27-
Log.d(TAG, "onSuggestionSelected")
28-
}
29-
30-
override fun onQuerySentToServer(query: String) {
31-
Log.d(TAG, "onQuerySentToServer")
32-
}
33-
34-
override fun onSuggestionsRetrieved(suggestions: List<Suggestion>) {
35-
Log.d(TAG, "onSuggestionsRetrieved")
36-
}
37-
38-
override fun onErrorGettingSuggestions(error: Throwable) {
39-
Log.d(TAG, "handle network error getting suggestion")
40-
}
41-
})
42-
}
43-
```
44-
45-
### Selecting Results
46-
To respond to a user selecting an autocomplete result, use the `onSuggestionSelected` method. If the autocomplete result has both a suggested term to search for and a group to search within (as in Apples in Juice Drinks), the group will be passed into the method.
47-
48-
### Performing Searches
49-
To respond to a user performing a search (instead of selecting an autocomplete result), use the `onQuerySentToServer` method.
50-
51-
## 4. Customize the Autocomplete UI
18+
import io.constructor.core.ConstructorIo
19+
import io.constructor.core.ConstructorIoConfig
5220

53-
### Using the Default UI
21+
// Create the client config
22+
let config = ConstructorIoConfig("YOUR API KEY")
5423

55-
To use the default, out-of-the-box UI, add the Sample Suggestions Fragment to your layout:
24+
// Create the client instance
25+
ConstructorIo.init(this, config)
5626

57-
```xml
58-
<fragment
59-
android:id="@+id/fragment_suggestions"
60-
android:name="io.constructor.sample.SuggestionsFragment"
61-
android:layout_width="match_parent"
62-
android:layout_height="match_parent"/>
27+
// Set the user ID (if a logged in and known user) for cross device personalization
28+
ConstructorIo.userId = "uid"
6329
```
6430

65-
### Using a Custom UI
66-
67-
To fully customize the UI, extend the `BaseSuggestionFragment` and the `BaseSuggestionsAdapter`
31+
## 4. Request Autocomplete Results
6832

6933
```kotlin
70-
class CustomSearchFragment : BaseSuggestionFragment() {
71-
72-
override fun onActivityCreated(savedInstanceState: Bundle?) {
73-
super.onActivityCreated(savedInstanceState)
74-
view?.backButton?.setOnClickListener {
75-
view?.input?.text?.clear()
76-
clearSuggestions()
77-
}
78-
view?.searchButton?.setOnClickListener { triggerSearch() }
79-
}
80-
81-
// Return your custom adapter
82-
override fun getSuggestionAdapter(): BaseSuggestionsAdapter {
83-
return CustomSuggestionsAdapter()
84-
}
85-
86-
// Return your id of the suggestion input field
87-
override fun getSuggestionsInputId(): Int {
88-
return R.id.input
34+
ConstructorIo.getAutocompleteResults("Dav")
35+
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
36+
.subscribe {
37+
it.onValue {
38+
it?.let {
39+
view.renderData(it)
8940
}
41+
}
42+
}
43+
```
9044

91-
// Return your id of the suggestion list
92-
override fun getSuggestionListId(): Int {
93-
return R.id.suggestions
94-
}
45+
## 5. Request Search Results
9546

96-
// Return your progress indicator id, used when request is being in progress. Return 0 for no progress
97-
override fun getProgressId(): Int {
98-
return 0
99-
}
100-
101-
// Return your custom layout resource id for the fragment
102-
override fun layoutId(): Int {
103-
return R.layout.fragment_custom_suggestions
47+
```kotlin
48+
var page = 1
49+
var perPage = 10
50+
var query = "Dave's bread"
51+
var selectedFacets: HashMap<String, MutableList<String>>? = null
52+
var selectedSortOption: SortOption? = null
53+
54+
ConstructorIo.getSearchResults(query, selectedFacets?.map { it.key to it.value }, page = page, perPage = limit, sortBy = selectedSortOption?.sortBy, sortOrder = selectedSortOption?.sortOrder)
55+
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
56+
.subscribe {
57+
it.onValue {
58+
it.searchData?.let {
59+
view.renderData(it)
10460
}
105-
61+
}
10662
}
63+
```
10764

108-
class CustomSuggestionsAdapter() : BaseSuggestionsAdapter() {
109-
110-
// Triggered when inflating an item which is a suggestion.
111-
override fun onViewTypeSuggestion(holder: ViewHolder, suggestion: String, highlightedSuggestion: Spannable, groupName: String?) {
112-
holder.suggestionName.text = highlightedSuggestion
113-
val spans = highlightedSuggestion.getSpans(0, highlightedSuggestion.length, StyleSpan::class.java)
114-
spans.forEach { highlightedSuggestion.setSpan(ForegroundColorSpan(Color.parseColor("#222222")), highlightedSuggestion.getSpanStart(it), highlightedSuggestion.getSpanEnd(it), 0) }
115-
groupName?.let { holder.suggestionGroupName.text = holder.suggestionGroupName.context.getString(R.string.suggestion_group, it) }
116-
}
117-
118-
// Return your custom adapter item layout id for suggestion
119-
override val itemLayoutId: Int
120-
get() = R.layout.item_suggestion
121-
122-
// Return your text view id - the text will be the suggestion.
123-
override val suggestionNameId: Int
124-
get() = R.id.suggestionName
125-
126-
// Return your text view id - the text will be the suggestion group name, if present
127-
override val suggestionGroupNameId: Int
128-
get() = R.id.suggestionGroupName
65+
## 6. Request Browse Events
12966

130-
}
67+
```kotlin
68+
// Coming end of October
13169
```
13270

133-
## 5. Instrument Behavioral Events
71+
## 7. Instrument Behavioral Events
13472

13573
The Android Client sends behavioral events to [Constructor.io](http://constructor.io/) in order to continuously learn and improve results for future Autosuggest and Search requests. The Client only sends events in response to being called by the consuming app or in response to user interaction . For example, if the consuming app never calls the SDK code, no events will be sent. Besides the explicitly passed in event parameters, all user events contain a GUID based user ID that the client sets to identify the user as well as a session ID.
13674

13775
Three types of these events exist:
13876

13977
1. **General Events** are sent as needed when an instance of the Client is created or initialized
140-
1. **Autocomplete Events** measure user interaction with autocomplete results and extending from `BaseSuggestionFragment` sends them automatically.
141-
1. **Search Events** measure user interaction with search results and the consuming app has to explicitly instrument them itself
78+
1. **Autocomplete Events** measure user interaction with autocomplete results
79+
1. **Search Events** measure user interaction with search results
14280

14381
### Autocomplete Events
14482

145-
If you decide to extend from the `BaseSuggestionFragment`, these events are sent automatically.
146-
14783
```kotlin
148-
import io.constructor.core.ConstructorIo
149-
import io.constructor.core.ConstructorIoConfig
150-
151-
val config = ConstructorIoConfig("pharmacy-api-key")
152-
ConstructorIo.init(this, config)
153-
15484
// Track when the user focuses into the search bar (searchTerm)
15585
ConstructorIo.trackInputFocus("")
15686

@@ -163,15 +93,7 @@ ConstructorIo.trackSearchSubmit("toothpicks", "tooth")
16393

16494
### Search Events
16595

166-
These events should be sent manually by the consuming app.
167-
16896
```kotlin
169-
import io.constructor.core.ConstructorIo
170-
import io.constructor.core.ConstructorIoConfig
171-
172-
val config = ConstructorIoConfig("pharmacy-api-key")
173-
ConstructorIo.init(this, config)
174-
17597
// Track when search results are loaded into view (searchTerm, resultCount)
17698
ConstructorIo.trackSearchResultsLoaded("tooth", 789)
17799

0 commit comments

Comments
 (0)