Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web2App #229

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ workRuntimeKtx_version = "2.9.0"
serialization_version = "1.6.0"
dropshot_version = "0.4.2"
ksp = "1.9.0-1.0.13"
install_referrer = "2.2"
[libraries]

# SQL
Expand Down Expand Up @@ -73,7 +74,7 @@ lifecycle_runtime_ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", v
uiautomator = { module = "androidx.test.uiautomator:uiautomator", version.ref = "uiautomator_version" }
work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx_version" }
lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycleProcessVersion" }

install_referrer = { module = "com.android.installreferrer:installreferrer", version.ref = "install_referrer"}
# Coroutines
kotlinx_coroutines_core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx_coroutines_core_version" }
kotlinx-coroutines-guava = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-guava", version.ref = "kotlinxCoroutinesGuavaVersion" }
Expand Down
1 change: 1 addition & 0 deletions superwall/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ dependencies {

// Serialization
implementation(libs.kotlinx.serialization.json)
implementation(libs.install.referrer)

// Test
testImplementation(libs.junit)
Expand Down
5 changes: 5 additions & 0 deletions superwall/src/main/java/com/superwall/sdk/Superwall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.superwall.sdk.models.assignment.ConfirmedAssignment
import com.superwall.sdk.models.entitlements.Entitlement
import com.superwall.sdk.models.entitlements.SubscriptionStatus
import com.superwall.sdk.models.events.EventData
import com.superwall.sdk.models.internal.VendorId
import com.superwall.sdk.network.device.InterfaceStyle
import com.superwall.sdk.paywall.presentation.PaywallCloseReason
import com.superwall.sdk.paywall.presentation.PaywallInfo
Expand Down Expand Up @@ -281,6 +282,9 @@ class Superwall(
dependencyContainer.entitlements.status
}

internal val vendorId: VendorId
get() = VendorId(dependencyContainer.deviceHelper.vendorId)

/**
* A property that indicates current configuration state of the SDK.
*
Expand Down Expand Up @@ -445,6 +449,7 @@ class Superwall(
dependencyContainer.storage.recordAppInstall {
track(event = it)
}
dependencyContainer.reedemer.checkForRefferal()
// Implicitly wait
dependencyContainer.configManager.fetchConfiguration()
dependencyContainer.identityManager.configure()
Expand Down
32 changes: 32 additions & 0 deletions superwall/src/main/java/com/superwall/sdk/config/ConfigManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.superwall.sdk.config

import android.content.Context
import com.superwall.sdk.Superwall
import com.superwall.sdk.analytics.internal.trackable.InternalSuperwallEvent
import com.superwall.sdk.config.models.ConfigState
import com.superwall.sdk.config.models.getConfig
Expand All @@ -21,6 +22,8 @@ import com.superwall.sdk.misc.into
import com.superwall.sdk.misc.onError
import com.superwall.sdk.misc.then
import com.superwall.sdk.models.config.Config
import com.superwall.sdk.models.entitlements.SubscriptionStatus
import com.superwall.sdk.models.internal.DeviceVendorId
import com.superwall.sdk.models.triggers.Experiment
import com.superwall.sdk.models.triggers.ExperimentID
import com.superwall.sdk.models.triggers.Trigger
Expand All @@ -35,6 +38,7 @@ import com.superwall.sdk.storage.LatestGeoInfo
import com.superwall.sdk.storage.Storage
import com.superwall.sdk.store.Entitlements
import com.superwall.sdk.store.StoreManager
import com.superwall.sdk.web.WebPaywallRedeemer
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.Flow
Expand All @@ -56,6 +60,7 @@ open class ConfigManager(
private val deviceHelper: DeviceHelper,
var options: SuperwallOptions,
private val paywallManager: PaywallManager,
private val webPaywallRedeemer: WebPaywallRedeemer,
private val factory: Factory,
private val assignments: Assignments,
private val paywallPreload: PaywallPreload,
Expand Down Expand Up @@ -313,6 +318,7 @@ open class ConfigManager(
}
ioScope.launch {
storeManager.loadPurchasedProducts()
checkForWebEntitlements()
}
}

Expand Down Expand Up @@ -393,4 +399,30 @@ open class ConfigManager(
fetchDuration = System.currentTimeMillis() - startTime,
)
}

// This runs only if user does not have all of the entitlements
suspend fun checkForWebEntitlements() {
if (entitlements.all.size != entitlements.active.size) {
webPaywallRedeemer
.checkForWebEntitlements(
Superwall.instance.userId,
DeviceVendorId(Superwall.instance.vendorId),
).fold(onSuccess = { webEntitlements ->
if (webEntitlements.isNotEmpty()) {
val localWithWeb = entitlements.active + webEntitlements.toSet()
entitlements.setSubscriptionStatus(
SubscriptionStatus.Active(localWithWeb),
)
}
}, onFailure = {
Logger.debug(
LogLevel.error,
LogScope.webEntitlements,
"Checking for web entitlements failed",
emptyMap(),
it,
)
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ import com.superwall.sdk.store.transactions.TransactionManager
import com.superwall.sdk.utilities.DateUtils
import com.superwall.sdk.utilities.ErrorTracker
import com.superwall.sdk.utilities.dateFormat
import com.superwall.sdk.web.DeepLinkReferrer
import com.superwall.sdk.web.WebPaywallRedeemer
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -145,7 +147,7 @@ class DependencyContainer(
val googleBillingWrapper: GoogleBillingWrapper

var entitlements: Entitlements

var reedemer: WebPaywallRedeemer
private val uiScope
get() = mainScope()
private val ioScope
Expand Down Expand Up @@ -291,6 +293,20 @@ class DependencyContainer(
scope = ioScope,
)

reedemer =
WebPaywallRedeemer(
context = context,
ioScope = ioScope,
deepLinkReferrer = DeepLinkReferrer({ context }, ioScope),
network = network,
setEntitlementStatus = {
Superwall.instance.setSubscriptionStatus(
SubscriptionStatus.Active(
it.toSet(),
),
)
},
)
configManager =
ConfigManager(
context = context,
Expand All @@ -308,6 +324,7 @@ class DependencyContainer(
Superwall.instance.track(it)
},
entitlements = entitlements,
webPaywallRedeemer = reedemer,
)

eventsQueue =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class IdentityManager(
Superwall.instance.track(trackableEvent)
}

configManager.checkForWebEntitlements()
if (options?.restorePaywallAssignments == true) {
identityJobs +=
ioScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.superwall.sdk.logger
enum class LogScope {
localizationManager,
bounceButton,
webEntitlements,
coreData,
configManager,
identityManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.superwall.sdk.models.entitlements

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RedeemRequest(
@SerialName("deviceId")
val deviceId: String,
@SerialName("appUserId")
val userId: String,
@SerialName("codes")
val codes: List<Reedemable>,
)

@Serializable
data class Reedemable(
@SerialName("code")
val code: String,
)

@Serializable
data class RedemptionEmail(
val email: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.superwall.sdk.models.entitlements

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class WebEntitlements(
@SerialName("entitlements")
val entitlements: List<Entitlement>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.superwall.sdk.models.internal

@JvmInline
value class UserId(
val value: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.superwall.sdk.models.internal

@JvmInline
value class VendorId(
val value: String,
) {
override fun toString(): String = value
}

class DeviceVendorId(
vendorId: VendorId,
) {
val value = "\$SuperwallDevice:${vendorId.value}"

override fun toString(): String = value
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import com.superwall.sdk.misc.Either
import com.superwall.sdk.models.assignment.AssignmentPostback
import com.superwall.sdk.models.assignment.ConfirmedAssignmentResponse
import com.superwall.sdk.models.config.Config
import com.superwall.sdk.models.entitlements.RedeemRequest
import com.superwall.sdk.models.entitlements.RedemptionEmail
import com.superwall.sdk.models.entitlements.Reedemable
import com.superwall.sdk.models.entitlements.WebEntitlements
import com.superwall.sdk.models.internal.DeviceVendorId
import com.superwall.sdk.models.internal.UserId
import com.superwall.sdk.models.paywall.Paywall
import com.superwall.sdk.models.paywall.Paywalls
import com.superwall.sdk.network.session.CustomHttpUrlConnection
Expand Down Expand Up @@ -75,4 +81,33 @@ class BaseHostService(

return get<Paywall>("paywall/$identifier", queryItems = queryItems, isForDebugging = true)
}

suspend fun redeemToken(
codes: List<String>,
userId: UserId,
vendorId: DeviceVendorId,
) = post<WebEntitlements>(
"redeem",
body =
json
.encodeToString(
RedeemRequest(
vendorId.value,
userId.value,
codes.map {
Reedemable(it)
},
),
).toByteArray(),
)

suspend fun redeemByEmail(email: String) =
post<WebEntitlements>(
"redeem",
body = json.encodeToString(RedemptionEmail(email)).toByteArray(),
)

suspend fun webEntitlementsByUserId(userId: UserId) = get<WebEntitlements>("users/$userId/entitlements")

suspend fun webEntitlementsByDeviceId(deviceId: DeviceVendorId) = get<WebEntitlements>("devices/$deviceId/entitlements")
}
25 changes: 25 additions & 0 deletions superwall/src/main/java/com/superwall/sdk/network/Network.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.superwall.sdk.models.events.EventData
import com.superwall.sdk.models.events.EventsRequest
import com.superwall.sdk.models.events.EventsResponse
import com.superwall.sdk.models.geo.GeoInfo
import com.superwall.sdk.models.internal.DeviceVendorId
import com.superwall.sdk.models.internal.UserId
import com.superwall.sdk.models.paywall.Paywall
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -102,6 +104,29 @@ open class Network(
it.assignments
}.logError("/assignments")

override suspend fun redeemToken(
codes: List<String>,
userId: UserId,
vendorId: DeviceVendorId,
) = baseHostService
.redeemToken(codes, userId, vendorId)
.logError("/redeem")

override suspend fun redeemEmail(email: String) =
baseHostService
.redeemByEmail(email)
.logError("/redeem")

override suspend fun webEntitlementsByUserId(userId: UserId) =
baseHostService
.webEntitlementsByUserId(userId)
.logError("/redeem")

override suspend fun webEntitlementsByDeviceID(deviceId: DeviceVendorId) =
baseHostService
.webEntitlementsByDeviceId(deviceId)
.logError("/redeem")

private suspend fun awaitUntilAppInForeground() {
// Wait until the app is not in the background.
factory.appLifecycleObserver
Expand Down
15 changes: 15 additions & 0 deletions superwall/src/main/java/com/superwall/sdk/network/SuperwallAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import com.superwall.sdk.misc.Either
import com.superwall.sdk.models.assignment.Assignment
import com.superwall.sdk.models.assignment.AssignmentPostback
import com.superwall.sdk.models.config.Config
import com.superwall.sdk.models.entitlements.WebEntitlements
import com.superwall.sdk.models.events.EventData
import com.superwall.sdk.models.events.EventsRequest
import com.superwall.sdk.models.geo.GeoInfo
import com.superwall.sdk.models.internal.DeviceVendorId
import com.superwall.sdk.models.internal.UserId
import com.superwall.sdk.models.paywall.Paywall

interface SuperwallAPI {
Expand All @@ -26,4 +29,16 @@ interface SuperwallAPI {
suspend fun getGeoInfo(): Either<GeoInfo, NetworkError>

suspend fun getAssignments(): Either<List<Assignment>, NetworkError>

suspend fun webEntitlementsByUserId(userId: UserId): Either<WebEntitlements, NetworkError>

suspend fun webEntitlementsByDeviceID(deviceId: DeviceVendorId): Either<WebEntitlements, NetworkError>

suspend fun redeemToken(
token: List<String>,
userId: UserId,
vendorId: DeviceVendorId,
): Either<WebEntitlements, NetworkError>

suspend fun redeemEmail(email: String): Either<WebEntitlements, NetworkError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Entitlements(
* All entitlements, regardless of whether they're active or not.
*/
val all: Set<Entitlement>
get() = _all.toSet()
get() = _all.toSet() + _entitlementsByProduct.values.flatten()

/**
* The active entitlements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ internal inline fun <T> withErrorTracking(block: () -> T): Either<T, Throwable>
try {
Either.Success(block())
} catch (e: Throwable) {
e.printStackTrace()
if (e.shouldLog()) {
Superwall.instance.trackError(e)
}
Expand Down
Loading