Skip to content

Commit

Permalink
version 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vladd-g committed Oct 22, 2024
2 parents 5368b1e + 8eb7f35 commit 3a32cca
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 48 deletions.
24 changes: 6 additions & 18 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ android {
versionNameSuffix "-lib-debug"
repositories {
google()
// Adapty helper library
maven { url "$rootDir/localMaven" }
maven { url "$rootDir/internalMaven" }
}

Expand All @@ -75,26 +73,16 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

rootProject.allprojects {
repositories {
maven {
url "$rootDir/../node_modules/react-native-adapty/lib/android/localMaven"
}
maven {
url "$rootDir/../node_modules/@adapty/react-native-ui/android/localMaven"
}
if (project.android.hasProperty("namespace")) {
namespace 'com.adapty.react.ui'
}
}


repositories {
mavenCentral()
google()
maven { url "$rootDir/../node_modules/react-native/android" }
maven { url "$rootDir/../node_modules/react-native-adapty/lib/android/localMaven" }
maven { url "$rootDir/../node_modules/@adapty/react-native-ui/android/localMaven" }
}

def kotlin_version = getExtOrDefault("kotlinVersion")
Expand All @@ -106,11 +94,11 @@ dependencies {
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

compileOnly 'io.adapty:android-sdk:2.11.5'
compileOnly 'io.adapty.internal:crossplatform:2.11.1'
implementation 'io.adapty.internal:crossplatform-ui:2.11.0'
compileOnly 'io.adapty:android-sdk:3.0.2'
compileOnly 'io.adapty.internal:crossplatform:3.0.0'
implementation 'io.adapty.internal:crossplatform-ui:3.0.0'

implementation 'io.adapty:android-ui:2.11.3'
implementation 'io.adapty:android-ui:3.0.1'
}

if (isNewArchitectureEnabled()) {
Expand Down
Binary file not shown.

This file was deleted.

1 change: 1 addition & 0 deletions android/src/main/java/com/adapty/react/ui/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum class ParamKey(val value: String) {
LOCALE("locale"),
PRODUCT_TITLES("products_titles"),
CUSTOM_TAGS("custom_tags"),
TIMER_INFO("timer_info"),
PREFETCH_PRODUCTS("prefetch_products"),
}

Expand Down
2 changes: 2 additions & 0 deletions android/src/main/java/com/adapty/react/ui/RNAUIModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ class RNAUIModule(private val ctx: ReactApplicationContext) : ReactContextBaseJa
val preloadProducts: Boolean = ctx.params.getOptionalValue(ParamKey.PREFETCH_PRODUCTS) ?: false
val personalizedOffers: HashMap<String, Boolean>? = ctx.params.getOptionalValue(ParamKey.PRODUCT_TITLES)
val customTags: HashMap<String, String>? = ctx.params.getDecodedOptionalValue(ParamKey.CUSTOM_TAGS)
val timerInfo: HashMap<String, String>? = ctx.params.getDecodedOptionalValue(ParamKey.TIMER_INFO)


helper.handleCreateView(
paywall,
preloadProducts,
personalizedOffers,
customTags,
timerInfo,
{ jsonView ->
ctx.resolve(jsonView.id, "") },
{ error -> ctx.forwardError(error, "") }
Expand Down
4 changes: 1 addition & 3 deletions ios/AdaptyUI+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import AdaptyUI
extension AdaptyUI {
struct View: Encodable {
let id: String
let templateId: String
let paywallId: String
let paywallVariationId: String

enum CodingKeys: String, CodingKey {
case id
case templateId = "template_id"
case paywallId = "paywall_id"
case paywallVariationId = "paywall_variation_id"
}
}
}

@available(iOS 15.0, *)
extension AdaptyPaywallController {
func toView() -> AdaptyUI.View {

AdaptyUI.View(id: id.uuidString,
templateId: viewConfiguration.templateId,
paywallId: paywall.placementId,
paywallVariationId: paywall.variationId)
}
Expand Down
80 changes: 67 additions & 13 deletions ios/RNAUICallHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Adapty
import AdaptyUI
import react_native_adapty_sdk

@available(iOS 15.0, *)
extension UIViewController {
var isOrContainsAdaptyController: Bool {
guard let presentedViewController = presentedViewController else {
Expand All @@ -17,7 +18,7 @@ extension UIViewController {
class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
// MARK: - Config

private var paywallControllers = [UUID: AdaptyPaywallController]()
private var paywallControllers = [UUID: Any]()
// private static var adaptyUIDelegate: AdaptyUIDelegate!

// TODO: Why
Expand Down Expand Up @@ -63,6 +64,7 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
}

/// Sends event to JS layer if client has listeners
@available(iOS 15.0, *)
private func pushEvent(_ event: EventName, view: AdaptyPaywallController) {
if !hasListeners {
return
Expand All @@ -83,6 +85,7 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
}

/// Sends event to JS layer if client has listeners
@available(iOS 15.0, *)
private func pushEvent<T: Encodable>(_ event: EventName, view: AdaptyPaywallController, data: T) {
if !hasListeners {
return
Expand All @@ -101,39 +104,51 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
self.sendEvent(withName: event.rawValue, body: str)
}

@available(iOS 15.0, *)
private func cachePaywallController(_ controller: AdaptyPaywallController, id: UUID) {
paywallControllers[id] = controller
}

@available(iOS 15.0, *)
private func deleteCachedPaywallController(_ id: String) {
guard let uuid = UUID(uuidString: id) else { return }
paywallControllers.removeValue(forKey: uuid)
}

@available(iOS 15.0, *)
private func cachedPaywallController(_ id: String) -> AdaptyPaywallController? {
guard let uuid = UUID(uuidString: id) else { return nil }
return paywallControllers[uuid]
return paywallControllers[uuid] as? AdaptyPaywallController
}

@available(iOS 15.0, *)
private func getConfigurationAndCreateView(
ctx: AdaptyContext,
paywall: AdaptyPaywall,
preloadProducts: Bool,
customTags: [String: String]?
customTags: [String: String]?,
timerInfo: [String: String]?
) {
AdaptyUI.getViewConfiguration(forPaywall: paywall) { result in
switch result {
case let .failure(error):
return ctx.forwardError(error)

case let .success(config):
let vc = AdaptyUI.paywallController(
for: paywall,
products: nil,
viewConfiguration: config,
delegate: self,
tagResolver: customTags
)
let vc: AdaptyPaywallController

do {
vc = try AdaptyUI.paywallController(
for: paywall,
products: nil,
viewConfiguration: config,
delegate: self,
tagResolver: customTags,
timerResolver: timerInfo
)
} catch {
return ctx.bridgeError(error)
}

self.cachePaywallController(vc, id: vc.id)

Expand Down Expand Up @@ -170,26 +185,35 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
}

private func handleCreateView(_ ctx: AdaptyContext) throws {
guard #available(iOS 15.0, *) else {
throw BridgeError.unsupportedIosVersion
}

let paywallStr: String = try ctx.params.getRequiredValue(for: .paywall)
let preloadProducts: Bool? = ctx.params.getOptionalValue(for: .prefetch_products)
let customTags: [String: String]? = try ctx.params.getDecodedOptionalValue(for: .custom_tags, jsonDecoder: AdaptyContext.jsonDecoder)
let timerInfo: [String: String]? = try ctx.params.getDecodedOptionalValue(for: .timer_info, jsonDecoder: AdaptyContext.jsonDecoder)

guard let paywallData = paywallStr.data(using: .utf8),
let paywall = try? AdaptyContext.jsonDecoder.decode(AdaptyPaywall.self, from: paywallData)
else {
throw BridgeError.typeMismatch(name: .paywall, type: "String")
}


getConfigurationAndCreateView(
ctx: ctx,
paywall: paywall,
preloadProducts: preloadProducts ?? false,
customTags: customTags
customTags: customTags,
timerInfo: timerInfo
)
}

private func handlePresentView(_ ctx: AdaptyContext) throws {
guard #available(iOS 15.0, *) else {
throw BridgeError.unsupportedIosVersion
}

let id: String = try ctx.params.getRequiredValue(for: .view_id)

guard let vc = cachedPaywallController(id) else {
Expand Down Expand Up @@ -221,6 +245,10 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
}

private func handleDismissView(_ ctx: AdaptyContext) throws {
guard #available(iOS 15.0, *) else {
throw BridgeError.unsupportedIosVersion
}

let id: String = try ctx.params.getRequiredValue(for: .view_id)

guard let vc = cachedPaywallController(id) else {
Expand All @@ -240,6 +268,7 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
}

// MARK: - Event Handlers
@available(iOS 15.0, *)
func paywallController(_ controller: AdaptyPaywallController, didPerform action: AdaptyUI.Action) {
self.pushEvent(EventName.onAction, view: controller)
switch action {
Expand All @@ -248,7 +277,6 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
break
case let .openURL(url):
self.pushEvent(.onUrlPress, view: controller, data: url.absoluteString)
UIApplication.shared.open(url, options: [:])
break
case let .custom(id):
self.pushEvent(.onCustomEvent, view: controller, data: id)
Expand All @@ -259,31 +287,36 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {


/// PRODUCT SELECTED
@available(iOS 15.0, *)
public func paywallController(_ controller: AdaptyPaywallController,
didSelectProduct product: AdaptyPaywallProduct) {
self.pushEvent(EventName.onProductSelected, view: controller, data: product)
}

/// PURCHASE STARTED
@available(iOS 15.0, *)
public func paywallController(_ controller: AdaptyPaywallController,
didStartPurchase product: AdaptyPaywallProduct) {
self.pushEvent(EventName.onPurchaseStarted, view: controller, data: product)
}

/// PURCHASE SUCCESS
@available(iOS 15.0, *)
public func paywallController(_ controller: AdaptyPaywallController,
didFinishPurchase product: AdaptyPaywallProduct,
purchasedInfo: AdaptyPurchasedInfo) {
self.pushEvent(EventName.onPurchaseCompleted, view: controller, data: purchasedInfo.profile)
}

/// RENDERING FAILED
@available(iOS 15.0, *)
public func paywallController(_ controller: AdaptyPaywallController,
didFailRenderingWith error: AdaptyError) {
self.pushEvent(EventName.onRenderingFailed, view: controller, data: error)
}

/// LOAD PRODUCTS FAILED
@available(iOS 15.0, *)
public func paywallController(_ controller: AdaptyPaywallController,
didFailLoadingProductsWith error: AdaptyError) -> Bool {
self.pushEvent(EventName.onLoadingProductsFailed, view: controller, data: error)
Expand All @@ -292,33 +325,54 @@ class RNAUICallHandler: RCTEventEmitter, AdaptyPaywallControllerDelegate {
}

/// PURCHASE FAILED
@available(iOS 15.0, *)
func paywallController(_ controller: AdaptyPaywallController,
didFailPurchase product: AdaptyPaywallProduct,
error: AdaptyError) {
self.pushEvent(EventName.onPurchaseFailed, view: controller, data: error)
}

/// CANCEL PURCHASE PRESS
@available(iOS 15.0, *)
func paywallController(_ controller: AdaptyPaywallController,
didCancelPurchase product: AdaptyPaywallProduct) {
self.pushEvent(EventName.onPurchaseCancelled, view: controller, data: product)
}

/// RESTORE STARTED
@available(iOS 15.0, *)
public func paywallControllerDidStartRestore(_ controller: AdaptyPaywallController) {
self.pushEvent(EventName.onRestoreStarted, view: controller)
}

/// RESTORE SUCCESS
@available(iOS 15.0, *)
func paywallController(_ controller: AdaptyPaywallController,
didFinishRestoreWith profile: AdaptyProfile) {
self.pushEvent(EventName.onRestoreCompleted, view: controller, data: profile)
}


/// RESTORE FAILED
@available(iOS 15.0, *)
public func paywallController(_ controller: AdaptyPaywallController,
didFailRestoreWith error: AdaptyError) {
self.pushEvent(EventName.onRestoreFailed, view: controller, data: error)
}
}

extension Dictionary<String, String>: AdaptyTimerResolver {
public func timerEndAtDate(for timerId: String) -> Date {
if let dateStr = self[timerId], let date = endTimeStrToDate(dateStr: dateStr) {
return date
}
return Date(timeIntervalSinceNow: 3600.0)
}

private func endTimeStrToDate(dateStr: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: dateStr)
}
}
1 change: 1 addition & 0 deletions ios/RNAUIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension RNAUIError: CustomDebugStringConvertible {
}
}

@available(iOS 15.0, *)
extension RNAUIError: CustomAdaptyError {
public static let errorDomain = AdaptyError.AdaptyUIErrorDomain

Expand Down
1 change: 1 addition & 0 deletions ios/RNAdapty+Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import AdaptyUI
import react_native_adapty_sdk

@available(iOS 15.0, *)
struct AdaptyViewResult<T: Encodable>: Encodable {
var adaptyResult: AdaptyResult<T>
var view: AdaptyPaywallController
Expand Down
Loading

0 comments on commit 3a32cca

Please sign in to comment.