Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/src/main/java/io/constructor/core/ConstructorIo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ object ConstructorIo {
}
}

fun appMovedToForeground() {
preferenceHelper.getSessionId(sessionIncrementEventHandler)
}

fun getAutocompleteResults(query: String): Observable<ConstructorData<List<Suggestion>?>> {
val params = mutableListOf<Pair<String, String>>()
configMemoryHolder.autocompleteResultCount?.entries?.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ constructor(@ApplicationContext context: Context, prefFileName: String = PREF_FI
get() = preferences.getLong(SESSION_LAST_ACCESS, System.currentTimeMillis())
set(value) = preferences.edit().putLong(SESSION_LAST_ACCESS, value).apply()

fun getSessionId(sessionIncrementAction: ((String) -> Unit)? = null): Int {
fun getSessionId(sessionIncrementAction: ((String) -> Unit)? = null, forceIncrement: Boolean = false): Int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this force an increment even if the time threshold has not passed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, calling app foreground forces it, thats why i added this param

if (!preferences.contains(SESSION_ID)) {
return resetSession(sessionIncrementAction)
}
val sessionTime = lastSessionAccess
val timeDiff = System.currentTimeMillis() - sessionTime
if (timeDiff > SESSION_TIME_THRESHOLD) {
if (timeDiff > SESSION_TIME_THRESHOLD || forceIncrement) {
var sessionId = preferences.getInt(SESSION_ID, 1)
preferences.edit().putInt(SESSION_ID, ++sessionId).apply()
sessionIncrementAction?.invoke(sessionId.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.constructor.ui.suggestion

import io.constructor.core.ConstructorIo
import io.constructor.data.ConstructorData
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.model.Suggestion
import io.constructor.features.base.BasePresenter
import io.constructor.injection.ConfigPersistent
import io.constructor.util.d
import io.constructor.util.rx.scheduler.SchedulerUtils
import io.constructor.util.io2ui
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit
Expand All @@ -21,7 +19,7 @@ constructor(private val preferencesHelper: PreferencesHelper) : BasePresenter<Su

override fun attachView(mvpView: SuggestionsView) {
super.attachView(mvpView)
disposables.add(mvpView.queryChanged().debounce(300, TimeUnit.MILLISECONDS).compose(SchedulerUtils.ioToMain<String>()).subscribe({ query ->
disposables.add(mvpView.queryChanged().debounce(300, TimeUnit.MILLISECONDS).io2ui().subscribe({ query ->
getSuggestions(query)
}, {error ->
error.printStackTrace()
Expand All @@ -46,10 +44,11 @@ constructor(private val preferencesHelper: PreferencesHelper) : BasePresenter<Su
mvpView.onError(IllegalStateException("token is null, please init library with token using ConstructorIo.init"))
return
}
disposables.add(ConstructorIo.getAutocompleteResults(text).compose(SchedulerUtils.ioToMain<ConstructorData<List<Suggestion>?>>()).subscribe { data ->
disposables.add(ConstructorIo.getAutocompleteResults(text).io2ui().subscribe { data ->
data.onValue {
ConstructorIo.trackSearchResultsLoaded(text, it!!.size)
mvpView.showSuggestions(it, preferencesHelper.groupsShownForFirstTerm)
it?.let {
mvpView.showSuggestions(it, preferencesHelper.groupsShownForFirstTerm)
}
}
data.onError {
if (it is NoSuchElementException) {
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/java/io/constructor/util/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.os.Parcelable
import android.support.v4.content.LocalBroadcastManager
import android.util.Base64
import android.util.Log
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.io.Serializable
import java.net.URLEncoder

Expand Down Expand Up @@ -43,3 +46,9 @@ fun String.base64Encode(): String? {
fun String.base64Decode(): String {
return String(Base64.decode(this, Base64.NO_WRAP or Base64.NO_PADDING))
}

fun <T : Any> Observable<T>.io2ui(): Observable<T> {
return compose {
it.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ConstructorIoTest {
every { pref.id } returns "1"
every { pref.getSessionId() } returns 1
every { pref.getSessionId(any()) } returns 1
every { pref.getSessionId(any(), any()) } returns 1
constructorIo.testInit(ctx, ConstructorIoConfig("dummyKey",
testCells = listOf("1" to "2", "3" to "4")), data, pref, configMemoryHolder)
}
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/java/io/constructor/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ConstructorIo.appMovedToForeground()
button.setOnClickListener { startActivity(Intent(this, SampleActivity::class.java)) }
button2.setOnClickListener { startActivity(Intent(this, SampleActivityCustom::class.java)) }
button3.setOnClickListener { ConstructorIo.trackConversion("testId", "id", 11.99) }
button3.setOnClickListener { ConstructorIo.trackConversion("testId", "id", 11.0) }
button4.setOnClickListener { ConstructorIo.trackSearchResultClick("testTerm", "testId", "1") }
button5.setOnClickListener { ConstructorIo.trackSearchResultsLoaded("testTerm", Random().nextInt(99) + 1) }
}
Expand Down