diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d07bf9..4b727cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## v3.3.5-SNAPSHOT * Decreased default offline storage lifetime for events +* Fixed bug with cyclic read/save current privacy mode after its expiration ## v3.3.4 * Added limit for event storage diff --git a/piano-analytics/src/main/java/io/piano/android/analytics/PrivacyModesStorage.kt b/piano-analytics/src/main/java/io/piano/android/analytics/PrivacyModesStorage.kt index dfbb447..21f3e63 100644 --- a/piano-analytics/src/main/java/io/piano/android/analytics/PrivacyModesStorage.kt +++ b/piano-analytics/src/main/java/io/piano/android/analytics/PrivacyModesStorage.kt @@ -15,13 +15,25 @@ class PrivacyModesStorage internal constructor( prefsStorage.privacyStorageFilter = ::isFeatureAllowed } private fun isFeatureAllowed(privacyStorageFeature: PrivacyStorageFeature): Boolean { - val isNotForbidden = PrivacyStorageFeature.ALL !in currentMode.forbiddenStorageFeatures || - privacyStorageFeature !in currentMode.forbiddenStorageFeatures - val isAllowed = PrivacyStorageFeature.ALL in currentMode.allowedStorageFeatures || - privacyStorageFeature in currentMode.allowedStorageFeatures + val isNotForbidden = PrivacyStorageFeature.ALL !in cachedMode.forbiddenStorageFeatures || + privacyStorageFeature !in cachedMode.forbiddenStorageFeatures + val isAllowed = PrivacyStorageFeature.ALL in cachedMode.allowedStorageFeatures || + privacyStorageFeature in cachedMode.allowedStorageFeatures return isNotForbidden && isAllowed } + /** + * All registered privacy modes. Add a [PrivacyMode] instance into [allModes] for registering it + */ + @Suppress("unused", "MemberVisibilityCanBePrivate") // Public API. + val allModes = mutableSetOf( + PrivacyMode.NO_CONSENT, + PrivacyMode.NO_STORAGE, + PrivacyMode.OPTIN, + PrivacyMode.OPTOUT, + PrivacyMode.EXEMPT + ) + /** * Current privacy visitor mode */ @@ -37,27 +49,19 @@ class PrivacyModesStorage internal constructor( } ?: configuration.defaultPrivacyMode } } + cachedMode = field return field } set(value) { require(value in allModes) { "Privacy mode ${value.visitorMode} is not registered." } + cachedMode = value field = value updatePrefs(value) } - /** - * All registered privacy modes. Add a [PrivacyMode] instance into [allModes] for registering it - */ - @Suppress("unused", "MemberVisibilityCanBePrivate") // Public API. - val allModes = mutableSetOf( - PrivacyMode.NO_CONSENT, - PrivacyMode.NO_STORAGE, - PrivacyMode.OPTIN, - PrivacyMode.OPTOUT, - PrivacyMode.EXEMPT - ) + private var cachedMode: PrivacyMode = currentMode // for mocking in tests @Suppress("NOTHING_TO_INLINE")