AppsFlyerSDK / appsflyer-flutter-plugin

Flutter Plugin for AppsFlyer SDK
MIT License
142 stars 111 forks source link

Passing options logic #297

Open stippo-tech opened 5 months ago

stippo-tech commented 5 months ago

In the latest SDK version (6.12.2), the class AppsflyerSdk has some initialization logic in the initSdk() method, but it also takes options as an argument. Additionally, AppsflyerSdk is a singleton. This logic leads to calls like AppsflyerSdk(null) every time after the object AppsflyerSdk is created. Also it is not obvious why calls like AppsflyerSdk([anyOtherAppsflyerOptions]) return the same instance of AppsflyerSdk. Please consider moving the logic for passing options to the initSdk() method.

To avoid this issue I need to wrap AppsflyerSdk in wrappers like this.

import 'package:appsflyer_sdk/appsflyer_sdk.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:my_application/app_consts.dart';

class AppsflyerWrapper {
  AppsflyerSdk? _appsflyerSdk;

  Future<void> init({
    double? timeToWaitForATTUserAuthorization,
    String? appInviteOneLink,
    bool? disableAdvertisingIdentifier,
    bool? disableCollectASA,
  }) async {
    final packageName =
        await PackageInfo.fromPlatform().then((info) => info.packageName);
    _appsflyerSdk ??= AppsflyerSdk(AppsFlyerOptions(
      afDevKey: AppConsts.afDevKey,
      appId: packageName,
      showDebug: false,
      timeToWaitForATTUserAuthorization: timeToWaitForATTUserAuthorization,
      appInviteOneLink: appInviteOneLink,
      disableAdvertisingIdentifier: disableAdvertisingIdentifier,
      disableCollectASA: disableCollectASA,
    ));
    await _appsflyerSdk?.initSdk();
  }

  Future<String?> getUid() async => _appsflyerSdk?.getAppsFlyerUID();

  Future<bool?> logEvent(
          String eventName, Map<dynamic, dynamic>? eventValues) async =>
      _appsflyerSdk?.logEvent(eventName, eventValues);
}