diff --git a/example/lib/main.dart b/example/lib/main.dart index 46d3d9e7..10d1d815 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'dart:async'; -import 'package:qonversion_flutter/qonversion.dart'; +import 'package:qonversion_flutter/qonversion_flutter.dart'; void main() => runApp(MyApp()); diff --git a/lib/qonversion.dart b/lib/qonversion.dart index 1dafec2b..bd9a9db5 100644 --- a/lib/qonversion.dart +++ b/lib/qonversion.dart @@ -1,170 +1,5 @@ -import 'dart:async'; -import 'dart:io'; +// This file exists to support old way to import package: 'package:qonversion_flutter/qonversion.dart'. +// Recommended way to import package no: 'package:qonversion_flutter/qonversion_flutter.dart'. -import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; - -import 'src/constants.dart'; - -enum QAttributionProvider { appsFlyer, branch } - -class Qonversion { - static const MethodChannel _channel = - const MethodChannel('qonversion_flutter_sdk'); - - /// Launches Qonversion SDK with the given API keys for each platform: - /// [androidApiKey] and [iosApiKey] respectively, - /// you can get one in your account on qonversion.io. - /// - /// Returns `userId` for Ads integrations. - /// - /// **Warning**: - /// Qonversion will track any purchase events (trials, subscriptions, basic purchases) automatically. - static Future launch({ - @required String androidApiKey, - @required String iosApiKey, - String userId, - }) async { - final apiKey = _obtainPlatformApiKey( - androidApiKey: androidApiKey, - iosApiKey: iosApiKey, - ); - - final args = { - Constants.kApiKey: apiKey, - Constants.kUserId: userId, - }; - - final uid = await _channel.invokeMethod(Constants.mLaunch, args); - - return uid; - } - - Future trackPurchase( - Map details, Map purchase) async { - final args = { - Constants.kDetails: details, - Constants.kPurchase: purchase, - }; - - final uid = await _channel.invokeMethod(Constants.mTrackPurchase, args); - - return uid; - } - - /// Launches Qonversion SDK with the given API keys for each platform: - /// [androidApiKey] and [iosApiKey] respectively, - /// you can get one in your account on qonversion.io. - /// - /// [onComplete] will return `uid` for Ads integrations. - /// - /// **Warning**: - /// Qonversion will track any purchase events (trials, subscriptions, basic purchases) automatically. - @Deprecated("Use `launch` method instead") - static Future launchWith({ - String androidApiKey, - String iosApiKey, - void Function(String) onComplete, - }) async { - final key = _obtainPlatformApiKey( - androidApiKey: androidApiKey, - iosApiKey: iosApiKey, - ); - - final args = {Constants.kApiKey: key}; - final uid = - await _channel.invokeMethod(Constants.mLaunchWithKeyCompletion, args); - - onComplete(uid); - } - - /// Launches Qonversion SDK with the given API keys for each platform: - /// [androidApiKey] and [iosApiKey] respectively, - /// you can get one in your account on qonversion.io. - /// - /// Sets client side [userid] (instead of Qonversion user-id) that will be used for matching data in the third party data. - @Deprecated("Use `launch` method instead") - static Future launchWithClientSideUserId( - String userID, { - String androidApiKey, - String iosApiKey, - }) { - final key = _obtainPlatformApiKey( - androidApiKey: androidApiKey, iosApiKey: iosApiKey); - - final args = { - Constants.kApiKey: key, - Constants.kUserId: userID, - }; - - return _channel.invokeMethod(Constants.mLaunchWithKeyUserId, args); - } - - /// **Don't use with autoTrackPurchases: false** now. - /// Functionality is under development yet. - /// - /// Launches Qonversion SDK with the given API keys for each platform: - /// [androidApiKey] and [iosApiKey] respectively, - /// you can get one in your account on qonversion.io. - /// - /// With [autoTrackPurchases] parameter turned off you need to call `trackPurchase:transaction:` method. - /// [onComplete] will return `uid` for Ads integrations. - /// **Warning**: - /// Will track any purchase events (trials, subscriptions, basic purchases) automatically. - /// But if `autoTrackPurchases` disabled you need to call `trackPurchase:transaction:` method (under development yet). - /// Otherwise, purchases tracking won't work. - @Deprecated("Use `launch` method instead") - static Future launchWithAutoTrackPurchases( - bool autoTrackPurchases, { - String androidApiKey, - String iosApiKey, - void Function(String) onComplete, - }) async { - final key = _obtainPlatformApiKey( - androidApiKey: androidApiKey, iosApiKey: iosApiKey); - - final args = { - Constants.kApiKey: key, - Constants.kAutoTrackPurchases: autoTrackPurchases, - }; - final uid = await _channel.invokeMethod( - Constants.mLaunchWithKeyAutoTrackPurchasesCompletion, args); - - onComplete(uid); - } - - /// Sends your attribution [data] to the [provider]. - /// - /// [userID], if specified, will also be sent to the provider - static Future addAttributionData( - Map data, QAttributionProvider provider) { - final args = { - Constants.kData: data, - Constants.kProvider: describeEnum(provider), - }; - - return _channel.invokeMethod(Constants.mAddAttributionData, args); - } - - static String _obtainPlatformApiKey({ - String androidApiKey, - String iosApiKey, - }) { - String key; - - if (Platform.isAndroid) { - key = androidApiKey; - } else if (Platform.isIOS) { - key = iosApiKey; - } else { - throw Exception('Unsupported platform'); - } - - if (key == null) { - throw Exception( - 'Please provide API key for the platform you are running an app on'); - } - - return key; - } -} +export 'src/qonversion.dart'; +export 'src/qa_provider.dart'; diff --git a/lib/qonversion_flutter.dart b/lib/qonversion_flutter.dart new file mode 100644 index 00000000..39147024 --- /dev/null +++ b/lib/qonversion_flutter.dart @@ -0,0 +1,2 @@ +export 'src/qonversion.dart'; +export 'src/qa_provider.dart'; diff --git a/lib/src/qa_provider.dart b/lib/src/qa_provider.dart new file mode 100644 index 00000000..3f7aaee6 --- /dev/null +++ b/lib/src/qa_provider.dart @@ -0,0 +1 @@ +enum QAttributionProvider { appsFlyer, branch } diff --git a/lib/src/qonversion.dart b/lib/src/qonversion.dart new file mode 100644 index 00000000..0d377850 --- /dev/null +++ b/lib/src/qonversion.dart @@ -0,0 +1,88 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +import 'constants.dart'; +import 'qa_provider.dart'; + +class Qonversion { + static const MethodChannel _channel = + const MethodChannel('qonversion_flutter_sdk'); + + /// Launches Qonversion SDK with the given API keys for each platform: + /// [androidApiKey] and [iosApiKey] respectively, + /// you can get one in your account on qonversion.io. + /// + /// Returns `userId` for Ads integrations. + /// + /// **Warning**: + /// Qonversion will track any purchase events (trials, subscriptions, basic purchases) automatically. + static Future launch({ + @required String androidApiKey, + @required String iosApiKey, + String userId, + }) async { + final apiKey = _obtainPlatformApiKey( + androidApiKey: androidApiKey, + iosApiKey: iosApiKey, + ); + + final args = { + Constants.kApiKey: apiKey, + Constants.kUserId: userId, + }; + + final uid = await _channel.invokeMethod(Constants.mLaunch, args); + + return uid; + } + + Future trackPurchase( + Map details, Map purchase) async { + final args = { + Constants.kDetails: details, + Constants.kPurchase: purchase, + }; + + final uid = await _channel.invokeMethod(Constants.mTrackPurchase, args); + + return uid; + } + + /// Sends your attribution [data] to the [provider]. + /// + /// [userID], if specified, will also be sent to the provider + static Future addAttributionData( + Map data, QAttributionProvider provider) { + final args = { + Constants.kData: data, + Constants.kProvider: describeEnum(provider), + }; + + return _channel.invokeMethod(Constants.mAddAttributionData, args); + } + + static String _obtainPlatformApiKey({ + String androidApiKey, + String iosApiKey, + }) { + String key; + + if (Platform.isAndroid) { + key = androidApiKey; + } else if (Platform.isIOS) { + key = iosApiKey; + } else { + throw Exception('Unsupported platform'); + } + + if (key == null) { + throw Exception( + 'Please provide API key for the platform you are running an app on'); + } + + return key; + } +}