Skip to content

Commit

Permalink
Merge pull request #93 from superwall-me/develop
Browse files Browse the repository at this point in the history
v1.0.0-alpha.44
  • Loading branch information
yusuftor authored Feb 1, 2024
2 parents 0efd1f3 + b65a476 commit 5813403
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 61 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The changelog for `Superwall`. Also see the [releases](https://github.com/superwall-me/Superwall-Android/releases) on GitHub.

## 1.0.0-alpha.44

### Fixes

- Fixes rare issue where paywall preloading was preventing paywalls from showing.

## 1.0.0-alpha.43

### Enhancements
Expand Down Expand Up @@ -32,7 +38,7 @@ Czech.
### Fixes

- Adds missing `presentationSourceType` to `PaywallInfo`.
- Fixes issue where the status bar color was always dark regardless of paywall color.
- Fixes issue where the status bar color was always dark regardless of paywall color.
- Adds `TypeToken` to proguard rules to prevent r8 from 'optimizing' our code and causing a crash.

## 1.0.0-alpha.38
Expand Down
2 changes: 1 addition & 1 deletion superwall/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
id("maven-publish")
}

version = "1.0.0-alpha.43"
version = "1.0.0-alpha.44"

android {
compileSdk = 33
Expand Down
123 changes: 64 additions & 59 deletions superwall/src/main/java/com/superwall/sdk/config/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import com.superwall.sdk.paywall.presentation.rule_logic.expression_evaluator.Ex
import com.superwall.sdk.paywall.request.ResponseIdentifiers
import com.superwall.sdk.storage.Storage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -84,38 +86,36 @@ open class ConfigManager(
// Remaining methods should be converted similarly, using Kotlin's coroutines for async tasks
// and other relevant Kotlin features. Here's an example of one method:
suspend fun fetchConfiguration() {
coroutineScope {
try {
val config = network.getConfig() {
launch {
configState.emit(Result.Success(ConfigState.Retrying))
}
try {
val config = network.getConfig() {
CoroutineScope(Dispatchers.IO).launch {
configState.emit(Result.Success(ConfigState.Retrying))
}
launch { sendProductsBack(config) }
}
CoroutineScope(Dispatchers.IO).launch { sendProductsBack(config) }

Logger.debug(
logLevel = LogLevel.debug,
scope = LogScope.superwallCore,
message = "Fetched Configuration: $config",
)
Logger.debug(
logLevel = LogLevel.debug,
scope = LogScope.superwallCore,
message = "Fetched Configuration: $config",
)

triggersByEventName = ConfigLogic.getTriggersByEventName(config.triggers)
choosePaywallVariants(config.triggers)
configState.emit(Result.Success(ConfigState.Retrieved(config)))
triggersByEventName = ConfigLogic.getTriggersByEventName(config.triggers)
choosePaywallVariants(config.triggers)
configState.emit(Result.Success(ConfigState.Retrieved(config)))

// TODO: Re-enable those params
// TODO: Re-enable those params
// storeKitManager.loadPurchasedProducts()
launch { preloadPaywalls() }
} catch (e: Throwable) {
configState.emit(Result.Failure(e))
Logger.debug(
logLevel = LogLevel.error,
scope = LogScope.superwallCore,
message = "Failed to Fetch Configuration",
info = null,
error = e
)
}
CoroutineScope(Dispatchers.IO).launch { preloadPaywalls() }
} catch (e: Throwable) {
configState.emit(Result.Failure(e))
Logger.debug(
logLevel = LogLevel.error,
scope = LogScope.superwallCore,
message = "Failed to Fetch Configuration",
info = null,
error = e
)
}
}

Expand Down Expand Up @@ -204,8 +204,8 @@ open class ConfigManager(


// Preloads paywalls.
private suspend fun preloadPaywalls() = coroutineScope {
if (!options.paywalls.shouldPreload) return@coroutineScope
private suspend fun preloadPaywalls() {
if (!options.paywalls.shouldPreload) return
preloadAllPaywalls()
}

Expand All @@ -214,37 +214,34 @@ open class ConfigManager(
if (currentPreloadingTask != null) {
return
}
currentPreloadingTask = CoroutineScope(Dispatchers.IO).launch {
val config = configState.awaitFirstValidConfig() ?: return@launch

currentPreloadingTask = coroutineScope {
launch {
val config = configState.awaitFirstValidConfig() ?: return@launch

val expressionEvaluator = ExpressionEvaluator(
context = context,
storage = storage,
factory = factory
)
val triggers = ConfigLogic.filterTriggers(
config.triggers,
preloadingDisabled = config.preloadingDisabled
)
val confirmedAssignments = storage.getConfirmedAssignments()
val paywallIds = ConfigLogic.getAllActiveTreatmentPaywallIds(
triggers = triggers,
confirmedAssignments = confirmedAssignments,
unconfirmedAssignments = unconfirmedAssignments,
expressionEvaluator = expressionEvaluator
)
preloadPaywalls(paywallIdentifiers = paywallIds)
val expressionEvaluator = ExpressionEvaluator(
context = context,
storage = storage,
factory = factory
)
val triggers = ConfigLogic.filterTriggers(
config.triggers,
preloadingDisabled = config.preloadingDisabled
)
val confirmedAssignments = storage.getConfirmedAssignments()
val paywallIds = ConfigLogic.getAllActiveTreatmentPaywallIds(
triggers = triggers,
confirmedAssignments = confirmedAssignments,
unconfirmedAssignments = unconfirmedAssignments,
expressionEvaluator = expressionEvaluator
)
preloadPaywalls(paywallIdentifiers = paywallIds)

currentPreloadingTask = null
}
currentPreloadingTask = null
}
}

// Preloads paywalls referenced by the provided triggers.
suspend fun preloadPaywallsByNames(eventNames: Set<String>) = coroutineScope {
val config = configState.awaitFirstValidConfig() ?: return@coroutineScope
suspend fun preloadPaywallsByNames(eventNames: Set<String>) {
val config = configState.awaitFirstValidConfig() ?: return
val triggersToPreload = config.triggers.filter { eventNames.contains(it.eventName) }
val triggerPaywallIdentifiers = getTreatmentPaywallIds(triggersToPreload.toSet())
preloadPaywalls(triggerPaywallIdentifiers)
Expand All @@ -253,8 +250,12 @@ open class ConfigManager(
// Preloads paywalls referenced by triggers.
private suspend fun preloadPaywalls(paywallIdentifiers: Set<String>) {
coroutineScope {
paywallIdentifiers.forEach { identifier ->
launch {
// List to hold all the Deferred objects
val tasks = mutableListOf<Deferred<Any>>()

for (identifier in paywallIdentifiers) {
val task = async {
// Your asynchronous operation
val request = factory.makePaywallRequest(
eventData = null,
responseIdentifiers = ResponseIdentifiers(
Expand All @@ -273,16 +274,20 @@ open class ConfigManager(
isPreloading = true,
delegate = null
)
} catch (e: Throwable) {
null
} catch (e: Exception) {
// Handle exception
}
}
tasks.add(task)
}

// Await all tasks
tasks.forEach { it.await() }
}
}

// This sends product data back to the dashboard.
private suspend fun sendProductsBack(config: Config) = coroutineScope {
private suspend fun sendProductsBack(config: Config) {
// if (!config.featureFlags.enablePostback) return@coroutineScope
// val milliseconds = 1000L
// val nanoseconds = milliseconds * 1_000_000L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SerialTaskManager(private val coroutineScope: CoroutineScope = CoroutineSc
}
currentTask?.await()


// After the task completes, recursively execute the next task
if (taskQueue.isNotEmpty()) {
executeNextTask()
Expand Down

0 comments on commit 5813403

Please sign in to comment.