Skip to content

Commit

Permalink
Adds support for handleLog and includes some additional cleanup; pi…
Browse files Browse the repository at this point in the history
…nned Android to `com.superwall.sdk:superwall-android:1.0.0-alpha.43`
  • Loading branch information
super-bryan committed Jan 26, 2024
1 parent 90dbaa3 commit 42e212f
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 50 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ dependencies {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'

implementation "com.superwall.sdk:superwall-android:1.0.0-alpha.42"
implementation "com.superwall.sdk:superwall-android:1.0.0-alpha.43"
implementation 'com.android.billingclient:billing:6.1.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,35 @@ class SuperwallDelegateProxyBridge(
override fun paywallWillOpenDeepLink(url: URL) {
communicator.invokeMethodOnMain("paywallWillOpenDeepLink", mapOf("url" to url.toString()))
}

override fun handleLog(
level: String,
scope: String,
message: String?,
info: Map<String, Any>?,
error: Throwable?
) {
val transformedInfo = info?.mapValues { (_, value) ->
when (value) {
is String -> value
is Int -> value
is Map<*, *> -> value
is List<*> -> value
is Boolean -> value
is Set<*> -> value.toList()
else -> null
}
}

val arguments: Map<String, Any?> = mapOf(
"level" to level,
"scope" to scope,
"message" to message,
"info" to transformedInfo,
"error" to error?.localizedMessage
)

communicator.invokeMethodOnMain("handleLog", arguments)
}
}

