Skip to content

Commit

Permalink
Merge pull request #151 from qonversion/release/2.8.2
Browse files Browse the repository at this point in the history
Release/2.8.2
  • Loading branch information
Maria-Bordunova authored Jun 9, 2021
2 parents 02e6a81 + 3fefd91 commit 5064dd3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
release = [
versionName: "2.8.1",
versionName: "2.8.2",
versionCode: 1
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class QIdentityManager @Inject constructor(
})
}

fun logout() {
userInfoService.logout()
fun logoutIfNeeded(): Boolean {
return userInfoService.logoutIfNeeded()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class QProductCenterManager internal constructor(
}

fun identify(userID: String) {
unhandledLogoutAvailable = false
if (!isLaunchingFinished) {
pendingIdentityUserID = userID
return
Expand All @@ -150,13 +151,20 @@ class QProductCenterManager internal constructor(
}

private fun processIdentity(userID: String) {
val currentUserID = userInfoService.obtainUserID()

identityManager.identify(userID, object : IdentityManagerCallback {
override fun onSuccess(identityID: String) {
pendingIdentityUserID = null
identityInProgress = false
repository.uid = identityID

launch()
if (currentUserID == identityID) {
executePermissionsBlock()
} else {
identityInProgress = false
repository.uid = identityID

launch()
}
}

override fun onError(error: QonversionError) {
Expand Down Expand Up @@ -507,11 +515,15 @@ class QProductCenterManager internal constructor(
}

fun logout() {
identityManager.logout()
unhandledLogoutAvailable = true
pendingIdentityUserID = null
val isLogoutNeeded = identityManager.logoutIfNeeded()

val userID = userInfoService.obtainUserID()
repository.uid = userID
if (isLogoutNeeded) {
unhandledLogoutAvailable = true

val userID = userInfoService.obtainUserID()
repository.uid = userID
}
}

private fun handleLogout() {
Expand Down
8 changes: 2 additions & 6 deletions sdk/src/main/java/com/qonversion/android/sdk/Qonversion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ object Qonversion : LifecycleDelegate {
private var automationsManager: QAutomationsManager? = null
private var logger = ConsoleLogger()
private var isDebugMode = false
private var shouldResetUser = false

init {
val lifecycleHandler = AppLifecycleHandler(this)
Expand Down Expand Up @@ -62,10 +61,6 @@ object Qonversion : LifecycleDelegate {
val userInfoService = QDependencyInjector.appComponent.userInfoService()
val identityManager = QDependencyInjector.appComponent.identityManager()

if (shouldResetUser) {
userInfoService.deleteUser()
}

val userID = userInfoService.obtainUserID()

repository.uid = userID
Expand Down Expand Up @@ -257,9 +252,10 @@ object Qonversion : LifecycleDelegate {
* Call this function to reset user ID and generate new anonymous user ID.
* Call this function before Qonversion.launch()
*/
@Deprecated("This function was used in debug mode only. You can reinstall the app if you need to reset the user ID.", level = DeprecationLevel.WARNING)
@JvmStatic
fun resetUser() {
shouldResetUser = true
logger.debug(object {}.javaClass.enclosingMethod?.name + " function was used in debug mode only. You can reinstall the app if you need to reset the user ID.")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ class AppModule(
}

companion object {
private const val SDK_VERSION = "2.8.1"
private const val SDK_VERSION = "2.8.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ class QUserInfoService @Inject constructor(
preferences.putString(PREFS_USER_ID_KEY, userID)
}

fun logout() {
fun logoutIfNeeded(): Boolean {
val originalUserID = preferences.getString(PREFS_ORIGINAL_USER_ID_KEY, null)
val defaultUserID = preferences.getString(PREFS_USER_ID_KEY, null)

if (originalUserID == defaultUserID) {
return false
}

preferences.putString(PREFS_USER_ID_KEY, originalUserID)

return true
}

fun deleteUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ class QIdentityManagerTest {
@Test
fun logout() {
// when
identityManager.logout()
identityManager.logoutIfNeeded()

//then
verify(exactly = 1) {
mockUserInfoService.logout()
mockUserInfoService.logoutIfNeeded()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.qonversion.android.sdk.services

import com.qonversion.android.sdk.Constants
import com.qonversion.android.sdk.storage.SharedPreferencesCache
import com.qonversion.android.sdk.storage.TokenStorage
import io.mockk.*
import junit.framework.Assert.assertEquals
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
Expand Down Expand Up @@ -265,22 +267,59 @@ class QUserInfoServiceTest {
}

@Test
fun logout() {
fun `should not logout if logged user ID is same as anon`() {
// given
val originalUserID = "originalUserID"

every {
mockSharedPreferencesCache.getString(prefsOriginalUserIdKey, null)
} returns originalUserID

every {
mockSharedPreferencesCache.getString(prefsUserIdKey, null)
} returns originalUserID

// when
val isLogoutNeeded = userInfoService.logoutIfNeeded()

// then
verifySequence {
mockSharedPreferencesCache.getString(prefsOriginalUserIdKey, null)
mockSharedPreferencesCache.getString(prefsUserIdKey, null)
}

verify(exactly = 0) {
mockSharedPreferencesCache.putString(any(), any())
}

assertEquals("must be false", false, isLogoutNeeded)
}

@Test
fun `should logout if logged user ID is not same as anon`() {
// given
val originalUserID = "originalUserID"
val userID = "userID"

every {
mockSharedPreferencesCache.getString(prefsOriginalUserIdKey, null)
} returns originalUserID

every {
mockSharedPreferencesCache.getString(prefsUserIdKey, null)
} returns userID

// when
userInfoService.logout()
val isLogoutNeeded = userInfoService.logoutIfNeeded()

// then
verifySequence {
mockSharedPreferencesCache.getString(prefsOriginalUserIdKey, null)
mockSharedPreferencesCache.getString(prefsUserIdKey, null)
mockSharedPreferencesCache.putString(prefsUserIdKey, originalUserID)
}

assertEquals("must be true", true, isLogoutNeeded)
}

@Nested
Expand Down

0 comments on commit 5064dd3

Please sign in to comment.