Skip to content

Commit 35c8b87

Browse files
authored
[CH:XXX] Model Update For Consistency (#52)
* Main refactor to adhere to expectations in Java SDK
1 parent 6166dbd commit 35c8b87

File tree

78 files changed

+403
-411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+403
-411
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ConstructorIo.getSearchResults(query, selectedFacets?.map { it.key to it.value }
5555
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
5656
.subscribe {
5757
it.onValue {
58-
it.searchData?.let {
58+
it.response?.let {
5959
view.renderData(it)
6060
}
6161
}
@@ -76,7 +76,7 @@ ConstructorIo.getBrowseResults(filterName, filterValue, selectedFacets?.map { it
7676
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
7777
.subscribe {
7878
it.onValue {
79-
it.browseData?.let {
79+
it.response?.let {
8080
view.renderData(it)
8181
}
8282
}

library/src/main/java/io/constructor/core/ConstructorIo.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import io.constructor.data.ConstructorData
66
import io.constructor.data.DataManager
77
import io.constructor.data.local.PreferencesHelper
88
import io.constructor.data.memory.ConfigMemoryHolder
9-
import io.constructor.data.model.Group
10-
import io.constructor.data.model.Suggestion
9+
import io.constructor.data.model.autocomplete.AutocompleteResponse
10+
import io.constructor.data.model.common.Result
11+
import io.constructor.data.model.common.ResultGroup
1112
import io.constructor.data.model.search.SearchResponse
1213
import io.constructor.data.model.browse.BrowseResponse
1314
import io.constructor.injection.component.AppComponent
@@ -96,7 +97,7 @@ object ConstructorIo {
9697
/**
9798
* Returns a list of autocomplete suggestions
9899
*/
99-
fun getAutocompleteResults(query: String): Observable<ConstructorData<List<Suggestion>?>> {
100+
fun getAutocompleteResults(query: String): Observable<ConstructorData<AutocompleteResponse>> {
100101
val params = mutableListOf<Pair<String, String>>()
101102
configMemoryHolder.autocompleteResultCount?.entries?.forEach {
102103
params.add(Pair(Constants.QueryConstants.NUM_RESULTS+it.key, it.value.toString()))
@@ -156,19 +157,19 @@ object ConstructorIo {
156157
/**
157158
* Tracks autocomplete select events
158159
*/
159-
fun trackAutocompleteSelect(searchTerm: String, originalQuery: String, sectionName: String, group: Group? = null, resultID: String? = null) {
160-
var completable = trackAutocompleteSelectInternal(searchTerm, originalQuery, sectionName, group, resultID);
160+
fun trackAutocompleteSelect(searchTerm: String, originalQuery: String, sectionName: String, resultGroup: ResultGroup? = null, resultID: String? = null) {
161+
var completable = trackAutocompleteSelectInternal(searchTerm, originalQuery, sectionName, resultGroup, resultID);
161162
disposable.add(completable.subscribeOn(Schedulers.io()).subscribe({
162163
context.broadcastIntent(Constants.EVENT_QUERY_SENT, Constants.EXTRA_TERM to searchTerm)
163164
}, {
164165
t -> e("Autocomplete Select error: ${t.message}")
165166
}))
166167
}
167-
internal fun trackAutocompleteSelectInternal(searchTerm: String, originalQuery: String, sectionName: String, group: Group? = null, resultID: String? = null): Completable {
168+
internal fun trackAutocompleteSelectInternal(searchTerm: String, originalQuery: String, sectionName: String, resultGroup: ResultGroup? = null, resultID: String? = null): Completable {
168169
preferenceHelper.getSessionId(sessionIncrementHandler)
169170
val encodedParams: ArrayList<Pair<String, String>> = arrayListOf()
170-
group?.groupId?.let { encodedParams.add(Constants.QueryConstants.GROUP_ID.urlEncode() to it) }
171-
group?.displayName?.let { encodedParams.add(Constants.QueryConstants.GROUP_DISPLAY_NAME.urlEncode() to it.urlEncode()) }
171+
resultGroup?.groupId?.let { encodedParams.add(Constants.QueryConstants.GROUP_ID.urlEncode() to it) }
172+
resultGroup?.displayName?.let { encodedParams.add(Constants.QueryConstants.GROUP_DISPLAY_NAME.urlEncode() to it.urlEncode()) }
172173
resultID?.let { encodedParams.add(Constants.QueryConstants.RESULT_ID.urlEncode() to it.urlEncode()) }
173174
return dataManager.trackAutocompleteSelect(searchTerm, arrayOf(
174175
Constants.QueryConstants.AUTOCOMPLETE_SECTION to sectionName,
@@ -180,19 +181,19 @@ object ConstructorIo {
180181
/**
181182
* Tracks search submit events
182183
*/
183-
fun trackSearchSubmit(searchTerm: String, originalQuery: String, group: Group?) {
184-
var completable = trackSearchSubmitInternal(searchTerm, originalQuery, group)
184+
fun trackSearchSubmit(searchTerm: String, originalQuery: String, resultGroup: ResultGroup?) {
185+
var completable = trackSearchSubmitInternal(searchTerm, originalQuery, resultGroup)
185186
disposable.add(completable.subscribeOn(Schedulers.io()).subscribe({
186187
context.broadcastIntent(Constants.EVENT_QUERY_SENT, Constants.EXTRA_TERM to searchTerm)
187188
}, {
188189
t -> e("Search Submit error: ${t.message}")
189190
}))
190191
}
191-
internal fun trackSearchSubmitInternal(searchTerm: String, originalQuery: String, group: Group?): Completable {
192+
internal fun trackSearchSubmitInternal(searchTerm: String, originalQuery: String, resultGroup: ResultGroup?): Completable {
192193
preferenceHelper.getSessionId(sessionIncrementHandler)
193194
val encodedParams: ArrayList<Pair<String, String>> = arrayListOf()
194-
group?.groupId?.let { encodedParams.add(Constants.QueryConstants.GROUP_ID.urlEncode() to it) }
195-
group?.displayName?.let { encodedParams.add(Constants.QueryConstants.GROUP_DISPLAY_NAME.urlEncode() to it.urlEncode()) }
195+
resultGroup?.groupId?.let { encodedParams.add(Constants.QueryConstants.GROUP_ID.urlEncode() to it) }
196+
resultGroup?.displayName?.let { encodedParams.add(Constants.QueryConstants.GROUP_DISPLAY_NAME.urlEncode() to it.urlEncode()) }
196197
return dataManager.trackSearchSubmit(searchTerm, arrayOf(
197198
Constants.QueryConstants.ORIGINAL_QUERY to originalQuery,
198199
Constants.QueryConstants.EVENT to Constants.QueryValues.EVENT_SEARCH
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package io.constructor.core
22

3-
import io.constructor.data.model.Group
4-
import io.constructor.data.model.Suggestion
5-
3+
import io.constructor.data.model.common.ResultGroup
4+
import io.constructor.data.model.common.Result
65

76
interface ConstructorListener {
8-
fun onSuggestionSelected(term: String, group: Group?, autocompleteSection: String?)
7+
fun onSuggestionSelected(term: String, resultGroup: ResultGroup?, autocompleteSection: String?)
98
fun onQuerySentToServer(query: String)
10-
fun onSuggestionsRetrieved(suggestions: List<Suggestion>)
9+
fun onSuggestionsRetrieved(suggestions: List<Result>)
1110
fun onErrorGettingSuggestions(error: Throwable)
1211
}

library/src/main/java/io/constructor/core/SuggestionListener.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package io.constructor.core
22

3-
import io.constructor.data.model.Suggestion
4-
3+
import io.constructor.data.model.common.Result
54

65
interface SuggestionListener {
76
/**
87
* triggered after getting suggestions
98
* @param suggestions list of suggestions
109
*/
11-
fun onSuggestionsResult(suggestions: List<Suggestion>)
10+
fun onSuggestionsResult(suggestions: List<Result>)
1211

1312
/**
1413
* triggered on error

library/src/main/java/io/constructor/data/DataManager.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.constructor.data
22

33
import com.squareup.moshi.Moshi
4-
import io.constructor.data.model.Suggestion
4+
import io.constructor.data.model.autocomplete.AutocompleteResponse
55
import io.constructor.data.model.search.SearchResponse
66
import io.constructor.data.model.browse.BrowseResponse
77
import io.constructor.data.remote.ApiPaths
@@ -15,19 +15,25 @@ import javax.inject.Singleton
1515
class DataManager @Inject
1616
constructor(private val constructorApi: ConstructorApi, private val moshi: Moshi) {
1717

18-
fun getAutocompleteResults(text: String, params: Array<Pair<String, String>> = arrayOf()): Observable<ConstructorData<List<Suggestion>?>> = constructorApi.getAutocompleteResults(text, params.toMap()).map {
19-
if (!it.isError) {
20-
it.response()?.let {
21-
if (it.isSuccessful) {
22-
ConstructorData.of(it.body()?.sections?.suggestions)
23-
} else {
24-
ConstructorData.networkError(it.errorBody()?.string())
25-
}
26-
} ?: ConstructorData.error(it.error())
27-
} else {
28-
ConstructorData.error(it.error())
29-
}
30-
}.toObservable()
18+
fun getAutocompleteResults(text: String, params: Array<Pair<String, String>> = arrayOf()): Observable<ConstructorData<AutocompleteResponse>> {
19+
return constructorApi.getAutocompleteResults(text, params.toMap()).map {
20+
if (!it.isError) {
21+
it.response()?.let {
22+
if (it.isSuccessful) {
23+
val adapter = moshi.adapter(AutocompleteResponse::class.java)
24+
val response = it.body()?.string()
25+
val result = response?.let { adapter.fromJson(it) }
26+
result?.rawData = response
27+
ConstructorData.of(result!!)
28+
} else {
29+
ConstructorData.networkError(it.errorBody()?.string())
30+
}
31+
} ?: ConstructorData.error(it.error())
32+
} else {
33+
ConstructorData.error(it.error())
34+
}
35+
}.toObservable()
36+
}
3137

3238
fun getSearchResults(text: String, encodedParams: Array<Pair<String, String>> = arrayOf()): Observable<ConstructorData<SearchResponse>> {
3339
var dynamicUrl = "/${ApiPaths.URL_SEARCH.format(text)}"
@@ -40,9 +46,9 @@ constructor(private val constructorApi: ConstructorApi, private val moshi: Moshi
4046
if (it.isSuccessful){
4147
val adapter = moshi.adapter(SearchResponse::class.java)
4248
val response = it.body()?.string()
43-
val result = response?.let { adapter.fromJson(it) }
44-
result?.rawData = response
45-
ConstructorData.of(result!!)
49+
val res = response?.let { adapter.fromJson(it) }
50+
res?.rawData = response
51+
ConstructorData.of(res!!)
4652
} else {
4753
ConstructorData.networkError(it.errorBody()?.string())
4854
}
@@ -96,9 +102,9 @@ constructor(private val constructorApi: ConstructorApi, private val moshi: Moshi
96102
if (it.isSuccessful){
97103
val adapter = moshi.adapter(BrowseResponse::class.java)
98104
val response = it.body()?.string()
99-
val result = response?.let { adapter.fromJson(it) }
100-
result?.rawData = response
101-
ConstructorData.of(result!!)
105+
val res = response?.let { adapter.fromJson(it) }
106+
res?.rawData = response
107+
ConstructorData.of(res!!)
102108
} else {
103109
ConstructorData.networkError(it.errorBody()?.string())
104110
}

library/src/main/java/io/constructor/data/model/AutocompleteResult.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

library/src/main/java/io/constructor/data/model/Group.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

library/src/main/java/io/constructor/data/model/Product.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

library/src/main/java/io/constructor/data/model/ProductData.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

library/src/main/java/io/constructor/data/model/Sections.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)