1 change: 0 additions & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ android {

buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
Expand Down
8 changes: 5 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,15 @@ class _MyAppState extends State<MyApp> implements SuperwallDelegate {
}

@override
void handleLog(String level, String scope, String? message, Map<String, dynamic>? info, String error) {
// TODO: implement handleLog
void handleLog(String level, String scope, String? message, Map<dynamic, dynamic>? info, String? error) {
// print("handleLog: $level, $scope, $message, $info, $error");
}

@override
void handleSuperwallEvent(SuperwallEventInfo eventInfo) async {
// This delegate function is noisy. Uncomment to debug.
return;

print("handleSuperwallEvent: $eventInfo");

switch (eventInfo.event.type) {
Expand All @@ -354,7 +357,6 @@ class _MyAppState extends State<MyApp> implements SuperwallDelegate {
default:
break;
}

}

@override
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.25"
version: "0.0.26"
sync_http:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/Bridges/BridgeInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class BridgeInstance: NSObject {
}

lazy var communicator: Communicator = {
// TODO: Consider simplifying bridgeId setting / do we still need it
let communicator = Communicator(name: bridgeId, binaryMessenger: BridgingCreator.shared.registrar.messenger())

// Setting an associated object here because `FlutterMethodChannel / Communicator` doesn't make the `name` publicly available. Consider subclassing `FlutterMethodChannel` instead to avoid this.
communicator.bridgeId = bridgeId

return communicator
Expand Down
36 changes: 36 additions & 0 deletions ios/Classes/Bridges/SuperwallDelegateProxyBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,41 @@ public class SuperwallDelegateProxyBridge: BridgeInstance, SuperwallDelegate {
public func paywallWillOpenDeepLink(url: URL) {
communicator.invokeMethodOnMain("paywallWillOpenDeepLink", arguments: ["url" : url.absoluteString])
}

public func handleLog(
level: String,
scope: String,
message: String?,
info: [String: Any]?,
error: Swift.Error?
) {
let transformedInfo = info?.compactMapValues { value -> Any? in
switch value {
case let stringValue as String:
return stringValue
case let intValue as Int:
return intValue
case let dictValue as [String: Any]:
return dictValue
case let arrayValue as [Any]:
return arrayValue
case let boolValue as Bool:
return boolValue
case let setValue as Set<AnyHashable>:
return Array(setValue)
default:
return nil
}
}

let arguments: [String: Any?] = [
"level": level,
"scope": scope,
"message": message,
"info": transformedInfo,
"error": error?.localizedDescription
]
communicator.invokeMethodOnMain("handleLog", arguments: arguments)
}
}

12 changes: 9 additions & 3 deletions ios/Classes/BridgingCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import SuperwallKit

/// Creates a method channel for a particular unique instance of a class
public class BridgingCreator: NSObject, FlutterPlugin {
// TODO: CHANGE
static var shared: BridgingCreator!
static private var _shared: BridgingCreator? = nil
static var shared: BridgingCreator {
guard let shared = _shared else {
fatalError("Attempting to access the shared BridgingCreator before `register(with registrar: FlutterPluginRegistrar)` has been called.")
}

return shared
}

let registrar: FlutterPluginRegistrar

Expand All @@ -31,7 +37,7 @@ public class BridgingCreator: NSObject, FlutterPlugin {
let communicator = Communicator(name: "SWK_BridgingCreator", binaryMessenger: registrar.messenger())

let bridge = BridgingCreator(registrar: registrar, communicator: communicator)
BridgingCreator.shared = bridge
BridgingCreator._shared = bridge

registrar.addMethodCallDelegate(bridge, channel: communicator)
}
Expand Down
10 changes: 1 addition & 9 deletions ios/Classes/SuperwallkitFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension BridgeId {

extension FlutterMethodCall {
var badArgs: FlutterError {
return FlutterError.badArgs(for: method)
return FlutterError(code: "BAD_ARGS", message: "Missing or invalid arguments for '\(method)'", details: nil)
}
}

Expand Down Expand Up @@ -82,11 +82,3 @@ extension FlutterMethodChannel {
}
}
}


// TODO: Remove
extension FlutterError {
static func badArgs(for method: String) -> FlutterError {
return FlutterError(code: "BAD_ARGS", message: "Missing or invalid arguments for '\(method)'", details: nil)
}
}
12 changes: 9 additions & 3 deletions lib/src/private/SuperwallDelegateProxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ class SuperwallDelegateProxy extends BridgeIdInstantiable {
break;
case 'handleSuperwallEvent':
final json = call.argument("eventInfo");
final event = json['event'];
final eventName = event['event'];

final eventInfo = SuperwallEventInfo.fromJson(json);
delegate.handleSuperwallEvent(eventInfo);
break;
case 'paywallWillOpenURL':
final url = call.argument("url");
delegate.paywallWillOpenURL(Uri.parse(url));
Expand All @@ -58,6 +56,14 @@ class SuperwallDelegateProxy extends BridgeIdInstantiable {
final url = call.argument("url");
delegate.paywallWillOpenDeepLink(Uri.parse(url));
break;
case 'handleLog':
final level = call.argument("level");
final scope = call.argument("scope");
final message = call.argument("message");
final info = call.argument("info");
final error = call.argument("error");
delegate.handleLog(level, scope, message, info, error);
break;
default:
throw ArgumentError('Method not implemented');
}
Expand Down
11 changes: 5 additions & 6 deletions lib/src/public/Superwall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ class Superwall extends BridgeIdInstantiable {
}

// Asynchronous method to get the presented paywall view controller
// TODO:This is a placeholder implementation as Dart does not have a direct equivalent for UIViewController
Future<dynamic> getPresentedViewController() async {
await _waitForBridgeInstanceCreation();

return await bridgeId.communicator.invokeBridgeMethod('getPresentedViewController');
}
// Future<dynamic> getPresentedViewController() async {
// await _waitForBridgeInstanceCreation();
//
// return await bridgeId.communicator.invokeBridgeMethod('getPresentedViewController');
// }

// Asynchronous method to get the latest PaywallInfo object
Future<PaywallInfo?> getLatestPaywallInfo() async {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/public/SuperwallDelegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ abstract class SuperwallDelegate {
void paywallWillOpenDeepLink(Uri url);

/// Receive all log messages generated by the SDK.
void handleLog(String level, String scope, String? message, Map<String, dynamic>? info, String error);
void handleLog(String level, String scope, String? message, Map<dynamic, dynamic>? info, String? error);
}
20 changes: 0 additions & 20 deletions lib/src/public/TransactionError.dart

This file was deleted.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: superwallkit_flutter
description: "Remotely configure every aspect of your paywall and double your revenue."
version: 0.0.26
version: 0.0.27
homepage: "https://superwall.com"

environment:
Expand Down

0 comments on commit 42e212f

Please sign in to comment.