diff --git a/library/src/main/java/io/constructor/core/Constants.kt b/library/src/main/java/io/constructor/core/Constants.kt index 71aae050..6a4ce316 100755 --- a/library/src/main/java/io/constructor/core/Constants.kt +++ b/library/src/main/java/io/constructor/core/Constants.kt @@ -21,6 +21,7 @@ class Constants { const val CLIENT = "c" const val EVENT = "tr" const val AUTOCOMPLETE_KEY = "autocomplete_key" + const val NUM_RESULTS = "num_results_" const val GROUP_ID = "group[group_id]" const val GROUP_DISPLAY_NAME = "group[display_name]" } @@ -30,6 +31,7 @@ class Constants { const val EVENT_SEARCH = "search" const val EVENT_SESSION_START = "session_start" const val SEARCH_SUGGESTIONS = "Search Suggestions" + const val PRODUCTS = "Products" const val EVENT_SEARCH_RESULTS = "search-results" const val EVENT_INPUT_FOCUS = "focus" } diff --git a/library/src/main/java/io/constructor/core/ConstructorIo.kt b/library/src/main/java/io/constructor/core/ConstructorIo.kt index 4db42f34..605c8e41 100755 --- a/library/src/main/java/io/constructor/core/ConstructorIo.kt +++ b/library/src/main/java/io/constructor/core/ConstructorIo.kt @@ -5,7 +5,7 @@ import android.content.Context import io.constructor.BuildConfig import io.constructor.data.DataManager import io.constructor.data.local.PreferencesHelper -import io.constructor.data.memory.TestCellMemoryHolder +import io.constructor.data.memory.ConfigMemoryHolder import io.constructor.data.model.SuggestionViewModel import io.constructor.injection.component.AppComponent import io.constructor.injection.component.DaggerAppComponent @@ -26,7 +26,7 @@ object ConstructorIo { private lateinit var dataManager: DataManager private lateinit var preferenceHelper: PreferencesHelper - private lateinit var testCellMemoryHolder: TestCellMemoryHolder + private lateinit var configMemoryHolder: ConfigMemoryHolder private lateinit var context: Context private var disposable = CompositeDisposable() @@ -49,15 +49,18 @@ object ConstructorIo { })) } - fun init(context: Context?, apiKey: String, defaultItemSection: String = BuildConfig.AUTOCOMPLETE_SECTION) { + fun init(context: Context?, apiKey: String, autocompleteResultCount: Map = mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10, + Constants.QueryValues.PRODUCTS to 0), defaultItemSection: String = BuildConfig.AUTOCOMPLETE_SECTION) { if (context == null) { throw IllegalStateException("context is null, please init library using ConstructorIo.with(context)") } this.context = context.applicationContext dataManager = component.dataManager() preferenceHelper = component.preferenceHelper() - testCellMemoryHolder = component.testCellMemoryHolder() + configMemoryHolder = component.configMemoryHolder() + configMemoryHolder.autocompleteResultCount = autocompleteResultCount preferenceHelper.token = apiKey + preferenceHelper.defaultItemSection = defaultItemSection if (preferenceHelper.id.isBlank()) { preferenceHelper.id = UUID.randomUUID().toString() @@ -69,21 +72,21 @@ object ConstructorIo { fun getClientId() = preferenceHelper.id fun setTestCellValues(pair1: Pair, pair2: Pair? = null, pair3: Pair? = null) { - testCellMemoryHolder.testCellParams = listOf(pair1, pair2, pair3) + configMemoryHolder.testCellParams = listOf(pair1, pair2, pair3) } fun clearTestCellValues() { - testCellMemoryHolder.testCellParams = emptyList() + configMemoryHolder.testCellParams = emptyList() } - internal fun testInit(context: Context?, apiKey: String, dataManager: DataManager, preferenceHelper: PreferencesHelper, testCellMemoryHolder: TestCellMemoryHolder) { + internal fun testInit(context: Context?, apiKey: String, dataManager: DataManager, preferenceHelper: PreferencesHelper, configMemoryHolder: ConfigMemoryHolder) { if (context == null) { throw IllegalStateException("Context is null, please init library using ConstructorIo.with(context)") } this.context = context.applicationContext this.dataManager = dataManager this.preferenceHelper = preferenceHelper - this.testCellMemoryHolder = testCellMemoryHolder + this.configMemoryHolder = configMemoryHolder preferenceHelper.token = apiKey if (preferenceHelper.id.isBlank()) { preferenceHelper.id = UUID.randomUUID().toString() diff --git a/library/src/main/java/io/constructor/data/interceptor/TokenInterceptor.kt b/library/src/main/java/io/constructor/data/interceptor/TokenInterceptor.kt index 42483aaa..584c02e1 100755 --- a/library/src/main/java/io/constructor/data/interceptor/TokenInterceptor.kt +++ b/library/src/main/java/io/constructor/data/interceptor/TokenInterceptor.kt @@ -4,12 +4,12 @@ import android.content.Context import io.constructor.BuildConfig import io.constructor.core.Constants import io.constructor.data.local.PreferencesHelper -import io.constructor.data.memory.TestCellMemoryHolder +import io.constructor.data.memory.ConfigMemoryHolder import okhttp3.Interceptor import okhttp3.Response -class TokenInterceptor(val context: Context, private val preferencesHelper: PreferencesHelper, private val testCellMemoryHolder: TestCellMemoryHolder) : Interceptor { +class TokenInterceptor(val context: Context, private val preferencesHelper: PreferencesHelper, private val configMemoryHolder: ConfigMemoryHolder) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { var request = chain.request() @@ -18,11 +18,14 @@ class TokenInterceptor(val context: Context, private val preferencesHelper: Pref .addQueryParameter(Constants.QueryConstants.IDENTITY, preferencesHelper.id) .addQueryParameter(Constants.QueryConstants.TIMESTAMP, System.currentTimeMillis().toString()) .addQueryParameter(Constants.QueryConstants.CLIENT, BuildConfig.CLIENT_VERSION) - testCellMemoryHolder.testCellParams.forEach { + configMemoryHolder.testCellParams.forEach { it?.let { builder.addQueryParameter(it.first, it.second) } } + configMemoryHolder.autocompleteResultCount?.entries?.forEach { + builder.addQueryParameter(Constants.QueryConstants.NUM_RESULTS+it.key, it.value.toString()) + } val url = builder.build() request = request.newBuilder().url(url).build() return chain.proceed(request) diff --git a/library/src/main/java/io/constructor/data/memory/TestCellMemoryHolder.kt b/library/src/main/java/io/constructor/data/memory/ConfigMemoryHolder.kt similarity index 90% rename from library/src/main/java/io/constructor/data/memory/TestCellMemoryHolder.kt rename to library/src/main/java/io/constructor/data/memory/ConfigMemoryHolder.kt index 481a9b93..71f516d1 100644 --- a/library/src/main/java/io/constructor/data/memory/TestCellMemoryHolder.kt +++ b/library/src/main/java/io/constructor/data/memory/ConfigMemoryHolder.kt @@ -4,7 +4,7 @@ import io.constructor.util.base64Decode import io.constructor.util.base64Encode import javax.inject.Inject -class TestCellMemoryHolder @Inject constructor() { +class ConfigMemoryHolder @Inject constructor() { private var backingString = "" @@ -32,4 +32,5 @@ class TestCellMemoryHolder @Inject constructor() { backingString = combined } + var autocompleteResultCount: Map? = null } \ No newline at end of file diff --git a/library/src/main/java/io/constructor/injection/component/AppComponent.kt b/library/src/main/java/io/constructor/injection/component/AppComponent.kt index 78d715b0..e0b0be1b 100755 --- a/library/src/main/java/io/constructor/injection/component/AppComponent.kt +++ b/library/src/main/java/io/constructor/injection/component/AppComponent.kt @@ -4,7 +4,7 @@ import android.content.Context import dagger.Component import io.constructor.data.DataManager import io.constructor.data.local.PreferencesHelper -import io.constructor.data.memory.TestCellMemoryHolder +import io.constructor.data.memory.ConfigMemoryHolder import io.constructor.data.remote.ConstructorApi import io.constructor.injection.ApplicationContext import io.constructor.injection.module.AppModule @@ -21,7 +21,7 @@ interface AppComponent { fun preferenceHelper(): PreferencesHelper - fun testCellMemoryHolder(): TestCellMemoryHolder + fun configMemoryHolder(): ConfigMemoryHolder fun constructorApi(): ConstructorApi } diff --git a/library/src/main/java/io/constructor/injection/module/AppModule.kt b/library/src/main/java/io/constructor/injection/module/AppModule.kt index 59765ab2..c48983ca 100755 --- a/library/src/main/java/io/constructor/injection/module/AppModule.kt +++ b/library/src/main/java/io/constructor/injection/module/AppModule.kt @@ -4,7 +4,7 @@ import android.content.Context import dagger.Module import dagger.Provides import io.constructor.data.local.PreferencesHelper -import io.constructor.data.memory.TestCellMemoryHolder +import io.constructor.data.memory.ConfigMemoryHolder import io.constructor.injection.ApplicationContext import javax.inject.Singleton @@ -24,7 +24,7 @@ class AppModule(private val application: Context) { @Provides @Singleton - internal fun provideTestCellHolder(): TestCellMemoryHolder { - return TestCellMemoryHolder() + internal fun provideConfigMemoryHolder(): ConfigMemoryHolder { + return ConfigMemoryHolder() } } \ No newline at end of file diff --git a/library/src/main/java/io/constructor/injection/module/NetworkModule.kt b/library/src/main/java/io/constructor/injection/module/NetworkModule.kt index ecd9388b..6f4dc333 100755 --- a/library/src/main/java/io/constructor/injection/module/NetworkModule.kt +++ b/library/src/main/java/io/constructor/injection/module/NetworkModule.kt @@ -8,7 +8,7 @@ import dagger.Provides import io.constructor.BuildConfig import io.constructor.data.interceptor.TokenInterceptor import io.constructor.data.local.PreferencesHelper -import io.constructor.data.memory.TestCellMemoryHolder +import io.constructor.data.memory.ConfigMemoryHolder import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit @@ -50,7 +50,7 @@ class NetworkModule(private val context: Context) { @Provides @Singleton - internal fun provideTokenInterceptor(prefHelper: PreferencesHelper, testCellMemoryHolder: TestCellMemoryHolder): TokenInterceptor = TokenInterceptor(context, prefHelper, testCellMemoryHolder) + internal fun provideTokenInterceptor(prefHelper: PreferencesHelper, configMemoryHolder: ConfigMemoryHolder): TokenInterceptor = TokenInterceptor(context, prefHelper, configMemoryHolder) @Provides @Singleton diff --git a/library/src/test/java/io/constructor/core/ConstructorIoTest.kt b/library/src/test/java/io/constructor/core/ConstructorIoTest.kt index ec83b019..c0a0795f 100755 --- a/library/src/test/java/io/constructor/core/ConstructorIoTest.kt +++ b/library/src/test/java/io/constructor/core/ConstructorIoTest.kt @@ -5,7 +5,7 @@ import io.constructor.BuildConfig import io.constructor.data.DataManager import io.constructor.data.interceptor.TokenInterceptor import io.constructor.data.local.PreferencesHelper -import io.constructor.data.memory.TestCellMemoryHolder +import io.constructor.data.memory.ConfigMemoryHolder import io.constructor.data.model.Group import io.constructor.data.model.SuggestionViewModel import io.constructor.util.RxSchedulersOverrideRule @@ -31,7 +31,7 @@ class ConstructorIoTest { private val ctx = mockk() private val pref = mockk() - private val testCellMemoryHolder = mockk() + private val configMemoryHolder = mockk() private val data = mockk() private var constructorIo = ConstructorIo private val sampleMillis = "1520000000000" @@ -44,7 +44,7 @@ class ConstructorIoTest { every { pref.id } returns "1" every { pref.getSessionId() } returns 1 every { pref.getSessionId(any()) } returns 1 - constructorIo.testInit(ctx, "dummyKey", data, pref, testCellMemoryHolder) + constructorIo.testInit(ctx, "dummyKey", data, pref, configMemoryHolder) } @After @@ -210,13 +210,14 @@ class ConstructorIoTest { val mockServer = MockWebServer() every { pref.token } returns "123" every { pref.id } returns "1" - every { testCellMemoryHolder.testCellParams = any() } just Runs - every { testCellMemoryHolder.testCellParams } returns listOf("ef-1" to "2", "ef-3" to "4") + every { configMemoryHolder.testCellParams = any() } just Runs + every { configMemoryHolder.autocompleteResultCount } returns mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10, Constants.QueryValues.PRODUCTS to 0) + every { configMemoryHolder.testCellParams } returns listOf("ef-1" to "2", "ef-3" to "4") constructorIo.setTestCellValues("1" to "2", "3" to "4") - verify(exactly = 1) { testCellMemoryHolder.testCellParams = any() } + verify(exactly = 1) { configMemoryHolder.testCellParams = any() } mockServer.start() mockServer.enqueue(MockResponse()) - var client = OkHttpClient.Builder().addInterceptor(TokenInterceptor(ctx, pref, testCellMemoryHolder)).build() + var client = OkHttpClient.Builder().addInterceptor(TokenInterceptor(ctx, pref, configMemoryHolder)).build() client.newCall(Request.Builder().url(mockServer.url("/")).build()).execute() var recordedRequest = mockServer.takeRequest() assert(recordedRequest.path.contains("ef-1=2")) diff --git a/library/src/test/java/io/constructor/data/memory/TestCellMemoryHolderTest.kt b/library/src/test/java/io/constructor/data/memory/ConfigMemoryHolderTest.kt similarity index 60% rename from library/src/test/java/io/constructor/data/memory/TestCellMemoryHolderTest.kt rename to library/src/test/java/io/constructor/data/memory/ConfigMemoryHolderTest.kt index c6c83b3a..8d9c675e 100644 --- a/library/src/test/java/io/constructor/data/memory/TestCellMemoryHolderTest.kt +++ b/library/src/test/java/io/constructor/data/memory/ConfigMemoryHolderTest.kt @@ -6,19 +6,19 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) -class TestCellMemoryHolderTest { +class ConfigMemoryHolderTest { - private lateinit var testCellMemoryHolder: TestCellMemoryHolder + private lateinit var configMemoryHolder: ConfigMemoryHolder @Before fun setUp() { - testCellMemoryHolder = TestCellMemoryHolder() + configMemoryHolder = ConfigMemoryHolder() } @Test fun verifyTestCellsWrittenAndEncoded() { - testCellMemoryHolder.testCellParams = listOf("1" to "2", "3" to "4") - val params = testCellMemoryHolder.testCellParams + configMemoryHolder.testCellParams = listOf("1" to "2", "3" to "4") + val params = configMemoryHolder.testCellParams assert(params[0]!!.first == "ef-1" && params[0]!!.second == "2") assert(params[1]!!.first == "ef-3" && params[1]!!.second == "4") }